View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transport.soap.axis;
8   
9   import org.mule.DefaultMuleMessage;
10  import org.mule.RequestContext;
11  import org.mule.api.MessagingException;
12  import org.mule.api.MuleEventContext;
13  import org.mule.api.MuleException;
14  import org.mule.api.MuleMessage;
15  import org.mule.api.config.MuleProperties;
16  import org.mule.api.endpoint.EndpointURI;
17  import org.mule.api.lifecycle.Callable;
18  import org.mule.api.lifecycle.Initialisable;
19  import org.mule.api.lifecycle.InitialisationException;
20  import org.mule.config.MuleManifest;
21  import org.mule.config.i18n.CoreMessages;
22  import org.mule.config.i18n.MessageFactory;
23  import org.mule.endpoint.MuleEndpointURI;
24  import org.mule.module.cxf.SoapConstants;
25  import org.mule.transport.http.HttpConnector;
26  import org.mule.transport.http.HttpConstants;
27  import org.mule.transport.soap.axis.extensions.AxisMuleSession;
28  import org.mule.transport.soap.axis.extensions.MuleConfigProvider;
29  import org.mule.util.StringUtils;
30  
31  import java.io.ByteArrayInputStream;
32  import java.io.ByteArrayOutputStream;
33  import java.io.File;
34  import java.io.FileInputStream;
35  import java.io.IOException;
36  import java.util.ArrayList;
37  import java.util.Iterator;
38  import java.util.Map;
39  import java.util.Properties;
40  
41  import javax.xml.namespace.QName;
42  
43  import org.apache.axis.AxisEngine;
44  import org.apache.axis.AxisFault;
45  import org.apache.axis.ConfigurationException;
46  import org.apache.axis.Constants;
47  import org.apache.axis.Message;
48  import org.apache.axis.MessageContext;
49  import org.apache.axis.description.OperationDesc;
50  import org.apache.axis.description.ServiceDesc;
51  import org.apache.axis.handlers.soap.SOAPService;
52  import org.apache.axis.i18n.Messages;
53  import org.apache.axis.security.servlet.ServletSecurityProvider;
54  import org.apache.axis.server.AxisServer;
55  import org.apache.axis.transport.http.HTTPConstants;
56  import org.apache.axis.transport.http.ServletEndpointContextImpl;
57  import org.apache.axis.utils.Admin;
58  import org.apache.axis.utils.XMLUtils;
59  import org.apache.commons.logging.Log;
60  import org.w3c.dom.Document;
61  
62  /**
63   * <code>AxisServiceComponent</code> is a Mule component implementation of the Axis
64   * servlet. This component supports all the features of the Axis servlet except -
65   * <ol>
66   * <li>Jws class services are not supported as they don't add any value to the Mule���
67   * model</li>
68   * <li>Currently there is no HttpSession support. This will be fixed when MuleSession
69   * support is added to the Http Connector</li>
70   * </ol>
71   */
72  
73  public class AxisServiceComponent implements Initialisable, Callable
74  {
75      protected static final Log logger = org.apache.commons.logging.LogFactory.getLog(AxisServiceComponent.class);
76  
77      public static final String INIT_PROPERTY_TRANSPORT_NAME = "transport.name";
78      public static final String INIT_PROPERTY_USE_SECURITY = "use-servlet-security";
79      public static final String INIT_PROPERTY_ENABLE_LIST = "axis.enableListQuery";
80      public static final String DEFAULT_AXIS_HOME = "/axisHome";
81  
82      private String transportName = "http";
83      private ServletSecurityProvider securityProvider = null;
84      private boolean enableList = true;
85      private String homeDir;
86      private AxisServer axis;
87  
88      /** For IoC */
89      public AxisServiceComponent()
90      {
91          // do nothing
92      }
93  
94      /**
95       * Passes the context to the listener
96       * 
97       * @param context the context to process
98       * @return Object this object can be anything. When the
99       *         <code>LifecycleAdapter</code> for the component receives this
100      *         object it will first see if the Object is an <code>MuleEvent</code>
101      *         if not and the Object is not null a new context will be created using
102      *         the returned object as the payload. This new context will then get
103      *         published to the configured outbound endpoint if-
104      *         <ol>
105      *         <li>One has been configured for the component.</li>
106      *         <li>the <code>setStopFurtherProcessing(true)</code> wasn't called
107      *         on the previous context.</li>
108      *         </ol>
109      * @throws Exception if the context fails to process properly. If exceptions
110      *             aren't handled by the implementation they will be handled by the
111      *             exceptionListener associated with the component
112      */
113     public Object onCall(MuleEventContext context) throws Exception
114     {
115         AxisStringWriter response = new AxisStringWriter();
116         String method = context.getMessage().getInboundProperty(HttpConnector.HTTP_METHOD_PROPERTY, HttpConstants.METHOD_POST);
117         if (HttpConstants.METHOD_GET.equalsIgnoreCase(method))
118         {
119             doGet(context, response);
120         }
121         else
122         {
123             doPost(context, response);
124         }
125         response.close();
126         
127         String payload = response.getWriter().toString();
128         Map<String, Object> properties = response.getProperties();
129         return new DefaultMuleMessage(payload, properties, context.getMuleContext());
130     }
131 
132     public void initialise() throws InitialisationException
133     {
134         if (axis == null)
135         {
136             throw new InitialisationException(MessageFactory.createStaticMessage("No Axis instance, this component has not been initialized properly."), this);
137         }
138     }
139 
140     public void doGet(MuleEventContext context, AxisStringWriter response)
141         throws MuleException, IOException
142     {
143         try
144         {
145             // We parse a new uri based on the listening host and port with the
146             // request parameters appended
147             // Using the soap prefix ensures that we use a soap endpoint builder
148             EndpointURI endpointUri = context.getEndpointURI();
149             //We need to re-parse the URI here because we are only give the listening endpoint, not the actual
150             //request endpoint. The request endpoint needs to have the query parameters from the client
151             //There is no need to do this for Servlet because it does things differently
152             if (!"true".equalsIgnoreCase(context.getEndpointURI().getParams().getProperty("servlet.endpoint")))
153             {
154                 String uri = SoapConstants.SOAP_ENDPOINT_PREFIX + context.getEndpointURI().getScheme()
155                                 + "://" + context.getEndpointURI().getHost() + ":"
156                                 + context.getEndpointURI().getPort();
157                 uri += context.getMessage().getInboundProperty(HttpConnector.HTTP_REQUEST_PROPERTY, StringUtils.EMPTY);
158                 endpointUri = new MuleEndpointURI(uri, context.getMuleContext());
159                 endpointUri.initialise();
160             }
161 
162             AxisEngine engine = getAxis();
163             String pathInfo = endpointUri.getPath();
164             boolean wsdlRequested = false;
165             boolean listRequested = false;
166 
167             if (endpointUri.getAddress().endsWith(".jws"))
168             {
169                 throw new AxisFault("Jws not supported by the Mule Axis service");
170             }
171 
172             String queryString = endpointUri.getQuery();
173             if (queryString != null)
174             {
175                 if (queryString.equalsIgnoreCase(SoapConstants.WSDL_PROPERTY))
176                 {
177                     wsdlRequested = true;
178                 }
179                 else
180                 {
181                     if (queryString.equalsIgnoreCase(SoapConstants.LIST_PROPERTY))
182                     {
183                         listRequested = true;
184                     }
185                 }
186             }
187 
188             boolean hasNoPath = (StringUtils.isEmpty(pathInfo) || pathInfo.equals("/"));
189             if (!wsdlRequested && !listRequested && hasNoPath)
190             {
191                 reportAvailableServices(context, response);
192             }
193             else
194             {
195                 MessageContext msgContext = new MessageContext(engine);
196                 populateMessageContext(msgContext, context, endpointUri);
197 
198                 msgContext.setProperty("transport.url", endpointUri.toString());
199                 if (wsdlRequested)
200                 {
201                     processWsdlRequest(msgContext, response);
202                 }
203                 else if (listRequested)
204                 {
205                     processListRequest(response);
206                 }
207                 else
208                 {
209                     processMethodRequest(msgContext, context, response, endpointUri);
210                 }
211             }
212         }
213         catch (AxisFault fault)
214         {
215             reportTroubleInGet(fault, response);
216         }
217         catch (Exception e)
218         {
219             reportTroubleInGet(e, response);
220         }
221     }
222 
223     protected void doPost(MuleEventContext context, AxisStringWriter response)
224         throws Exception
225     {
226         String soapAction;
227         Message responseMsg;
228         AxisEngine engine = getAxis();
229         if (engine == null)
230         {
231 
232             throw new MessagingException(CoreMessages.objectIsNull("Axis Engine"), context.getMessage());
233         }
234         MessageContext msgContext = new MessageContext(engine);
235 
236         String contentType;
237         try
238         {
239             //EndpointURI endpointUri = getEndpoint(context);
240             EndpointURI endpointUri = context.getEndpointURI();
241             populateMessageContext(msgContext, context, endpointUri);
242             if (securityProvider != null)
243             {
244                 if (logger.isDebugEnabled())
245                 {
246                     logger.debug("securityProvider:" + securityProvider);
247                 }
248                 msgContext.setProperty("securityProvider", securityProvider);
249             }
250 
251             Object request = context.getMessage().getPayload();
252             if (request instanceof File)
253             {
254                 request = new FileInputStream((File)request);
255             }
256             else if (request instanceof byte[])
257             {
258                 request = new ByteArrayInputStream((byte[])request);
259             }
260 
261             final String cType = context.getMessage().getInboundProperty(HTTPConstants.HEADER_CONTENT_TYPE);
262             final String cLocation = context.getMessage().getInboundProperty(HTTPConstants.HEADER_CONTENT_LOCATION);
263             Message requestMsg = new Message(request, false, cType, cLocation);
264 
265             if (logger.isDebugEnabled())
266             {
267                 logger.debug("Request Message:" + requestMsg);
268             }
269             msgContext.setRequestMessage(requestMsg);
270             msgContext.setProperty("transport.url", endpointUri.toString());
271 
272             soapAction = getSoapAction(context);
273             if (soapAction != null)
274             {
275                 msgContext.setUseSOAPAction(true);
276                 msgContext.setSOAPActionURI(soapAction);
277             }
278             msgContext.setSession(new AxisMuleSession(context.getSession()));
279 
280             if (logger.isDebugEnabled())
281             {
282                 logger.debug("Invoking Axis Engine.");
283             }
284             AxisServiceProxy.setProperties(RequestContext.getEvent().getEndpoint().getProperties());
285             engine.invoke(msgContext);
286             if (logger.isDebugEnabled())
287             {
288                 logger.debug("Return from Axis Engine.");
289             }
290             if (RequestContext.getExceptionPayload() instanceof Exception)
291             {
292                 throw (Exception)RequestContext.getExceptionPayload().getException();
293             }
294             // remove temporary file used for soap message with attachment
295             if (request instanceof File)
296             {
297                 ((File)request).delete();
298             }
299             responseMsg = msgContext.getResponseMessage();
300             if (responseMsg == null)
301             {
302                 throw new Exception(Messages.getMessage("noResponse01"));
303             }
304         }
305         catch (AxisFault fault)
306         {
307             logger.error(fault.toString() + " target service is: " + msgContext.getTargetService()
308                             + ". MuleEvent is: " + context.toString(), fault);
309             processAxisFault(fault);
310             configureResponseFromAxisFault(response, fault);
311             responseMsg = msgContext.getResponseMessage();
312             if (responseMsg == null)
313             {
314                 responseMsg = new Message(fault);
315             }
316         }
317         catch (Exception e)
318         {
319             responseMsg = msgContext.getResponseMessage();
320             response.setProperty(HttpConnector.HTTP_STATUS_PROPERTY, "500");
321             responseMsg = convertExceptionToAxisFault(e, responseMsg);
322         }
323 
324         contentType = responseMsg.getContentType(msgContext.getSOAPConstants());
325 
326         sendResponse(contentType, response, responseMsg);
327 
328         if (logger.isDebugEnabled())
329         {
330             logger.debug("Response sent.");
331         }
332     }
333 
334     private void reportTroubleInGet(Exception exception, AxisStringWriter response)
335     {
336         response.setProperty(HttpConstants.HEADER_CONTENT_TYPE, "text/html");
337         response.setProperty(HttpConnector.HTTP_STATUS_PROPERTY, "500");
338         response.write("<h2>" + Messages.getMessage("error00") + "</h2>");
339         response.write("<p>" + Messages.getMessage("somethingWrong00") + "</p>");
340         if (exception instanceof AxisFault)
341         {
342             AxisFault fault = (AxisFault)exception;
343             processAxisFault(fault);
344             writeFault(response, fault);
345         }
346         else
347         {
348             logger.error(exception.getMessage(), exception);
349             response.write("<pre>Exception - " + exception + "<br>");
350             response.write("</pre>");
351         }
352     }
353 
354     protected void processAxisFault(AxisFault fault)
355     {
356         org.w3c.dom.Element runtimeException = fault
357             .lookupFaultDetail(Constants.QNAME_FAULTDETAIL_RUNTIMEEXCEPTION);
358         if (runtimeException != null)
359         {
360             logger.info(Messages.getMessage("axisFault00"), fault);
361             fault.removeFaultDetail(Constants.QNAME_FAULTDETAIL_RUNTIMEEXCEPTION);
362         }
363         else if (logger.isDebugEnabled())
364         {
365             logger.debug(Messages.getMessage("axisFault00"), fault);
366         }
367 
368     }
369 
370     private void writeFault(AxisStringWriter response, AxisFault axisFault)
371     {
372         String localizedMessage = XMLUtils.xmlEncodeString(axisFault.getLocalizedMessage());
373         response.write("<pre>Fault - " + localizedMessage + "<br>");
374         response.write(axisFault.dumpToString());
375         response.write("</pre>");
376     }
377 
378     protected void processMethodRequest(MessageContext msgContext,
379                                         MuleEventContext context,
380                                         AxisStringWriter response,
381                                         EndpointURI endpointUri) throws AxisFault
382     {
383         Properties params = endpointUri.getUserParams();
384 
385         String method = (String)params.remove(MuleProperties.MULE_METHOD_PROPERTY);
386         if (method == null)
387         {
388             method = endpointUri.getPath().substring(endpointUri.getPath().lastIndexOf("/") + 1);
389         }
390         StringBuffer args = new StringBuffer(64);
391 
392         Map.Entry entry;
393         for (Iterator iterator = params.entrySet().iterator(); iterator.hasNext();)
394         {
395             entry = (Map.Entry)iterator.next();
396             args.append("<").append(entry.getKey()).append(">");
397             args.append(entry.getValue());
398             args.append("</").append(entry.getKey()).append(">");
399         }
400 
401         if (method == null)
402         {
403             response.setProperty(HttpConstants.HEADER_CONTENT_TYPE, "text/html");
404             response.setProperty(HttpConnector.HTTP_STATUS_PROPERTY, "400");
405             response.write("<h2>" + Messages.getMessage("error00") + ":  "
406                             + Messages.getMessage("invokeGet00") + "</h2>");
407             response.write("<p>" + Messages.getMessage("noMethod01") + "</p>");
408         }
409         else
410         {
411             invokeEndpointFromGet(msgContext, response, method, args.toString());
412         }
413     }
414 
415     protected void processWsdlRequest(MessageContext msgContext, AxisStringWriter response)
416         throws AxisFault
417     {
418         AxisEngine engine = getAxis();
419         try
420         {
421             engine.generateWSDL(msgContext);
422             Document doc = (Document)msgContext.getProperty("WSDL");
423             if (doc != null)
424             {
425                 response.setProperty(HttpConstants.HEADER_CONTENT_TYPE, "text/xml");
426                 XMLUtils.DocumentToWriter(doc, response.getWriter());
427             }
428             else
429             {
430                 if (logger.isDebugEnabled())
431                 {
432                     logger.debug("processWsdlRequest: failed to create WSDL");
433                 }
434                 reportNoWSDL(response, "noWSDL02", null);
435             }
436         }
437         catch (AxisFault axisFault)
438         {
439             if (axisFault.getFaultCode().equals(Constants.QNAME_NO_SERVICE_FAULT_CODE))
440             {
441                 processAxisFault(axisFault);
442                 response.setProperty(HttpConnector.HTTP_STATUS_PROPERTY, "404");
443                 reportNoWSDL(response, "noWSDL01", axisFault);
444             }
445             else
446             {
447                 throw axisFault;
448             }
449         }
450     }
451 
452     protected void invokeEndpointFromGet(MessageContext msgContext,
453                                          AxisStringWriter response,
454                                          String method,
455                                          String args) throws AxisFault
456     {
457         String body = "<" + method + ">" + args + "</" + method + ">";
458         String msgtxt = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"><SOAP-ENV:Body>"
459                         + body + "</SOAP-ENV:Body>" + "</SOAP-ENV:Envelope>";
460         Message responseMsg = null;
461         try
462         {
463             ByteArrayInputStream istream = new ByteArrayInputStream(msgtxt.getBytes("ISO-8859-1"));
464             AxisEngine engine = getAxis();
465             Message msg = new Message(istream, false);
466             msgContext.setRequestMessage(msg);
467             AxisServiceProxy.setProperties(RequestContext.getEvent().getEndpoint().getProperties());
468             engine.invoke(msgContext);
469             responseMsg = msgContext.getResponseMessage();
470             response.setProperty(HTTPConstants.HEADER_CACHE_CONTROL, "no-cache");
471             response.setProperty(HTTPConstants.HEADER_PRAGMA, "no-cache");
472             if (responseMsg == null)
473             {
474                 throw new Exception(Messages.getMessage("noResponse01"));
475             }
476         }
477         catch (AxisFault fault)
478         {
479             processAxisFault(fault);
480             configureResponseFromAxisFault(response, fault);
481             responseMsg = new Message(fault);
482         }
483         catch (Exception e)
484         {
485             response.setProperty(HttpConnector.HTTP_STATUS_PROPERTY, "500");
486             responseMsg = convertExceptionToAxisFault(e, responseMsg);
487         }
488         response.setProperty(HTTPConstants.HEADER_CONTENT_TYPE, "text/xml");
489         response.write(responseMsg.getSOAPPartAsString());
490     }
491 
492     protected void reportServiceInfo(AxisStringWriter response, SOAPService service, String serviceName)
493     {
494         response.setProperty(HttpConstants.HEADER_CONTENT_TYPE, "text/html");
495         response.write("<h1>" + service.getName() + "</h1>");
496         response.write("<p>" + Messages.getMessage("axisService00") + "</p>");
497         response.write("<i>" + Messages.getMessage("perhaps00") + "</i>");
498     }
499 
500     protected void processListRequest(AxisStringWriter response) throws AxisFault
501     {
502         AxisEngine engine = getAxis();
503         response.setProperty(HTTPConstants.HEADER_CONTENT_TYPE, "text/html");
504         if (enableList)
505         {
506             Document doc = Admin.listConfig(engine);
507             if (doc != null)
508             {
509                 XMLUtils.DocumentToWriter(doc, response.getWriter());
510             }
511             else
512             {
513                 response.setProperty(HttpConnector.HTTP_STATUS_PROPERTY, "404");
514                 response.write("<h2>" + Messages.getMessage("error00") + "</h2>");
515                 response.write("<p>" + Messages.getMessage("noDeploy00") + "</p>");
516             }
517         }
518         else
519         {
520             response.setProperty(HttpConnector.HTTP_STATUS_PROPERTY, "403");
521             response.write("<h2>" + Messages.getMessage("error00") + "</h2>");
522             response.write("<p><i>?list</i> " + Messages.getMessage("disabled00") + "</p>");
523         }
524     }
525 
526     private void reportNoWSDL(AxisStringWriter response, String moreDetailCode, AxisFault axisFault)
527     {
528         response.setProperty(HttpConnector.HTTP_STATUS_PROPERTY, "404");
529         response.setProperty(HTTPConstants.HEADER_CONTENT_TYPE, "text/html");
530         response.write("<h2>" + Messages.getMessage("error00") + "</h2>");
531         response.write("<p>" + Messages.getMessage("noWSDL00") + "</p>");
532         if (moreDetailCode != null)
533         {
534             response.write("<p>" + Messages.getMessage(moreDetailCode) + "</p>");
535         }
536 
537     }
538 
539     protected void reportAvailableServices(MuleEventContext context, AxisStringWriter response)
540         throws ConfigurationException, AxisFault
541     {
542         AxisEngine engine = getAxis();
543         response.setProperty(HttpConstants.HEADER_CONTENT_TYPE, "text/html");
544         response.write("<h2>And now... Some Services</h2>");
545         String version = MuleManifest.getProductVersion();
546         if (version == null)
547         {
548             version = "Version Not Set";
549         }
550         response.write("<h5>(Mule - " + version + ")</h5>");
551         Iterator i;
552 
553         try
554         {
555             response
556                 .write("<table width=\"400\"><tr><th>Mule Component Services</th><th>Axis Services</th></tr><tr><td width=\"200\" valign=\"top\">");
557             i = engine.getConfig().getDeployedServices();
558             listServices(i, response);
559             response.write("</td><td width=\"200\" valign=\"top\">");
560             i = ((MuleConfigProvider)engine.getConfig()).getAxisDeployedServices();
561             listServices(i, response);
562             response.write("</td></tr></table>");
563         }
564         catch (ConfigurationException configException)
565         {
566             if (configException.getContainedException() instanceof AxisFault)
567             {
568                 throw (AxisFault)configException.getContainedException();
569             }
570             else
571             {
572                 throw configException;
573             }
574         }
575 
576     }
577 
578     private void listServices(Iterator i, AxisStringWriter response)
579     {
580         response.write("<ul>");
581         while (i.hasNext())
582         {
583             ServiceDesc sd = (ServiceDesc)i.next();
584             StringBuffer sb = new StringBuffer(512);
585             sb.append("<li>");
586             String name = sd.getName();
587             sb.append(name);
588             sb.append(" <a href=\"");
589             if (sd.getEndpointURL() != null)
590             {
591                 sb.append(sd.getEndpointURL());
592                 if (!sd.getEndpointURL().endsWith("/"))
593                 {
594                     sb.append("/");
595                 }
596             }
597             sb.append(name);
598             sb.append("?wsdl\"><i>(wsdl)</i></a></li>");
599             response.write(sb.toString());
600             if (sd.getDocumentation() != null)
601             {
602                 response.write("<ul><h6>" + sd.getDocumentation() + "</h6></ul>");
603             }
604             ArrayList operations = sd.getOperations();
605             if (!operations.isEmpty())
606             {
607                 response.write("<ul>");
608                 OperationDesc desc;
609                 for (Iterator it = operations.iterator(); it.hasNext();)
610                 {
611                     desc = (OperationDesc)it.next();
612                     response.write("<li>" + desc.getName());
613                 }
614                 response.write("</ul>");
615             }
616         }
617         response.write("</ul>");
618     }
619 
620     protected void reportCantGetAxisService(MuleEventContext context, AxisStringWriter response)
621     {
622         response.setProperty(HttpConnector.HTTP_STATUS_PROPERTY, "404");
623         response.setProperty(HttpConstants.HEADER_CONTENT_TYPE, "text/html");
624         response.write("<h2>" + Messages.getMessage("error00") + "</h2>");
625         response.write("<p>" + Messages.getMessage("noService06") + "</p>");
626     }
627 
628     private void configureResponseFromAxisFault(AxisStringWriter response, AxisFault fault)
629     {
630         int status = getHttpResponseStatus(fault);
631         if (status == 401)
632         {
633             response.setProperty(HttpConstants.HEADER_WWW_AUTHENTICATE, "Basic realm=\"AXIS\"");
634         }
635         response.setProperty(HttpConnector.HTTP_STATUS_PROPERTY, String.valueOf(status));
636     }
637 
638     private Message convertExceptionToAxisFault(Exception exception, Message responseMsg)
639     {
640         logger.error(exception.getMessage(), exception);
641         if (responseMsg == null)
642         {
643             AxisFault fault = AxisFault.makeFault(exception);
644             processAxisFault(fault);
645             responseMsg = new Message(fault);
646         }
647         return responseMsg;
648     }
649 
650     protected int getHttpResponseStatus(AxisFault af)
651     {
652         return af.getFaultCode().getLocalPart().startsWith("Server.Unauth") ? 401 : '\u01F4';
653     }
654 
655     private void sendResponse(String contentType,
656                               AxisStringWriter response,
657                               Message responseMsg) throws Exception
658     {
659         if (responseMsg == null)
660         {
661             response.setProperty(HttpConnector.HTTP_STATUS_PROPERTY, "204");
662             if (logger.isDebugEnabled())
663             {
664                 logger.debug("NO AXIS MESSAGE TO RETURN!");
665             }
666         }
667         else
668         {
669             if (logger.isDebugEnabled())
670             {
671                 logger.debug("Returned Content-Type:" + contentType);
672             }
673                 response.setProperty(HttpConstants.HEADER_CONTENT_TYPE, contentType);
674                 ByteArrayOutputStream baos = new ByteArrayOutputStream(8192);
675                 responseMsg.writeTo(baos);
676                 response.write(baos.toString());
677         }
678     }
679 
680     private void populateMessageContext(MessageContext msgContext,
681                                         MuleEventContext context,
682                                         EndpointURI endpointUri) throws AxisFault, ConfigurationException
683     {
684         MuleMessage msg = context.getMessage();
685 
686         if (logger.isDebugEnabled())
687         {
688             logger.debug("MessageContext:" + msgContext);
689             logger.debug("HEADER_CONTENT_TYPE:" + msg.getInboundProperty(HttpConstants.HEADER_CONTENT_TYPE));
690             logger.debug("HEADER_CONTENT_LOCATION:" + msg.getInboundProperty(HttpConstants.HEADER_CONTENT_LOCATION));
691             logger.debug("Constants.MC_HOME_DIR:" + String.valueOf(getHomeDir()));
692             logger.debug("Constants.MC_RELATIVE_PATH:" + endpointUri.getPath());
693             logger.debug("HTTPConstants.HEADER_AUTHORIZATION:" + msg.getInboundProperty("Authorization"));
694             logger.debug("Constants.MC_REMOTE_ADDR:" + endpointUri.getHost());
695         }
696 
697         msgContext.setTransportName(transportName);
698         msgContext.setProperty("home.dir", getHomeDir());
699         msgContext.setProperty("path", endpointUri.getPath());
700         msgContext.setProperty(HTTPConstants.MC_HTTP_SERVLET, this);
701         msgContext.setProperty(HTTPConstants.MC_HTTP_SERVLETLOCATION, endpointUri.getPath());
702         // determine service name
703         String serviceName = getServiceName(context, endpointUri);
704         // Validate Service path against request path
705         SOAPService service = msgContext.getAxisEngine().getConfig().getService(
706             new QName(serviceName.substring(1)));
707 
708         // if using jms or vm we can skip this
709         String scheme = endpointUri.getScheme();
710         if (!("vm".equalsIgnoreCase(scheme)
711                         || "jms".equalsIgnoreCase(scheme)
712                         || "servlet".equalsIgnoreCase(scheme)))
713         {
714             // Component Name is set by Mule so if its null we can skip this check
715             if (service.getOption(AxisConnector.SERVICE_PROPERTY_COMPONENT_NAME) != null)
716             {
717                 String servicePath = (String)service.getOption("servicePath");
718                 if (StringUtils.isEmpty(endpointUri.getPath()))
719                 {
720                     if (!("/" + endpointUri.getAddress()).startsWith(servicePath + serviceName))
721                     {
722                         throw new AxisFault("Failed to find service: " + "/" + endpointUri.getAddress());
723                     }
724                 }
725                 //We use ends with rather than starts with because if a servlet binding is used we do not have the full
726                 //path info when the service is registered.  Track MULE-3931 for more info.
727                 else if (!endpointUri.getPath().endsWith(servicePath + serviceName))
728                 {
729                     throw new AxisFault("Failed to find service: " + endpointUri.getPath());
730                 }
731             }
732         }
733 
734         msgContext.setProperty(HTTPConstants.MC_HTTP_SERVLETPATHINFO, serviceName);
735         msgContext.setProperty("serviceName", serviceName);
736 
737         msgContext.setProperty("Authorization", msg.getInboundProperty("Authorization"));
738         msgContext.setProperty("remoteaddr", endpointUri.getHost());
739         ServletEndpointContextImpl sec = new ServletEndpointContextImpl();
740         msgContext.setProperty("servletEndpointContext", sec);
741     }
742 
743     private String getSoapAction(MuleEventContext context) throws AxisFault
744     {
745         String soapAction = context.getMessage().getInboundProperty(SoapConstants.SOAP_ACTION_PROPERTY_CAPS);
746         if (logger.isDebugEnabled())
747         {
748             logger.debug("Header Soap Action:" + soapAction);
749         }
750 
751         if (StringUtils.isEmpty(soapAction))
752         {
753             soapAction = context.getEndpointURI().getAddress();
754         }
755         return soapAction;
756     }
757 
758     protected String getServiceName(MuleEventContext context, EndpointURI endpointUri) throws AxisFault
759     {
760         String serviceName = endpointUri.getPath();
761         if (StringUtils.isEmpty(serviceName))
762         {
763             serviceName = getSoapAction(context);
764             serviceName = serviceName.replaceAll("\"", "");
765             int i = serviceName.indexOf("/", serviceName.indexOf("//"));
766             if (i < -1)
767             {
768                 serviceName = serviceName.substring(i + 2);
769             }
770 
771         }
772 
773         int i = serviceName.lastIndexOf('/');
774         if (i > -1)
775         {
776             serviceName = serviceName.substring(i);
777         }
778         i = serviceName.lastIndexOf('?');
779         if (i > -1)
780         {
781             serviceName = serviceName.substring(0, i);
782         }
783         return serviceName;
784     }
785 
786     public String getTransportName()
787     {
788         return transportName;
789     }
790 
791     public void setTransportName(String transportName)
792     {
793         this.transportName = transportName;
794     }
795 
796     public boolean isEnableList()
797     {
798         return enableList;
799     }
800 
801     public void setEnableList(boolean enableList)
802     {
803         this.enableList = enableList;
804     }
805 
806     public String getHomeDir()
807     {
808         if (homeDir == null)
809         {
810             //TODO fix homeDir = muleContext.getConfiguration().getWorkingDirectory() + DEFAULT_AXIS_HOME;
811             homeDir = DEFAULT_AXIS_HOME;
812         }
813         return homeDir;
814     }
815 
816     public void setHomeDir(String homeDir)
817     {
818         this.homeDir = homeDir;
819     }
820 
821     public AxisServer getAxis()
822     {
823         return axis;
824     }
825 
826     public void setAxis(AxisServer axisServer)
827     {
828         this.axis = axisServer;
829     }
830 }