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.DefaultMuleEvent;
10  import org.mule.DefaultMuleMessage;
11  import org.mule.RequestContext;
12  import org.mule.api.MuleEvent;
13  import org.mule.api.MuleException;
14  import org.mule.api.routing.OutboundRouterCollection;
15  import org.mule.api.service.Service;
16  import org.mule.component.simple.PassThroughComponent;
17  import org.mule.processor.AbstractInterceptingMessageProcessor;
18  import org.mule.transport.NullPayload;
19  
20  /**
21   * Responsible for determining if the Service outbound phase should be used and
22   * making a copy of the event to use.
23   * <p>
24   * If the service component is a {@link PassThroughComponent} a null from the
25   * outbound phase will result in a {@link NullPayload} being returned, otherwise when
26   * the outbound phase returns null this MessageProcessor will return the request
27   * event.
28   */
29  public class ServiceOutboundMessageProcessor extends AbstractInterceptingMessageProcessor
30  {
31  
32      protected Service service;
33  
34      public ServiceOutboundMessageProcessor(Service service)
35      {
36          this.service = service;
37      }
38  
39      public MuleEvent process(MuleEvent event) throws MuleException
40      {
41          // Skip outbound phase is inbound is sync and payload is NullPayload
42          boolean syncNullPayload = event.getEndpoint().getExchangePattern().hasResponse()
43                                    && (event.getMessage().getPayload() instanceof NullPayload);
44  
45          if (event.isStopFurtherProcessing())
46          {
47              logger.debug("MuleEvent stop further processing has been set, no outbound routing will be performed.");
48              return event;
49          }
50  
51          else if (event != null && !syncNullPayload)
52          {
53              if (!(service.getOutboundMessageProcessor() instanceof OutboundRouterCollection)
54                  || (service.getOutboundMessageProcessor() instanceof OutboundRouterCollection && ((OutboundRouterCollection) service.getOutboundMessageProcessor()).hasEndpoints()))
55              {
56                  MuleEvent outboundEvent;
57                  if (event.getEndpoint().getExchangePattern().hasResponse())
58                  {
59                      // Copy of the inbound event for outbound phase
60                      outboundEvent = new DefaultMuleEvent(new DefaultMuleMessage(event.getMessage()
61                          .getPayload(), event.getMessage(), event.getMuleContext()), event);
62                  }
63                  else
64                  {
65                      outboundEvent = event;
66                  }
67  
68                  MuleEvent outboundResult = processNext(outboundEvent);
69  
70                  if (outboundResult != null)
71                  {
72                      event = outboundResult;
73                  }
74                  else if (service.getComponent() instanceof PassThroughComponent)
75                  {
76                      // If there was no component, then we really want to return
77                      // the response from the outbound router as the actual
78                      // payload - even if it's null.
79                      event = new DefaultMuleEvent(new DefaultMuleMessage(NullPayload.getInstance(),
80                          event.getMessage(), service.getMuleContext()), event);
81                  }
82              }
83              else
84              {
85                  logger.debug("Outbound router on service '" + service.getName()
86                               + "' doesn't have any targets configured.");
87              }
88              event = RequestContext.setEvent(new DefaultMuleEvent(event.getMessage(), event));
89          }
90          return event;
91      }
92  }