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

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 368
0989ad8c0860
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

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

mercurial