View Javadoc

1   /*
2    * $Id: MessagePropertyFilter.java 10489 2008-01-23 17:53:38Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.filters;
12  
13  import org.mule.api.MuleMessage;
14  import org.mule.api.routing.filter.Filter;
15  
16  import org.apache.commons.logging.Log;
17  import org.apache.commons.logging.LogFactory;
18  
19  /**
20   * <code>MessagePropertyFilter</code> can be used to filter against properties on
21   * an event. This can be very useful as the event properties represent all the meta
22   * information about the event from the underlying transport, so for an event
23   * received over HTTP you can check for HTTP headers etc. The pattern should be
24   * expressed as a key/value pair, i.e. "propertyName=value". If you want to compare
25   * more than one property you can use the logic filters for And, Or and Not
26   * expressions. By default the comparison is case sensitive; you can set the
27   * <i>caseSensitive</i> property to override this.
28   */
29  public class MessagePropertyFilter implements Filter
30  {
31      /**
32       * logger used by this class
33       */
34      protected transient final Log logger = LogFactory.getLog(MessagePropertyFilter.class);
35      private boolean caseSensitive = true;
36      private boolean not = false;
37  
38      private String propertyName;
39      private String propertyValue;
40  
41      public MessagePropertyFilter()
42      {
43          super();
44      }
45  
46      public MessagePropertyFilter(String expression)
47      {
48          setExpression(expression);
49      }
50  
51      public boolean accept(MuleMessage message)
52      {
53          if (message == null)
54          {
55              return false;
56          }
57  
58          Object value = message.getProperty(propertyName);
59          boolean match;
60          if (value == null)
61          {
62              match = compare(null, propertyValue);
63          }
64          else
65          {
66              match = compare(value.toString(), propertyValue);
67          }
68          if(!match && logger.isDebugEnabled())
69          {
70              logger.debug("Property: " + propertyName + " not found on message with Id: " + message.getUniqueId());
71          }
72          return match;
73      }
74  
75      protected boolean compare(String value1, String value2)
76      {
77          if (value1 == null && value2 != null && !"null".equals(value2) && not)
78          {
79              return true;
80          }
81  
82          if (value1 == null)
83          {
84              value1 = "null";
85          }
86  
87          if (value2 == null)
88          {
89              value2 = "null";
90          }
91  
92          boolean result = false;
93  
94          if (caseSensitive)
95          {
96              result = value1.equals(value2);
97          }
98          else
99          {
100             result = value1.equalsIgnoreCase(value2);
101         }
102 
103         return (not ? !result : result);
104     }
105 
106     public String getExpression()
107     {
108         return propertyName + '=' + propertyValue;
109     }
110 
111     public void setExpression(String expression)
112     {
113         int i = expression.indexOf('=');
114         if (i == -1)
115         {
116             throw new IllegalArgumentException(
117                 "Pattern is malformed - it should be a key value pair, i.e. property=value: " + expression);
118         }
119         else
120         {
121             if (expression.charAt(i - 1) == '!')
122             {
123                 not = true;
124                 propertyName = expression.substring(0, i - 1).trim();
125             }
126             else
127             {
128                 propertyName = expression.substring(0, i).trim();
129             }
130             propertyValue = expression.substring(i + 1).trim();
131         }
132     }
133 
134     public boolean isCaseSensitive()
135     {
136         return caseSensitive;
137     }
138 
139     public void setCaseSensitive(boolean caseSensitive)
140     {
141         this.caseSensitive = caseSensitive;
142     }
143     
144     /**
145      * All Filters that are configured via spring have to implement this method.
146      */
147     public void setPattern(String pattern)
148     {
149         this.setExpression(pattern);
150     }
151 }