View Javadoc

1   /*
2    * $Id: OGNLFilter.java 19191 2010-08-25 21:05:23Z tcarlson $
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.module.ognl.filters;
12  
13  import static org.mule.util.ClassUtils.equal;
14  import static org.mule.util.ClassUtils.hash;
15  import ognl.Ognl;
16  import ognl.OgnlException;
17  
18  import org.mule.api.MuleMessage;
19  import org.mule.api.config.ConfigurationException;
20  import org.mule.api.routing.filter.Filter;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  
25  public class OGNLFilter implements Filter
26  {
27      protected final Log logger = LogFactory.getLog(this.getClass());
28  
29      private volatile String expression;
30      private volatile Object compiledExpression;
31  
32      public String getExpression()
33      {
34          return expression;
35      }
36  
37      /**
38       * Sets the expression for this filter. The argument must be a valid expression
39       * as described in the OGNL documentation.
40       *
41       * @param expression the expression to use for message evaluation
42       * @throws ConfigurationException if the expression cannot be parsed
43       * @see Ognl#parseExpression(String)
44       */
45      public void setExpression(String expression) throws ConfigurationException
46      {
47          try
48          {
49              this.compiledExpression = Ognl.parseExpression(expression);
50              this.expression = expression;
51          }
52          catch (OgnlException ex)
53          {
54              throw new ConfigurationException(ex);
55          }
56      }
57  
58      public boolean accept(MuleMessage message)
59      {
60          // no message: nothing to filter
61          if (message == null)
62          {
63              return false;
64          }
65  
66          Object candidate = message.getPayload();
67          // no payload: still nothing to filter
68          if (candidate == null)
69          {
70              return false;
71          }
72  
73          // no expression configured: we reject by default
74          if (compiledExpression == null)
75          {
76              logger.warn("No expression configured - rejecting message.");
77              return false;
78          }
79  
80          try
81          {
82              Object result = Ognl.getValue(compiledExpression, candidate);
83              // we only need to take boolean expressoin results into account
84              if (result instanceof Boolean)
85              {
86                  return ((Boolean) result).booleanValue();
87              }
88          }
89          catch (OgnlException ex)
90          {
91              logger.error(ex);
92          }
93  
94          // default action: reject
95          return false;
96      }
97  
98      public boolean equals(Object obj)
99      {
100         if (this == obj) return true;
101         if (obj == null || getClass() != obj.getClass()) return false;
102 
103         final OGNLFilter other = (OGNLFilter) obj;
104         return equal(expression, other.expression);
105     }
106 
107     public int hashCode()
108     {
109         return hash(new Object[]{this.getClass(), expression});
110     }
111 }