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 OutboundAttachmentsMap implements Map<String, DataHandler>
23  {
24      private MuleMessage message;
25  
26      public OutboundAttachmentsMap(MuleMessage message)
27      {
28          this.message = message;
29      }
30  
31      public int size()
32      {
33          return message.getOutboundAttachmentNames().size();
34      }
35  
36      public boolean isEmpty()
37      {
38          return message.getOutboundAttachmentNames().size() == 0;
39      }
40  
41      public boolean containsKey(Object key)
42      {
43          return message.getOutboundAttachmentNames().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.getOutboundAttachment(key.toString());
54      }
55  
56      public DataHandler put(String key, DataHandler value)
57      {
58          try
59          {
60              message.addOutboundAttachment(key, value);
61              return value;
62          }
63          catch (Exception e)
64          {
65              //Not sure we can do anything else here
66              throw new RuntimeException(e);
67          }
68      }
69  
70      public DataHandler put(String key, Object value, String contentType)
71      {
72          try
73          {
74              message.addOutboundAttachment(key, value, contentType);
75              return get(key);
76          }
77          catch (Exception e)
78          {
79              //Not sure we can do anything else here
80              throw new RuntimeException(e);
81          }
82      }
83  
84  
85      public DataHandler remove(Object key)
86      {
87          DataHandler attachment = message.getOutboundAttachment(key.toString());
88          try
89          {
90              message.removeOutboundAttachment(key.toString());
91          }
92          catch (Exception e)
93          {
94              //ignore (some message types may throw an exception if the attachment does not exist)
95          }
96          return attachment;
97      }
98  
99  
100     public void putAll(Map<? extends String, ? extends DataHandler> map)
101     {
102         for (Entry<? extends String, ? extends DataHandler> entry : map.entrySet())
103         {
104             put(entry.getKey(), entry.getValue());
105         }
106     }
107 
108     public void clear()
109     {
110         throw new UnsupportedOperationException("clear");
111     }
112 
113     public Set<String> keySet()
114     {
115         return message.getOutboundAttachmentNames();
116     }
117 
118     public Collection<DataHandler> values()
119     {
120         return getAttachments().values();
121     }
122 
123     public Set<Entry<String, DataHandler>> entrySet()
124     {
125         return getAttachments().entrySet();
126     }
127 
128     //TODO Could optimise this to cache if no writes are made
129     private Map<String, DataHandler> getAttachments()
130     {
131         Map<String, DataHandler> props = new HashMap<String, DataHandler>();
132         for (String s : message.getOutboundAttachmentNames())
133         {
134             props.put(s, message.getOutboundAttachment(s));
135         }
136         return props;
137     }
138 }