View Javadoc
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  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          Object payload = message.getPayload();
32  
33          if (!(payload instanceof Map))
34          {
35              return null;
36          }
37          
38          if (expression.indexOf(DELIM) > -1)
39          {
40  
41              String[] strings = StringUtils.splitAndTrim(expression, DELIM);
42              Map<String, Object> result = new HashMap<String, Object>(strings.length);
43  
44              for (String s : strings)
45              {
46                  Object val = getValue(s, (Map) payload);
47                  if (val != null)
48                  {
49                      if (s.endsWith(OPTIONAL_ARGUMENT))
50                      {
51                          s = s.substring(0, s.length() - OPTIONAL_ARGUMENT.length());
52                      }
53                      result.put(s, val);
54                  }
55              }
56              return result;
57          }
58          else
59          {
60              return getValue(expression, (Map)payload);
61          }
62      }
63  
64      protected Object getValue(String key, Map map)
65      {
66          boolean required;
67          if (key.endsWith(OPTIONAL_ARGUMENT))
68          {
69              key = key.substring(0, key.length() - OPTIONAL_ARGUMENT.length());
70              required = false;
71          }
72          else
73          {
74              required = true;
75          }
76          Object val = map.get(key);
77          if (val != null)
78          {
79              return val;
80          }
81          else if (required)
82          {
83              throw new RequiredValueException(CoreMessages.expressionEvaluatorReturnedNull(NAME, key));
84          }
85          return null;
86      }
87  
88      /**
89       * {@inheritDoc}
90       */
91      public String getName()
92      {
93          return NAME;
94      }
95  
96      /**
97       * {@inheritDoc}
98       */
99      public void setName(String name)
100     {
101         throw new UnsupportedOperationException();
102     }
103 
104 }