View Javadoc

1   /*
2    * $Id: ReplyToPropertyRequestReplyReplier.java 20320 2010-11-24 15:03:31Z 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.routing.requestreply;
12  
13  import org.mule.api.MuleEvent;
14  import org.mule.api.MuleException;
15  import org.mule.api.MuleMessage;
16  import org.mule.api.config.MuleProperties;
17  import org.mule.api.endpoint.InboundEndpoint;
18  import org.mule.api.processor.MessageProcessor;
19  import org.mule.api.processor.RequestReplyReplierMessageProcessor;
20  import org.mule.api.transport.ReplyToHandler;
21  import org.mule.processor.AbstractInterceptingMessageProcessor;
22  import org.mule.transport.AbstractConnector;
23  
24  import org.apache.commons.lang.BooleanUtils;
25  
26  public class ReplyToPropertyRequestReplyReplier extends AbstractInterceptingMessageProcessor
27      implements RequestReplyReplierMessageProcessor
28  {
29  
30      public MuleEvent process(MuleEvent event) throws MuleException
31      {
32          Object replyTo = event.getMessage().getReplyTo();
33          ReplyToHandler replyToHandler = getReplyToHandler(event.getMessage(),
34              (InboundEndpoint) event.getEndpoint());
35          // Do not propagate REPLY_TO
36          event.getMessage().setReplyTo(null);
37  
38          MuleEvent resultEvent = processNext(event);
39  
40          // Allow components to stop processing of the ReplyTo property (e.g. CXF)
41          final String replyToStop = resultEvent.getMessage().getInvocationProperty(
42              MuleProperties.MULE_REPLY_TO_STOP_PROPERTY);
43          if (resultEvent != null && !BooleanUtils.toBoolean(replyToStop))
44          {
45              processReplyTo(event, resultEvent, replyToHandler, replyTo);
46          }
47  
48          return resultEvent;
49      }
50  
51      protected ReplyToHandler getReplyToHandler(MuleMessage message, InboundEndpoint endpoint)
52      {
53          Object replyTo = message.getReplyTo();
54          ReplyToHandler replyToHandler = null;
55          if (replyTo != null)
56          {
57              replyToHandler = ((AbstractConnector) endpoint.getConnector()).getReplyToHandler(endpoint);
58              // Use the response transformer for the event if one is set
59              if (endpoint.getResponseTransformers() != null)
60              {
61                  replyToHandler.setTransformers(endpoint.getResponseTransformers());
62              }
63          }
64          return replyToHandler;
65      }
66  
67      protected void processReplyTo(MuleEvent event,
68                                    MuleEvent result,
69                                    ReplyToHandler replyToHandler,
70                                    Object replyTo) throws MuleException
71      {
72          if (result != null && replyToHandler != null)
73          {
74              String requestor = result.getMessage().getOutboundProperty(
75                  MuleProperties.MULE_REPLY_TO_REQUESTOR_PROPERTY);
76              if ((requestor != null && !requestor.equals(event.getFlowConstruct().getName()))
77                  || requestor == null)
78              {
79                  replyToHandler.processReplyTo(event, result.getMessage(), replyTo);
80              }
81          }
82      }
83  
84      public void setReplyProcessor(MessageProcessor replyMessageProcessor)
85      {
86          // Not used
87      }
88  
89  }