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