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;
8   
9   import org.mule.DefaultMuleEvent;
10  import org.mule.api.MuleEvent;
11  import org.mule.api.endpoint.OutboundEndpoint;
12  import org.mule.api.processor.MessageProcessor;
13  import org.mule.api.routing.RoutingException;
14  import org.mule.config.i18n.CoreMessages;
15  
16  /**
17   * <code>ForwardingCatchAllStrategy</code> acts as a catch and forward router for
18   * any events not caught by the router this strategy is associated with. Users can
19   * assign an endpoint to this strategy to forward all events to. This can be used as
20   * a dead letter/error queue.
21   *
22   */
23  public class ForwardingCatchAllStrategy extends AbstractCatchAllStrategy
24  {
25      private boolean sendTransformed = false;
26  
27      protected OutboundEndpoint endpoint;
28  
29      public void setEndpoint(OutboundEndpoint endpoint)
30      {
31          this.endpoint = endpoint;
32      }
33      
34      public void setMessageProcessor(MessageProcessor processor)
35      {
36          this.endpoint = (OutboundEndpoint) processor;
37      }
38  
39      public OutboundEndpoint getEndpoint()
40      {
41          return endpoint;
42      }
43  
44      @Override
45      public MuleEvent doCatchMessage(MuleEvent event) throws RoutingException
46      {
47          if (getEndpoint() == null)
48          {
49              throw new RoutingException(CoreMessages.noCatchAllEndpointSet(), event, getEndpoint());
50          }
51          try
52          {
53              OutboundEndpoint endpoint = getEndpoint();
54              if (sendTransformed && endpoint.getTransformers() != null)
55              {
56                  event.getMessage().applyTransformers(event, endpoint.getTransformers());
57              }
58  
59              // Recreate event with outbound endpoint incase anything uses event
60              // endpoint down the line
61              event = new DefaultMuleEvent(event.getMessage(), endpoint, event.getFlowConstruct(), event);
62              MuleEvent result = endpoint.process(event);
63              if (statistics != null && statistics.isEnabled())
64              {
65                  statistics.incrementRoutedMessage(getEndpoint());
66              }
67              return result;
68          }
69          catch (Exception e)
70          {
71              throw new RoutingException(event, getEndpoint(), e);
72  
73          }
74      }
75  
76      public boolean isSendTransformed()
77      {
78          return sendTransformed;
79      }
80  
81      public void setSendTransformed(boolean sendTransformed)
82      {
83          this.sendTransformed = sendTransformed;
84      }
85  }