View Javadoc

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