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 | 32 | protected transient final Log logger = LogFactory.getLog(MessagePropertyFilter.class); |
35 | 32 | private boolean caseSensitive = true; |
36 | 32 | private boolean not = false; |
37 | |
|
38 | |
private String propertyName; |
39 | |
private String propertyValue; |
40 | |
|
41 | |
public MessagePropertyFilter() |
42 | |
{ |
43 | 2 | super(); |
44 | 2 | } |
45 | |
|
46 | |
public MessagePropertyFilter(String expression) |
47 | 30 | { |
48 | 30 | setExpression(expression); |
49 | 30 | } |
50 | |
|
51 | |
public boolean accept(MuleMessage message) |
52 | |
{ |
53 | 46 | if (message == null) |
54 | |
{ |
55 | 2 | return false; |
56 | |
} |
57 | |
|
58 | 44 | Object value = message.getProperty(propertyName); |
59 | |
boolean match; |
60 | 44 | if (value == null) |
61 | |
{ |
62 | 20 | match = compare(null, propertyValue); |
63 | |
} |
64 | |
else |
65 | |
{ |
66 | 24 | match = compare(value.toString(), propertyValue); |
67 | |
} |
68 | 44 | if(!match && logger.isDebugEnabled()) |
69 | |
{ |
70 | 0 | logger.debug("Property: " + propertyName + " not found on message with Id: " + message.getUniqueId()); |
71 | |
} |
72 | 44 | return match; |
73 | |
} |
74 | |
|
75 | |
protected boolean compare(String value1, String value2) |
76 | |
{ |
77 | 44 | if (value1 == null && value2 != null && !"null".equals(value2) && not) |
78 | |
{ |
79 | 4 | return true; |
80 | |
} |
81 | |
|
82 | 40 | if (value1 == null) |
83 | |
{ |
84 | 16 | value1 = "null"; |
85 | |
} |
86 | |
|
87 | 40 | if (value2 == null) |
88 | |
{ |
89 | 0 | value2 = "null"; |
90 | |
} |
91 | |
|
92 | 40 | boolean result = false; |
93 | |
|
94 | 40 | if (caseSensitive) |
95 | |
{ |
96 | 38 | result = value1.equals(value2); |
97 | |
} |
98 | |
else |
99 | |
{ |
100 | 2 | result = value1.equalsIgnoreCase(value2); |
101 | |
} |
102 | |
|
103 | 40 | return (not ? !result : result); |
104 | |
} |
105 | |
|
106 | |
public String getExpression() |
107 | |
{ |
108 | 0 | return propertyName + '=' + propertyValue; |
109 | |
} |
110 | |
|
111 | |
public void setExpression(String expression) |
112 | |
{ |
113 | 30 | int i = expression.indexOf('='); |
114 | 30 | if (i == -1) |
115 | |
{ |
116 | 0 | throw new IllegalArgumentException( |
117 | |
"Pattern is malformed - it should be a key value pair, i.e. property=value: " + expression); |
118 | |
} |
119 | |
else |
120 | |
{ |
121 | 30 | if (expression.charAt(i - 1) == '!') |
122 | |
{ |
123 | 16 | not = true; |
124 | 16 | propertyName = expression.substring(0, i - 1).trim(); |
125 | |
} |
126 | |
else |
127 | |
{ |
128 | 14 | propertyName = expression.substring(0, i).trim(); |
129 | |
} |
130 | 30 | propertyValue = expression.substring(i + 1).trim(); |
131 | |
} |
132 | 30 | } |
133 | |
|
134 | |
public boolean isCaseSensitive() |
135 | |
{ |
136 | 0 | return caseSensitive; |
137 | |
} |
138 | |
|
139 | |
public void setCaseSensitive(boolean caseSensitive) |
140 | |
{ |
141 | 4 | this.caseSensitive = caseSensitive; |
142 | 4 | } |
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
public void setPattern(String pattern) |
148 | |
{ |
149 | 0 | this.setExpression(pattern); |
150 | 0 | } |
151 | |
} |