Coverage Report - org.mule.util.expression.MessageHeadersExpressionEvaluator
 
Classes in this File Line Coverage Branch Coverage Complexity
MessageHeadersExpressionEvaluator
83%
19/23
83%
10/12
4.333
 
 1  
 /*
 2  
  * $Id: MessageHeadersExpressionEvaluator.java 11433 2008-03-20 03:43:57Z 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.util.expression;
 12  
 
 13  
 import org.mule.api.transport.MessageAdapter;
 14  
 import org.mule.config.i18n.CoreMessages;
 15  
 
 16  
 import java.util.HashMap;
 17  
 import java.util.Map;
 18  
 import java.util.StringTokenizer;
 19  
 
 20  
 /**
 21  
  * Looks up the property on the message using the expression given. The expression can contain a comma-separated list
 22  
  * of header names to lookup. A {@link java.util.Map} of key value pairs is returned.
 23  
  *
 24  
  * @see org.mule.util.expression.MessageHeadersListExpressionEvaluator
 25  
  * @see org.mule.util.expression.ExpressionEvaluator
 26  
  * @see org.mule.util.expression.ExpressionEvaluatorManager
 27  
  */
 28  1148
 public class MessageHeadersExpressionEvaluator implements ExpressionEvaluator
 29  
 {
 30  
     public static final String NAME = "headers";
 31  
     public static final String DELIM = ",";
 32  
 
 33  
     public Object evaluate(String expression, Object message)
 34  
     {
 35  12
         boolean required = false;
 36  
 
 37  
         //This is a bit of a hack to manage required headers
 38  12
         if(expression.endsWith("required"))
 39  
         {
 40  0
             required = true;
 41  0
             expression = expression.substring(0, expression.length() - 8);
 42  
         }
 43  
 
 44  12
         if (message instanceof MessageAdapter)
 45  
         {
 46  8
             StringTokenizer tokenizer = new StringTokenizer(expression, DELIM);
 47  8
             Map result = new HashMap(tokenizer.countTokens());
 48  20
             while(tokenizer.hasMoreTokens())
 49  
             {
 50  12
                 String s = tokenizer.nextToken();
 51  12
                 s = s.trim();
 52  12
                 Object val = ((MessageAdapter) message).getProperty(s);
 53  12
                 if(val!=null)
 54  
                 {
 55  8
                     result.put(s, val);
 56  
                 }
 57  4
                 else if(required)
 58  
                 {
 59  0
                     throw new ExpressionRuntimeException(CoreMessages.expressionEvaluatorReturnedNull(NAME, expression));
 60  
                 }
 61  12
             }
 62  8
             if(result.size()==0)
 63  
             {
 64  4
                 return null;
 65  
             }
 66  
             else
 67  
             {
 68  4
                 return result;
 69  
             }
 70  
         }
 71  4
         return null;
 72  
     }
 73  
 
 74  
     /** {@inheritDoc} */
 75  
     public String getName()
 76  
     {
 77  1146
         return NAME;
 78  
     }
 79  
 
 80  
     /** {@inheritDoc} */
 81  
     public void setName(String name)
 82  
     {
 83  0
         throw new UnsupportedOperationException("setName");
 84  
     }
 85  
 }