Coverage Report - org.mule.transport.soap.axis.extensions.UniversalSender
 
Classes in this File Line Coverage Branch Coverage Complexity
UniversalSender
0%
0/118
0%
0/62
0
 
 1  
 /*
 2  
  * $Id: UniversalSender.java 19191 2010-08-25 21:05:23Z tcarlson $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.extensions;
 12  
 
 13  
 import org.mule.DefaultMuleEvent;
 14  
 import org.mule.DefaultMuleMessage;
 15  
 import org.mule.MessageExchangePattern;
 16  
 import org.mule.RequestContext;
 17  
 import org.mule.api.MuleContext;
 18  
 import org.mule.api.MuleEvent;
 19  
 import org.mule.api.MuleException;
 20  
 import org.mule.api.MuleMessage;
 21  
 import org.mule.api.MuleSession;
 22  
 import org.mule.api.config.MuleProperties;
 23  
 import org.mule.api.endpoint.EndpointBuilder;
 24  
 import org.mule.api.endpoint.EndpointURI;
 25  
 import org.mule.api.endpoint.ImmutableEndpoint;
 26  
 import org.mule.api.endpoint.OutboundEndpoint;
 27  
 import org.mule.api.processor.MessageProcessor;
 28  
 import org.mule.api.routing.OutboundRouter;
 29  
 import org.mule.api.routing.OutboundRouterCollection;
 30  
 import org.mule.api.service.Service;
 31  
 import org.mule.endpoint.EndpointURIEndpointBuilder;
 32  
 import org.mule.endpoint.MuleEndpointURI;
 33  
 import org.mule.module.cxf.SoapConstants;
 34  
 import org.mule.session.DefaultMuleSession;
 35  
 import org.mule.transport.http.HttpConstants;
 36  
 import org.mule.transport.soap.axis.AxisConnector;
 37  
 import org.mule.transport.soap.axis.extras.AxisCleanAndAddProperties;
 38  
 
 39  
 import java.io.File;
 40  
 import java.io.FileInputStream;
 41  
 import java.io.FileOutputStream;
 42  
 import java.util.HashMap;
 43  
 import java.util.Iterator;
 44  
 import java.util.Map;
 45  
 
 46  
 import org.apache.axis.AxisFault;
 47  
 import org.apache.axis.Message;
 48  
 import org.apache.axis.MessageContext;
 49  
 import org.apache.axis.client.Call;
 50  
 import org.apache.axis.handlers.BasicHandler;
 51  
 import org.apache.commons.io.output.ByteArrayOutputStream;
 52  
 import org.apache.commons.logging.Log;
 53  
 import org.apache.commons.logging.LogFactory;
 54  
 
 55  
 /**
 56  
  * An Axis handler that will dispatch the SOAP event via a Mule endpoint
 57  
  */
 58  0
 public class UniversalSender extends BasicHandler
 59  
 {
 60  
     /**
 61  
      * Serial version
 62  
      */
 63  
     private static final long serialVersionUID = 7943380365092172940L;
 64  
 
 65  
     /**
 66  
      * logger used by this class
 67  
      */
 68  0
     protected transient Log logger = LogFactory.getLog(getClass());
 69  
 
 70  0
     protected Map endpointsCache = new HashMap();
 71  
 
 72  
     protected MuleContext muleContext;
 73  
 
 74  
 
 75  
     public void invoke(MessageContext msgContext) throws AxisFault
 76  
     {
 77  
         try
 78  
         {
 79  0
             boolean sync = true;
 80  0
             Call call = (Call)msgContext.getProperty("call_object");
 81  
         
 82  0
             if (call == null)
 83  
             {
 84  0
                 throw new IllegalStateException(
 85  
                     "The call_object property must be set on the message context to the client Call object");
 86  
             }
 87  
         
 88  0
             muleContext = (MuleContext)call.getProperty(MuleProperties.MULE_CONTEXT_PROPERTY);
 89  0
             if(muleContext==null)
 90  
             {
 91  0
                 throw new IllegalArgumentException("Property org.mule.MuleContext not set on Axis MessageContext");
 92  
             }
 93  
         
 94  
             // Get the event stored in call if a request call is made there will be no event
 95  0
             MuleEvent event = (MuleEvent)call.getProperty(MuleProperties.MULE_EVENT_PROPERTY);
 96  
         
 97  0
             if (Boolean.TRUE.equals(call.getProperty("axis.one.way")))
 98  
             {
 99  0
                 sync = false;
 100  
             }
 101  
         
 102  
             // Get the dispatch endpoint
 103  0
             String uri = msgContext.getStrProp(MessageContext.TRANS_URL);
 104  0
             ImmutableEndpoint requestEndpoint = (ImmutableEndpoint)call
 105  
                 .getProperty(MuleProperties.MULE_ENDPOINT_PROPERTY);
 106  
             
 107  
             OutboundEndpoint endpoint;
 108  
         
 109  
             // put username and password in URI if they are set on the current event
 110  0
             if (msgContext.getUsername() != null)
 111  
             {
 112  0
                 String[] tempEndpoint = uri.split("//");
 113  0
                 String credentialString = msgContext.getUsername() + ":"
 114  
                                           + msgContext.getPassword();
 115  0
                 uri = tempEndpoint[0] + "//" + credentialString + "@" + tempEndpoint[1];
 116  0
                 endpoint = lookupEndpoint(uri);
 117  0
             }
 118  
             else
 119  
             {
 120  0
                 endpoint = lookupEndpoint(uri);
 121  
             }
 122  
 
 123  0
             if (requestEndpoint.getConnector() instanceof AxisConnector)
 124  
             {
 125  0
                 msgContext.setTypeMappingRegistry(((AxisConnector)requestEndpoint.getConnector())
 126  
                     .getAxis().getTypeMappingRegistry());
 127  
             }
 128  
             
 129  0
             Map<String, Object> props = new HashMap<String, Object>();
 130  
             Object payload;
 131  0
             int contentLength = 0;
 132  0
             String contentType = null;
 133  0
             if (msgContext.getRequestMessage().countAttachments() > 0)
 134  
             {
 135  0
                 File temp = File.createTempFile("soap", ".tmp");
 136  0
                 temp.deleteOnExit(); // TODO cleanup files earlier (IOUtils has a
 137  
                 // file tracker)
 138  0
                 FileOutputStream fos = new FileOutputStream(temp);
 139  0
                 msgContext.getRequestMessage().writeTo(fos);
 140  0
                 fos.close();
 141  0
                 contentLength = (int)temp.length();
 142  0
                 payload = new FileInputStream(temp);
 143  0
                 contentType = "multipart/related";
 144  0
             }
 145  
             else
 146  
             {
 147  0
                 ByteArrayOutputStream baos = new ByteArrayOutputStream(8192);
 148  0
                 msgContext.getRequestMessage().writeTo(baos);
 149  0
                 baos.close();
 150  0
                 payload = baos.toByteArray();
 151  
             }
 152  
 
 153  
             // props.putAll(event.getProperties());
 154  0
             for (Iterator iterator = msgContext.getPropertyNames(); iterator.hasNext();)
 155  
             {
 156  0
                 String name = (String)iterator.next();
 157  0
                 if (!name.equals("call_object") && !name.equals("wsdl.service"))
 158  
                 {
 159  0
                     props.put(name, msgContext.getProperty(name));
 160  
                 }
 161  0
             }
 162  
 
 163  
             // add all custom headers, filter out all mule headers (such as
 164  
             // MULE_SESSION) except
 165  
             // for MULE_USER header. Filter out other headers like "soapMethods" and
 166  
             // MuleProperties.MULE_METHOD_PROPERTY and "soapAction"
 167  
             // and also filter out any http related header
 168  0
             if ((RequestContext.getEvent() != null)
 169  
                 && (RequestContext.getEvent().getMessage() != null))
 170  
             {
 171  0
                 props = AxisCleanAndAddProperties.cleanAndAdd(RequestContext.getEventContext());
 172  
             }
 173  
             
 174  
             // with jms and vm the default SOAPAction will result in the name of the endpoint, which we may not necessarily want. This should be set manually on the endpoint
 175  0
             String scheme = requestEndpoint.getEndpointURI().getScheme();
 176  0
             if (!("vm".equalsIgnoreCase(scheme) || "jms".equalsIgnoreCase(scheme)))
 177  
             {
 178  0
                 if (call.useSOAPAction())
 179  
                 {
 180  0
                     uri = call.getSOAPActionURI();
 181  
                 }
 182  0
                 props.put(SoapConstants.SOAP_ACTION_PROPERTY_CAPS, uri);
 183  
             }
 184  0
             if (contentLength > 0)
 185  
             {
 186  0
                 props.put(HttpConstants.HEADER_CONTENT_LENGTH, Integer.toString(contentLength)); // necessary
 187  
                 // for
 188  
                 // supporting
 189  
                 // httpclient
 190  
             }
 191  
 
 192  
             
 193  0
             if (props.get(HttpConstants.HEADER_CONTENT_TYPE) == null)
 194  
             {
 195  0
                 if (contentType == null)
 196  
                 {
 197  0
                     contentType = "text/xml";
 198  
                 }
 199  
                 
 200  0
                 props.put(HttpConstants.HEADER_CONTENT_TYPE, contentType);
 201  
             }
 202  0
             MuleMessage message = new DefaultMuleMessage(payload, props, muleContext);
 203  
             MuleSession session;
 204  
 
 205  0
             if(event != null)
 206  
             {
 207  0
                 session = event.getSession();
 208  
             }
 209  
             else
 210  
             {
 211  0
                 session = new DefaultMuleSession(muleContext);
 212  
             }
 213  
 
 214  0
             logger.info("Making Axis soap request on: " + uri);
 215  0
             if (logger.isDebugEnabled())
 216  
             {
 217  0
                 logger.debug("Soap request is:\n" + new String((payload instanceof byte[] ? (byte[])payload : payload.toString().getBytes())));
 218  
             }
 219  
 
 220  0
             if (sync)
 221  
             {
 222  0
                 EndpointBuilder builder = new EndpointURIEndpointBuilder(endpoint);
 223  0
                 builder.setExchangePattern(MessageExchangePattern.REQUEST_RESPONSE);
 224  0
                 OutboundEndpoint syncEndpoint = muleContext.getRegistry()
 225  
                     .lookupEndpointFactory()
 226  
                     .getOutboundEndpoint(builder);
 227  0
                 MuleEvent dispatchEvent = new DefaultMuleEvent(message, syncEndpoint, session);
 228  0
                 MuleMessage result = null;
 229  0
                 MuleEvent resultEvent = syncEndpoint.process(dispatchEvent);
 230  0
                 if (resultEvent != null)
 231  
                 {
 232  0
                     result = resultEvent.getMessage();
 233  
                 }
 234  
 
 235  0
                 if (result != null)
 236  
                 {
 237  0
                     byte[] response = result.getPayloadAsBytes();
 238  0
                     Message responseMessage = new Message(response);
 239  0
                     msgContext.setResponseMessage(responseMessage);
 240  
 
 241  0
                 }
 242  
                 else
 243  
                 {
 244  0
                     logger
 245  
                         .warn("No response message was returned from synchronous call to: " + uri);
 246  
                 }
 247  
                 // remove temp file created for streaming
 248  0
                 if (payload instanceof File)
 249  
                 {
 250  0
                     ((File)payload).delete();
 251  
                 }
 252  0
             }
 253  
             else
 254  
             {
 255  0
                 MuleEvent dispatchEvent = new DefaultMuleEvent(message, endpoint, session);
 256  0
                 endpoint.process(dispatchEvent);
 257  
             }
 258  
         }
 259  0
         catch (Exception e)
 260  
         {
 261  0
             if (e instanceof AxisFault)
 262  
             {
 263  0
                 throw (AxisFault) e;
 264  
             }
 265  
             else
 266  
             {
 267  0
                 throw new AxisFault(e.getMessage(), e);
 268  
             }
 269  0
         }
 270  
 
 271  0
     }
 272  
 
 273  
     protected OutboundEndpoint lookupEndpoint(String uri) throws MuleException
 274  
     {
 275  0
         Service axis = muleContext.getRegistry().lookupService(AxisConnector.AXIS_SERVICE_COMPONENT_NAME);
 276  0
         EndpointURI endpoint = new MuleEndpointURI(uri, muleContext);
 277  
 
 278  
         OutboundEndpoint ep;
 279  
 
 280  0
         if (axis != null)
 281  
         {
 282  0
             synchronized (endpointsCache)
 283  
             {
 284  0
                 ep = (OutboundEndpoint) endpointsCache.get(endpoint.getAddress());
 285  0
                 if (ep == null)
 286  
                 {
 287  0
                     updateEndpointCache((OutboundRouterCollection) axis.getOutboundMessageProcessor());
 288  0
                     ep = (OutboundEndpoint) endpointsCache.get(endpoint.getAddress());
 289  0
                     if (ep == null)
 290  
                     {
 291  0
                         logger.debug("Dispatch Endpoint uri: " + uri
 292  
                                      + " not found on the cache. Creating the endpoint instead.");
 293  0
                         ep = muleContext.getRegistry().lookupEndpointFactory()
 294  
                                 .getOutboundEndpoint(uri);
 295  
                     }
 296  
                     else
 297  
                     {
 298  0
                         logger.info("Found endpoint: " + uri + " on the Axis service component");
 299  
                     }
 300  
                 }
 301  
                 else
 302  
                 {
 303  0
                     logger.info("Found endpoint: " + uri + " on the Axis service component");
 304  
                 }
 305  0
             }
 306  
         }
 307  
         else
 308  
         {
 309  0
             ep = muleContext.getRegistry().lookupEndpointFactory()
 310  
                     .getOutboundEndpoint(uri);
 311  
         }
 312  0
         return ep;
 313  
     }
 314  
 
 315  
     private void updateEndpointCache(OutboundRouterCollection router)
 316  
     {
 317  0
         endpointsCache.clear();
 318  0
         for (Iterator iterator = router.getRoutes().iterator(); iterator.hasNext();)
 319  
         {
 320  0
             OutboundRouter r = (OutboundRouter)iterator.next();
 321  0
             for (MessageProcessor mp : r.getRoutes())
 322  
             {
 323  0
                 if (mp instanceof ImmutableEndpoint)
 324  
                 {
 325  0
                     ImmutableEndpoint endpoint = (ImmutableEndpoint) mp;
 326  0
                     endpointsCache.put(endpoint.getEndpointURI().getAddress(), endpoint);
 327  0
                 }
 328  
             }
 329  0
         }
 330  0
     }
 331  
 }