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