View Javadoc

1   /*
2    * $Id: ServiceInternalMessageProcessor.java 20332 2010-11-24 18:14:09Z aperepel $
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      private AtomicReference<ReplyToHandler> cachedReplyToHandler = new AtomicReference<ReplyToHandler>();
35  
36  
37      public ServiceInternalMessageProcessor(Service service)
38      {
39          this.service = service;
40      }
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          MuleEvent resultEvent;
49          try
50          {
51              Object replyTo = event.getMessage().getReplyTo();
52              ReplyToHandler replyToHandler = getReplyToHandler(event.getMessage(), event.getEndpoint());
53              // Do not propagate REPLY_TO beyond the inbound endpoint
54              event.getMessage().setReplyTo(null);
55  
56              resultEvent = service.getComponent().process(event);
57              resultEvent = processNext(resultEvent);
58  
59              // Allow components to stop processing of the ReplyTo property (e.g.
60              // CXF)
61              if (resultEvent != null)
62              {
63                  String replyToStop = resultEvent.getMessage().getInvocationProperty(MuleProperties.MULE_REPLY_TO_STOP_PROPERTY);
64                  if (!event.getEndpoint().getExchangePattern().hasResponse() || !BooleanUtils.toBoolean(replyToStop))
65                  {
66                      processReplyTo(event, resultEvent, replyToHandler, replyTo);
67                  }
68              }
69              return resultEvent;
70          }
71          catch (Exception e)
72          {
73              event.getSession().setValid(false);
74              if (e instanceof MuleException)
75              {
76                  throw (MuleException) e;
77              }
78              else
79              {
80                  throw new MessagingException(event, e);
81              }
82          }
83      }
84  
85      protected ReplyToHandler getReplyToHandler(MuleMessage message, ImmutableEndpoint endpoint)
86      {
87          Object replyTo = message.getReplyTo();
88          if (replyTo != null)
89          {
90              if (cachedReplyToHandler.get() == null)
91              {
92                  ReplyToHandler replyToHandler = ((AbstractConnector) endpoint.getConnector()).getReplyToHandler(endpoint);
93                  // Use the response transformer for the event if one is set
94                  if (endpoint.getResponseTransformers() != null)
95                  {
96                      replyToHandler.setTransformers(endpoint.getResponseTransformers());
97                  }
98                  cachedReplyToHandler.compareAndSet(null, replyToHandler);
99              }
100             return cachedReplyToHandler.get();
101         }
102         return null;
103     }
104 
105     protected void processReplyTo(MuleEvent event,
106                                   MuleEvent result,
107                                   ReplyToHandler replyToHandler,
108                                   Object replyTo) throws MuleException
109     {
110         if (result != null && replyToHandler != null)
111         {
112             String requestor = result.getMessage().getOutboundProperty(MuleProperties.MULE_REPLY_TO_REQUESTOR_PROPERTY);
113             if ((requestor != null && !requestor.equals(service.getName())) || requestor == null)
114             {
115                 replyToHandler.processReplyTo(event, result.getMessage(), replyTo);
116             }
117         }
118     }
119 
120 }