View Javadoc

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