1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/api/message/saaj/SAAJFactory.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,339 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.xml.internal.ws.api.message.saaj; 1.30 + 1.31 +import java.util.Iterator; 1.32 + 1.33 +import javax.xml.soap.AttachmentPart; 1.34 +import javax.xml.soap.MessageFactory; 1.35 +import javax.xml.soap.SAAJMetaFactory; 1.36 +import javax.xml.soap.SOAPException; 1.37 +import javax.xml.soap.SOAPFactory; 1.38 +import javax.xml.soap.SOAPMessage; 1.39 +import javax.xml.stream.XMLStreamException; 1.40 + 1.41 +import org.xml.sax.SAXException; 1.42 + 1.43 +import com.sun.xml.internal.bind.marshaller.SAX2DOMEx; 1.44 +import com.sun.xml.internal.ws.api.SOAPVersion; 1.45 +import com.sun.xml.internal.ws.api.message.Attachment; 1.46 +import com.sun.xml.internal.ws.api.message.AttachmentEx; 1.47 +import com.sun.xml.internal.ws.api.message.Message; 1.48 +import com.sun.xml.internal.ws.api.message.Packet; 1.49 +import com.sun.xml.internal.ws.message.saaj.SAAJMessage; 1.50 +import com.sun.xml.internal.ws.util.ServiceFinder; 1.51 +import com.sun.xml.internal.ws.util.xml.XmlUtil; 1.52 + 1.53 +/** 1.54 + * Factory SPI for SAAJ implementations 1.55 + * 1.56 + * @since 2.2.6 1.57 + */ 1.58 +public class SAAJFactory { 1.59 + private static final SAAJFactory instance = new SAAJFactory(); 1.60 + 1.61 + /** 1.62 + * Creates a new <code>MessageFactory</code> object that is an instance 1.63 + * of the specified implementation. May be a dynamic message factory, 1.64 + * a SOAP 1.1 message factory, or a SOAP 1.2 message factory. A dynamic 1.65 + * message factory creates messages based on the MIME headers specified 1.66 + * as arguments to the <code>createMessage</code> method. 1.67 + * 1.68 + * This method uses the SAAJMetaFactory to locate the implementation class 1.69 + * and create the MessageFactory instance. 1.70 + * 1.71 + * @return a new instance of a <code>MessageFactory</code> 1.72 + * 1.73 + * @param protocol a string constant representing the class of the 1.74 + * specified message factory implementation. May be 1.75 + * either <code>DYNAMIC_SOAP_PROTOCOL</code>, 1.76 + * <code>DEFAULT_SOAP_PROTOCOL</code> (which is the same 1.77 + * as) <code>SOAP_1_1_PROTOCOL</code>, or 1.78 + * <code>SOAP_1_2_PROTOCOL</code>. 1.79 + * 1.80 + * @exception SOAPException if there was an error in creating the 1.81 + * specified implementation of <code>MessageFactory</code>. 1.82 + * @see SAAJMetaFactory 1.83 + */ 1.84 + public static MessageFactory getMessageFactory(String protocol) throws SOAPException { 1.85 + for (SAAJFactory s : ServiceFinder.find(SAAJFactory.class)) { 1.86 + MessageFactory mf = s.createMessageFactory(protocol); 1.87 + if (mf != null) 1.88 + return mf; 1.89 + } 1.90 + 1.91 + return instance.createMessageFactory(protocol); 1.92 + } 1.93 + 1.94 + /** 1.95 + * Creates a new <code>SOAPFactory</code> object that is an instance of 1.96 + * the specified implementation, this method uses the SAAJMetaFactory to 1.97 + * locate the implementation class and create the SOAPFactory instance. 1.98 + * 1.99 + * @return a new instance of a <code>SOAPFactory</code> 1.100 + * 1.101 + * @param protocol a string constant representing the protocol of the 1.102 + * specified SOAP factory implementation. May be 1.103 + * either <code>DYNAMIC_SOAP_PROTOCOL</code>, 1.104 + * <code>DEFAULT_SOAP_PROTOCOL</code> (which is the same 1.105 + * as) <code>SOAP_1_1_PROTOCOL</code>, or 1.106 + * <code>SOAP_1_2_PROTOCOL</code>. 1.107 + * 1.108 + * @exception SOAPException if there was an error creating the 1.109 + * specified <code>SOAPFactory</code> 1.110 + * @see SAAJMetaFactory 1.111 + */ 1.112 + public static SOAPFactory getSOAPFactory(String protocol) throws SOAPException { 1.113 + for (SAAJFactory s : ServiceFinder.find(SAAJFactory.class)) { 1.114 + SOAPFactory sf = s.createSOAPFactory(protocol); 1.115 + if (sf != null) 1.116 + return sf; 1.117 + } 1.118 + 1.119 + return instance.createSOAPFactory(protocol); 1.120 + } 1.121 + 1.122 + /** 1.123 + * Creates Message from SOAPMessage 1.124 + * @param saaj SOAPMessage 1.125 + * @return created Message 1.126 + */ 1.127 + public static Message create(SOAPMessage saaj) { 1.128 + for (SAAJFactory s : ServiceFinder.find(SAAJFactory.class)) { 1.129 + Message m = s.createMessage(saaj); 1.130 + if (m != null) 1.131 + return m; 1.132 + } 1.133 + 1.134 + return instance.createMessage(saaj); 1.135 + } 1.136 + 1.137 + /** 1.138 + * Reads Message as SOAPMessage. After this call message is consumed. 1.139 + * @param soapVersion SOAP version 1.140 + * @param message Message 1.141 + * @return Created SOAPMessage 1.142 + * @throws SOAPException if SAAJ processing fails 1.143 + */ 1.144 + public static SOAPMessage read(SOAPVersion soapVersion, Message message) throws SOAPException { 1.145 + for (SAAJFactory s : ServiceFinder.find(SAAJFactory.class)) { 1.146 + SOAPMessage msg = s.readAsSOAPMessage(soapVersion, message); 1.147 + if (msg != null) 1.148 + return msg; 1.149 + } 1.150 + 1.151 + return instance.readAsSOAPMessage(soapVersion, message); 1.152 + } 1.153 + 1.154 + /** 1.155 + * Reads Message as SOAPMessage. After this call message is consumed. 1.156 + * @param soapVersion SOAP version 1.157 + * @param message Message 1.158 + * @param packet The packet that owns the Message 1.159 + * @return Created SOAPMessage 1.160 + * @throws SOAPException if SAAJ processing fails 1.161 + */ 1.162 + public static SOAPMessage read(SOAPVersion soapVersion, Message message, Packet packet) throws SOAPException { 1.163 + for (SAAJFactory s : ServiceFinder.find(SAAJFactory.class)) { 1.164 + SOAPMessage msg = s.readAsSOAPMessage(soapVersion, message, packet); 1.165 + if (msg != null) 1.166 + return msg; 1.167 + } 1.168 + 1.169 + return instance.readAsSOAPMessage(soapVersion, message, packet); 1.170 + } 1.171 + 1.172 + /** 1.173 + * Reads the message within the Packet to a SAAJMessage. After this call message is consumed. 1.174 + * @param packet Packet 1.175 + * @return Created SAAJPMessage 1.176 + * @throws SOAPException if SAAJ processing fails 1.177 + */ 1.178 + public static SAAJMessage read(Packet packet) throws SOAPException { 1.179 + // Use the Component from the Packet if it exists. Note the logic 1.180 + // in the ServiceFinder is such that find(Class) is not equivalent 1.181 + // to find (Class, null), so the ternary operator is needed. 1.182 + ServiceFinder<SAAJFactory> factories = (packet.component != null ? 1.183 + ServiceFinder.find(SAAJFactory.class, packet.component) : 1.184 + ServiceFinder.find(SAAJFactory.class)); 1.185 + for (SAAJFactory s : factories) { 1.186 + SAAJMessage msg = s.readAsSAAJ(packet); 1.187 + if (msg != null) return msg; 1.188 + } 1.189 + return instance.readAsSAAJ(packet); 1.190 + } 1.191 + 1.192 + /** 1.193 + * Reads the message within the Packet to a SAAJMessage. After this call message is consumed. 1.194 + * @param packet Packet 1.195 + * @return Created SAAJPMessage 1.196 + * @throws SOAPException if SAAJ processing fails 1.197 + */ 1.198 + public SAAJMessage readAsSAAJ(Packet packet) throws SOAPException { 1.199 + SOAPVersion v = packet.getMessage().getSOAPVersion(); 1.200 + SOAPMessage msg = readAsSOAPMessage(v, packet.getMessage()); 1.201 + return new SAAJMessage(msg); 1.202 + } 1.203 + 1.204 + /** 1.205 + * Creates a new <code>MessageFactory</code> object that is an instance 1.206 + * of the specified implementation. May be a dynamic message factory, 1.207 + * a SOAP 1.1 message factory, or a SOAP 1.2 message factory. A dynamic 1.208 + * message factory creates messages based on the MIME headers specified 1.209 + * as arguments to the <code>createMessage</code> method. 1.210 + * 1.211 + * This method uses the SAAJMetaFactory to locate the implementation class 1.212 + * and create the MessageFactory instance. 1.213 + * 1.214 + * @return a new instance of a <code>MessageFactory</code> 1.215 + * 1.216 + * @param protocol a string constant representing the class of the 1.217 + * specified message factory implementation. May be 1.218 + * either <code>DYNAMIC_SOAP_PROTOCOL</code>, 1.219 + * <code>DEFAULT_SOAP_PROTOCOL</code> (which is the same 1.220 + * as) <code>SOAP_1_1_PROTOCOL</code>, or 1.221 + * <code>SOAP_1_2_PROTOCOL</code>. 1.222 + * 1.223 + * @exception SOAPException if there was an error in creating the 1.224 + * specified implementation of <code>MessageFactory</code>. 1.225 + * @see SAAJMetaFactory 1.226 + */ 1.227 + public MessageFactory createMessageFactory(String protocol) throws SOAPException { 1.228 + return MessageFactory.newInstance(protocol); 1.229 + } 1.230 + 1.231 + /** 1.232 + * Creates a new <code>SOAPFactory</code> object that is an instance of 1.233 + * the specified implementation, this method uses the SAAJMetaFactory to 1.234 + * locate the implementation class and create the SOAPFactory instance. 1.235 + * 1.236 + * @return a new instance of a <code>SOAPFactory</code> 1.237 + * 1.238 + * @param protocol a string constant representing the protocol of the 1.239 + * specified SOAP factory implementation. May be 1.240 + * either <code>DYNAMIC_SOAP_PROTOCOL</code>, 1.241 + * <code>DEFAULT_SOAP_PROTOCOL</code> (which is the same 1.242 + * as) <code>SOAP_1_1_PROTOCOL</code>, or 1.243 + * <code>SOAP_1_2_PROTOCOL</code>. 1.244 + * 1.245 + * @exception SOAPException if there was an error creating the 1.246 + * specified <code>SOAPFactory</code> 1.247 + * @see SAAJMetaFactory 1.248 + */ 1.249 + public SOAPFactory createSOAPFactory(String protocol) throws SOAPException { 1.250 + return SOAPFactory.newInstance(protocol); 1.251 + } 1.252 + 1.253 + /** 1.254 + * Creates Message from SOAPMessage 1.255 + * @param saaj SOAPMessage 1.256 + * @return created Message 1.257 + */ 1.258 + public Message createMessage(SOAPMessage saaj) { 1.259 + return new SAAJMessage(saaj); 1.260 + } 1.261 + 1.262 + /** 1.263 + * Reads Message as SOAPMessage. After this call message is consumed. 1.264 + * @param soapVersion SOAP version 1.265 + * @param message Message 1.266 + * @return Created SOAPMessage 1.267 + * @throws SOAPException if SAAJ processing fails 1.268 + */ 1.269 + public SOAPMessage readAsSOAPMessage(final SOAPVersion soapVersion, final Message message) throws SOAPException { 1.270 + SOAPMessage msg = soapVersion.getMessageFactory().createMessage(); 1.271 + SaajStaxWriter writer = new SaajStaxWriter(msg); 1.272 + try { 1.273 + message.writeTo(writer); 1.274 + } catch (XMLStreamException e) { 1.275 + throw (e.getCause() instanceof SOAPException) ? (SOAPException) e.getCause() : new SOAPException(e); 1.276 + } 1.277 + msg = writer.getSOAPMessage(); 1.278 + addAttachmentsToSOAPMessage(msg, message); 1.279 + if (msg.saveRequired()) 1.280 + msg.saveChanges(); 1.281 + return msg; 1.282 + } 1.283 + 1.284 + public SOAPMessage readAsSOAPMessageSax2Dom(final SOAPVersion soapVersion, final Message message) throws SOAPException { 1.285 + SOAPMessage msg = soapVersion.getMessageFactory().createMessage(); 1.286 + SAX2DOMEx s2d = new SAX2DOMEx(msg.getSOAPPart()); 1.287 + try { 1.288 + message.writeTo(s2d, XmlUtil.DRACONIAN_ERROR_HANDLER); 1.289 + } catch (SAXException e) { 1.290 + throw new SOAPException(e); 1.291 + } 1.292 + addAttachmentsToSOAPMessage(msg, message); 1.293 + if (msg.saveRequired()) 1.294 + msg.saveChanges(); 1.295 + return msg; 1.296 + } 1.297 + 1.298 + static protected void addAttachmentsToSOAPMessage(SOAPMessage msg, Message message) { 1.299 + for(Attachment att : message.getAttachments()) { 1.300 + AttachmentPart part = msg.createAttachmentPart(); 1.301 + part.setDataHandler(att.asDataHandler()); 1.302 + 1.303 + // Be safe and avoid double angle-brackets. 1.304 + String cid = att.getContentId(); 1.305 + if (cid != null) { 1.306 + if (cid.startsWith("<") && cid.endsWith(">")) 1.307 + part.setContentId(cid); 1.308 + else 1.309 + part.setContentId('<' + cid + '>'); 1.310 + } 1.311 + 1.312 + // Add any MIME headers beside Content-ID, which is already 1.313 + // accounted for above, and Content-Type, which is provided 1.314 + // by the DataHandler above. 1.315 + if (att instanceof AttachmentEx) { 1.316 + AttachmentEx ax = (AttachmentEx) att; 1.317 + Iterator<AttachmentEx.MimeHeader> imh = ax.getMimeHeaders(); 1.318 + while (imh.hasNext()) { 1.319 + AttachmentEx.MimeHeader ame = imh.next(); 1.320 + if ((!"Content-ID".equals(ame.getName())) 1.321 + && (!"Content-Type".equals(ame.getName()))) 1.322 + part.addMimeHeader(ame.getName(), ame.getValue()); 1.323 + } 1.324 + } 1.325 + msg.addAttachmentPart(part); 1.326 + } 1.327 + } 1.328 + 1.329 + /** 1.330 + * Reads Message as SOAPMessage. After this call message is consumed. 1.331 + * The implementation in this class simply calls readAsSOAPMessage(SOAPVersion, Message), 1.332 + * and ignores the other parameters 1.333 + * Subclasses can override and choose to base SOAPMessage creation on Packet properties if needed 1.334 + * @param soapVersion SOAP version 1.335 + * @param message Message 1.336 + * @return Created SOAPMessage 1.337 + * @throws SOAPException if SAAJ processing fails 1.338 + */ 1.339 + public SOAPMessage readAsSOAPMessage(SOAPVersion soapVersion, Message message, Packet packet) throws SOAPException { 1.340 + return readAsSOAPMessage(soapVersion, message); 1.341 + } 1.342 +}