| 1 |
/* |
| 2 |
* Copyright 2001-2004 The Apache Software Foundation. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
package javax.xml.soap; |
| 17 |
|
| 18 |
import java.io.IOException; |
| 19 |
import java.io.InputStream; |
| 20 |
|
| 21 |
/** |
| 22 |
* <P>A factory for creating <CODE>SOAPMessage</CODE> objects.</P> |
| 23 |
* |
| 24 |
* <P>A JAXM client performs the following steps to create a |
| 25 |
* message.</P> |
| 26 |
* |
| 27 |
* <UL> |
| 28 |
* <LI> |
| 29 |
* Creates a <CODE>MessageFactory</CODE> object from a <CODE> |
| 30 |
* ProviderConnection</CODE> object (<CODE>con</CODE> in the |
| 31 |
* following line of code). The <CODE>String</CODE> passed to |
| 32 |
* the <CODE>createMessageFactory</CODE> method is the name of |
| 33 |
* of a messaging profile, which must be the URL for the |
| 34 |
* schema. |
| 35 |
* <PRE> |
| 36 |
* MessageFactory mf = con.createMessageFactory(schemaURL); |
| 37 |
* </PRE> |
| 38 |
* </LI> |
| 39 |
* |
| 40 |
* <LI> |
| 41 |
* Calls the method <CODE>createMessage</CODE> on the <CODE> |
| 42 |
* MessageFactory</CODE> object. All messages produced by this |
| 43 |
* <CODE>MessageFactory</CODE> object will have the header |
| 44 |
* information appropriate for the messaging profile that was |
| 45 |
* specified when the <CODE>MessageFactory</CODE> object was |
| 46 |
* created. |
| 47 |
* <PRE> |
| 48 |
* SOAPMessage m = mf.createMessage(); |
| 49 |
* </PRE> |
| 50 |
* </LI> |
| 51 |
* </UL> |
| 52 |
* It is also possible to create a <CODE>MessageFactory</CODE> |
| 53 |
* object using the method <CODE>newInstance</CODE>, as shown in |
| 54 |
* the following line of code. |
| 55 |
* <PRE> |
| 56 |
* MessageFactory mf = MessageFactory.newInstance(); |
| 57 |
* </PRE> |
| 58 |
* A standalone client (a client that is not running in a |
| 59 |
* container) can use the <CODE>newInstance</CODE> method to |
| 60 |
* create a <CODE>MessageFactory</CODE> object. |
| 61 |
* |
| 62 |
* <P>All <CODE>MessageFactory</CODE> objects, regardless of how |
| 63 |
* they are created, will produce <CODE>SOAPMessage</CODE> objects |
| 64 |
* that have the following elements by default:</P> |
| 65 |
* |
| 66 |
* <UL> |
| 67 |
* <LI>A <CODE>SOAPPart</CODE> object</LI> |
| 68 |
* |
| 69 |
* <LI>A <CODE>SOAPEnvelope</CODE> object</LI> |
| 70 |
* |
| 71 |
* <LI>A <CODE>SOAPBody</CODE> object</LI> |
| 72 |
* |
| 73 |
* <LI>A <CODE>SOAPHeader</CODE> object</LI> |
| 74 |
* </UL> |
| 75 |
* If a <CODE>MessageFactory</CODE> object was created using a |
| 76 |
* <CODE>ProviderConnection</CODE> object, which means that it was |
| 77 |
* initialized with a specified profile, it will produce messages |
| 78 |
* that also come prepopulated with additional entries in the |
| 79 |
* <CODE>SOAPHeader</CODE> object and the <CODE>SOAPBody</CODE> |
| 80 |
* object. The content of a new <CODE>SOAPMessage</CODE> object |
| 81 |
* depends on which of the two <CODE>MessageFactory</CODE> methods |
| 82 |
* is used to create it. |
| 83 |
* |
| 84 |
* <UL> |
| 85 |
* <LI><CODE>createMessage()</CODE> -- message has no |
| 86 |
* content<BR> |
| 87 |
* This is the method clients would normally use to create a |
| 88 |
* request message.</LI> |
| 89 |
* |
| 90 |
* <LI><CODE>createMessage(MimeHeaders, |
| 91 |
* java.io.InputStream)</CODE> -- message has content from the |
| 92 |
* <CODE>InputStream</CODE> object and headers from the <CODE> |
| 93 |
* MimeHeaders</CODE> object<BR> |
| 94 |
* This method can be used internally by a service |
| 95 |
* implementation to create a message that is a response to a |
| 96 |
* request.</LI> |
| 97 |
* </UL> |
| 98 |
*/ |
| 99 |
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 |
} |