Coverage Report - org.mule.expression.MapPayloadExpressionEvaluator
 
Classes in this File Line Coverage Branch Coverage Complexity
MapPayloadExpressionEvaluator
0%
0/27
0%
0/16
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.expression;
 8  
 
 9  
 import org.mule.api.MuleMessage;
 10  
 import org.mule.api.expression.ExpressionEvaluator;
 11  
 import org.mule.api.expression.RequiredValueException;
 12  
 import org.mule.config.i18n.CoreMessages;
 13  
 import org.mule.util.StringUtils;
 14  
 
 15  
 import java.util.HashMap;
 16  
 import java.util.Map;
 17  
 
 18  
 import static org.mule.expression.ExpressionConstants.DELIM;
 19  
 import static org.mule.expression.ExpressionConstants.OPTIONAL_ARGUMENT;
 20  
 
 21  
 /**
 22  
  * If the message payload is a map this extractor will look up the property value in
 23  
  * the map
 24  
  */
 25  0
 public class MapPayloadExpressionEvaluator implements ExpressionEvaluator
 26  
 {
 27  
     public static final String NAME = "map-payload";
 28  
 
 29  
     public Object evaluate(String expression, MuleMessage message)
 30  
     {
 31  0
         Object payload = message.getPayload();
 32  
 
 33  0
         if (!(payload instanceof Map))
 34  
         {
 35  0
             return null;
 36  
         }
 37  
         
 38  0
         if (expression.indexOf(DELIM) > -1)
 39  
         {
 40  
 
 41  0
             String[] strings = StringUtils.splitAndTrim(expression, DELIM);
 42  0
             Map<String, Object> result = new HashMap<String, Object>(strings.length);
 43  
 
 44  0
             for (String s : strings)
 45  
             {
 46  0
                 Object val = getValue(s, (Map) payload);
 47  0
                 if (val != null)
 48  
                 {
 49  0
                     if (s.endsWith(OPTIONAL_ARGUMENT))
 50  
                     {
 51  0
                         s = s.substring(0, s.length() - OPTIONAL_ARGUMENT.length());
 52  
                     }
 53  0
                     result.put(s, val);
 54  
                 }
 55  
             }
 56  0
             return result;
 57  
         }
 58  
         else
 59  
         {
 60  0
             return getValue(expression, (Map)payload);
 61  
         }
 62  
     }
 63  
 
 64  
     protected Object getValue(String key, Map map)
 65  
     {
 66  
         boolean required;
 67  0
         if (key.endsWith(OPTIONAL_ARGUMENT))
 68  
         {
 69  0
             key = key.substring(0, key.length() - OPTIONAL_ARGUMENT.length());
 70  0
             required = false;
 71  
         }
 72  
         else
 73  
         {
 74  0
             required = true;
 75  
         }
 76  0
         Object val = map.get(key);
 77  0
         if (val != null)
 78  
         {
 79  0
             return val;
 80  
         }
 81  0
         else if (required)
 82  
         {
 83  0
             throw new RequiredValueException(CoreMessages.expressionEvaluatorReturnedNull(NAME, key));
 84  
         }
 85  0
         return null;
 86  
     }
 87  
 
 88  
     /**
 89  
      * {@inheritDoc}
 90  
      */
 91  
     public String getName()
 92  
     {
 93  0
         return NAME;
 94  
     }
 95  
 
 96  
     /**
 97  
      * {@inheritDoc}
 98  
      */
 99  
     public void setName(String name)
 100  
     {
 101  0
         throw new UnsupportedOperationException();
 102  
     }
 103  
 
 104  
 }