1 /*
   2  * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.xml.internal.ws.api.message.saaj;
  27 
  28 import java.util.Iterator;
  29 
  30 import javax.xml.soap.AttachmentPart;
  31 import javax.xml.soap.MessageFactory;
  32 import javax.xml.soap.SAAJMetaFactory;
  33 import javax.xml.soap.SOAPException;
  34 import javax.xml.soap.SOAPFactory;
  35 import javax.xml.soap.SOAPMessage;
  36 import javax.xml.stream.XMLStreamException;
  37 
  38 import org.xml.sax.SAXException;
  39 
  40 import com.sun.xml.internal.bind.marshaller.SAX2DOMEx;
  41 import com.sun.xml.internal.ws.api.SOAPVersion;
  42 import com.sun.xml.internal.ws.api.message.Attachment;
  43 import com.sun.xml.internal.ws.api.message.AttachmentEx;
  44 import com.sun.xml.internal.ws.api.message.Message;
  45 import com.sun.xml.internal.ws.api.message.Packet;
  46 import com.sun.xml.internal.ws.message.saaj.SAAJMessage;
  47 import com.sun.xml.internal.ws.util.ServiceFinder;
  48 import com.sun.xml.internal.ws.util.xml.XmlUtil;
  49 
  50 /**
  51  * Factory SPI for SAAJ implementations
  52  *
  53  * @since 2.2.6
  54  */
  55 public class SAAJFactory {
  56         private static final SAAJFactory instance = new SAAJFactory();
  57 
  58     /**
  59      * Creates a new <code>MessageFactory</code> object that is an instance
  60      * of the specified implementation.  May be a dynamic message factory,
  61      * a SOAP 1.1 message factory, or a SOAP 1.2 message factory. A dynamic
  62      * message factory creates messages based on the MIME headers specified
  63      * as arguments to the <code>createMessage</code> method.
  64      *
  65      * This method uses the SAAJMetaFactory to locate the implementation class
  66      * and create the MessageFactory instance.
  67      *
  68      * @return a new instance of a <code>MessageFactory</code>
  69      *
  70      * @param protocol  a string constant representing the class of the
  71      *                   specified message factory implementation. May be
  72      *                   either <code>DYNAMIC_SOAP_PROTOCOL</code>,
  73      *                   <code>DEFAULT_SOAP_PROTOCOL</code> (which is the same
  74      *                   as) <code>SOAP_1_1_PROTOCOL</code>, or
  75      *                   <code>SOAP_1_2_PROTOCOL</code>.
  76      *
  77      * @exception SOAPException if there was an error in creating the
  78      *            specified implementation of  <code>MessageFactory</code>.
  79      * @see SAAJMetaFactory
  80      */
  81         public static MessageFactory getMessageFactory(String protocol) throws SOAPException {
  82                 for (SAAJFactory s : ServiceFinder.find(SAAJFactory.class)) {
  83                         MessageFactory mf = s.createMessageFactory(protocol);
  84                         if (mf != null)
  85                                 return mf;
  86                 }
  87 
  88         return instance.createMessageFactory(protocol);
  89         }
  90 
  91     /**
  92      * Creates a new <code>SOAPFactory</code> object that is an instance of
  93      * the specified implementation, this method uses the SAAJMetaFactory to
  94      * locate the implementation class and create the SOAPFactory instance.
  95      *
  96      * @return a new instance of a <code>SOAPFactory</code>
  97      *
  98      * @param protocol  a string constant representing the protocol of the
  99      *                   specified SOAP factory implementation. May be
 100      *                   either <code>DYNAMIC_SOAP_PROTOCOL</code>,
 101      *                   <code>DEFAULT_SOAP_PROTOCOL</code> (which is the same
 102      *                   as) <code>SOAP_1_1_PROTOCOL</code>, or
 103      *                   <code>SOAP_1_2_PROTOCOL</code>.
 104      *
 105      * @exception SOAPException if there was an error creating the
 106      *            specified <code>SOAPFactory</code>
 107      * @see SAAJMetaFactory
 108      */
 109         public static SOAPFactory getSOAPFactory(String protocol) throws SOAPException {
 110                 for (SAAJFactory s : ServiceFinder.find(SAAJFactory.class)) {
 111                         SOAPFactory sf = s.createSOAPFactory(protocol);
 112                         if (sf != null)
 113                                 return sf;
 114                 }
 115 
 116         return instance.createSOAPFactory(protocol);
 117         }
 118 
 119         /**
 120          * Creates Message from SOAPMessage
 121          * @param saaj SOAPMessage
 122          * @return created Message
 123          */
 124         public static Message create(SOAPMessage saaj) {
 125                 for (SAAJFactory s : ServiceFinder.find(SAAJFactory.class)) {
 126                         Message m = s.createMessage(saaj);
 127                         if (m != null)
 128                                 return m;
 129                 }
 130 
 131         return instance.createMessage(saaj);
 132         }
 133 
 134         /**
 135          * Reads Message as SOAPMessage.  After this call message is consumed.
 136          * @param soapVersion SOAP version
 137          * @param message Message
 138          * @return Created SOAPMessage
 139          * @throws SOAPException if SAAJ processing fails
 140          */
 141         public static SOAPMessage read(SOAPVersion soapVersion, Message message) throws SOAPException {
 142                 for (SAAJFactory s : ServiceFinder.find(SAAJFactory.class)) {
 143                         SOAPMessage msg = s.readAsSOAPMessage(soapVersion, message);
 144                         if (msg != null)
 145                                 return msg;
 146                 }
 147 
 148         return instance.readAsSOAPMessage(soapVersion, message);
 149         }
 150 
 151         /**
 152      * Reads Message as SOAPMessage.  After this call message is consumed.
 153      * @param soapVersion SOAP version
 154      * @param message Message
 155      * @param packet The packet that owns the Message
 156      * @return Created SOAPMessage
 157      * @throws SOAPException if SAAJ processing fails
 158      */
 159     public static SOAPMessage read(SOAPVersion soapVersion, Message message, Packet packet) throws SOAPException {
 160         for (SAAJFactory s : ServiceFinder.find(SAAJFactory.class)) {
 161             SOAPMessage msg = s.readAsSOAPMessage(soapVersion, message, packet);
 162             if (msg != null)
 163                 return msg;
 164         }
 165 
 166         return instance.readAsSOAPMessage(soapVersion, message, packet);
 167     }
 168 
 169     /**
 170      * Reads the message within the Packet to a SAAJMessage.  After this call message is consumed.
 171      * @param packet Packet
 172      * @return Created SAAJPMessage
 173      * @throws SOAPException if SAAJ processing fails
 174      */
 175     public static SAAJMessage read(Packet packet) throws SOAPException {
 176         for (SAAJFactory s : ServiceFinder.find(SAAJFactory.class)) {
 177             SAAJMessage msg = s.readAsSAAJ(packet);
 178             if (msg != null) return msg;
 179         }
 180         return instance.readAsSAAJ(packet);
 181     }
 182 
 183     /**
 184      * Reads the message within the Packet to a SAAJMessage.  After this call message is consumed.
 185      * @param packet Packet
 186      * @return Created SAAJPMessage
 187      * @throws SOAPException if SAAJ processing fails
 188      */
 189     public SAAJMessage readAsSAAJ(Packet packet) throws SOAPException {
 190         SOAPVersion v = packet.getMessage().getSOAPVersion();
 191         SOAPMessage msg = readAsSOAPMessage(v, packet.getMessage());
 192         return new SAAJMessage(msg);
 193     }
 194 
 195     /**
 196      * Creates a new <code>MessageFactory</code> object that is an instance
 197      * of the specified implementation.  May be a dynamic message factory,
 198      * a SOAP 1.1 message factory, or a SOAP 1.2 message factory. A dynamic
 199      * message factory creates messages based on the MIME headers specified
 200      * as arguments to the <code>createMessage</code> method.
 201      *
 202      * This method uses the SAAJMetaFactory to locate the implementation class
 203      * and create the MessageFactory instance.
 204      *
 205      * @return a new instance of a <code>MessageFactory</code>
 206      *
 207      * @param protocol  a string constant representing the class of the
 208      *                   specified message factory implementation. May be
 209      *                   either <code>DYNAMIC_SOAP_PROTOCOL</code>,
 210      *                   <code>DEFAULT_SOAP_PROTOCOL</code> (which is the same
 211      *                   as) <code>SOAP_1_1_PROTOCOL</code>, or
 212      *                   <code>SOAP_1_2_PROTOCOL</code>.
 213      *
 214      * @exception SOAPException if there was an error in creating the
 215      *            specified implementation of  <code>MessageFactory</code>.
 216      * @see SAAJMetaFactory
 217      */
 218         public MessageFactory createMessageFactory(String protocol) throws SOAPException {
 219                 return MessageFactory.newInstance(protocol);
 220         }
 221 
 222     /**
 223      * Creates a new <code>SOAPFactory</code> object that is an instance of
 224      * the specified implementation, this method uses the SAAJMetaFactory to
 225      * locate the implementation class and create the SOAPFactory instance.
 226      *
 227      * @return a new instance of a <code>SOAPFactory</code>
 228      *
 229      * @param protocol  a string constant representing the protocol of the
 230      *                   specified SOAP factory implementation. May be
 231      *                   either <code>DYNAMIC_SOAP_PROTOCOL</code>,
 232      *                   <code>DEFAULT_SOAP_PROTOCOL</code> (which is the same
 233      *                   as) <code>SOAP_1_1_PROTOCOL</code>, or
 234      *                   <code>SOAP_1_2_PROTOCOL</code>.
 235      *
 236      * @exception SOAPException if there was an error creating the
 237      *            specified <code>SOAPFactory</code>
 238      * @see SAAJMetaFactory
 239      */
 240         public SOAPFactory createSOAPFactory(String protocol) throws SOAPException {
 241                 return SOAPFactory.newInstance(protocol);
 242         }
 243 
 244         /**
 245          * Creates Message from SOAPMessage
 246          * @param saaj SOAPMessage
 247          * @return created Message
 248          */
 249         public Message createMessage(SOAPMessage saaj) {
 250                 return new SAAJMessage(saaj);
 251         }
 252 
 253         /**
 254          * Reads Message as SOAPMessage.  After this call message is consumed.
 255          * @param soapVersion SOAP version
 256          * @param message Message
 257          * @return Created SOAPMessage
 258          * @throws SOAPException if SAAJ processing fails
 259          */
 260         public SOAPMessage readAsSOAPMessage(final SOAPVersion soapVersion, final Message message) throws SOAPException {
 261         SOAPMessage msg = soapVersion.getMessageFactory().createMessage();
 262         SaajStaxWriter writer = new SaajStaxWriter(msg);
 263         try {
 264             message.writeTo(writer);
 265         } catch (XMLStreamException e) {
 266             throw (e.getCause() instanceof SOAPException) ? (SOAPException) e.getCause() : new SOAPException(e);
 267         }
 268         msg = writer.getSOAPMessage();
 269         addAttachmentsToSOAPMessage(msg, message);
 270         if (msg.saveRequired())
 271                 msg.saveChanges();
 272         return msg;
 273         }
 274 
 275     public SOAPMessage readAsSOAPMessageSax2Dom(final SOAPVersion soapVersion, final Message message) throws SOAPException {
 276         SOAPMessage msg = soapVersion.getMessageFactory().createMessage();
 277         SAX2DOMEx s2d = new SAX2DOMEx(msg.getSOAPPart());
 278         try {
 279             message.writeTo(s2d, XmlUtil.DRACONIAN_ERROR_HANDLER);
 280         } catch (SAXException e) {
 281             throw new SOAPException(e);
 282         }
 283         addAttachmentsToSOAPMessage(msg, message);
 284         if (msg.saveRequired())
 285             msg.saveChanges();
 286         return msg;
 287     }
 288 
 289         static protected void addAttachmentsToSOAPMessage(SOAPMessage msg, Message message) {
 290         for(Attachment att : message.getAttachments()) {
 291             AttachmentPart part = msg.createAttachmentPart();
 292             part.setDataHandler(att.asDataHandler());
 293 
 294             // Be safe and avoid double angle-brackets.
 295             String cid = att.getContentId();
 296             if (cid != null) {
 297                 if (cid.startsWith("<") && cid.endsWith(">"))
 298                     part.setContentId(cid);
 299                 else
 300                     part.setContentId('<' + cid + '>');
 301             }
 302 
 303             // Add any MIME headers beside Content-ID, which is already
 304             // accounted for above, and Content-Type, which is provided
 305             // by the DataHandler above.
 306             if (att instanceof AttachmentEx) {
 307                 AttachmentEx ax = (AttachmentEx) att;
 308                 Iterator<AttachmentEx.MimeHeader> imh = ax.getMimeHeaders();
 309                 while (imh.hasNext()) {
 310                     AttachmentEx.MimeHeader ame = imh.next();
 311                     if ((!"Content-ID".equals(ame.getName()))
 312                             && (!"Content-Type".equals(ame.getName())))
 313                         part.addMimeHeader(ame.getName(), ame.getValue());
 314                 }
 315             }
 316             msg.addAttachmentPart(part);
 317         }
 318     }
 319 
 320     /**
 321      * Reads Message as SOAPMessage.  After this call message is consumed.
 322      * The implementation in this class simply calls readAsSOAPMessage(SOAPVersion, Message),
 323      * and ignores the other parameters
 324      * Subclasses can override and choose to base SOAPMessage creation on Packet properties if needed
 325      * @param soapVersion SOAP version
 326      * @param message Message
 327      * @return Created SOAPMessage
 328      * @throws SOAPException if SAAJ processing fails
 329      */
 330         public SOAPMessage readAsSOAPMessage(SOAPVersion soapVersion, Message message, Packet packet) throws SOAPException {
 331             return readAsSOAPMessage(soapVersion, message);
 332         }
 333 }