Coverage Report - org.mule.routing.filters.MessagePropertyFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
MessagePropertyFilter
89%
33/37
88%
23/26
2.5
 
 1  
 /*
 2  
  * $Id: MessagePropertyFilter.java 7963 2007-08-21 08:53:15Z dirk.olmes $
 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.umo.UMOFilter;
 14  
 import org.mule.umo.UMOMessage;
 15  
 
 16  
 /**
 17  
  * <code>MessagePropertyFilter</code> can be used to filter against properties on
 18  
  * an event. This can be very useful as the event properties represent all the meta
 19  
  * information about the event from the underlying transport, so for an event
 20  
  * received over HTTP you can check for HTTP headers etc. The pattern should be
 21  
  * expressed as a key/value pair, i.e. "propertyName=value". If you want to compare
 22  
  * more than one property you can use the logic filters for And, Or and Not
 23  
  * expressions. By default the comparison is case sensitive; you can set the
 24  
  * <i>caseSensitive</i> property to override this.
 25  
  */
 26  
 public class MessagePropertyFilter implements UMOFilter
 27  
 {
 28  16
     private boolean caseSensitive = true;
 29  16
     private boolean not = false;
 30  
 
 31  
     private String propertyName;
 32  
     private String propertyValue;
 33  
 
 34  
     public MessagePropertyFilter()
 35  
     {
 36  2
         super();
 37  2
     }
 38  
 
 39  
     public MessagePropertyFilter(String expression)
 40  14
     {
 41  14
         setExpression(expression);
 42  14
     }
 43  
 
 44  
     public boolean accept(UMOMessage message)
 45  
     {
 46  30
         if (message == null)
 47  
         {
 48  2
             return false;
 49  
         }
 50  
 
 51  28
         Object value = message.getProperty(propertyName);
 52  
 
 53  28
         if (value == null)
 54  
         {
 55  12
             return compare(null, propertyValue);
 56  
         }
 57  
         else
 58  
         {
 59  16
             return compare(value.toString(), propertyValue);
 60  
         }
 61  
     }
 62  
 
 63  
     protected boolean compare(String value1, String value2)
 64  
     {
 65  28
         if (value1 == null && value2 != null && !"null".equals(value2) && not)
 66  
         {
 67  2
             return true;
 68  
         }
 69  
 
 70  26
         if (value1 == null)
 71  
         {
 72  10
             value1 = "null";
 73  
         }
 74  
 
 75  26
         if (value2 == null)
 76  
         {
 77  0
             value2 = "null";
 78  
         }
 79  
 
 80  26
         boolean result = false;
 81  
 
 82  26
         if (caseSensitive)
 83  
         {
 84  24
             result = value1.equals(value2);
 85  
         }
 86  
         else
 87  
         {
 88  2
             result = value1.equalsIgnoreCase(value2);
 89  
         }
 90  
 
 91  26
         return (not ? !result : result);
 92  
     }
 93  
 
 94  
     public String getExpression()
 95  
     {
 96  0
         return propertyName + '=' + propertyValue;
 97  
     }
 98  
 
 99  
     public void setExpression(String expression)
 100  
     {
 101  14
         int i = expression.indexOf('=');
 102  14
         if (i == -1)
 103  
         {
 104  0
             throw new IllegalArgumentException(
 105  
                 "Pattern is malformed - it should be a key value pair, i.e. property=value: " + expression);
 106  
         }
 107  
         else
 108  
         {
 109  14
             if (expression.charAt(i - 1) == '!')
 110  
             {
 111  4
                 not = true;
 112  4
                 propertyName = expression.substring(0, i - 1).trim();
 113  
             }
 114  
             else
 115  
             {
 116  10
                 propertyName = expression.substring(0, i).trim();
 117  
             }
 118  14
             propertyValue = expression.substring(i + 1).trim();
 119  
         }
 120  14
     }
 121  
 
 122  
     public boolean isCaseSensitive()
 123  
     {
 124  0
         return caseSensitive;
 125  
     }
 126  
 
 127  
     public void setCaseSensitive(boolean caseSensitive)
 128  
     {
 129  4
         this.caseSensitive = caseSensitive;
 130  4
     }
 131  
 
 132  
 }