View Javadoc

1   /*
2    * $Id: RegExFilter.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 static org.mule.util.ClassUtils.equal;
14  import static org.mule.util.ClassUtils.hash;
15  
16  import org.mule.api.MuleMessage;
17  import org.mule.api.routing.filter.Filter;
18  import org.mule.api.routing.filter.ObjectFilter;
19  import org.mule.api.transformer.TransformerException;
20  import org.mule.config.i18n.CoreMessages;
21  import org.mule.transformer.simple.ByteArrayToObject;
22  
23  import java.util.regex.Pattern;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  
28  /**
29   * <code>RegExFilter</code> is used to match a String argument against a regular expression.
30   */
31  public class RegExFilter implements Filter, ObjectFilter
32  {
33      protected transient Log logger = LogFactory.getLog(getClass());
34  
35      private Pattern pattern;
36  
37      public RegExFilter()
38      {
39          super();
40      }
41  
42      public RegExFilter(String pattern)
43      {
44          this.pattern = Pattern.compile(pattern);
45      }
46  
47      public boolean accept(MuleMessage message)
48      {
49          try
50          {
51              return accept(message.getPayloadAsString());
52          }
53          catch (Exception e)
54          {
55              throw new IllegalArgumentException(e);
56          }
57      }
58  
59      public boolean accept(Object object)
60      {
61          if (object == null)
62          {
63              return false;
64          }
65  
66          Object tempObject = object;
67  
68          // check whether the payload is a byte[] or a char[]. If it is, then it has
69          // to be transformed otherwise the toString will not represent the true
70          // contents
71          // of the payload for the RegEx filter to use.
72          if (object instanceof byte[])
73          {
74              ByteArrayToObject transformer = new ByteArrayToObject();
75              try
76              {
77                  object = transformer.transform(object);
78              }
79              catch (TransformerException e)
80              {
81                  logger.warn(CoreMessages.transformFailedBeforeFilter(), e);
82                  // revert transformation
83                  object = tempObject;
84              }
85          }
86          else if (object instanceof char[])
87          {
88              object = new String((char[]) object);
89          }
90  
91          return (pattern != null && pattern.matcher(object.toString()).find());
92      }
93  
94      public String getPattern()
95      {
96          return (pattern == null ? null : pattern.pattern());
97      }
98  
99      public void setPattern(String pattern)
100     {
101         this.pattern = (pattern != null ? Pattern.compile(pattern) : null);
102     }
103     
104     @Override
105     public boolean equals(Object obj)
106     {
107         if (this == obj) return true;
108         if (obj == null || getClass() != obj.getClass()) return false;
109 
110         final RegExFilter other = (RegExFilter) obj;
111         return equal(pattern, other.pattern);
112     }
113 
114     @Override
115     public int hashCode()
116     {
117         return hash(new Object[]{this.getClass(), pattern});
118     }
119 }