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.transport.PropertyScope;
12  
13  import java.util.Collection;
14  import java.util.HashMap;
15  import java.util.Map;
16  import java.util.Set;
17  
18  import org.apache.commons.logging.Log;
19  import org.apache.commons.logging.LogFactory;
20  
21  /**
22   * Creates a Map facade around a {@link org.mule.api.MuleMessage} instance to allow access to outbound
23   * headers from within components and transformers without the these objects needing access to the Mule Message
24   */
25  public class OutboundHeadersExpressionEvaluator implements ExpressionEvaluator
26  {
27      public static final String NAME = "outboundHeaders";
28  
29      /**
30       * logger used by this class
31       */
32      protected transient final Log logger = LogFactory.getLog(OutboundHeadersExpressionEvaluator.class);
33  
34  
35      public Object evaluate(String expression, MuleMessage message)
36      {
37          if (message == null)
38          {
39              return null;
40          }
41          return new SendHeadersMap(message);
42      }
43  
44      /**
45       * {@inheritDoc}
46       */
47      public String getName()
48      {
49          return NAME;
50      }
51  
52      /**
53       * {@inheritDoc}
54       */
55      public void setName(String name)
56      {
57          throw new UnsupportedOperationException("name");
58      }
59  
60  
61      public static class SendHeadersMap implements Map<String, Object>
62      {
63          private MuleMessage message;
64  
65          public SendHeadersMap(MuleMessage message)
66          {
67              this.message = message;
68          }
69  
70          public int size()
71          {
72              return message.getOutboundPropertyNames().size();
73          }
74  
75          public boolean isEmpty()
76          {
77              return message.getOutboundPropertyNames().size() == 0;
78          }
79  
80          public boolean containsKey(Object key)
81          {
82              return message.getOutboundPropertyNames().contains(key.toString());
83          }
84  
85          public boolean containsValue(Object value)
86          {
87              return values().contains(value);
88          }
89  
90          public Object get(Object key)
91          {
92              return message.getOutboundProperty(key.toString());
93          }
94  
95          public Object put(String key, Object value)
96          {
97              message.setOutboundProperty(key, value);
98              return value;
99          }
100 
101 
102         public Object remove(Object key)
103         {
104             return message.removeProperty(key.toString(), PropertyScope.OUTBOUND);
105         }
106 
107 
108         public void putAll(Map<? extends String, ?> t)
109         {
110             for (Entry<? extends String, ?> entry : t.entrySet())
111             {
112                 put(entry.getKey(), entry.getValue());
113             }
114         }
115 
116         public void clear()
117         {
118             message.clearProperties(PropertyScope.OUTBOUND);
119         }
120 
121         public Set<String> keySet()
122         {
123             return message.getOutboundPropertyNames();
124         }
125 
126         public Collection<Object> values()
127         {
128             return getPropertiesInScope(PropertyScope.OUTBOUND).values();
129         }
130 
131         public Set<Entry<String, Object>> entrySet()
132         {
133             return getPropertiesInScope(PropertyScope.OUTBOUND).entrySet();
134         }
135 
136         //TODO Could optimise this to cache if no writes are made
137         private Map<String, Object> getPropertiesInScope(PropertyScope scope)
138         {
139             Map<String, Object> props = new HashMap<String, Object>();
140             for (String s : message.getPropertyNames(scope))
141             {
142                 props.put(s, message.getProperty(s, scope));
143             }
144             return props;
145         }
146     }
147 }