KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > transport > TransportUtils


1 /*
2  * Copyright 2004,2005 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 org.apache.axis2.transport;
17
18 import org.apache.axis2.context.MessageContext;
19 import org.apache.axis2.context.OperationContext;
20 import org.apache.axis2.engine.AxisFault;
21 import org.apache.axis2.om.impl.llom.builder.StAXBuilder;
22 import org.apache.axis2.om.impl.llom.builder.StAXOMBuilder;
23 import org.apache.axis2.soap.SOAPEnvelope;
24 import org.apache.axis2.soap.SOAPFactory;
25 import org.apache.axis2.soap.impl.llom.builder.StAXSOAPModelBuilder;
26 import org.apache.axis2.soap.impl.llom.soap11.SOAP11Factory;
27 import org.apache.axis2.transport.http.HTTPConstants;
28 import org.apache.axis2.transport.http.HTTPTransportUtils;
29
30 import javax.xml.stream.XMLInputFactory;
31 import javax.xml.stream.XMLStreamReader;
32 import java.io.InputStream JavaDoc;
33 import java.io.InputStreamReader JavaDoc;
34 import java.io.Reader JavaDoc;
35
36 public class TransportUtils {
37     public static SOAPEnvelope createSOAPMessage(MessageContext msgContext) throws AxisFault {
38
39         InputStream JavaDoc inStream = (InputStream JavaDoc) msgContext.getProperty(MessageContext.TRANSPORT_IN);
40         msgContext.setProperty(MessageContext.TRANSPORT_IN, null);
41         if (inStream == null) {
42             throw new AxisFault("Input stream is Null");
43         }
44         return createSOAPMessage(msgContext, inStream);
45     }
46
47     public static SOAPEnvelope createSOAPMessage(MessageContext msgContext, InputStream JavaDoc inStream)
48         throws AxisFault {
49         try {
50             Object JavaDoc contentType = null;
51             OperationContext opContext = msgContext.getOperationContext();
52             if (opContext != null) {
53                 contentType = opContext.getProperty(HTTPConstants.MTOM_RECIVED_CONTENT_TYPE);
54             }else{
55                 throw new AxisFault("Operation Context can not be Null");
56             }
57
58             StAXBuilder builder = null;
59             SOAPEnvelope envelope = null;
60
61             if (contentType != null) {
62                 msgContext.setDoingMTOM(true);
63                 builder = HTTPTransportUtils.selectBuilderForMIME(msgContext, inStream, (String JavaDoc)contentType);
64                 envelope = (SOAPEnvelope) builder.getDocumentElement();
65             }else if (msgContext.isDoingREST()) {
66                 Reader reader = new InputStreamReader JavaDoc(inStream);
67                 XMLStreamReader xmlreader =
68                     XMLInputFactory.newInstance().createXMLStreamReader(reader);
69                 SOAPFactory soapFactory = new SOAP11Factory();
70                 builder = new StAXOMBuilder(xmlreader);
71                 builder.setOmbuilderFactory(soapFactory);
72                 envelope = soapFactory.getDefaultEnvelope();
73                 envelope.getBody().addChild(builder.getDocumentElement());
74             } else {
75                 Reader reader = new InputStreamReader JavaDoc(inStream);
76                 XMLStreamReader xmlreader =
77                     XMLInputFactory.newInstance().createXMLStreamReader(reader);
78                 builder = new StAXSOAPModelBuilder(xmlreader);
79                 envelope = (SOAPEnvelope) builder.getDocumentElement();
80             }
81             return envelope;
82         } catch (Exception JavaDoc e) {
83             throw new AxisFault(e);
84         }
85     }
86
87 }
88
Popular Tags