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