View Javadoc

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