Thu, 31 Aug 2017 15:18:52 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2004, 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 javax.xml.soap; |
aoqi@0 | 27 | |
aoqi@0 | 28 | |
aoqi@0 | 29 | import java.io.IOException; |
aoqi@0 | 30 | import java.io.InputStream; |
aoqi@0 | 31 | |
aoqi@0 | 32 | /** |
aoqi@0 | 33 | * A factory for creating <code>SOAPMessage</code> objects. |
aoqi@0 | 34 | * <P> |
aoqi@0 | 35 | * A SAAJ client can create a <code>MessageFactory</code> object |
aoqi@0 | 36 | * using the method <code>newInstance</code>, as shown in the following |
aoqi@0 | 37 | * lines of code. |
aoqi@0 | 38 | * <PRE> |
aoqi@0 | 39 | * MessageFactory mf = MessageFactory.newInstance(); |
aoqi@0 | 40 | * MessageFactory mf12 = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL); |
aoqi@0 | 41 | * </PRE> |
aoqi@0 | 42 | * <P> |
aoqi@0 | 43 | * All <code>MessageFactory</code> objects, regardless of how they are |
aoqi@0 | 44 | * created, will produce <code>SOAPMessage</code> objects that |
aoqi@0 | 45 | * have the following elements by default: |
aoqi@0 | 46 | * <UL> |
aoqi@0 | 47 | * <LI>A <code>SOAPPart</code> object |
aoqi@0 | 48 | * <LI>A <code>SOAPEnvelope</code> object |
aoqi@0 | 49 | * <LI>A <code>SOAPBody</code> object |
aoqi@0 | 50 | * <LI>A <code>SOAPHeader</code> object |
aoqi@0 | 51 | * </UL> |
aoqi@0 | 52 | * In some cases, specialized MessageFactory objects may be obtained that produce messages |
aoqi@0 | 53 | * prepopulated with additional entries in the <code>SOAPHeader</code> object and the |
aoqi@0 | 54 | * <code>SOAPBody</code> object. |
aoqi@0 | 55 | * The content of a new <code>SOAPMessage</code> object depends on which of the two |
aoqi@0 | 56 | * <code>MessageFactory</code> methods is used to create it. |
aoqi@0 | 57 | * <UL> |
aoqi@0 | 58 | * <LI><code>createMessage()</code> <BR> |
aoqi@0 | 59 | * This is the method clients would normally use to create a request message. |
aoqi@0 | 60 | * <LI><code>createMessage(MimeHeaders, java.io.InputStream)</code> -- message has |
aoqi@0 | 61 | * content from the <code>InputStream</code> object and headers from the |
aoqi@0 | 62 | * <code>MimeHeaders</code> object <BR> |
aoqi@0 | 63 | * This method can be used internally by a service implementation to |
aoqi@0 | 64 | * create a message that is a response to a request. |
aoqi@0 | 65 | * </UL> |
aoqi@0 | 66 | */ |
aoqi@0 | 67 | public abstract class MessageFactory { |
aoqi@0 | 68 | |
aoqi@0 | 69 | static final String DEFAULT_MESSAGE_FACTORY |
aoqi@0 | 70 | = "com.sun.xml.internal.messaging.saaj.soap.ver1_1.SOAPMessageFactory1_1Impl"; |
aoqi@0 | 71 | |
aoqi@0 | 72 | static private final String MESSAGE_FACTORY_PROPERTY |
aoqi@0 | 73 | = "javax.xml.soap.MessageFactory"; |
aoqi@0 | 74 | |
aoqi@0 | 75 | /** |
aoqi@0 | 76 | * Creates a new <code>MessageFactory</code> object that is an instance |
aoqi@0 | 77 | * of the default implementation (SOAP 1.1), |
aoqi@0 | 78 | * |
aoqi@0 | 79 | * This method uses the following ordered lookup procedure to determine the MessageFactory implementation class to load: |
aoqi@0 | 80 | * <UL> |
aoqi@0 | 81 | * <LI> Use the javax.xml.soap.MessageFactory system property. |
aoqi@0 | 82 | * <LI> Use the properties file "lib/jaxm.properties" in the JRE directory. This configuration file is in standard |
aoqi@0 | 83 | * java.util.Properties format and contains the fully qualified name of the implementation class with the key being the |
aoqi@0 | 84 | * system property defined above. |
aoqi@0 | 85 | * <LI> Use the Services API (as detailed in the JAR specification), if available, to determine the classname. The Services API |
aoqi@0 | 86 | * will look for a classname in the file META-INF/services/javax.xml.soap.MessageFactory in jars available to the runtime. |
aoqi@0 | 87 | * <LI> Use the SAAJMetaFactory instance to locate the MessageFactory implementation class. |
aoqi@0 | 88 | * </UL> |
aoqi@0 | 89 | |
aoqi@0 | 90 | * |
aoqi@0 | 91 | * @return a new instance of a <code>MessageFactory</code> |
aoqi@0 | 92 | * |
aoqi@0 | 93 | * @exception SOAPException if there was an error in creating the |
aoqi@0 | 94 | * default implementation of the |
aoqi@0 | 95 | * <code>MessageFactory</code>. |
aoqi@0 | 96 | * @see SAAJMetaFactory |
aoqi@0 | 97 | */ |
aoqi@0 | 98 | |
aoqi@0 | 99 | public static MessageFactory newInstance() throws SOAPException { |
aoqi@0 | 100 | |
aoqi@0 | 101 | |
aoqi@0 | 102 | try { |
aoqi@0 | 103 | MessageFactory factory = (MessageFactory) FactoryFinder.find( |
aoqi@0 | 104 | MESSAGE_FACTORY_PROPERTY, |
aoqi@0 | 105 | DEFAULT_MESSAGE_FACTORY, |
aoqi@0 | 106 | false); |
aoqi@0 | 107 | |
aoqi@0 | 108 | if (factory != null) { |
aoqi@0 | 109 | return factory; |
aoqi@0 | 110 | } |
aoqi@0 | 111 | return newInstance(SOAPConstants.SOAP_1_1_PROTOCOL); |
aoqi@0 | 112 | |
aoqi@0 | 113 | } catch (Exception ex) { |
aoqi@0 | 114 | throw new SOAPException( |
aoqi@0 | 115 | "Unable to create message factory for SOAP: " |
aoqi@0 | 116 | +ex.getMessage()); |
aoqi@0 | 117 | } |
aoqi@0 | 118 | |
aoqi@0 | 119 | } |
aoqi@0 | 120 | |
aoqi@0 | 121 | /** |
aoqi@0 | 122 | * Creates a new <code>MessageFactory</code> object that is an instance |
aoqi@0 | 123 | * of the specified implementation. May be a dynamic message factory, |
aoqi@0 | 124 | * a SOAP 1.1 message factory, or a SOAP 1.2 message factory. A dynamic |
aoqi@0 | 125 | * message factory creates messages based on the MIME headers specified |
aoqi@0 | 126 | * as arguments to the <code>createMessage</code> method. |
aoqi@0 | 127 | * |
aoqi@0 | 128 | * This method uses the SAAJMetaFactory to locate the implementation class |
aoqi@0 | 129 | * and create the MessageFactory instance. |
aoqi@0 | 130 | * |
aoqi@0 | 131 | * @return a new instance of a <code>MessageFactory</code> |
aoqi@0 | 132 | * |
aoqi@0 | 133 | * @param protocol a string constant representing the class of the |
aoqi@0 | 134 | * specified message factory implementation. May be |
aoqi@0 | 135 | * either <code>DYNAMIC_SOAP_PROTOCOL</code>, |
aoqi@0 | 136 | * <code>DEFAULT_SOAP_PROTOCOL</code> (which is the same |
aoqi@0 | 137 | * as) <code>SOAP_1_1_PROTOCOL</code>, or |
aoqi@0 | 138 | * <code>SOAP_1_2_PROTOCOL</code>. |
aoqi@0 | 139 | * |
aoqi@0 | 140 | * @exception SOAPException if there was an error in creating the |
aoqi@0 | 141 | * specified implementation of <code>MessageFactory</code>. |
aoqi@0 | 142 | * @see SAAJMetaFactory |
aoqi@0 | 143 | * @since SAAJ 1.3 |
aoqi@0 | 144 | */ |
aoqi@0 | 145 | public static MessageFactory newInstance(String protocol) throws SOAPException { |
aoqi@0 | 146 | return SAAJMetaFactory.getInstance().newMessageFactory(protocol); |
aoqi@0 | 147 | } |
aoqi@0 | 148 | |
aoqi@0 | 149 | /** |
aoqi@0 | 150 | * Creates a new <code>SOAPMessage</code> object with the default |
aoqi@0 | 151 | * <code>SOAPPart</code>, <code>SOAPEnvelope</code>, <code>SOAPBody</code>, |
aoqi@0 | 152 | * and <code>SOAPHeader</code> objects. Profile-specific message factories |
aoqi@0 | 153 | * can choose to prepopulate the <code>SOAPMessage</code> object with |
aoqi@0 | 154 | * profile-specific headers. |
aoqi@0 | 155 | * <P> |
aoqi@0 | 156 | * Content can be added to this message's <code>SOAPPart</code> object, and |
aoqi@0 | 157 | * the message can be sent "as is" when a message containing only a SOAP part |
aoqi@0 | 158 | * is sufficient. Otherwise, the <code>SOAPMessage</code> object needs |
aoqi@0 | 159 | * to create one or more <code>AttachmentPart</code> objects and |
aoqi@0 | 160 | * add them to itself. Any content that is not in XML format must be |
aoqi@0 | 161 | * in an <code>AttachmentPart</code> object. |
aoqi@0 | 162 | * |
aoqi@0 | 163 | * @return a new <code>SOAPMessage</code> object |
aoqi@0 | 164 | * @exception SOAPException if a SOAP error occurs |
aoqi@0 | 165 | * @exception UnsupportedOperationException if the protocol of this |
aoqi@0 | 166 | * <code>MessageFactory</code> instance is <code>DYNAMIC_SOAP_PROTOCOL</code> |
aoqi@0 | 167 | */ |
aoqi@0 | 168 | public abstract SOAPMessage createMessage() |
aoqi@0 | 169 | throws SOAPException; |
aoqi@0 | 170 | |
aoqi@0 | 171 | /** |
aoqi@0 | 172 | * Internalizes the contents of the given <code>InputStream</code> object into a |
aoqi@0 | 173 | * new <code>SOAPMessage</code> object and returns the <code>SOAPMessage</code> |
aoqi@0 | 174 | * object. |
aoqi@0 | 175 | * |
aoqi@0 | 176 | * @param in the <code>InputStream</code> object that contains the data |
aoqi@0 | 177 | * for a message |
aoqi@0 | 178 | * @param headers the transport-specific headers passed to the |
aoqi@0 | 179 | * message in a transport-independent fashion for creation of the |
aoqi@0 | 180 | * message |
aoqi@0 | 181 | * @return a new <code>SOAPMessage</code> object containing the data from |
aoqi@0 | 182 | * the given <code>InputStream</code> object |
aoqi@0 | 183 | * |
aoqi@0 | 184 | * @exception IOException if there is a problem in reading data from |
aoqi@0 | 185 | * the input stream |
aoqi@0 | 186 | * |
aoqi@0 | 187 | * @exception SOAPException may be thrown if the message is invalid |
aoqi@0 | 188 | * |
aoqi@0 | 189 | * @exception IllegalArgumentException if the <code>MessageFactory</code> |
aoqi@0 | 190 | * requires one or more MIME headers to be present in the |
aoqi@0 | 191 | * <code>headers</code> parameter and they are missing. |
aoqi@0 | 192 | * <code>MessageFactory</code> implementations for |
aoqi@0 | 193 | * <code>SOAP_1_1_PROTOCOL</code> or |
aoqi@0 | 194 | * <code>SOAP_1_2_PROTOCOL</code> must not throw |
aoqi@0 | 195 | * <code>IllegalArgumentException</code> for this reason. |
aoqi@0 | 196 | */ |
aoqi@0 | 197 | public abstract SOAPMessage createMessage(MimeHeaders headers, |
aoqi@0 | 198 | InputStream in) |
aoqi@0 | 199 | throws IOException, SOAPException; |
aoqi@0 | 200 | } |