Tue, 06 Mar 2012 16:09:35 -0800
7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom
1 /*
2 * Copyright (c) 1997, 2011, 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;
37 import org.xml.sax.SAXException;
39 import com.sun.xml.internal.bind.marshaller.SAX2DOMEx;
40 import com.sun.xml.internal.ws.api.SOAPVersion;
41 import com.sun.xml.internal.ws.api.message.Attachment;
42 import com.sun.xml.internal.ws.api.message.AttachmentEx;
43 import com.sun.xml.internal.ws.api.message.Message;
44 import com.sun.xml.internal.ws.message.saaj.SAAJMessage;
45 import com.sun.xml.internal.ws.util.ServiceFinder;
46 import com.sun.xml.internal.ws.util.xml.XmlUtil;
48 /**
49 * Factory SPI for SAAJ implementations
50 *
51 * @since 2.2.6
52 */
53 public class SAAJFactory {
54 private static final SAAJFactory instance = new SAAJFactory();
56 /**
57 * Creates a new <code>MessageFactory</code> object that is an instance
58 * of the specified implementation. May be a dynamic message factory,
59 * a SOAP 1.1 message factory, or a SOAP 1.2 message factory. A dynamic
60 * message factory creates messages based on the MIME headers specified
61 * as arguments to the <code>createMessage</code> method.
62 *
63 * This method uses the SAAJMetaFactory to locate the implementation class
64 * and create the MessageFactory instance.
65 *
66 * @return a new instance of a <code>MessageFactory</code>
67 *
68 * @param protocol a string constant representing the class of the
69 * specified message factory implementation. May be
70 * either <code>DYNAMIC_SOAP_PROTOCOL</code>,
71 * <code>DEFAULT_SOAP_PROTOCOL</code> (which is the same
72 * as) <code>SOAP_1_1_PROTOCOL</code>, or
73 * <code>SOAP_1_2_PROTOCOL</code>.
74 *
75 * @exception SOAPException if there was an error in creating the
76 * specified implementation of <code>MessageFactory</code>.
77 * @see SAAJMetaFactory
78 */
79 public static MessageFactory getMessageFactory(String saajFactoryString) throws SOAPException {
80 for (SAAJFactory s : ServiceFinder.find(SAAJFactory.class)) {
81 MessageFactory mf = s.createMessageFactory(saajFactoryString);
82 if (mf != null)
83 return mf;
84 }
86 return instance.createMessageFactory(saajFactoryString);
87 }
89 /**
90 * Creates a new <code>SOAPFactory</code> object that is an instance of
91 * the specified implementation, this method uses the SAAJMetaFactory to
92 * locate the implementation class and create the SOAPFactory instance.
93 *
94 * @return a new instance of a <code>SOAPFactory</code>
95 *
96 * @param protocol a string constant representing the protocol of the
97 * specified SOAP factory implementation. May be
98 * either <code>DYNAMIC_SOAP_PROTOCOL</code>,
99 * <code>DEFAULT_SOAP_PROTOCOL</code> (which is the same
100 * as) <code>SOAP_1_1_PROTOCOL</code>, or
101 * <code>SOAP_1_2_PROTOCOL</code>.
102 *
103 * @exception SOAPException if there was an error creating the
104 * specified <code>SOAPFactory</code>
105 * @see SAAJMetaFactory
106 */
107 public static SOAPFactory getSOAPFactory(String saajFactoryString) throws SOAPException {
108 for (SAAJFactory s : ServiceFinder.find(SAAJFactory.class)) {
109 SOAPFactory sf = s.createSOAPFactory(saajFactoryString);
110 if (sf != null)
111 return sf;
112 }
114 return instance.createSOAPFactory(saajFactoryString);
115 }
117 /**
118 * Creates Message from SOAPMessage
119 * @param saaj SOAPMessage
120 * @return created Message
121 */
122 public static Message create(SOAPMessage saaj) {
123 for (SAAJFactory s : ServiceFinder.find(SAAJFactory.class)) {
124 Message m = s.createMessage(saaj);
125 if (m != null)
126 return m;
127 }
129 return instance.createMessage(saaj);
130 }
132 /**
133 * Reads Message as SOAPMessage. After this call message is consumed.
134 * @param soapVersion SOAP version
135 * @param message Message
136 * @return Created SOAPMessage
137 * @throws SOAPException if SAAJ processing fails
138 */
139 public static SOAPMessage read(SOAPVersion soapVersion, Message message) throws SOAPException {
140 for (SAAJFactory s : ServiceFinder.find(SAAJFactory.class)) {
141 SOAPMessage msg = s.readAsSOAPMessage(soapVersion, message);
142 if (msg != null)
143 return msg;
144 }
146 return instance.readAsSOAPMessage(soapVersion, message);
147 }
149 /**
150 * Creates a new <code>MessageFactory</code> object that is an instance
151 * of the specified implementation. May be a dynamic message factory,
152 * a SOAP 1.1 message factory, or a SOAP 1.2 message factory. A dynamic
153 * message factory creates messages based on the MIME headers specified
154 * as arguments to the <code>createMessage</code> method.
155 *
156 * This method uses the SAAJMetaFactory to locate the implementation class
157 * and create the MessageFactory instance.
158 *
159 * @return a new instance of a <code>MessageFactory</code>
160 *
161 * @param protocol a string constant representing the class of the
162 * specified message factory implementation. May be
163 * either <code>DYNAMIC_SOAP_PROTOCOL</code>,
164 * <code>DEFAULT_SOAP_PROTOCOL</code> (which is the same
165 * as) <code>SOAP_1_1_PROTOCOL</code>, or
166 * <code>SOAP_1_2_PROTOCOL</code>.
167 *
168 * @exception SOAPException if there was an error in creating the
169 * specified implementation of <code>MessageFactory</code>.
170 * @see SAAJMetaFactory
171 */
172 public MessageFactory createMessageFactory(String saajFactoryString) throws SOAPException {
173 return MessageFactory.newInstance(saajFactoryString);
174 }
176 /**
177 * Creates a new <code>SOAPFactory</code> object that is an instance of
178 * the specified implementation, this method uses the SAAJMetaFactory to
179 * locate the implementation class and create the SOAPFactory instance.
180 *
181 * @return a new instance of a <code>SOAPFactory</code>
182 *
183 * @param protocol a string constant representing the protocol of the
184 * specified SOAP factory implementation. May be
185 * either <code>DYNAMIC_SOAP_PROTOCOL</code>,
186 * <code>DEFAULT_SOAP_PROTOCOL</code> (which is the same
187 * as) <code>SOAP_1_1_PROTOCOL</code>, or
188 * <code>SOAP_1_2_PROTOCOL</code>.
189 *
190 * @exception SOAPException if there was an error creating the
191 * specified <code>SOAPFactory</code>
192 * @see SAAJMetaFactory
193 */
194 public SOAPFactory createSOAPFactory(String saajFactoryString) throws SOAPException {
195 return SOAPFactory.newInstance(saajFactoryString);
196 }
198 /**
199 * Creates Message from SOAPMessage
200 * @param saaj SOAPMessage
201 * @return created Message
202 */
203 public Message createMessage(SOAPMessage saaj) {
204 return new SAAJMessage(saaj);
205 }
207 /**
208 * Reads Message as SOAPMessage. After this call message is consumed.
209 * @param soapVersion SOAP version
210 * @param message Message
211 * @return Created SOAPMessage
212 * @throws SOAPException if SAAJ processing fails
213 */
214 public SOAPMessage readAsSOAPMessage(SOAPVersion soapVersion, Message message) throws SOAPException {
215 SOAPMessage msg = soapVersion.getMessageFactory().createMessage();
217 SAX2DOMEx s2d = new SAX2DOMEx(msg.getSOAPPart());
218 try {
219 message.writeTo(s2d, XmlUtil.DRACONIAN_ERROR_HANDLER);
220 } catch (SAXException e) {
221 throw new SOAPException(e);
222 }
224 for(Attachment att : message.getAttachments()) {
225 AttachmentPart part = msg.createAttachmentPart();
226 part.setDataHandler(att.asDataHandler());
228 // Be safe and avoid double angle-brackets.
229 String cid = att.getContentId();
230 if (cid != null) {
231 if (cid.startsWith("<") && cid.endsWith(">"))
232 part.setContentId(cid);
233 else
234 part.setContentId('<' + cid + '>');
235 }
237 // Add any MIME headers beside Content-ID, which is already
238 // accounted for above, and Content-Type, which is provided
239 // by the DataHandler above.
240 if (att instanceof AttachmentEx) {
241 AttachmentEx ax = (AttachmentEx) att;
242 Iterator<AttachmentEx.MimeHeader> imh = ax.getMimeHeaders();
243 while (imh.hasNext()) {
244 AttachmentEx.MimeHeader ame = imh.next();
245 if ((!"Content-ID".equals(ame.getName()))
246 && (!"Content-Type".equals(ame.getName())))
247 part.addMimeHeader(ame.getName(), ame.getValue());
248 }
249 }
250 msg.addAttachmentPart(part);
251 }
253 if (msg.saveRequired())
254 msg.saveChanges();
255 return msg;
256 }
257 }