1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.mule.routing.filters; |
11 | |
|
12 | |
import org.mule.api.MuleMessage; |
13 | |
import org.mule.api.routing.filter.Filter; |
14 | |
import org.mule.config.i18n.CoreMessages; |
15 | |
import org.mule.util.expression.ExpressionEvaluatorManager; |
16 | |
|
17 | |
import org.apache.commons.logging.Log; |
18 | |
import org.apache.commons.logging.LogFactory; |
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
public class ExpressionFilter implements Filter |
24 | |
{ |
25 | |
|
26 | |
|
27 | |
|
28 | 24 | protected transient final Log logger = LogFactory.getLog(ExpressionFilter.class); |
29 | |
|
30 | |
private String evaluator; |
31 | |
private String expression; |
32 | |
private String customEvaluator; |
33 | |
private String fullExpression; |
34 | 24 | private boolean nullReturnsTrue = false; |
35 | |
private static final String TRUE = "true"; |
36 | |
|
37 | |
|
38 | |
private Filter delegateFilter; |
39 | |
|
40 | |
public ExpressionFilter(String evaluator, String customEvaluator, String expression) |
41 | 0 | { |
42 | 0 | this.customEvaluator = customEvaluator; |
43 | 0 | this.evaluator = evaluator; |
44 | 0 | this.expression = expression; |
45 | 0 | } |
46 | |
|
47 | |
public ExpressionFilter(String evaluator, String expression) |
48 | 8 | { |
49 | 8 | this.evaluator = evaluator; |
50 | 8 | this.expression = expression; |
51 | 8 | } |
52 | |
|
53 | |
public ExpressionFilter(String expression) |
54 | 16 | { |
55 | 16 | int i = expression.indexOf(":"); |
56 | 16 | if(i < 0) |
57 | |
{ |
58 | 0 | throw new IllegalArgumentException("Expression is invalid: " + expression); |
59 | |
} |
60 | 16 | this.evaluator = expression.substring(0, i); |
61 | 16 | this.expression = expression.substring(i+1); |
62 | 16 | } |
63 | |
|
64 | |
public ExpressionFilter() |
65 | |
{ |
66 | 0 | super(); |
67 | 0 | } |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
public boolean accept(MuleMessage message) |
76 | |
{ |
77 | 64 | String expr = getFullExpression(); |
78 | 64 | if (delegateFilter != null) |
79 | |
{ |
80 | 46 | return delegateFilter.accept(message); |
81 | |
} |
82 | |
|
83 | 18 | Object result = ExpressionEvaluatorManager.evaluate(expr, message); |
84 | 18 | if (result == null) |
85 | |
{ |
86 | 0 | return nullReturnsTrue; |
87 | |
} |
88 | 18 | else if (result instanceof Boolean) |
89 | |
{ |
90 | 0 | return ((Boolean) result).booleanValue(); |
91 | |
} |
92 | 18 | else if (result instanceof String) |
93 | |
{ |
94 | 18 | return ((String) result).toLowerCase().equals(TRUE); |
95 | |
} |
96 | |
else |
97 | |
{ |
98 | 0 | logger.warn("Expression: " + expr + ", returned an non-boolean result. Returning: " |
99 | |
+ !nullReturnsTrue); |
100 | 0 | return !nullReturnsTrue; |
101 | |
} |
102 | |
} |
103 | |
|
104 | |
protected String getFullExpression() |
105 | |
{ |
106 | 64 | if(fullExpression==null) |
107 | |
{ |
108 | 52 | if(evaluator==null) |
109 | |
{ |
110 | 0 | throw new IllegalArgumentException(CoreMessages.objectIsNull("evaluator").getMessage()); |
111 | |
} |
112 | 52 | if(evaluator.equals("custom")) |
113 | |
{ |
114 | 0 | if(customEvaluator==null) |
115 | |
{ |
116 | 0 | throw new IllegalArgumentException(CoreMessages.objectIsNull("customEvaluator").getMessage()); |
117 | |
} |
118 | |
else |
119 | |
{ |
120 | 0 | evaluator = customEvaluator; |
121 | |
} |
122 | |
} |
123 | 52 | if(evaluator.equals("header")) |
124 | |
{ |
125 | 16 | delegateFilter = new MessagePropertyFilter(expression); |
126 | |
} |
127 | 36 | else if(evaluator.equals("regex")) |
128 | |
{ |
129 | 8 | delegateFilter = new RegExFilter(expression); |
130 | |
} |
131 | 28 | else if(evaluator.equals("wildcard")) |
132 | |
{ |
133 | 6 | delegateFilter = new WildcardFilter(expression); |
134 | |
} |
135 | 22 | else if(evaluator.equals("payload-type")) |
136 | |
{ |
137 | |
try |
138 | |
{ |
139 | 8 | delegateFilter = new PayloadTypeFilter(expression); |
140 | |
} |
141 | 0 | catch (ClassNotFoundException e) |
142 | |
{ |
143 | 0 | IllegalArgumentException iae = new IllegalArgumentException(); |
144 | 0 | iae.initCause(e); |
145 | 0 | throw iae; |
146 | 8 | } |
147 | |
} |
148 | 14 | else if(evaluator.equals("exception-type")) |
149 | |
{ |
150 | |
try |
151 | |
{ |
152 | 8 | delegateFilter = new ExceptionTypeFilter(expression); |
153 | |
} |
154 | 0 | catch (ClassNotFoundException e) |
155 | |
{ |
156 | 0 | IllegalArgumentException iae = new IllegalArgumentException(); |
157 | 0 | iae.initCause(e); |
158 | 0 | throw iae; |
159 | 8 | } |
160 | |
} |
161 | |
else |
162 | |
{ |
163 | |
|
164 | 6 | fullExpression = evaluator + ":" + (expression==null ? "" : expression); |
165 | |
} |
166 | |
} |
167 | 64 | return fullExpression; |
168 | |
} |
169 | |
|
170 | |
public String getCustomEvaluator() |
171 | |
{ |
172 | 0 | return customEvaluator; |
173 | |
} |
174 | |
|
175 | |
public void setCustomEvaluator(String customEvaluator) |
176 | |
{ |
177 | 0 | this.customEvaluator = customEvaluator; |
178 | 0 | } |
179 | |
|
180 | |
public String getEvaluator() |
181 | |
{ |
182 | 0 | return evaluator; |
183 | |
} |
184 | |
|
185 | |
public void setEvaluator(String evaluator) |
186 | |
{ |
187 | 0 | this.evaluator = evaluator; |
188 | 0 | } |
189 | |
|
190 | |
public String getExpression() |
191 | |
{ |
192 | 2 | return expression; |
193 | |
} |
194 | |
|
195 | |
public void setExpression(String expression) |
196 | |
{ |
197 | 0 | this.expression = expression; |
198 | 0 | } |
199 | |
|
200 | |
public boolean isNullReturnsTrue() |
201 | |
{ |
202 | 0 | return nullReturnsTrue; |
203 | |
} |
204 | |
|
205 | |
public void setNullReturnsTrue(boolean nullReturnsTrue) |
206 | |
{ |
207 | 6 | this.nullReturnsTrue = nullReturnsTrue; |
208 | 6 | } |
209 | |
} |