Coverage Report - org.mule.service.processor.ServiceInternalMessageProcessor
 
Classes in this File Line Coverage Branch Coverage Complexity
ServiceInternalMessageProcessor
0%
0/34
0%
0/24
0
 
 1  
 /*
 2  
  * $Id$
 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.service.processor;
 12  
 
 13  
 import org.mule.api.MessagingException;
 14  
 import org.mule.api.MuleEvent;
 15  
 import org.mule.api.MuleException;
 16  
 import org.mule.api.MuleMessage;
 17  
 import org.mule.api.config.MuleProperties;
 18  
 import org.mule.api.endpoint.ImmutableEndpoint;
 19  
 import org.mule.api.processor.MessageProcessor;
 20  
 import org.mule.api.service.Service;
 21  
 import org.mule.api.transport.ReplyToHandler;
 22  
 import org.mule.processor.AbstractInterceptingMessageProcessor;
 23  
 import org.mule.transport.AbstractConnector;
 24  
 
 25  
 import java.util.concurrent.atomic.AtomicReference;
 26  
 
 27  
 import org.apache.commons.lang.BooleanUtils;
 28  
 
 29  
 public class ServiceInternalMessageProcessor extends AbstractInterceptingMessageProcessor
 30  
 {
 31  
 
 32  
     protected Service service;
 33  
     protected MessageProcessor receiveAsyncReplyMessageProcessor;
 34  0
     private AtomicReference<ReplyToHandler> cachedReplyToHandler = new AtomicReference<ReplyToHandler>();
 35  
 
 36  
 
 37  
     public ServiceInternalMessageProcessor(Service service)
 38  0
     {
 39  0
         this.service = service;
 40  0
     }
 41  
 
 42  
     /**
 43  
      * We do all this together here rather than chaining them in order to conserve
 44  
      * 2.x exception handling behaviour
 45  
      */
 46  
     public MuleEvent process(MuleEvent event) throws MuleException
 47  
     {
 48  0
         MuleEvent resultEvent = null;
 49  
         try
 50  
         {
 51  0
             Object replyTo = event.getMessage().getReplyTo();
 52  0
             ReplyToHandler replyToHandler = getReplyToHandler(event.getMessage(), event.getEndpoint());
 53  
             // Do not propagate REPLY_TO beyond the inbound endpoint
 54  0
             event.getMessage().setReplyTo(null);
 55  
 
 56  0
             resultEvent = service.getComponent().process(event);
 57  0
             resultEvent = processNext(resultEvent);
 58  
 
 59  
             // Allow components to stop processing of the ReplyTo property (e.g.
 60  
             // CXF)
 61  0
             if (resultEvent != null)
 62  
             {
 63  0
                 String replyToStop = resultEvent.getMessage().getInvocationProperty(MuleProperties.MULE_REPLY_TO_STOP_PROPERTY);
 64  0
                 if (!event.getEndpoint().getExchangePattern().hasResponse() || !BooleanUtils.toBoolean(replyToStop))
 65  
                 {
 66  0
                     processReplyTo(event, resultEvent, replyToHandler, replyTo);
 67  
                 }
 68  
             }
 69  0
             return resultEvent;
 70  
         }
 71  0
         catch (Exception e)
 72  
         {
 73  0
             event.getSession().setValid(false);
 74  0
             if (e instanceof MuleException)
 75  
             {
 76  0
                 throw (MuleException) e;
 77  
             }
 78  
             else
 79  
             {
 80  0
                 throw new MessagingException(event, e);
 81  
             }
 82  
         }
 83  
     }
 84  
 
 85  
     protected ReplyToHandler getReplyToHandler(MuleMessage message, ImmutableEndpoint endpoint)
 86  
     {
 87  0
         Object replyTo = message.getReplyTo();
 88  0
         if (replyTo != null)
 89  
         {
 90  0
             if (cachedReplyToHandler.get() == null)
 91  
             {
 92  0
                 ReplyToHandler replyToHandler = ((AbstractConnector) endpoint.getConnector()).getReplyToHandler(endpoint);
 93  
                 // Use the response transformer for the event if one is set
 94  0
                 if (endpoint.getResponseTransformers() != null)
 95  
                 {
 96  0
                     replyToHandler.setTransformers(endpoint.getResponseTransformers());
 97  
                 }
 98  0
                 cachedReplyToHandler.compareAndSet(null, replyToHandler);
 99  
             }
 100  0
             return cachedReplyToHandler.get();
 101  
         }
 102  0
         return null;
 103  
     }
 104  
 
 105  
     protected void processReplyTo(MuleEvent event,
 106  
                                   MuleEvent result,
 107  
                                   ReplyToHandler replyToHandler,
 108  
                                   Object replyTo) throws MuleException
 109  
     {
 110  0
         if (result != null && replyToHandler != null)
 111  
         {
 112  0
             String requestor = result.getMessage().getOutboundProperty(MuleProperties.MULE_REPLY_TO_REQUESTOR_PROPERTY);
 113  0
             if ((requestor != null && !requestor.equals(service.getName())) || requestor == null)
 114  
             {
 115  0
                 replyToHandler.processReplyTo(event, result.getMessage(), replyTo);
 116  
             }
 117  
         }
 118  0
     }
 119  
 
 120  
 }