1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.mule.routing.filters; |
11 | |
|
12 | |
import static org.mule.util.ClassUtils.equal; |
13 | |
import static org.mule.util.ClassUtils.hash; |
14 | |
|
15 | |
import org.mule.api.MuleContext; |
16 | |
import org.mule.api.MuleMessage; |
17 | |
import org.mule.api.context.MuleContextAware; |
18 | |
import org.mule.api.routing.filter.Filter; |
19 | |
import org.mule.api.transport.PropertyScope; |
20 | |
import org.mule.expression.ExpressionConfig; |
21 | |
import org.mule.util.StringUtils; |
22 | |
|
23 | |
import java.text.MessageFormat; |
24 | |
|
25 | |
import org.apache.commons.logging.Log; |
26 | |
import org.apache.commons.logging.LogFactory; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
public class ExpressionFilter implements Filter, MuleContextAware |
49 | |
{ |
50 | |
|
51 | |
|
52 | |
|
53 | 0 | protected transient final Log logger = LogFactory.getLog(ExpressionFilter.class); |
54 | |
|
55 | |
private ExpressionConfig config; |
56 | |
private String fullExpression; |
57 | 0 | private boolean nullReturnsTrue = false; |
58 | |
private MuleContext muleContext; |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | 0 | private ClassLoader expressionEvaluationClassLoader = Thread.currentThread().getContextClassLoader(); |
65 | |
|
66 | |
|
67 | |
private Filter delegateFilter; |
68 | |
|
69 | |
public ExpressionFilter(String evaluator, String customEvaluator, String expression) |
70 | 0 | { |
71 | 0 | this.config = new ExpressionConfig(expression, evaluator, customEvaluator); |
72 | 0 | } |
73 | |
|
74 | |
public ExpressionFilter(String evaluator, String expression) |
75 | 0 | { |
76 | 0 | this.config = new ExpressionConfig(expression, evaluator, null); |
77 | 0 | } |
78 | |
|
79 | |
public ExpressionFilter(String expression) |
80 | 0 | { |
81 | 0 | this.config = new ExpressionConfig(); |
82 | 0 | this.config.parse(expression); |
83 | 0 | } |
84 | |
|
85 | |
public ExpressionFilter() |
86 | |
{ |
87 | 0 | super(); |
88 | 0 | this.config = new ExpressionConfig(); |
89 | 0 | } |
90 | |
|
91 | |
public void setMuleContext(MuleContext context) |
92 | |
{ |
93 | 0 | this.muleContext = context; |
94 | 0 | } |
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
public boolean accept(MuleMessage message) |
103 | |
{ |
104 | 0 | String expr = getFullExpression(); |
105 | 0 | if (delegateFilter != null) |
106 | |
{ |
107 | 0 | boolean result = delegateFilter.accept(message); |
108 | 0 | if (logger.isDebugEnabled()) |
109 | |
{ |
110 | 0 | logger.debug(MessageFormat.format("Result of expression filter: {0} is: {1}", expr, result)); |
111 | |
} |
112 | 0 | return result; |
113 | |
} |
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | 0 | ClassLoader originalContextClassLoader = Thread.currentThread().getContextClassLoader(); |
122 | |
try |
123 | |
{ |
124 | 0 | Thread.currentThread().setContextClassLoader(expressionEvaluationClassLoader); |
125 | 0 | return muleContext.getExpressionManager().evaluateBoolean(expr, message, nullReturnsTrue, |
126 | |
!nullReturnsTrue); |
127 | |
} |
128 | |
finally |
129 | |
{ |
130 | |
|
131 | 0 | Thread.currentThread().setContextClassLoader(originalContextClassLoader); |
132 | |
} |
133 | |
} |
134 | |
|
135 | |
protected String getFullExpression() |
136 | |
{ |
137 | 0 | if(fullExpression==null) |
138 | |
{ |
139 | |
|
140 | 0 | if(config.getEvaluator().equals("header")) |
141 | |
{ |
142 | 0 | delegateFilter = new MessagePropertyFilter(config.getExpression()); |
143 | |
} |
144 | 0 | else if (config.getEvaluator().equals("variable")) |
145 | |
{ |
146 | 0 | delegateFilter = new MessagePropertyFilter(PropertyScope.INVOCATION_NAME + ":" |
147 | |
+ config.getExpression()); |
148 | |
} |
149 | 0 | else if(config.getEvaluator().equals("regex")) |
150 | |
{ |
151 | 0 | delegateFilter = new RegExFilter(config.getExpression()); |
152 | |
} |
153 | 0 | else if(config.getEvaluator().equals("wildcard")) |
154 | |
{ |
155 | 0 | delegateFilter = new WildcardFilter(config.getExpression()); |
156 | |
} |
157 | 0 | else if(config.getEvaluator().equals("payload-type")) |
158 | |
{ |
159 | |
try |
160 | |
{ |
161 | 0 | delegateFilter = new PayloadTypeFilter(config.getExpression()); |
162 | |
} |
163 | 0 | catch (ClassNotFoundException e) |
164 | |
{ |
165 | 0 | IllegalArgumentException iae = new IllegalArgumentException(); |
166 | 0 | iae.initCause(e); |
167 | 0 | throw iae; |
168 | 0 | } |
169 | |
} |
170 | 0 | else if(config.getEvaluator().equals("exception-type")) |
171 | |
{ |
172 | |
try |
173 | |
{ |
174 | 0 | if (StringUtils.isEmpty(config.getExpression())) |
175 | |
{ |
176 | 0 | delegateFilter = new ExceptionTypeFilter(); |
177 | |
} |
178 | |
else |
179 | |
{ |
180 | 0 | delegateFilter = new ExceptionTypeFilter(config.getExpression()); |
181 | |
} |
182 | |
} |
183 | 0 | catch (ClassNotFoundException e) |
184 | |
{ |
185 | 0 | IllegalArgumentException iae = new IllegalArgumentException(); |
186 | 0 | iae.initCause(e); |
187 | 0 | throw iae; |
188 | 0 | } |
189 | |
} |
190 | |
else |
191 | |
{ |
192 | |
|
193 | 0 | fullExpression = config.getFullExpression(muleContext.getExpressionManager()); |
194 | |
} |
195 | |
} |
196 | 0 | return fullExpression; |
197 | |
} |
198 | |
|
199 | |
public String getCustomEvaluator() |
200 | |
{ |
201 | 0 | return config.getCustomEvaluator(); |
202 | |
} |
203 | |
|
204 | |
public void setCustomEvaluator(String customEvaluator) |
205 | |
{ |
206 | 0 | this.config.setCustomEvaluator(customEvaluator); |
207 | 0 | fullExpression=null; |
208 | 0 | } |
209 | |
|
210 | |
public String getEvaluator() |
211 | |
{ |
212 | 0 | return config.getEvaluator(); |
213 | |
} |
214 | |
|
215 | |
public void setEvaluator(String evaluator) |
216 | |
{ |
217 | 0 | this.config.setEvaluator(evaluator); |
218 | 0 | fullExpression=null; |
219 | 0 | } |
220 | |
|
221 | |
public String getExpression() |
222 | |
{ |
223 | 0 | return config.getExpression(); |
224 | |
} |
225 | |
|
226 | |
public void setExpression(String expression) |
227 | |
{ |
228 | 0 | this.config.setExpression(expression); |
229 | 0 | fullExpression=null; |
230 | 0 | } |
231 | |
|
232 | |
public boolean isNullReturnsTrue() |
233 | |
{ |
234 | 0 | return nullReturnsTrue; |
235 | |
} |
236 | |
|
237 | |
public void setNullReturnsTrue(boolean nullReturnsTrue) |
238 | |
{ |
239 | 0 | this.nullReturnsTrue = nullReturnsTrue; |
240 | 0 | } |
241 | |
|
242 | |
@Override |
243 | |
public boolean equals(Object obj) |
244 | |
{ |
245 | 0 | if (this == obj) return true; |
246 | 0 | if (obj == null || getClass() != obj.getClass()) return false; |
247 | |
|
248 | 0 | final ExpressionFilter other = (ExpressionFilter) obj; |
249 | 0 | return equal(config, other.config) |
250 | |
&& equal(delegateFilter, other.delegateFilter) |
251 | |
&& nullReturnsTrue == other.nullReturnsTrue; |
252 | |
} |
253 | |
|
254 | |
@Override |
255 | |
public int hashCode() |
256 | |
{ |
257 | 0 | return hash(new Object[]{this.getClass(), config, delegateFilter, nullReturnsTrue}); |
258 | |
} |
259 | |
} |