1 18 package org.apache.axis2.transport.http; 19 20 import org.apache.axis2.Constants; 21 import org.apache.axis2.addressing.AddressingConstants; 22 import org.apache.axis2.addressing.EndpointReference; 23 import org.apache.axis2.attachments.MIMEHelper; 24 import org.apache.axis2.context.ConfigurationContext; 25 import org.apache.axis2.context.MessageContext; 26 import org.apache.axis2.engine.AxisEngine; 27 import org.apache.axis2.engine.AxisFault; 28 import org.apache.axis2.om.*; 29 import org.apache.axis2.om.impl.llom.OMNamespaceImpl; 30 import org.apache.axis2.om.impl.llom.builder.StAXBuilder; 31 import org.apache.axis2.om.impl.llom.builder.StAXOMBuilder; 32 import org.apache.axis2.om.impl.llom.mtom.MTOMStAXSOAPModelBuilder; 33 import org.apache.axis2.soap.SOAPEnvelope; 34 import org.apache.axis2.soap.SOAPFactory; 35 import org.apache.axis2.soap.impl.llom.SOAPProcessingException; 36 import org.apache.axis2.soap.impl.llom.builder.StAXSOAPModelBuilder; 37 import org.apache.axis2.soap.impl.llom.soap11.SOAP11Constants; 38 import org.apache.axis2.soap.impl.llom.soap11.SOAP11Factory; 39 import org.apache.axis2.util.Utils; 40 41 import javax.xml.parsers.FactoryConfigurationError ; 42 import javax.xml.stream.XMLInputFactory; 43 import javax.xml.stream.XMLStreamException; 44 import javax.xml.stream.XMLStreamReader; 45 import java.io.*; 46 import java.util.Iterator ; 47 import java.util.Map ; 48 49 public class HTTPTransportUtils { 50 51 public static void processHTTPPostRequest(MessageContext msgContext, 52 InputStream in, OutputStream out, String contentType, 53 String soapAction, String requestURI, 54 ConfigurationContext configurationContext) throws AxisFault { 55 try { 56 if(soapAction != null && soapAction.startsWith("\"") && soapAction.endsWith("\"")){ 57 soapAction = soapAction.substring(1,soapAction.length() -1); 58 } 59 msgContext.setWSAAction(soapAction); 60 msgContext.setSoapAction(soapAction); 61 msgContext.setTo(new EndpointReference(AddressingConstants.WSA_TO, 62 requestURI)); 63 msgContext.setProperty(MessageContext.TRANSPORT_OUT, out); 64 msgContext.setServerSide(true); 65 66 SOAPEnvelope envelope = null; 67 StAXBuilder builder = null; 68 69 70 if (contentType.indexOf(HTTPConstants.HEADER_ACCEPT_MULTIPART_RELATED) >= 0){ 71 builder = selectBuilderForMIME(msgContext, in, contentType); 72 envelope = (SOAPEnvelope) builder.getDocumentElement(); 73 } else if (contentType != null 74 && contentType.indexOf(SOAP11Constants.SOAP_11_CONTENT_TYPE) > -1) { 75 Object enable = msgContext.getProperty(Constants.Configuration.ENABLE_REST); 78 if ((soapAction == null || soapAction.length() == 0) 79 && Constants.VALUE_TRUE.equals(enable)) { 80 msgContext.setDoingREST(true); 81 SOAPFactory soapFactory = new SOAP11Factory(); 82 Reader reader = new InputStreamReader(in); 83 XMLStreamReader xmlreader = XMLInputFactory.newInstance() 84 .createXMLStreamReader(reader); 85 builder = new StAXOMBuilder(xmlreader); 86 builder.setOmbuilderFactory(soapFactory); 87 envelope = soapFactory.getDefaultEnvelope(); 88 envelope.getBody().addChild(builder.getDocumentElement()); 89 } 90 } 91 92 if (envelope == null) { 93 Reader reader = new InputStreamReader(in); 94 XMLStreamReader xmlreader = XMLInputFactory.newInstance() 95 .createXMLStreamReader(reader); 96 builder = new StAXSOAPModelBuilder(xmlreader); 97 envelope = (SOAPEnvelope) builder.getDocumentElement(); 98 } 99 100 msgContext.setEnvelope(envelope); 101 AxisEngine engine = new AxisEngine(configurationContext); 102 engine.receive(msgContext); 103 } catch (SOAPProcessingException e) { 104 throw new AxisFault(e); 105 } catch (OMException e) { 106 throw new AxisFault(e); 107 } catch (XMLStreamException e) { 108 throw new AxisFault(e); 109 } 110 } 111 112 public static boolean processHTTPGetRequest(MessageContext msgContext, 113 InputStream in, OutputStream out, String contentType, 114 String soapAction, String requestURI, 115 ConfigurationContext configurationContext, Map requestParameters) 116 throws AxisFault { 117 if(soapAction != null && soapAction.startsWith("\"") && soapAction.endsWith("\"")){ 118 soapAction = soapAction.substring(1,soapAction.length() -1); 119 } 120 msgContext.setWSAAction(soapAction); 121 msgContext.setSoapAction(soapAction); 122 msgContext.setTo(new EndpointReference(AddressingConstants.WSA_TO, 123 requestURI)); 124 msgContext.setProperty(MessageContext.TRANSPORT_OUT, out); 125 msgContext.setServerSide(true); 126 try { 127 SOAPEnvelope envelope = HTTPTransportUtils 128 .createEnvelopeFromGetRequest(requestURI, requestParameters); 129 if (envelope == null) { 130 return false; 131 } else { 132 msgContext.setDoingREST(true); 133 msgContext.setEnvelope(envelope); 134 AxisEngine engine = new AxisEngine(configurationContext); 135 engine.receive(msgContext); 136 return true; 137 } 138 } catch (IOException e) { 139 throw new AxisFault(e); 140 } 141 } 142 143 public static final SOAPEnvelope createEnvelopeFromGetRequest( 144 String requestUrl, Map map) { 145 String [] values = Utils 146 .parseRequestURLForServiceAndOperation(requestUrl); 147 148 if (values[1] != null && values[0] != null) { 149 String operation = values[1]; 150 SOAPFactory soapFactory = new SOAP11Factory(); 151 SOAPEnvelope envelope = soapFactory.getDefaultEnvelope(); 152 153 OMNamespace omNs = soapFactory.createOMNamespace(values[0], 154 "services"); 155 OMNamespace defualtNs = new OMNamespaceImpl("", null); 156 157 OMElement opElement = soapFactory.createOMElement(operation, omNs); 158 159 Iterator it = map.keySet().iterator(); 160 while (it.hasNext()) { 161 String name = (String ) it.next(); 162 String value = (String ) map.get(name); 163 OMElement omEle = soapFactory.createOMElement(name, defualtNs); 164 omEle.setText(value); 165 opElement.addChild(omEle); 166 } 167 168 envelope.getBody().addChild(opElement); 169 return envelope; 170 } else { 171 return null; 172 } 173 } 174 175 public static StAXBuilder selectBuilderForMIME(MessageContext msgContext, 176 InputStream inStream, String contentTypeString) throws OMException, 177 XMLStreamException, FactoryConfigurationError { 178 StAXBuilder builder = null; 179 180 boolean fileCacheForAttachments = (Constants.VALUE_TRUE.equals(msgContext.getProperty(Constants.Configuration.CACHE_ATTACHMENTS))); 181 String attachmentRepoDir=null; 182 if (fileCacheForAttachments) 183 { 184 attachmentRepoDir = (String )msgContext.getProperty(Constants.Configuration.ATTACHMENT_TEMP_DIR); 185 } 186 187 MIMEHelper mimeHelper = new MIMEHelper(inStream, contentTypeString,fileCacheForAttachments,attachmentRepoDir); 188 XMLStreamReader reader = XMLInputFactory.newInstance() 189 .createXMLStreamReader( 190 new BufferedReader(new InputStreamReader(mimeHelper 191 .getSOAPPartInputStream()))); 192 195 msgContext.setProperty("Attachments", mimeHelper); 196 if (mimeHelper.getAttachmentSpecType().equals(MIMEHelper.MTOM_TYPE)) { 197 200 builder = new MTOMStAXSOAPModelBuilder(reader, mimeHelper); 201 } else if (mimeHelper.getAttachmentSpecType().equals( 202 MIMEHelper.SWA_TYPE)) { 203 builder = new StAXSOAPModelBuilder(reader); 204 } 205 return builder; 206 } 207 208 public static boolean checkEnvelopeForOptimise(SOAPEnvelope envelope) { 209 return isOptimised(envelope); 210 } 211 212 private static boolean isOptimised(OMElement element) { 213 Iterator childrenIter = element.getChildren(); 214 boolean isOptimized = false; 215 while (childrenIter.hasNext()&& !isOptimized) { 216 OMNode node = (OMNode) childrenIter.next(); 217 if (OMNode.TEXT_NODE == node.getType() 218 && ((OMText) node).isOptimized()) { 219 isOptimized = true; 220 } else if (OMNode.ELEMENT_NODE == node.getType()) { 221 isOptimized = isOptimised((OMElement) node); 222 } 223 } 224 return isOptimized; 225 } 226 227 public static boolean doWriteMTOM(MessageContext msgContext) { 228 boolean enableMTOM = false; 229 if (msgContext.getProperty(Constants.Configuration.ENABLE_MTOM) != null) { 230 enableMTOM = Constants.VALUE_TRUE.equals(msgContext 231 .getProperty(Constants.Configuration.ENABLE_MTOM)); 232 } 233 boolean envelopeContainsOptimise = HTTPTransportUtils 234 .checkEnvelopeForOptimise(msgContext.getEnvelope()); 235 boolean doMTOM = enableMTOM && envelopeContainsOptimise; 236 msgContext.setDoingMTOM(doMTOM); 237 return doMTOM; 238 } 239 } | Popular Tags |