1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.routing.filters; |
12 | |
|
13 | |
import static org.mule.util.ClassUtils.equal; |
14 | |
import static org.mule.util.ClassUtils.hash; |
15 | |
|
16 | |
import org.mule.api.MuleMessage; |
17 | |
import org.mule.api.routing.filter.Filter; |
18 | |
import org.mule.api.routing.filter.ObjectFilter; |
19 | |
import org.mule.api.transformer.TransformerException; |
20 | |
import org.mule.config.i18n.CoreMessages; |
21 | |
import org.mule.transformer.simple.ByteArrayToObject; |
22 | |
|
23 | |
import java.util.regex.Pattern; |
24 | |
|
25 | |
import org.apache.commons.logging.Log; |
26 | |
import org.apache.commons.logging.LogFactory; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
public class RegExFilter implements Filter, ObjectFilter |
32 | |
{ |
33 | 0 | protected transient Log logger = LogFactory.getLog(getClass()); |
34 | |
|
35 | |
private Pattern pattern; |
36 | |
|
37 | |
public RegExFilter() |
38 | |
{ |
39 | 0 | super(); |
40 | 0 | } |
41 | |
|
42 | |
public RegExFilter(String pattern) |
43 | 0 | { |
44 | 0 | this.pattern = Pattern.compile(pattern); |
45 | 0 | } |
46 | |
|
47 | |
public boolean accept(MuleMessage message) |
48 | |
{ |
49 | |
try |
50 | |
{ |
51 | 0 | return accept(message.getPayloadAsString()); |
52 | |
} |
53 | 0 | catch (Exception e) |
54 | |
{ |
55 | 0 | throw new IllegalArgumentException(e); |
56 | |
} |
57 | |
} |
58 | |
|
59 | |
public boolean accept(Object object) |
60 | |
{ |
61 | 0 | if (object == null) |
62 | |
{ |
63 | 0 | return false; |
64 | |
} |
65 | |
|
66 | 0 | Object tempObject = object; |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | 0 | if (object instanceof byte[]) |
73 | |
{ |
74 | 0 | ByteArrayToObject transformer = new ByteArrayToObject(); |
75 | |
try |
76 | |
{ |
77 | 0 | object = transformer.transform(object); |
78 | |
} |
79 | 0 | catch (TransformerException e) |
80 | |
{ |
81 | 0 | logger.warn(CoreMessages.transformFailedBeforeFilter(), e); |
82 | |
|
83 | 0 | object = tempObject; |
84 | 0 | } |
85 | 0 | } |
86 | 0 | else if (object instanceof char[]) |
87 | |
{ |
88 | 0 | object = new String((char[]) object); |
89 | |
} |
90 | |
|
91 | 0 | return (pattern != null && pattern.matcher(object.toString()).find()); |
92 | |
} |
93 | |
|
94 | |
public String getPattern() |
95 | |
{ |
96 | 0 | return (pattern == null ? null : pattern.pattern()); |
97 | |
} |
98 | |
|
99 | |
public void setPattern(String pattern) |
100 | |
{ |
101 | 0 | this.pattern = (pattern != null ? Pattern.compile(pattern) : null); |
102 | 0 | } |
103 | |
|
104 | |
@Override |
105 | |
public boolean equals(Object obj) |
106 | |
{ |
107 | 0 | if (this == obj) return true; |
108 | 0 | if (obj == null || getClass() != obj.getClass()) return false; |
109 | |
|
110 | 0 | final RegExFilter other = (RegExFilter) obj; |
111 | 0 | return equal(pattern, other.pattern); |
112 | |
} |
113 | |
|
114 | |
@Override |
115 | |
public int hashCode() |
116 | |
{ |
117 | 0 | return hash(new Object[]{this.getClass(), pattern}); |
118 | |
} |
119 | |
} |