View Javadoc

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