View Javadoc

1   /*
2    * $Id: OutboundAttachmentsMap.java 18943 2010-08-11 15:56:11Z rossmason $
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  
14  import java.util.Collection;
15  import java.util.HashMap;
16  import java.util.Map;
17  import java.util.Set;
18  
19  import javax.activation.DataHandler;
20  
21  /**
22   * Creates a wrapper around Mule Message with a MAp facade used for allowing developers to add attachments to
23   * an outgoing message in a transformer of component without needing to access the Mule API directly
24   */
25  public class OutboundAttachmentsMap implements Map<String, DataHandler>
26  {
27      private MuleMessage message;
28  
29      public OutboundAttachmentsMap(MuleMessage message)
30      {
31          this.message = message;
32      }
33  
34      public int size()
35      {
36          return message.getOutboundAttachmentNames().size();
37      }
38  
39      public boolean isEmpty()
40      {
41          return message.getOutboundAttachmentNames().size() == 0;
42      }
43  
44      public boolean containsKey(Object key)
45      {
46          return message.getOutboundAttachmentNames().contains(key.toString());
47      }
48  
49      public boolean containsValue(Object value)
50      {
51          return values().contains(value);
52      }
53  
54      public DataHandler get(Object key)
55      {
56          return message.getOutboundAttachment(key.toString());
57      }
58  
59      public DataHandler put(String key, DataHandler value)
60      {
61          try
62          {
63              message.addOutboundAttachment(key, value);
64              return value;
65          }
66          catch (Exception e)
67          {
68              //Not sure we can do anything else here
69              throw new RuntimeException(e);
70          }
71      }
72  
73      public DataHandler put(String key, Object value, String contentType)
74      {
75          try
76          {
77              message.addOutboundAttachment(key, value, contentType);
78              return get(key);
79          }
80          catch (Exception e)
81          {
82              //Not sure we can do anything else here
83              throw new RuntimeException(e);
84          }
85      }
86  
87  
88      public DataHandler remove(Object key)
89      {
90          DataHandler attachment = message.getOutboundAttachment(key.toString());
91          try
92          {
93              message.removeOutboundAttachment(key.toString());
94          }
95          catch (Exception e)
96          {
97              //ignore (some message types may throw an exception if the attachment does not exist)
98          }
99          return attachment;
100     }
101 
102 
103     public void putAll(Map<? extends String, ? extends DataHandler> map)
104     {
105         for (Entry<? extends String, ? extends DataHandler> entry : map.entrySet())
106         {
107             put(entry.getKey(), entry.getValue());
108         }
109     }
110 
111     public void clear()
112     {
113         throw new UnsupportedOperationException("clear");
114     }
115 
116     public Set<String> keySet()
117     {
118         return message.getOutboundAttachmentNames();
119     }
120 
121     public Collection<DataHandler> values()
122     {
123         return getAttachments().values();
124     }
125 
126     public Set<Entry<String, DataHandler>> entrySet()
127     {
128         return getAttachments().entrySet();
129     }
130 
131     //TODO Could optimise this to cache if no writes are made
132     private Map<String, DataHandler> getAttachments()
133     {
134         Map<String, DataHandler> props = new HashMap<String, DataHandler>();
135         for (String s : message.getOutboundAttachmentNames())
136         {
137             props.put(s, message.getOutboundAttachment(s));
138         }
139         return props;
140     }
141 }