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