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