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.processor;
8   
9   import org.mule.api.MuleEvent;
10  import org.mule.api.MuleException;
11  import org.mule.api.construct.FlowConstruct;
12  import org.mule.api.processor.InterceptingMessageProcessor;
13  import org.mule.api.processor.MessageProcessor;
14  import org.mule.api.routing.filter.FilterUnacceptedException;
15  import org.mule.config.i18n.CoreMessages;
16  
17  /**
18   * Abstract {@link InterceptingMessageProcessor} that can be easily be extended and
19   * used for filtering message flow through a {@link MessageProcessor} chain. The
20   * default behaviour when the filter is not accepted is to return the request event.
21   */
22  public abstract class AbstractFilteringMessageProcessor extends AbstractInterceptingMessageProcessor
23  {
24      /** 
25       * Throw a FilterUnacceptedException when a message is rejected by the filter? 
26       */
27      protected boolean throwOnUnaccepted = false;
28      protected boolean onUnacceptedFlowConstruct;
29  
30      
31      /** 
32       * The <code>MessageProcessor</code> that should be used to handle messages that are not accepted by the filter.
33       */
34      protected MessageProcessor unacceptedMessageProcessor;
35  
36      public MuleEvent process(MuleEvent event) throws MuleException
37      {
38          if (accept(event))
39          {
40              return processNext(event);
41          }
42          else
43          {
44              return handleUnaccepted(event);
45          }
46      }
47  
48      protected abstract boolean accept(MuleEvent event);
49  
50      protected MuleEvent handleUnaccepted(MuleEvent event) throws MuleException
51      {        
52          if (unacceptedMessageProcessor != null)
53          {
54              return unacceptedMessageProcessor.process(event);
55          }
56          else if (throwOnUnaccepted)
57          {
58              throw filterUnacceptedException(event);
59          }
60          else
61          {
62              return null;
63          }
64      }
65  
66      protected MuleException filterUnacceptedException(MuleEvent event)
67      {
68          return new FilterUnacceptedException(CoreMessages.messageRejectedByFilter(), event);        
69      }
70      
71      public MessageProcessor getUnacceptedMessageProcessor()
72      {
73          return unacceptedMessageProcessor;
74      }
75  
76      public void setUnacceptedMessageProcessor(MessageProcessor unacceptedMessageProcessor)
77      {
78          this.unacceptedMessageProcessor = unacceptedMessageProcessor;
79          if (unacceptedMessageProcessor instanceof FlowConstruct)
80          {
81              onUnacceptedFlowConstruct = true;
82          }
83      }
84  
85      public boolean isThrowOnUnaccepted()
86      {
87          return throwOnUnaccepted;
88      }
89  
90      public void setThrowOnUnaccepted(boolean throwOnUnaccepted)
91      {
92          this.throwOnUnaccepted = throwOnUnaccepted;
93      }
94  }