001    /*
002     * Copyright 2001-2004 The Apache Software Foundation.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package javax.xml.soap;
017    
018    import java.io.IOException;
019    import java.io.InputStream;
020    
021    /**
022     * <P>A factory for creating <CODE>SOAPMessage</CODE> objects.</P>
023     *
024     *   <P>A JAXM client performs the following steps to create a
025     *   message.</P>
026     *
027     *   <UL>
028     *     <LI>
029     *       Creates a <CODE>MessageFactory</CODE> object from a <CODE>
030     *       ProviderConnection</CODE> object (<CODE>con</CODE> in the
031     *       following line of code). The <CODE>String</CODE> passed to
032     *       the <CODE>createMessageFactory</CODE> method is the name of
033     *       of a messaging profile, which must be the URL for the
034     *       schema.
035     * <PRE>
036     *      MessageFactory mf = con.createMessageFactory(schemaURL);
037     * </PRE>
038     *     </LI>
039     *
040     *     <LI>
041     *       Calls the method <CODE>createMessage</CODE> on the <CODE>
042     *       MessageFactory</CODE> object. All messages produced by this
043     *       <CODE>MessageFactory</CODE> object will have the header
044     *       information appropriate for the messaging profile that was
045     *       specified when the <CODE>MessageFactory</CODE> object was
046     *       created.
047     * <PRE>
048     *      SOAPMessage m = mf.createMessage();
049     * </PRE>
050     *     </LI>
051     *   </UL>
052     *   It is also possible to create a <CODE>MessageFactory</CODE>
053     *   object using the method <CODE>newInstance</CODE>, as shown in
054     *   the following line of code.
055     * <PRE>
056     *      MessageFactory mf = MessageFactory.newInstance();
057     * </PRE>
058     *   A standalone client (a client that is not running in a
059     *   container) can use the <CODE>newInstance</CODE> method to
060     *   create a <CODE>MessageFactory</CODE> object.
061     *
062     *   <P>All <CODE>MessageFactory</CODE> objects, regardless of how
063     *   they are created, will produce <CODE>SOAPMessage</CODE> objects
064     *   that have the following elements by default:</P>
065     *
066     *   <UL>
067     *     <LI>A <CODE>SOAPPart</CODE> object</LI>
068     *
069     *     <LI>A <CODE>SOAPEnvelope</CODE> object</LI>
070     *
071     *     <LI>A <CODE>SOAPBody</CODE> object</LI>
072     *
073     *     <LI>A <CODE>SOAPHeader</CODE> object</LI>
074     *   </UL>
075     *   If a <CODE>MessageFactory</CODE> object was created using a
076     *   <CODE>ProviderConnection</CODE> object, which means that it was
077     *   initialized with a specified profile, it will produce messages
078     *   that also come prepopulated with additional entries in the
079     *   <CODE>SOAPHeader</CODE> object and the <CODE>SOAPBody</CODE>
080     *   object. The content of a new <CODE>SOAPMessage</CODE> object
081     *   depends on which of the two <CODE>MessageFactory</CODE> methods
082     *   is used to create it.
083     *
084     *   <UL>
085     *     <LI><CODE>createMessage()</CODE> -- message has no
086     *     content<BR>
087     *      This is the method clients would normally use to create a
088     *     request message.</LI>
089     *
090     *     <LI><CODE>createMessage(MimeHeaders,
091     *     java.io.InputStream)</CODE> -- message has content from the
092     *     <CODE>InputStream</CODE> object and headers from the <CODE>
093     *     MimeHeaders</CODE> object<BR>
094     *      This method can be used internally by a service
095     *     implementation to create a message that is a response to a
096     *     request.</LI>
097     *   </UL>
098     */
099    public abstract class MessageFactory {
100    
101        // fixme: this should be protected as the class is abstract.
102        /** Create a new MessageFactory. */
103        public MessageFactory() {}
104    
105        /**
106         * Creates a new <CODE>MessageFactory</CODE> object that is
107         * an instance of the default implementation.
108         * @return a new <CODE>MessageFactory</CODE> object
109         * @throws  SOAPException  if there was an error in
110         *     creating the default implementation of the <CODE>
111         *     MessageFactory</CODE>
112         */
113        public static MessageFactory newInstance() throws SOAPException {
114    
115            try {
116                return (MessageFactory) FactoryFinder.find(MESSAGE_FACTORY_PROPERTY,
117                                                           DEFAULT_MESSAGE_FACTORY);
118            } catch (Exception exception) {
119                throw new SOAPException(
120                    "Unable to create message factory for SOAP: "
121                    + exception.getMessage());
122            }
123        }
124    
125        /**
126         * Creates a new <CODE>SOAPMessage</CODE> object with the
127         *   default <CODE>SOAPPart</CODE>, <CODE>SOAPEnvelope</CODE>,
128         *   <CODE>SOAPBody</CODE>, and <CODE>SOAPHeader</CODE> objects.
129         *   Profile-specific message factories can choose to
130         *   prepopulate the <CODE>SOAPMessage</CODE> object with
131         *   profile-specific headers.
132         *
133         *   <P>Content can be added to this message's <CODE>
134         *   SOAPPart</CODE> object, and the message can be sent "as is"
135         *   when a message containing only a SOAP part is sufficient.
136         *   Otherwise, the <CODE>SOAPMessage</CODE> object needs to
137         *   create one or more <CODE>AttachmentPart</CODE> objects and
138         *   add them to itself. Any content that is not in XML format
139         *   must be in an <CODE>AttachmentPart</CODE> object.</P>
140         * @return  a new <CODE>SOAPMessage</CODE> object
141         * @throws  SOAPException if a SOAP error occurs
142         */
143        public abstract SOAPMessage createMessage() throws SOAPException;
144    
145        /**
146         * Internalizes the contents of the given <CODE>
147         * InputStream</CODE> object into a new <CODE>SOAPMessage</CODE>
148         * object and returns the <CODE>SOAPMessage</CODE> object.
149         * @param   mimeheaders    the transport-specific headers
150         *     passed to the message in a transport-independent fashion
151         *     for creation of the message
152         * @param   inputstream    the <CODE>InputStream</CODE> object
153         *     that contains the data for a message
154         * @return a new <CODE>SOAPMessage</CODE> object containing the
155         *     data from the given <CODE>InputStream</CODE> object
156         * @throws  IOException    if there is a
157         *     problem in reading data from the input stream
158         * @throws  SOAPException  if the message is invalid
159         */
160        public abstract SOAPMessage createMessage(
161            MimeHeaders mimeheaders, InputStream inputstream)
162                throws IOException, SOAPException;
163    
164        private static final String DEFAULT_MESSAGE_FACTORY =
165            "org.apache.axis.soap.MessageFactoryImpl";
166    
167        private static final String MESSAGE_FACTORY_PROPERTY =
168            "javax.xml.soap.MessageFactory";
169    }