Thu, 31 Aug 2017 15:18:52 +0800
merge
1 /*
2 * Copyright (c) 1997, 2013, 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 */
26 package com.sun.xml.internal.ws.api.message.saaj;
28 import java.util.Iterator;
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;
38 import org.xml.sax.SAXException;
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;
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();
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 }
88 return instance.createMessageFactory(protocol);
89 }
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 }
116 return instance.createSOAPFactory(protocol);
117 }
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 }
131 return instance.createMessage(saaj);
132 }
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 }
148 return instance.readAsSOAPMessage(soapVersion, message);
149 }
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 }
166 return instance.readAsSOAPMessage(soapVersion, message, packet);
167 }
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 // Use the Component from the Packet if it exists. Note the logic
177 // in the ServiceFinder is such that find(Class) is not equivalent
178 // to find (Class, null), so the ternary operator is needed.
179 ServiceFinder<SAAJFactory> factories = (packet.component != null ?
180 ServiceFinder.find(SAAJFactory.class, packet.component) :
181 ServiceFinder.find(SAAJFactory.class));
182 for (SAAJFactory s : factories) {
183 SAAJMessage msg = s.readAsSAAJ(packet);
184 if (msg != null) return msg;
185 }
186 return instance.readAsSAAJ(packet);
187 }
189 /**
190 * Reads the message within the Packet to a SAAJMessage. After this call message is consumed.
191 * @param packet Packet
192 * @return Created SAAJPMessage
193 * @throws SOAPException if SAAJ processing fails
194 */
195 public SAAJMessage readAsSAAJ(Packet packet) throws SOAPException {
196 SOAPVersion v = packet.getMessage().getSOAPVersion();
197 SOAPMessage msg = readAsSOAPMessage(v, packet.getMessage());
198 return new SAAJMessage(msg);
199 }
201 /**
202 * Creates a new <code>MessageFactory</code> object that is an instance
203 * of the specified implementation. May be a dynamic message factory,
204 * a SOAP 1.1 message factory, or a SOAP 1.2 message factory. A dynamic
205 * message factory creates messages based on the MIME headers specified
206 * as arguments to the <code>createMessage</code> method.
207 *
208 * This method uses the SAAJMetaFactory to locate the implementation class
209 * and create the MessageFactory instance.
210 *
211 * @return a new instance of a <code>MessageFactory</code>
212 *
213 * @param protocol a string constant representing the class of the
214 * specified message factory implementation. May be
215 * either <code>DYNAMIC_SOAP_PROTOCOL</code>,
216 * <code>DEFAULT_SOAP_PROTOCOL</code> (which is the same
217 * as) <code>SOAP_1_1_PROTOCOL</code>, or
218 * <code>SOAP_1_2_PROTOCOL</code>.
219 *
220 * @exception SOAPException if there was an error in creating the
221 * specified implementation of <code>MessageFactory</code>.
222 * @see SAAJMetaFactory
223 */
224 public MessageFactory createMessageFactory(String protocol) throws SOAPException {
225 return MessageFactory.newInstance(protocol);
226 }
228 /**
229 * Creates a new <code>SOAPFactory</code> object that is an instance of
230 * the specified implementation, this method uses the SAAJMetaFactory to
231 * locate the implementation class and create the SOAPFactory instance.
232 *
233 * @return a new instance of a <code>SOAPFactory</code>
234 *
235 * @param protocol a string constant representing the protocol of the
236 * specified SOAP factory implementation. May be
237 * either <code>DYNAMIC_SOAP_PROTOCOL</code>,
238 * <code>DEFAULT_SOAP_PROTOCOL</code> (which is the same
239 * as) <code>SOAP_1_1_PROTOCOL</code>, or
240 * <code>SOAP_1_2_PROTOCOL</code>.
241 *
242 * @exception SOAPException if there was an error creating the
243 * specified <code>SOAPFactory</code>
244 * @see SAAJMetaFactory
245 */
246 public SOAPFactory createSOAPFactory(String protocol) throws SOAPException {
247 return SOAPFactory.newInstance(protocol);
248 }
250 /**
251 * Creates Message from SOAPMessage
252 * @param saaj SOAPMessage
253 * @return created Message
254 */
255 public Message createMessage(SOAPMessage saaj) {
256 return new SAAJMessage(saaj);
257 }
259 /**
260 * Reads Message as SOAPMessage. After this call message is consumed.
261 * @param soapVersion SOAP version
262 * @param message Message
263 * @return Created SOAPMessage
264 * @throws SOAPException if SAAJ processing fails
265 */
266 public SOAPMessage readAsSOAPMessage(final SOAPVersion soapVersion, final Message message) throws SOAPException {
267 SOAPMessage msg = soapVersion.getMessageFactory().createMessage();
268 SaajStaxWriter writer = new SaajStaxWriter(msg);
269 try {
270 message.writeTo(writer);
271 } catch (XMLStreamException e) {
272 throw (e.getCause() instanceof SOAPException) ? (SOAPException) e.getCause() : new SOAPException(e);
273 }
274 msg = writer.getSOAPMessage();
275 addAttachmentsToSOAPMessage(msg, message);
276 if (msg.saveRequired())
277 msg.saveChanges();
278 return msg;
279 }
281 public SOAPMessage readAsSOAPMessageSax2Dom(final SOAPVersion soapVersion, final Message message) throws SOAPException {
282 SOAPMessage msg = soapVersion.getMessageFactory().createMessage();
283 SAX2DOMEx s2d = new SAX2DOMEx(msg.getSOAPPart());
284 try {
285 message.writeTo(s2d, XmlUtil.DRACONIAN_ERROR_HANDLER);
286 } catch (SAXException e) {
287 throw new SOAPException(e);
288 }
289 addAttachmentsToSOAPMessage(msg, message);
290 if (msg.saveRequired())
291 msg.saveChanges();
292 return msg;
293 }
295 static protected void addAttachmentsToSOAPMessage(SOAPMessage msg, Message message) {
296 for(Attachment att : message.getAttachments()) {
297 AttachmentPart part = msg.createAttachmentPart();
298 part.setDataHandler(att.asDataHandler());
300 // Be safe and avoid double angle-brackets.
301 String cid = att.getContentId();
302 if (cid != null) {
303 if (cid.startsWith("<") && cid.endsWith(">"))
304 part.setContentId(cid);
305 else
306 part.setContentId('<' + cid + '>');
307 }
309 // Add any MIME headers beside Content-ID, which is already
310 // accounted for above, and Content-Type, which is provided
311 // by the DataHandler above.
312 if (att instanceof AttachmentEx) {
313 AttachmentEx ax = (AttachmentEx) att;
314 Iterator<AttachmentEx.MimeHeader> imh = ax.getMimeHeaders();
315 while (imh.hasNext()) {
316 AttachmentEx.MimeHeader ame = imh.next();
317 if ((!"Content-ID".equals(ame.getName()))
318 && (!"Content-Type".equals(ame.getName())))
319 part.addMimeHeader(ame.getName(), ame.getValue());
320 }
321 }
322 msg.addAttachmentPart(part);
323 }
324 }
326 /**
327 * Reads Message as SOAPMessage. After this call message is consumed.
328 * The implementation in this class simply calls readAsSOAPMessage(SOAPVersion, Message),
329 * and ignores the other parameters
330 * Subclasses can override and choose to base SOAPMessage creation on Packet properties if needed
331 * @param soapVersion SOAP version
332 * @param message Message
333 * @return Created SOAPMessage
334 * @throws SOAPException if SAAJ processing fails
335 */
336 public SOAPMessage readAsSOAPMessage(SOAPVersion soapVersion, Message message, Packet packet) throws SOAPException {
337 return readAsSOAPMessage(soapVersion, message);
338 }
339 }