KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > routing > filters > MessagePropertyFilter


1 /*
2  * $Id: MessagePropertyFilter.java 4219 2006-12-09 10:15:14Z lajos $
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 MuleSource MPL
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.umo.UMOFilter;
14 import org.mule.umo.UMOMessage;
15
16 /**
17  * <code>MessagePropertyFilter</code> can be used to filter against properties on
18  * an event. This can be very useful as the event properties represent all the meta
19  * information about the event from the underlying transport, so for an event
20  * received over HTTP you can check for HTTP headers etc. The pattern should be
21  * expressed as a key/value pair, i.e. "propertyName=value". If you want to compare
22  * more than one property you can use the logic filters for And, Or and Not
23  * expressions. By default the comparison is case sensitive; you can set the
24  * <i>caseSensitive</i> property to override this.
25  */

26 public class MessagePropertyFilter implements UMOFilter
27 {
28     private boolean caseSensitive = true;
29     private boolean not = false;
30
31     private String JavaDoc propertyName;
32     private String JavaDoc propertyValue;
33
34     public MessagePropertyFilter()
35     {
36         super();
37     }
38
39     public MessagePropertyFilter(String JavaDoc expression)
40     {
41         setExpression(expression);
42     }
43
44     public boolean accept(UMOMessage message)
45     {
46         if (message == null)
47         {
48             return false;
49         }
50
51         Object JavaDoc value = message.getProperty(propertyName);
52
53         if (value == null)
54         {
55             return compare(null, propertyValue);
56         }
57         else
58         {
59             return compare(value.toString(), propertyValue);
60         }
61     }
62
63     protected boolean compare(String JavaDoc value1, String JavaDoc value2)
64     {
65         if (value1 == null && value2 != null && !"null".equals(value2) && not)
66         {
67             return true;
68         }
69
70         if (value1 == null)
71         {
72             value1 = "null";
73         }
74
75         if (value2 == null)
76         {
77             value2 = "null";
78         }
79
80         boolean result = false;
81
82         if (caseSensitive)
83         {
84             result = value1.equals(value2);
85         }
86         else
87         {
88             result = value1.equalsIgnoreCase(value2);
89         }
90
91         return (not ? !result : result);
92     }
93
94     public String JavaDoc getExpression()
95     {
96         return propertyName + '=' + propertyValue;
97     }
98
99     public void setExpression(String JavaDoc expression)
100     {
101         int i = expression.indexOf('=');
102         if (i == -1)
103         {
104             throw new IllegalArgumentException JavaDoc(
105                 "Pattern is malformed - it should be a key value pair, i.e. property=value: " + expression);
106         }
107         else
108         {
109             if (expression.charAt(i - 1) == '!')
110             {
111                 not = true;
112                 propertyName = expression.substring(0, i - 1).trim();
113             }
114             else
115             {
116                 propertyName = expression.substring(0, i).trim();
117             }
118             propertyValue = expression.substring(i + 1).trim();
119         }
120     }
121
122     public boolean isCaseSensitive()
123     {
124         return caseSensitive;
125     }
126
127     public void setCaseSensitive(boolean caseSensitive)
128     {
129         this.caseSensitive = caseSensitive;
130     }
131
132 }
133
Popular Tags