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