1 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 ; 33 import java.io.InputStreamReader ; 34 import java.io.Reader ; 35 36 public class TransportUtils { 37 public static SOAPEnvelope createSOAPMessage(MessageContext msgContext) throws AxisFault { 38 39 InputStream inStream = (InputStream ) 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 inStream) 48 throws AxisFault { 49 try { 50 Object 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 )contentType); 64 envelope = (SOAPEnvelope) builder.getDocumentElement(); 65 }else if (msgContext.isDoingREST()) { 66 Reader reader = new InputStreamReader (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 (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 e) { 83 throw new AxisFault(e); 84 } 85 } 86 87 } 88 | Popular Tags |