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.routing.outbound;
8   
9   import org.mule.DefaultMuleEvent;
10  import org.mule.api.MuleEvent;
11  import org.mule.api.MuleException;
12  import org.mule.api.MuleMessage;
13  import org.mule.api.lifecycle.InitialisationException;
14  import org.mule.api.processor.MessageProcessor;
15  import org.mule.api.routing.CouldNotRouteOutboundMessageException;
16  import org.mule.api.routing.RoutePathNotFoundException;
17  import org.mule.api.routing.RoutingException;
18  import org.mule.config.i18n.CoreMessages;
19  import org.mule.transport.NullPayload;
20  
21  /**
22   * <code>ChainingRouter</code> is used to pass a Mule event through multiple
23   * targets using the result of the first as the input for the second.
24   */
25  public class ChainingRouter extends FilteringOutboundRouter
26  {
27      @Override
28      public void initialise() throws InitialisationException
29      {
30          super.initialise();
31          if (routes == null || routes.size() == 0)
32          {
33              throw new InitialisationException(CoreMessages.objectIsNull("targets"), this);
34          }
35      }
36  
37      @Override
38      public MuleEvent route(MuleEvent event) throws RoutingException
39      {
40          MuleEvent resultToReturn = event;
41          if (routes == null || routes.size() == 0)
42          {
43              throw new RoutePathNotFoundException(CoreMessages.noEndpointsForRouter(), event, null);
44          }
45  
46          final int endpointsCount = routes.size();
47          if (logger.isDebugEnabled())
48          {
49              logger.debug("About to chain " + endpointsCount + " targets.");
50          }
51  
52          // need that ref for an error message
53          MessageProcessor endpoint = null;
54          try
55          {
56              MuleMessage intermediaryResult = event.getMessage();
57  
58              for (int i = 0; i < endpointsCount; i++)
59              {
60                  endpoint = getRoute(i, resultToReturn);
61                  // if it's not the last endpoint in the chain,
62                  // enforce the synchronous call, otherwise we lose response
63                  boolean lastEndpointInChain = (i == endpointsCount - 1);
64  
65                  if (logger.isDebugEnabled())
66                  {
67                      logger.debug("Sending Chained message '" + i + "': "
68                                   + (intermediaryResult == null ? "null" : intermediaryResult.toString()));
69                  }
70  
71                  if (!lastEndpointInChain)
72                  {
73                      MuleEvent endpointResult = sendRequest(resultToReturn, intermediaryResult, endpoint, true);
74                      resultToReturn = endpointResult != null ? endpointResult : resultToReturn;
75                      MuleMessage localResult = endpointResult == null ? null : endpointResult.getMessage();
76                      // Need to propagate correlation info and replyTo, because there
77                      // is no guarantee that an external system will preserve headers
78                      // (in fact most will not)
79                      if (localResult != null &&
80                          // null result can be wrapped in a NullPayload
81                          localResult.getPayload() != NullPayload.getInstance() &&
82                          intermediaryResult != null)
83                      {
84                          processIntermediaryResult(localResult, intermediaryResult);
85                      }
86                      intermediaryResult = localResult;
87  
88                      if (logger.isDebugEnabled())
89                      {
90                          logger.debug("Received Chain result '" + i + "': "
91                                       + (intermediaryResult != null ? intermediaryResult.toString() : "null"));
92                      }
93  
94                      if (intermediaryResult == null || intermediaryResult.getPayload() == NullPayload.getInstance())
95                      {
96                          // if there was an error in the first link of the chain, make sure we propagate back
97                          // any exception payloads alongside the NullPayload
98                          resultToReturn = intermediaryResult == null ? null : new DefaultMuleEvent(intermediaryResult, resultToReturn);
99                          logger.warn("Chaining router cannot process any further targets. "
100                                     + "There was no result returned from endpoint invocation: " + endpoint);
101                         break;
102                     }
103                 }
104                 else
105                 {
106                     // ok, the last call,
107                     // use the 'sync/async' method parameter
108                     resultToReturn = sendRequest(resultToReturn, intermediaryResult, endpoint, true);
109                     if (logger.isDebugEnabled())
110                     {
111                         MuleMessage resultMessage = resultToReturn == null ? null : resultToReturn.getMessage();
112                         logger.debug("Received final Chain result '" + i + "': "
113                             + (resultMessage == null ? "null" : resultMessage.toString()));
114                     }
115                 }
116             }
117 
118         }
119         catch (MuleException e)
120         {
121             throw new CouldNotRouteOutboundMessageException(resultToReturn, endpoint, e);
122         }
123         return resultToReturn;
124     }
125 
126     /**
127      * Process intermediary result of invocation. The method will be invoked
128      * <strong>only</strong> if both local and intermediary results are available
129      * (not null).
130      * <p/>
131      * Overriding methods must call <code>super(localResult, intermediaryResult)</code>,
132      * unless they are modifying the correlation workflow (if you know what that means,
133      * you know what you are doing and when to do it).
134      * <p/>
135      * Default implementation propagates
136      * the following properties:
137      * <ul>
138      * <li>correlationId
139      * <li>correlationSequence
140      * <li>correlationGroupSize
141      * <li>replyTo
142      * </ul>
143      * @param localResult result of the last endpoint invocation
144      * @param intermediaryResult the message travelling across the targets
145      */
146     protected void processIntermediaryResult(MuleMessage localResult, MuleMessage intermediaryResult)
147     {
148         localResult.setCorrelationId(intermediaryResult.getCorrelationId());
149         localResult.setCorrelationSequence(intermediaryResult.getCorrelationSequence());
150         localResult.setCorrelationGroupSize(intermediaryResult.getCorrelationGroupSize());
151         localResult.setReplyTo(intermediaryResult.getReplyTo());
152     }
153 }