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