1 /*
2 * $Id: MessageFactory.java,v 1.9 2004/04/02 01:24:17 ofung Exp $
3 * $Revision: 1.9 $
4 * $Date: 2004/04/02 01:24:17 $
5 */
6
7 /*
8 * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
9 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
10 *
11 * This code is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License version 2 only, as
13 * published by the Free Software Foundation. Sun designates this
14 * particular file as subject to the "Classpath" exception as provided
15 * by Sun in the LICENSE file that accompanied this code.
16 *
17 * This code is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * version 2 for more details (a copy is included in the LICENSE file that
21 * accompanied this code).
22 *
23 * You should have received a copy of the GNU General Public License version
24 * 2 along with this work; if not, write to the Free Software Foundation,
25 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
26 *
27 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
28 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * have any questions.
30 */
31 package javax.xml.soap;
32
33
34 import java.io.IOException;
35 import java.io.InputStream;
36
37 /**
38 * A factory for creating <code>SOAPMessage</code> objects.
39 * <P>
40 * A SAAJ client can create a <code>MessageFactory</code> object
41 * using the method <code>newInstance</code>, as shown in the following
42 * lines of code.
43 * <PRE>
44 * MessageFactory mf = MessageFactory.newInstance();
45 * MessageFactory mf12 = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
46 * </PRE>
47 * <P>
48 * All <code>MessageFactory</code> objects, regardless of how they are
49 * created, will produce <code>SOAPMessage</code> objects that
50 * have the following elements by default:
51 * <UL>
52 * <LI>A <code>SOAPPart</code> object
53 * <LI>A <code>SOAPEnvelope</code> object
54 * <LI>A <code>SOAPBody</code> object
55 * <LI>A <code>SOAPHeader</code> object
56 * </UL>
57 * In some cases, specialized MessageFactory objects may be obtained that produce messages
58 * prepopulated with additional entries in the <code>SOAPHeader</code> object and the
59 * <code>SOAPBody</code> object.
60 * The content of a new <code>SOAPMessage</code> object depends on which of the two
61 * <code>MessageFactory</code> methods is used to create it.
62 * <UL>
63 * <LI><code>createMessage()</code> <BR>
64 * This is the method clients would normally use to create a request message.
65 * <LI><code>createMessage(MimeHeaders, java.io.InputStream)</code> -- message has
66 * content from the <code>InputStream</code> object and headers from the
67 * <code>MimeHeaders</code> object <BR>
68 * This method can be used internally by a service implementation to
69 * create a message that is a response to a request.
70 * </UL>
71 */
72 public abstract class MessageFactory {
73
74 static private final String DEFAULT_MESSAGE_FACTORY
75 = "com.sun.xml.internal.messaging.saaj.soap.ver1_1.SOAPMessageFactory1_1Impl";
76
77 static private final String MESSAGE_FACTORY_PROPERTY
78 = "javax.xml.soap.MessageFactory";
79
80 /**
81 * Creates a new <code>MessageFactory</code> object that is an instance
82 * of the default implementation (SOAP 1.1),
83 *
84 * This method uses the following ordered lookup procedure to determine the MessageFactory implementation class to load:
85 * <UL>
86 * <LI> Use the javax.xml.soap.MessageFactory system property.
87 * <LI> Use the properties file "lib/jaxm.properties" in the JRE directory. This configuration file is in standard
88 * java.util.Properties format and contains the fully qualified name of the implementation class with the key being the
89 * system property defined above.
90 * <LI> Use the Services API (as detailed in the JAR specification), if available, to determine the classname. The Services API
91 * will look for a classname in the file META-INF/services/javax.xml.soap.MessageFactory in jars available to the runtime.
92 * <LI> Use the SAAJMetaFactory instance to locate the MessageFactory implementation class.
93 * </UL>
94
95 *
96 * @return a new instance of a <code>MessageFactory</code>
97 *
98 * @exception SOAPException if there was an error in creating the
99 * default implementation of the
100 * <code>MessageFactory</code>.
101 * @see SAAJMetaFactory
102 */
103
104 public static MessageFactory newInstance()
105 throws SOAPException {
106 try {
107 MessageFactory factory = (MessageFactory)
108 FactoryFinder.find(MESSAGE_FACTORY_PROPERTY);
109
110 if (factory != null)
111 return factory;
112
113 return newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
114 } catch (Exception ex) {
115 throw new SOAPException(
116 "Unable to create message factory for SOAP: "
117 +ex.getMessage());
118 }
119
120 }
121
122 /**
123 * Creates a new <code>MessageFactory</code> object that is an instance
124 * of the specified implementation. May be a dynamic message factory,
125 * a SOAP 1.1 message factory, or a SOAP 1.2 message factory. A dynamic
126 * message factory creates messages based on the MIME headers specified
127 * as arguments to the <code>createMessage</code> method.
128 *
129 * This method uses the SAAJMetaFactory to locate the implementation class
130 * and create the MessageFactory instance.
131 *
132 * @return a new instance of a <code>MessageFactory</code>
133 *
134 * @param protocol a string constant representing the class of the
135 * specified message factory implementation. May be
136 * either <code>DYNAMIC_SOAP_PROTOCOL</code>,
137 * <code>DEFAULT_SOAP_PROTOCOL</code> (which is the same
138 * as) <code>SOAP_1_1_PROTOCOL</code>, or
139 * <code>SOAP_1_2_PROTOCOL</code>.
140 *
141 * @exception SOAPException if there was an error in creating the
142 * specified implementation of <code>MessageFactory</code>.
143 * @see SAAJMetaFactory
144 * @since SAAJ 1.3
145 */
146 public static MessageFactory newInstance(String protocol) throws SOAPException {
147 return SAAJMetaFactory.getInstance().newMessageFactory(protocol);
148 }
149
150 /**
151 * Creates a new <code>SOAPMessage</code> object with the default
152 * <code>SOAPPart</code>, <code>SOAPEnvelope</code>, <code>SOAPBody</code>,
153 * and <code>SOAPHeader</code> objects. Profile-specific message factories
154 * can choose to prepopulate the <code>SOAPMessage</code> object with
155 * profile-specific headers.
156 * <P>
157 * Content can be added to this message's <code>SOAPPart</code> object, and
158 * the message can be sent "as is" when a message containing only a SOAP part
159 * is sufficient. Otherwise, the <code>SOAPMessage</code> object needs
160 * to create one or more <code>AttachmentPart</code> objects and
161 * add them to itself. Any content that is not in XML format must be
162 * in an <code>AttachmentPart</code> object.
163 *
164 * @return a new <code>SOAPMessage</code> object
165 * @exception SOAPException if a SOAP error occurs
166 * @exception UnsupportedOperationException if the protocol of this
167 * <code>MessageFactory</code> instance is <code>DYNAMIC_SOAP_PROTOCOL</code>
168 */
169 public abstract SOAPMessage createMessage()
170 throws SOAPException;
171
172 /**
173 * Internalizes the contents of the given <code>InputStream</code> object into a
174 * new <code>SOAPMessage</code> object and returns the <code>SOAPMessage</code>
175 * object.
176 *
177 * @param in the <code>InputStream</code> object that contains the data
178 * for a message
179 * @param headers the transport-specific headers passed to the
180 * message in a transport-independent fashion for creation of the
181 * message
182 * @return a new <code>SOAPMessage</code> object containing the data from
183 * the given <code>InputStream</code> object
184 *
185 * @exception IOException if there is a problem in reading data from
186 * the input stream
187 *
188 * @exception SOAPException may be thrown if the message is invalid
189 *
190 * @exception IllegalArgumentException if the <code>MessageFactory</code>
191 * requires one or more MIME headers to be present in the
192 * <code>headers</code> parameter and they are missing.
193 * <code>MessageFactory</code> implementations for
194 * <code>SOAP_1_1_PROTOCOL</code> or
195 * <code>SOAP_1_2_PROTOCOL</code> must not throw
196 * <code>IllegalArgumentException</code> for this reason.
197 */
198 public abstract SOAPMessage createMessage(MimeHeaders headers,
199 InputStream in)
200 throws IOException, SOAPException;
201 }