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 org.apache.axis2.saaj;
020:
021: import org.apache.axiom.soap.impl.dom.soap11.SOAP11Factory;
022: import org.apache.axiom.soap.impl.dom.soap12.SOAP12Factory;
023:
024: import javax.xml.soap.MessageFactory;
025: import javax.xml.soap.MimeHeaders;
026: import javax.xml.soap.SOAPConstants;
027: import javax.xml.soap.SOAPException;
028: import javax.xml.soap.SOAPMessage;
029: import java.io.IOException;
030: import java.io.InputStream;
031:
032: /**
033: * <P>A factory for creating <CODE>SOAPMessage</CODE> objects.</P>
034: * <p/>
035: * <P>A JAXM client performs the following steps to create a message.</P>
036: * <p/>
037: * <UL> <LI> Creates a <CODE>MessageFactory</CODE> object from a <CODE> ProviderConnection</CODE>
038: * object (<CODE>con</CODE> in the following line of code). The <CODE>String</CODE> passed to the
039: * <CODE>createMessageFactory</CODE> method is the name of of a messaging profile, which must be the
040: * URL for the schema. <PRE> MessageFactory mf = con.createMessageFactory(schemaURL); </PRE> </LI>
041: * <p/>
042: * <LI> Calls the method <CODE>createMessage</CODE> on the <CODE> MessageFactory</CODE> object. All
043: * messages produced by this <CODE>MessageFactory</CODE> object will have the header information
044: * appropriate for the messaging profile that was specified when the <CODE>MessageFactory</CODE>
045: * object was created. <PRE> SOAPMessage m = mf.createMessage(); </PRE> </LI> </UL> It is also
046: * possible to create a <CODE>MessageFactory</CODE> object using the method
047: * <CODE>newInstance</CODE>, as shown in the following line of code. <PRE> MessageFactory mf =
048: * MessageFactory.newInstance(); </PRE> A standalone client (a client that is not running in a
049: * container) can use the <CODE>newInstance</CODE> method to create a <CODE>MessageFactory</CODE>
050: * object.
051: * <p/>
052: * <P>All <CODE>MessageFactory</CODE> objects, regardless of how they are created, will produce
053: * <CODE>SOAPMessage</CODE> objects that have the following elements by default:</P>
054: * <p/>
055: * <UL> <LI>A <CODE>SOAPPart</CODE> object</LI>
056: * <p/>
057: * <LI>A <CODE>SOAPEnvelope</CODE> object</LI>
058: * <p/>
059: * <LI>A <CODE>SOAPBody</CODE> object</LI>
060: * <p/>
061: * <LI>A <CODE>SOAPHeader</CODE> object</LI> </UL> If a <CODE>MessageFactory</CODE> object was
062: * created using a <CODE>ProviderConnection</CODE> object, which means that it was initialized with
063: * a specified profile, it will produce messages that also come prepopulated with additional entries
064: * in the <CODE>SOAPHeader</CODE> object and the <CODE>SOAPBody</CODE> object. The content of a new
065: * <CODE>SOAPMessage</CODE> object depends on which of the two <CODE>MessageFactory</CODE> methods
066: * is used to create it.
067: * <p/>
068: * <UL> <LI><CODE>createMessage()</CODE> -- message has no content<BR> This is the method clients
069: * would normally use to create a request message.</LI>
070: * <p/>
071: * <LI><CODE>createMessage(MimeHeaders, java.io.InputStream)</CODE> -- message has content from the
072: * <CODE>InputStream</CODE> object and headers from the <CODE> MimeHeaders</CODE> object<BR> This
073: * method can be used internally by a service implementation to create a message that is a response
074: * to a request.</LI> </UL>
075: */
076: public class MessageFactoryImpl extends MessageFactory {
077:
078: protected String soapVersion = SOAPConstants.SOAP_1_1_PROTOCOL;
079:
080: /**
081: * Creates a new <CODE>SOAPMessage</CODE> object with the default <CODE>SOAPPart</CODE>,
082: * <CODE>SOAPEnvelope</CODE>, <CODE>SOAPBody</CODE>, and <CODE>SOAPHeader</CODE> objects.
083: * Profile-specific message factories can choose to prepopulate the <CODE>SOAPMessage</CODE>
084: * object with profile-specific headers.
085: * <p/>
086: * <P>Content can be added to this message's <CODE> SOAPPart</CODE> object, and the message can
087: * be sent "as is" when a message containing only a SOAP part is sufficient. Otherwise, the
088: * <CODE>SOAPMessage</CODE> object needs to create one or more <CODE>AttachmentPart</CODE>
089: * objects and add them to itself. Any content that is not in XML format must be in an
090: * <CODE>AttachmentPart</CODE> object.</P>
091: *
092: * @return a new <CODE>SOAPMessage</CODE> object
093: * @throws SOAPException if a SOAP error occurs java.lang.UnsupportedOperationException - if the
094: * protocol of this MessageFactory instance is DYNAMIC_SOAP_PROTOCOL
095: */
096: public SOAPMessage createMessage() throws SOAPException {
097: SOAPEnvelopeImpl soapEnvelope;
098: if (soapVersion.equals(SOAPConstants.SOAP_1_2_PROTOCOL)) {
099: soapEnvelope = new SOAPEnvelopeImpl(
100: (org.apache.axiom.soap.impl.dom.SOAPEnvelopeImpl) new SOAP12Factory()
101: .getDefaultEnvelope());
102: } else if (soapVersion
103: .equals(SOAPConstants.DYNAMIC_SOAP_PROTOCOL)) {
104: throw new UnsupportedOperationException(
105: "createMessage() is not supported for "
106: + "DYNAMIC_SOAP_PROTOCOL");
107: } else {
108: //SOAP 1.1
109: soapEnvelope = new SOAPEnvelopeImpl(
110: (org.apache.axiom.soap.impl.dom.SOAPEnvelopeImpl) new SOAP11Factory()
111: .getDefaultEnvelope());
112: }
113: SOAPMessageImpl soapMessage = new SOAPMessageImpl(soapEnvelope);
114: soapMessage.setSaveRequired();
115: return soapMessage;
116: }
117:
118: /**
119: * Internalizes the contents of the given <CODE> InputStream</CODE> object into a new
120: * <CODE>SOAPMessage</CODE> object and returns the <CODE>SOAPMessage</CODE> object.
121: *
122: * @param mimeheaders the transport-specific headers passed to the message in a
123: * transport-independent fashion for creation of the message
124: * @param inputstream the <CODE>InputStream</CODE> object that contains the data for a message
125: * @return a new <CODE>SOAPMessage</CODE> object containing the data from the given
126: * <CODE>InputStream</CODE> object
127: * @throws IOException if there is a problem in reading data from the input stream
128: * @throws SOAPException if the message is invalid
129: */
130: public SOAPMessage createMessage(MimeHeaders mimeheaders,
131: InputStream inputstream) throws IOException, SOAPException {
132: SOAPMessageImpl soapMessage = new SOAPMessageImpl(inputstream,
133: mimeheaders);
134: soapMessage.setSaveRequired();
135: return soapMessage;
136: }
137:
138: public void setSOAPVersion(String soapVersion) {
139: this.soapVersion = soapVersion;
140: }
141: }
|