View Javadoc

1   /*
2    * $Id: ServiceInternalMessageProcessor.java 23059 2011-10-03 02:36:25Z pablo.lagreca $
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.config.MuleProperties;
17  import org.mule.api.service.Service;
18  import org.mule.api.transport.ReplyToHandler;
19  import org.mule.processor.AbstractInterceptingMessageProcessor;
20  
21  import org.apache.commons.lang.BooleanUtils;
22  
23  public class ServiceInternalMessageProcessor extends AbstractInterceptingMessageProcessor
24  {
25  
26      protected Service service;
27  
28      public ServiceInternalMessageProcessor(Service service)
29      {
30          this.service = service;
31      }
32  
33      /**
34       * We do all this together here rather than chaining them in order to conserve
35       * 2.x exception handling behaviour
36       */
37      public MuleEvent process(MuleEvent event) throws MuleException
38      {
39          MuleEvent resultEvent;
40          try
41          {
42  
43              resultEvent = service.getComponent().process(event);
44              resultEvent = processNext(resultEvent);
45  
46              if (!event.getExchangePattern().hasResponse())
47              {
48  
49                  Object replyTo = event.getReplyToDestination();
50                  ReplyToHandler replyToHandler = event.getReplyToHandler();
51  
52                  // Allow components to stop processing of the ReplyTo property (e.g.
53                  // CXF)
54                  if (resultEvent != null && replyTo != null)
55                  {
56                      String replyToStop = resultEvent.getMessage().getInvocationProperty(MuleProperties.MULE_REPLY_TO_STOP_PROPERTY);
57                      if (!event.getExchangePattern().hasResponse() || !BooleanUtils.toBoolean(replyToStop))
58                      {
59                          processReplyTo(event, resultEvent, replyToHandler, replyTo);
60                      }
61                  }
62              }
63              return resultEvent;
64          }
65          catch (Exception e)
66          {
67              event.getSession().setValid(false);
68              if (e instanceof MuleException)
69              {
70                  throw (MuleException) e;
71              }
72              else
73              {
74                  throw new MessagingException(event, e);
75              }
76          }
77      }
78  
79      protected void processReplyTo(MuleEvent event,
80                                    MuleEvent result,
81                                    ReplyToHandler replyToHandler,
82                                    Object replyTo) throws MuleException
83      {
84          if (result != null && replyToHandler != null)
85          {
86              String requestor = result.getMessage().getOutboundProperty(MuleProperties.MULE_REPLY_TO_REQUESTOR_PROPERTY);
87              if ((requestor != null && !requestor.equals(service.getName())) || requestor == null)
88              {
89                  replyToHandler.processReplyTo(event, result.getMessage(), replyTo);
90              }
91          }
92      }
93  
94  }