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  
11  import java.util.Collection;
12  import java.util.HashMap;
13  import java.util.Map;
14  import java.util.Set;
15  
16  import javax.activation.DataHandler;
17  
18  /**
19   * Creates a wrapper around Mule Message with a MAp facade used for allowing developers to add attachments to
20   * an outgoing message in a transformer of component without needing to access the Mule API directly
21   */
22  public class InboundAttachmentsMap implements Map<String, DataHandler>
23  {
24      private MuleMessage message;
25  
26      public InboundAttachmentsMap(MuleMessage message)
27      {
28          this.message = message;
29      }
30  
31      public int size()
32      {
33          return message.getInboundAttachmentNames().size();
34      }
35  
36      public boolean isEmpty()
37      {
38          return message.getInboundAttachmentNames().size() == 0;
39      }
40  
41      public boolean containsKey(Object key)
42      {
43          return message.getInboundAttachmentNames().contains(key.toString());
44      }
45  
46      public boolean containsValue(Object value)
47      {
48          return values().contains(value);
49      }
50  
51      public DataHandler get(Object key)
52      {
53          return message.getInboundAttachment(key.toString());
54      }
55  
56      public DataHandler put(String key, DataHandler value)
57      {
58          throw new UnsupportedOperationException("put(): Inbound attachments are read-only");
59      }
60  
61  
62      public DataHandler remove(Object key)
63      {
64          throw new UnsupportedOperationException("remove(): Inbound attachments are read-only");
65      }
66  
67  
68      public void putAll(Map<? extends String, ? extends DataHandler> map)
69      {
70          for (Entry<? extends String, ? extends DataHandler> entry : map.entrySet())
71          {
72              put(entry.getKey(), entry.getValue());
73          }
74      }
75  
76      public void clear()
77      {
78          throw new UnsupportedOperationException("clear");
79      }
80  
81      public Set<String> keySet()
82      {
83          return message.getInboundAttachmentNames();
84      }
85  
86      public Collection<DataHandler> values()
87      {
88          return getAttachments().values();
89      }
90  
91      public Set<Entry<String, DataHandler>> entrySet()
92      {
93          return getAttachments().entrySet();
94      }
95  
96      //TODO Could optimise this to cache if no writes are made
97      private Map<String, DataHandler> getAttachments()
98      {
99          Map<String, DataHandler> props = new HashMap<String, DataHandler>();
100         for (String s : message.getInboundAttachmentNames())
101         {
102             props.put(s, message.getInboundAttachment(s));
103         }
104         return props;
105     }
106 }