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.api.MuleContext;
10  import org.mule.api.MuleEvent;
11  import org.mule.api.MuleException;
12  import org.mule.api.construct.FlowConstruct;
13  import org.mule.api.construct.FlowConstructAware;
14  import org.mule.api.context.MuleContextAware;
15  import org.mule.api.lifecycle.Disposable;
16  import org.mule.api.lifecycle.Initialisable;
17  import org.mule.api.lifecycle.InitialisationException;
18  import org.mule.api.lifecycle.Lifecycle;
19  import org.mule.api.lifecycle.Startable;
20  import org.mule.api.lifecycle.Stoppable;
21  import org.mule.api.processor.InterceptingMessageProcessor;
22  import org.mule.api.processor.MessageProcessor;
23  import org.mule.api.routing.filter.Filter;
24  import org.mule.api.routing.filter.FilterUnacceptedException;
25  import org.mule.config.i18n.CoreMessages;
26  import org.mule.processor.AbstractFilteringMessageProcessor;
27  
28  /**
29   * Implementation of {@link InterceptingMessageProcessor} that filters message flow
30   * using a {@link Filter}. Is the filter accepts the message then message flow
31   * continues to the next message processor. If the filter does not accept the message
32   * processor and a message processor is configured for handling unaccepted message
33   * then this will be invoked, otherwise <code>null</code> will be returned.
34   * <p/>
35   * <b>EIP Reference:</b> <a
36   * href="http://www.eaipatterns.com/Filter.html">http://www.eaipatterns
37   * .com/Filter.html<a/>
38   */
39  public class MessageFilter extends AbstractFilteringMessageProcessor implements FlowConstructAware, Lifecycle
40  {
41      protected Filter filter;
42  
43      /**
44       * For IoC only
45       * 
46       * @deprecated Use MessageFilter(Filter filter)
47       */
48      @Deprecated
49      public MessageFilter()
50      {
51          super();
52      }
53  
54      public MessageFilter(Filter filter)
55      {
56          super();
57          this.filter = filter;
58      }
59  
60      /**
61       * @param filter
62       * @param throwExceptionOnUnaccepted throw a FilterUnacceptedException when a
63       *            message is rejected by the filter?
64       * @param messageProcessor used to handler unaccepted messages
65       */
66      public MessageFilter(Filter filter, boolean throwExceptionOnUnaccepted, MessageProcessor messageProcessor)
67      {
68          this.filter = filter;
69          this.throwOnUnaccepted = throwExceptionOnUnaccepted;
70          this.unacceptedMessageProcessor = messageProcessor;
71          setUnacceptedMessageProcessor(unacceptedMessageProcessor);
72      }
73  
74      @Override
75      protected boolean accept(MuleEvent event)
76      {
77          if (filter == null)
78          {
79              return true;
80          }
81  
82          if (event != null)
83          {
84              return filter.accept(event.getMessage());
85          }
86          else
87          {
88              return false;
89          }
90      }
91  
92      @Override
93      protected MuleException filterUnacceptedException(MuleEvent event)
94      {
95          return new FilterUnacceptedException(CoreMessages.messageRejectedByFilter(), event, filter);
96      }
97  
98      public Filter getFilter()
99      {
100         return filter;
101     }
102 
103     public void setFilter(Filter filter)
104     {
105         this.filter = filter;
106     }
107 
108     @Override
109     public String toString()
110     {
111         return (filter == null ? "null filter" : filter.getClass().getName()) + " (wrapped by "
112                + this.getClass().getSimpleName() + ")";
113     }
114 
115     @Override
116     public void setMuleContext(MuleContext context)
117     {
118         super.setMuleContext(context);
119         if (!onUnacceptedFlowConstruct && unacceptedMessageProcessor instanceof MuleContextAware)
120         {
121             ((MuleContextAware) unacceptedMessageProcessor).setMuleContext(context);
122         }
123     }
124 
125     public void setFlowConstruct(FlowConstruct flowConstruct)
126     {
127         if (!onUnacceptedFlowConstruct && unacceptedMessageProcessor instanceof FlowConstructAware)
128         {
129             ((FlowConstructAware) unacceptedMessageProcessor).setFlowConstruct(flowConstruct);
130         }
131     }
132 
133     public void initialise() throws InitialisationException
134     {
135         if (!onUnacceptedFlowConstruct && unacceptedMessageProcessor instanceof Initialisable)
136         {
137             ((Initialisable) unacceptedMessageProcessor).initialise();
138         }
139     }
140 
141     public void start() throws MuleException
142     {
143         if (!onUnacceptedFlowConstruct && unacceptedMessageProcessor instanceof Startable)
144         {
145             ((Startable) unacceptedMessageProcessor).start();
146         }
147     }
148 
149     public void stop() throws MuleException
150     {
151         if (!onUnacceptedFlowConstruct && unacceptedMessageProcessor instanceof Stoppable)
152         {
153             ((Stoppable) unacceptedMessageProcessor).stop();
154         }
155     }
156 
157     public void dispose()
158     {
159         if (!onUnacceptedFlowConstruct && unacceptedMessageProcessor instanceof Disposable)
160         {
161             ((Disposable) unacceptedMessageProcessor).dispose();
162         }
163     }
164 }