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.util.ClassUtils; |
13 | |
import org.mule.util.StringUtils; |
14 | |
|
15 | |
import org.apache.commons.logging.Log; |
16 | |
import org.apache.commons.logging.LogFactory; |
17 | |
|
18 | |
import static org.mule.util.ClassUtils.equal; |
19 | |
import static org.mule.util.ClassUtils.hash; |
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
public class WildcardFilter implements Filter, ObjectFilter |
31 | |
{ |
32 | 0 | protected final Log logger = LogFactory.getLog(this.getClass()); |
33 | |
|
34 | |
protected volatile String pattern; |
35 | |
protected volatile String[] patterns; |
36 | 0 | private volatile boolean caseSensitive = true; |
37 | |
|
38 | |
public WildcardFilter() |
39 | |
{ |
40 | 0 | super(); |
41 | 0 | } |
42 | |
|
43 | |
public WildcardFilter(String pattern) |
44 | 0 | { |
45 | 0 | this.setPattern(pattern); |
46 | 0 | } |
47 | |
|
48 | |
public boolean accept(MuleMessage message) |
49 | |
{ |
50 | |
try |
51 | |
{ |
52 | 0 | return accept(message.getPayloadAsString()); |
53 | |
} |
54 | 0 | catch (Exception e) |
55 | |
{ |
56 | 0 | logger.warn("An exception occured while filtering", e); |
57 | 0 | return false; |
58 | |
} |
59 | |
} |
60 | |
|
61 | |
public boolean accept(Object object) |
62 | |
{ |
63 | 0 | if (object == null || pattern ==null) |
64 | |
{ |
65 | 0 | return false; |
66 | |
} |
67 | |
|
68 | 0 | if (this.pattern.equals(object)) |
69 | |
{ |
70 | 0 | return true; |
71 | |
} |
72 | |
|
73 | 0 | String[] currentPatterns = this.patterns; |
74 | 0 | if (currentPatterns != null) |
75 | |
{ |
76 | 0 | for (String pattern : currentPatterns) |
77 | |
{ |
78 | |
boolean foundMatch; |
79 | |
|
80 | 0 | if ("*".equals(pattern) || "**".equals(pattern)) |
81 | |
{ |
82 | 0 | return true; |
83 | |
} |
84 | |
|
85 | 0 | String candidate = object.toString(); |
86 | |
|
87 | 0 | if (!isCaseSensitive()) |
88 | |
{ |
89 | 0 | pattern = pattern.toLowerCase(); |
90 | 0 | candidate = candidate.toLowerCase(); |
91 | |
} |
92 | |
|
93 | 0 | int i = pattern.indexOf('*'); |
94 | 0 | if (i == -1) |
95 | |
{ |
96 | 0 | foundMatch = pattern.equals(candidate); |
97 | |
} |
98 | |
else |
99 | |
{ |
100 | 0 | int i2 = pattern.indexOf('*', i + 1); |
101 | 0 | if (i2 > 1) |
102 | |
{ |
103 | 0 | foundMatch = candidate.indexOf(pattern.substring(1, i2)) > -1; |
104 | |
} |
105 | 0 | else if (i == 0) |
106 | |
{ |
107 | 0 | foundMatch = candidate.endsWith(pattern.substring(1)); |
108 | |
} |
109 | |
else |
110 | |
{ |
111 | 0 | foundMatch = candidate.startsWith(pattern.substring(0, i)); |
112 | |
} |
113 | |
} |
114 | |
|
115 | 0 | if (foundMatch) |
116 | |
{ |
117 | 0 | return true; |
118 | |
} |
119 | 0 | else if (pattern.endsWith("+") && pattern.length() > 1) |
120 | |
{ |
121 | 0 | String className = pattern.substring(0, pattern.length() - 1); |
122 | |
try |
123 | |
{ |
124 | 0 | Class<?> theClass = ClassUtils.loadClass(className, this.getClass()); |
125 | 0 | if (theClass.isInstance(object)) |
126 | |
{ |
127 | 0 | return true; |
128 | |
} |
129 | |
} |
130 | 0 | catch (ClassNotFoundException e) |
131 | |
{ |
132 | 0 | } |
133 | |
} |
134 | |
} |
135 | |
} |
136 | |
|
137 | 0 | return false; |
138 | |
} |
139 | |
|
140 | |
public String getPattern() |
141 | |
{ |
142 | 0 | return pattern; |
143 | |
} |
144 | |
|
145 | |
public void setPattern(String pattern) |
146 | |
{ |
147 | 0 | this.pattern = pattern; |
148 | 0 | this.patterns = StringUtils.splitAndTrim(pattern, ","); |
149 | 0 | } |
150 | |
|
151 | |
public boolean isCaseSensitive() |
152 | |
{ |
153 | 0 | return caseSensitive; |
154 | |
} |
155 | |
|
156 | |
public void setCaseSensitive(boolean caseSensitive) |
157 | |
{ |
158 | 0 | this.caseSensitive = caseSensitive; |
159 | 0 | } |
160 | |
|
161 | |
public boolean equals(Object obj) |
162 | |
{ |
163 | 0 | if (this == obj) return true; |
164 | 0 | if (obj == null || getClass() != obj.getClass()) return false; |
165 | |
|
166 | 0 | final WildcardFilter other = (WildcardFilter) obj; |
167 | 0 | return equal(pattern, other.pattern) |
168 | |
&& equal(patterns, other.patterns) |
169 | |
&& caseSensitive == other.caseSensitive; |
170 | |
} |
171 | |
|
172 | |
public int hashCode() |
173 | |
{ |
174 | 0 | return hash(new Object[]{this.getClass(), pattern, patterns, caseSensitive}); |
175 | |
} |
176 | |
} |