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