View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.routing.requestreply;
8   
9   import org.apache.commons.lang.BooleanUtils;
10  import org.mule.api.MuleEvent;
11  import org.mule.api.MuleException;
12  import org.mule.api.config.MuleProperties;
13  import org.mule.api.processor.InterceptingMessageProcessor;
14  import org.mule.api.transport.ReplyToHandler;
15  import org.mule.construct.SimpleFlowConstruct;
16  import org.mule.processor.AbstractInterceptingMessageProcessor;
17  
18  /**
19   * Send message according to reply to property
20   */
21  public class ReplyToMessageProcessor extends AbstractInterceptingMessageProcessor
22          implements InterceptingMessageProcessor
23  {
24  
25      public MuleEvent process(MuleEvent event) throws MuleException
26      {
27          MuleEvent resultEvent;
28          //In config is service then this is executed by ServiceInternalMessageProcessor
29          if (event.getFlowConstruct() instanceof SimpleFlowConstruct)
30          {
31              Object replyTo = event.getReplyToDestination();
32              ReplyToHandler replyToHandler = event.getReplyToHandler();
33              // Do not propagate REPLY_TO
34              event.getMessage().setReplyTo(null);
35  
36              resultEvent = processNext(event);
37  
38              // Allow components to stop processing of the ReplyTo property (e.g. CXF)
39              final String replyToStop = resultEvent.getMessage().getInvocationProperty(
40                      MuleProperties.MULE_REPLY_TO_STOP_PROPERTY);
41              if (resultEvent != null && !BooleanUtils.toBoolean(replyToStop))
42              {
43                  processReplyTo(event, resultEvent, replyToHandler, replyTo);
44              }
45          }
46          else
47          {
48              resultEvent = processNext(event);
49          }
50          return resultEvent;
51      }
52  
53      protected void processReplyTo(MuleEvent event,
54                                    MuleEvent result,
55                                    ReplyToHandler replyToHandler,
56                                    Object replyTo) throws MuleException
57      {
58          if (result != null && replyToHandler != null)
59          {
60              String requestor = result.getMessage().getOutboundProperty(
61                      MuleProperties.MULE_REPLY_TO_REQUESTOR_PROPERTY);
62              if ((requestor != null && !requestor.equals(event.getFlowConstruct().getName()))
63                      || requestor == null)
64              {
65                  replyToHandler.processReplyTo(event, result.getMessage(), replyTo);
66              }
67          }
68      }
69  
70  }