src/share/jaxws_classes/com/sun/xml/internal/ws/api/message/saaj/SAAJFactory.java

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 384
8f2986ff0235
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

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

mercurial