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