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