View Javadoc

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