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