Coverage Report - org.mule.transport.DefaultReplyToHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultReplyToHandler
0%
0/43
0%
0/16
0
 
 1  
 /*
 2  
  * $Id: DefaultReplyToHandler.java 20495 2010-12-07 21:20:09Z dfeist $
 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;
 12  
 
 13  
 import org.mule.DefaultMuleEvent;
 14  
 import org.mule.DefaultMuleMessage;
 15  
 import org.mule.api.MuleContext;
 16  
 import org.mule.api.MuleEvent;
 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.EndpointBuilder;
 21  
 import org.mule.api.endpoint.EndpointFactory;
 22  
 import org.mule.api.endpoint.ImmutableEndpoint;
 23  
 import org.mule.api.endpoint.OutboundEndpoint;
 24  
 import org.mule.api.service.Service;
 25  
 import org.mule.api.transformer.Transformer;
 26  
 import org.mule.api.transport.DispatchException;
 27  
 import org.mule.api.transport.ReplyToHandler;
 28  
 import org.mule.config.i18n.CoreMessages;
 29  
 import org.mule.management.stats.ServiceStatistics;
 30  
 
 31  
 import java.util.HashMap;
 32  
 import java.util.List;
 33  
 import java.util.Map;
 34  
 
 35  
 import org.apache.commons.logging.Log;
 36  
 import org.apache.commons.logging.LogFactory;
 37  
 
 38  
 /**
 39  
  * <code>DefaultReplyToHandler</code> is responsible for processing a message
 40  
  * replyTo header.
 41  
  */
 42  
 
 43  
 public class DefaultReplyToHandler implements ReplyToHandler
 44  
 {
 45  
     /**
 46  
      * logger used by this class
 47  
      */
 48  0
     protected transient final Log logger = LogFactory.getLog(getClass());
 49  
 
 50  
     private volatile List<Transformer> transformers;
 51  0
     private final Map<String, ImmutableEndpoint> endpointCache = new HashMap<String, ImmutableEndpoint>();
 52  
     protected MuleContext muleContext;
 53  
 
 54  
     public DefaultReplyToHandler(List<Transformer> transformers, MuleContext muleContext)
 55  0
     {
 56  0
         this.transformers = transformers;
 57  0
         this.muleContext = muleContext;
 58  0
     }
 59  
 
 60  
     public void processReplyTo(MuleEvent event, MuleMessage returnMessage, Object replyTo) throws MuleException
 61  
     {
 62  0
         if (logger.isDebugEnabled())
 63  
         {
 64  0
             logger.debug("sending reply to: " + replyTo);
 65  
         }
 66  0
         String replyToEndpoint = replyTo.toString();
 67  
 
 68  
         // get the endpoint for this url
 69  0
         OutboundEndpoint endpoint = getEndpoint(event, replyToEndpoint);
 70  
 
 71  
         // make sure remove the replyTo property as not cause a a forever
 72  
         // replyto loop
 73  0
         returnMessage.removeProperty(MuleProperties.MULE_REPLY_TO_PROPERTY);
 74  
 
 75  
         // MULE-4617. This is fixed with MULE-4620, but lets remove this property
 76  
         // anyway as it should never be true from a replyTo dispatch
 77  0
         returnMessage.removeProperty(MuleProperties.MULE_REMOTE_SYNC_PROPERTY);
 78  
 
 79  
         // Create a new copy of the message so that response MessageProcessors don't end up screwing up the reply
 80  0
         returnMessage = new DefaultMuleMessage(returnMessage.getPayload(), returnMessage, muleContext);
 81  
 
 82  
         // Create the replyTo event asynchronous
 83  0
         MuleEvent replyToEvent = new DefaultMuleEvent(returnMessage, endpoint, event.getSession(), event.getProcessingTime());
 84  
 
 85  
         // carry over properties
 86  0
         List<String> responseProperties = endpoint.getResponseProperties();
 87  0
         for (String propertyName : responseProperties)
 88  
         {
 89  0
             Object propertyValue = event.getMessage().getInboundProperty(propertyName);
 90  0
             if (propertyValue != null)
 91  
             {
 92  0
                 replyToEvent.getMessage().setOutboundProperty(propertyName, propertyValue);
 93  
             }
 94  0
         }
 95  
 
 96  
         // dispatch the event
 97  
         try
 98  
         {
 99  0
             if (event.getFlowConstruct() instanceof Service)
 100  
             {
 101  0
                 ServiceStatistics stats = ((Service) event.getFlowConstruct()).getStatistics();
 102  0
                 if (stats.isEnabled())
 103  
                 {
 104  0
                     stats.incSentReplyToEvent();
 105  
                 }
 106  
             }
 107  0
             endpoint.process(replyToEvent);
 108  0
             if (logger.isInfoEnabled())
 109  
             {
 110  0
                 logger.info("reply to sent: " + endpoint);
 111  
             }
 112  
         }
 113  0
         catch (Exception e)
 114  
         {
 115  0
             throw new DispatchException(CoreMessages.failedToDispatchToReplyto(endpoint),
 116  
                 replyToEvent, endpoint, e);
 117  0
         }
 118  
 
 119  0
     }
 120  
 
 121  
     protected synchronized OutboundEndpoint getEndpoint(MuleEvent event, String endpointUri) throws MuleException
 122  
     {
 123  0
         OutboundEndpoint endpoint = (OutboundEndpoint) endpointCache.get(endpointUri);
 124  0
         if (endpoint == null)
 125  
         {
 126  0
             EndpointFactory endpointFactory = muleContext.getRegistry().lookupEndpointFactory();
 127  0
             EndpointBuilder endpointBuilder = endpointFactory.getEndpointBuilder(endpointUri);
 128  0
             if (transformers == null)
 129  
             {
 130  0
                 endpointBuilder.setTransformers(event.getEndpoint().getResponseTransformers());
 131  
             }
 132  0
             endpoint = endpointFactory.getOutboundEndpoint(endpointBuilder);
 133  0
             endpointCache.put(endpointUri, endpoint);
 134  
         }
 135  0
         return endpoint;
 136  
     }
 137  
 
 138  
     public List<Transformer> getTransformers()
 139  
     {
 140  0
         return transformers;
 141  
     }
 142  
 
 143  
     public void setTransformers(List<Transformer> transformers)
 144  
     {
 145  0
         this.transformers = transformers;
 146  0
     }
 147  
 }