001 /*
002 * $Id: MessageFactory.java,v 1.9 2004/04/02 01:24:17 ofung Exp $
003 * $Revision: 1.9 $
004 * $Date: 2004/04/02 01:24:17 $
005 */
006
007 /*
008 * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
009 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
010 *
011 * This code is free software; you can redistribute it and/or modify it
012 * under the terms of the GNU General Public License version 2 only, as
013 * published by the Free Software Foundation. Sun designates this
014 * particular file as subject to the "Classpath" exception as provided
015 * by Sun in the LICENSE file that accompanied this code.
016 *
017 * This code is distributed in the hope that it will be useful, but WITHOUT
018 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
019 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
020 * version 2 for more details (a copy is included in the LICENSE file that
021 * accompanied this code).
022 *
023 * You should have received a copy of the GNU General Public License version
024 * 2 along with this work; if not, write to the Free Software Foundation,
025 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
026 *
027 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
028 * CA 95054 USA or visit www.sun.com if you need additional information or
029 * have any questions.
030 */
031 package javax.xml.soap;
032
033 import java.io.IOException;
034 import java.io.InputStream;
035
036 /**
037 * A factory for creating <code>SOAPMessage</code> objects.
038 * <P>
039 * A SAAJ client can create a <code>MessageFactory</code> object
040 * using the method <code>newInstance</code>, as shown in the following
041 * lines of code.
042 * <PRE>
043 * MessageFactory mf = MessageFactory.newInstance();
044 * MessageFactory mf12 = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
045 * </PRE>
046 * <P>
047 * All <code>MessageFactory</code> objects, regardless of how they are
048 * created, will produce <code>SOAPMessage</code> objects that
049 * have the following elements by default:
050 * <UL>
051 * <LI>A <code>SOAPPart</code> object
052 * <LI>A <code>SOAPEnvelope</code> object
053 * <LI>A <code>SOAPBody</code> object
054 * <LI>A <code>SOAPHeader</code> object
055 * </UL>
056 * In some cases, specialized MessageFactory objects may be obtained that produce messages
057 * prepopulated with additional entries in the <code>SOAPHeader</code> object and the
058 * <code>SOAPBody</code> object.
059 * The content of a new <code>SOAPMessage</code> object depends on which of the two
060 * <code>MessageFactory</code> methods is used to create it.
061 * <UL>
062 * <LI><code>createMessage()</code> <BR>
063 * This is the method clients would normally use to create a request message.
064 * <LI><code>createMessage(MimeHeaders, java.io.InputStream)</code> -- message has
065 * content from the <code>InputStream</code> object and headers from the
066 * <code>MimeHeaders</code> object <BR>
067 * This method can be used internally by a service implementation to
068 * create a message that is a response to a request.
069 * </UL>
070 */
071 public abstract class MessageFactory {
072
073 static private final String DEFAULT_MESSAGE_FACTORY = "com.sun.xml.internal.messaging.saaj.soap.ver1_1.SOAPMessageFactory1_1Impl";
074
075 static private final String MESSAGE_FACTORY_PROPERTY = "javax.xml.soap.MessageFactory";
076
077 /**
078 * Creates a new <code>MessageFactory</code> object that is an instance
079 * of the default implementation (SOAP 1.1),
080 *
081 * This method uses the following ordered lookup procedure to determine the MessageFactory implementation class to load:
082 * <UL>
083 * <LI> Use the javax.xml.soap.MessageFactory system property.
084 * <LI> Use the properties file "lib/jaxm.properties" in the JRE directory. This configuration file is in standard
085 * java.util.Properties format and contains the fully qualified name of the implementation class with the key being the
086 * system property defined above.
087 * <LI> Use the Services API (as detailed in the JAR specification), if available, to determine the classname. The Services API
088 * will look for a classname in the file META-INF/services/javax.xml.soap.MessageFactory in jars available to the runtime.
089 * <LI> Use the SAAJMetaFactory instance to locate the MessageFactory implementation class.
090 * </UL>
091
092 *
093 * @return a new instance of a <code>MessageFactory</code>
094 *
095 * @exception SOAPException if there was an error in creating the
096 * default implementation of the
097 * <code>MessageFactory</code>.
098 * @see SAAJMetaFactory
099 */
100
101 public static MessageFactory newInstance() throws SOAPException {
102 try {
103 MessageFactory factory = (MessageFactory) FactoryFinder
104 .find(MESSAGE_FACTORY_PROPERTY);
105
106 if (factory != null)
107 return factory;
108
109 return newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
110 } catch (Exception ex) {
111 throw new SOAPException(
112 "Unable to create message factory for SOAP: "
113 + ex.getMessage());
114 }
115
116 }
117
118 /**
119 * Creates a new <code>MessageFactory</code> object that is an instance
120 * of the specified implementation. May be a dynamic message factory,
121 * a SOAP 1.1 message factory, or a SOAP 1.2 message factory. A dynamic
122 * message factory creates messages based on the MIME headers specified
123 * as arguments to the <code>createMessage</code> method.
124 *
125 * This method uses the SAAJMetaFactory to locate the implementation class
126 * and create the MessageFactory instance.
127 *
128 * @return a new instance of a <code>MessageFactory</code>
129 *
130 * @param protocol a string constant representing the class of the
131 * specified message factory implementation. May be
132 * either <code>DYNAMIC_SOAP_PROTOCOL</code>,
133 * <code>DEFAULT_SOAP_PROTOCOL</code> (which is the same
134 * as) <code>SOAP_1_1_PROTOCOL</code>, or
135 * <code>SOAP_1_2_PROTOCOL</code>.
136 *
137 * @exception SOAPException if there was an error in creating the
138 * specified implementation of <code>MessageFactory</code>.
139 * @see SAAJMetaFactory
140 * @since SAAJ 1.3
141 */
142 public static MessageFactory newInstance(String protocol)
143 throws SOAPException {
144 return SAAJMetaFactory.getInstance()
145 .newMessageFactory(protocol);
146 }
147
148 /**
149 * Creates a new <code>SOAPMessage</code> object with the default
150 * <code>SOAPPart</code>, <code>SOAPEnvelope</code>, <code>SOAPBody</code>,
151 * and <code>SOAPHeader</code> objects. Profile-specific message factories
152 * can choose to prepopulate the <code>SOAPMessage</code> object with
153 * profile-specific headers.
154 * <P>
155 * Content can be added to this message's <code>SOAPPart</code> object, and
156 * the message can be sent "as is" when a message containing only a SOAP part
157 * is sufficient. Otherwise, the <code>SOAPMessage</code> object needs
158 * to create one or more <code>AttachmentPart</code> objects and
159 * add them to itself. Any content that is not in XML format must be
160 * in an <code>AttachmentPart</code> object.
161 *
162 * @return a new <code>SOAPMessage</code> object
163 * @exception SOAPException if a SOAP error occurs
164 * @exception UnsupportedOperationException if the protocol of this
165 * <code>MessageFactory</code> instance is <code>DYNAMIC_SOAP_PROTOCOL</code>
166 */
167 public abstract SOAPMessage createMessage() throws SOAPException;
168
169 /**
170 * Internalizes the contents of the given <code>InputStream</code> object into a
171 * new <code>SOAPMessage</code> object and returns the <code>SOAPMessage</code>
172 * object.
173 *
174 * @param in the <code>InputStream</code> object that contains the data
175 * for a message
176 * @param headers the transport-specific headers passed to the
177 * message in a transport-independent fashion for creation of the
178 * message
179 * @return a new <code>SOAPMessage</code> object containing the data from
180 * the given <code>InputStream</code> object
181 *
182 * @exception IOException if there is a problem in reading data from
183 * the input stream
184 *
185 * @exception SOAPException may be thrown if the message is invalid
186 *
187 * @exception IllegalArgumentException if the <code>MessageFactory</code>
188 * requires one or more MIME headers to be present in the
189 * <code>headers</code> parameter and they are missing.
190 * <code>MessageFactory</code> implementations for
191 * <code>SOAP_1_1_PROTOCOL</code> or
192 * <code>SOAP_1_2_PROTOCOL</code> must not throw
193 * <code>IllegalArgumentException</code> for this reason.
194 */
195 public abstract SOAPMessage createMessage(MimeHeaders headers,
196 InputStream in) throws IOException, SOAPException;
197 }
|