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