Coverage Report - org.mule.module.ognl.filters.OGNLFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
OGNLFilter
0%
0/29
0%
0/14
0
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.module.ognl.filters;
 8  
 
 9  
 import static org.mule.util.ClassUtils.equal;
 10  
 import static org.mule.util.ClassUtils.hash;
 11  
 import ognl.Ognl;
 12  
 import ognl.OgnlException;
 13  
 
 14  
 import org.mule.api.MuleMessage;
 15  
 import org.mule.api.config.ConfigurationException;
 16  
 import org.mule.api.routing.filter.Filter;
 17  
 
 18  
 import org.apache.commons.logging.Log;
 19  
 import org.apache.commons.logging.LogFactory;
 20  
 
 21  0
 public class OGNLFilter implements Filter
 22  
 {
 23  0
     protected final Log logger = LogFactory.getLog(this.getClass());
 24  
 
 25  
     private volatile String expression;
 26  
     private volatile Object compiledExpression;
 27  
 
 28  
     public String getExpression()
 29  
     {
 30  0
         return expression;
 31  
     }
 32  
 
 33  
     /**
 34  
      * Sets the expression for this filter. The argument must be a valid expression
 35  
      * as described in the OGNL documentation.
 36  
      *
 37  
      * @param expression the expression to use for message evaluation
 38  
      * @throws ConfigurationException if the expression cannot be parsed
 39  
      * @see Ognl#parseExpression(String)
 40  
      */
 41  
     public void setExpression(String expression) throws ConfigurationException
 42  
     {
 43  
         try
 44  
         {
 45  0
             this.compiledExpression = Ognl.parseExpression(expression);
 46  0
             this.expression = expression;
 47  
         }
 48  0
         catch (OgnlException ex)
 49  
         {
 50  0
             throw new ConfigurationException(ex);
 51  0
         }
 52  0
     }
 53  
 
 54  
     public boolean accept(MuleMessage message)
 55  
     {
 56  
         // no message: nothing to filter
 57  0
         if (message == null)
 58  
         {
 59  0
             return false;
 60  
         }
 61  
 
 62  0
         Object candidate = message.getPayload();
 63  
         // no payload: still nothing to filter
 64  0
         if (candidate == null)
 65  
         {
 66  0
             return false;
 67  
         }
 68  
 
 69  
         // no expression configured: we reject by default
 70  0
         if (compiledExpression == null)
 71  
         {
 72  0
             logger.warn("No expression configured - rejecting message.");
 73  0
             return false;
 74  
         }
 75  
 
 76  
         try
 77  
         {
 78  0
             Object result = Ognl.getValue(compiledExpression, candidate);
 79  
             // we only need to take boolean expressoin results into account
 80  0
             if (result instanceof Boolean)
 81  
             {
 82  0
                 return ((Boolean) result).booleanValue();
 83  
             }
 84  
         }
 85  0
         catch (OgnlException ex)
 86  
         {
 87  0
             logger.error(ex);
 88  0
         }
 89  
 
 90  
         // default action: reject
 91  0
         return false;
 92  
     }
 93  
 
 94  
     public boolean equals(Object obj)
 95  
     {
 96  0
         if (this == obj) return true;
 97  0
         if (obj == null || getClass() != obj.getClass()) return false;
 98  
 
 99  0
         final OGNLFilter other = (OGNLFilter) obj;
 100  0
         return equal(expression, other.expression);
 101  
     }
 102  
 
 103  
     public int hashCode()
 104  
     {
 105  0
         return hash(new Object[]{this.getClass(), expression});
 106  
     }
 107  
 }