View Javadoc

1   /*
2    * $Id: WildcardFilter.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
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   * <code>WildcardFilter</code> is used to match Strings against wildcards. It
26   * performs matches with "*", i.e. "jms.events.*" would catch "jms.events.customer"
27   * and "jms.events.receipts". This filter accepts a comma-separated list of patterns,
28   * so more than one filter pattern can be matched for a given argument:
29   * "jms.events.*, jms.actions.*" will match "jms.events.system" and "jms.actions" but
30   * not "jms.queue".
31   */
32  
33  public class WildcardFilter implements Filter, ObjectFilter
34  {
35      protected final Log logger = LogFactory.getLog(this.getClass());
36  
37      protected volatile String pattern;
38      protected volatile String[] patterns;
39      private volatile boolean caseSensitive = true;
40  
41      public WildcardFilter()
42      {
43          super();
44      }
45  
46      public WildcardFilter(String pattern)
47      {
48          this.setPattern(pattern);
49      }
50  
51      public boolean accept(MuleMessage message)
52      {
53          try
54          {
55              return accept(message.getPayloadAsString());
56          }
57          catch (Exception e)
58          {
59              logger.warn("An exception occured while filtering", e);
60              return false;
61          }
62      }
63  
64      public boolean accept(Object object)
65      {
66          if (object == null || pattern ==null)
67          {
68              return false;
69          }
70  
71          if (this.pattern.equals(object))
72          {
73              return true;
74          }
75  
76          String[] currentPatterns = this.patterns;
77          if (currentPatterns != null)
78          {
79              for (String pattern : currentPatterns)
80              {
81                  boolean foundMatch;
82  
83                  if ("*".equals(pattern) || "**".equals(pattern))
84                  {
85                      return true;
86                  }
87  
88                  String candidate = object.toString();
89  
90                  if (!isCaseSensitive())
91                  {
92                      pattern = pattern.toLowerCase();
93                      candidate = candidate.toLowerCase();
94                  }
95  
96                  int i = pattern.indexOf('*');
97                  if (i == -1)
98                  {
99                      foundMatch = pattern.equals(candidate);
100                 }
101                 else
102                 {
103                     int i2 = pattern.indexOf('*', i + 1);
104                     if (i2 > 1)
105                     {
106                         foundMatch = candidate.indexOf(pattern.substring(1, i2)) > -1;
107                     }
108                     else if (i == 0)
109                     {
110                         foundMatch = candidate.endsWith(pattern.substring(1));
111                     }
112                     else
113                     {
114                         foundMatch = candidate.startsWith(pattern.substring(0, i));
115                     }
116                 }
117 
118                 if (foundMatch)
119                 {
120                     return true;
121                 }
122             }
123         }
124 
125         return false;
126     }
127 
128     public String getPattern()
129     {
130         return pattern;
131     }
132 
133     public void setPattern(String pattern)
134     {
135         this.pattern = pattern;
136         this.patterns = StringUtils.splitAndTrim(pattern, ",");
137     }
138 
139     public boolean isCaseSensitive()
140     {
141         return caseSensitive;
142     }
143 
144     public void setCaseSensitive(boolean caseSensitive)
145     {
146         this.caseSensitive = caseSensitive;
147     }
148     
149     public boolean equals(Object obj)
150     {
151         if (this == obj) return true;
152         if (obj == null || getClass() != obj.getClass()) return false;
153 
154         final WildcardFilter other = (WildcardFilter) obj;
155         return equal(pattern, other.pattern)
156             && equal(patterns, other.patterns)
157             && caseSensitive == other.caseSensitive;
158     }
159 
160     public int hashCode()
161     {
162         return hash(new Object[]{this.getClass(), pattern, patterns, caseSensitive});
163     }
164 }