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.routing;
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.Filter;
18  import org.mule.api.routing.filter.FilterUnacceptedException;
19  import org.mule.config.i18n.CoreMessages;
20  import org.mule.processor.AbstractFilteringMessageProcessor;
21  
22  /**
23   * Implementation of {@link InterceptingMessageProcessor} that filters message flow using a {@link Filter}. Is
24   * the filter accepts the message then message flow continues to the next message processor. If the filter
25   * does not accept the message processor and a message processor is configured for handling unaccepted message
26   * then this will be invoked, otherwise <code>null</code> will be returned.
27   * <p/>
28   * <b>EIP Reference:</b> <a href="http://www.eaipatterns.com/Filter.html">http://www.eaipatterns.com/Filter.html<a/>
29   */
30  public class MessageFilter extends AbstractFilteringMessageProcessor
31  {
32      protected Filter filter;
33  
34      /** 
35       * For IoC only
36       * @deprecated Use MessageFilter(Filter filter) 
37       */
38      public MessageFilter()
39      {
40          // empty
41      }
42  
43      public MessageFilter(Filter filter)
44      {
45          this.filter = filter;
46      }
47  
48      /**
49       * @param filter
50       * @param throwExceptionOnUnaccepted throw a FilterUnacceptedException when a message is rejected by the filter?
51       * @param messageProcessor used to handler unaccepted messages
52       */
53      public MessageFilter(Filter filter, boolean throwExceptionOnUnaccepted, MessageProcessor messageProcessor)
54      {
55          this.filter = filter;
56          this.throwOnUnaccepted = throwExceptionOnUnaccepted;
57          this.unacceptedMessageProcessor = messageProcessor;
58      }
59  
60      @Override
61      protected boolean accept(MuleEvent event)
62      {
63          if (filter == null)
64          {
65              return true;
66          }
67  
68          if (event != null)
69          {
70              return filter.accept(event.getMessage());
71          }
72          else
73          {
74              return false;
75          }
76      }
77  
78      @Override
79      protected MuleException filterUnacceptedException(MuleEvent event)
80      {
81          return new FilterUnacceptedException(CoreMessages.messageRejectedByFilter(), event, filter);        
82      }
83      
84      public Filter getFilter()
85      {
86          return filter;
87      }
88  
89      public void setFilter(Filter filter)
90      {
91          this.filter = filter;
92      }
93  
94      @Override
95      public String toString()
96      {
97          return (filter == null ? "null filter" : filter.getClass().getName()) + " (wrapped by " + this.getClass().getSimpleName() + ")";
98      }
99  }