1
2
3
4
5
6
7
8
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
21
22
23
24
25
26
27
28
29 public class MessagePropertyFilter implements Filter
30 {
31
32
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
146
147 public void setPattern(String pattern)
148 {
149 this.setExpression(pattern);
150 }
151 }