Coverage Report - org.mule.routing.filters.MessagePropertyFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
MessagePropertyFilter
84%
36/43
87%
26/30
2.333
 
 1  
 /*
 2  
  * $Id: MessagePropertyFilter.java 10489 2008-01-23 17:53:38Z dfeist $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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  
 
 16  
 import org.apache.commons.logging.Log;
 17  
 import org.apache.commons.logging.LogFactory;
 18  
 
 19  
 /**
 20  
  * <code>MessagePropertyFilter</code> can be used to filter against properties on
 21  
  * an event. This can be very useful as the event properties represent all the meta
 22  
  * information about the event from the underlying transport, so for an event
 23  
  * received over HTTP you can check for HTTP headers etc. The pattern should be
 24  
  * expressed as a key/value pair, i.e. "propertyName=value". If you want to compare
 25  
  * more than one property you can use the logic filters for And, Or and Not
 26  
  * expressions. By default the comparison is case sensitive; you can set the
 27  
  * <i>caseSensitive</i> property to override this.
 28  
  */
 29  
 public class MessagePropertyFilter implements Filter
 30  
 {
 31  
     /**
 32  
      * logger used by this class
 33  
      */
 34  32
     protected transient final Log logger = LogFactory.getLog(MessagePropertyFilter.class);
 35  32
     private boolean caseSensitive = true;
 36  32
     private boolean not = false;
 37  
 
 38  
     private String propertyName;
 39  
     private String propertyValue;
 40  
 
 41  
     public MessagePropertyFilter()
 42  
     {
 43  2
         super();
 44  2
     }
 45  
 
 46  
     public MessagePropertyFilter(String expression)
 47  30
     {
 48  30
         setExpression(expression);
 49  30
     }
 50  
 
 51  
     public boolean accept(MuleMessage message)
 52  
     {
 53  46
         if (message == null)
 54  
         {
 55  2
             return false;
 56  
         }
 57  
 
 58  44
         Object value = message.getProperty(propertyName);
 59  
         boolean match;
 60  44
         if (value == null)
 61  
         {
 62  20
             match = compare(null, propertyValue);
 63  
         }
 64  
         else
 65  
         {
 66  24
             match = compare(value.toString(), propertyValue);
 67  
         }
 68  44
         if(!match && logger.isDebugEnabled())
 69  
         {
 70  0
             logger.debug("Property: " + propertyName + " not found on message with Id: " + message.getUniqueId());
 71  
         }
 72  44
         return match;
 73  
     }
 74  
 
 75  
     protected boolean compare(String value1, String value2)
 76  
     {
 77  44
         if (value1 == null && value2 != null && !"null".equals(value2) && not)
 78  
         {
 79  4
             return true;
 80  
         }
 81  
 
 82  40
         if (value1 == null)
 83  
         {
 84  16
             value1 = "null";
 85  
         }
 86  
 
 87  40
         if (value2 == null)
 88  
         {
 89  0
             value2 = "null";
 90  
         }
 91  
 
 92  40
         boolean result = false;
 93  
 
 94  40
         if (caseSensitive)
 95  
         {
 96  38
             result = value1.equals(value2);
 97  
         }
 98  
         else
 99  
         {
 100  2
             result = value1.equalsIgnoreCase(value2);
 101  
         }
 102  
 
 103  40
         return (not ? !result : result);
 104  
     }
 105  
 
 106  
     public String getExpression()
 107  
     {
 108  0
         return propertyName + '=' + propertyValue;
 109  
     }
 110  
 
 111  
     public void setExpression(String expression)
 112  
     {
 113  30
         int i = expression.indexOf('=');
 114  30
         if (i == -1)
 115  
         {
 116  0
             throw new IllegalArgumentException(
 117  
                 "Pattern is malformed - it should be a key value pair, i.e. property=value: " + expression);
 118  
         }
 119  
         else
 120  
         {
 121  30
             if (expression.charAt(i - 1) == '!')
 122  
             {
 123  16
                 not = true;
 124  16
                 propertyName = expression.substring(0, i - 1).trim();
 125  
             }
 126  
             else
 127  
             {
 128  14
                 propertyName = expression.substring(0, i).trim();
 129  
             }
 130  30
             propertyValue = expression.substring(i + 1).trim();
 131  
         }
 132  30
     }
 133  
 
 134  
     public boolean isCaseSensitive()
 135  
     {
 136  0
         return caseSensitive;
 137  
     }
 138  
 
 139  
     public void setCaseSensitive(boolean caseSensitive)
 140  
     {
 141  4
         this.caseSensitive = caseSensitive;
 142  4
     }
 143  
     
 144  
     /**
 145  
      * All Filters that are configured via spring have to implement this method.
 146  
      */
 147  
     public void setPattern(String pattern)
 148  
     {
 149  0
         this.setExpression(pattern);
 150  0
     }
 151  
 }