View Javadoc

1   /*
2    * $Id: InboundAttachmentsMap.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  
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 InboundAttachmentsMap implements Map<String, DataHandler>
26  {
27      private MuleMessage message;
28  
29      public InboundAttachmentsMap(MuleMessage message)
30      {
31          this.message = message;
32      }
33  
34      public int size()
35      {
36          return message.getInboundAttachmentNames().size();
37      }
38  
39      public boolean isEmpty()
40      {
41          return message.getInboundAttachmentNames().size() == 0;
42      }
43  
44      public boolean containsKey(Object key)
45      {
46          return message.getInboundAttachmentNames().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.getInboundAttachment(key.toString());
57      }
58  
59      public DataHandler put(String key, DataHandler value)
60      {
61          throw new UnsupportedOperationException("put(): Inbound attachments are read-only");
62      }
63  
64  
65      public DataHandler remove(Object key)
66      {
67          throw new UnsupportedOperationException("remove(): Inbound attachments are read-only");
68      }
69  
70  
71      public void putAll(Map<? extends String, ? extends DataHandler> map)
72      {
73          for (Entry<? extends String, ? extends DataHandler> entry : map.entrySet())
74          {
75              put(entry.getKey(), entry.getValue());
76          }
77      }
78  
79      public void clear()
80      {
81          throw new UnsupportedOperationException("clear");
82      }
83  
84      public Set<String> keySet()
85      {
86          return message.getInboundAttachmentNames();
87      }
88  
89      public Collection<DataHandler> values()
90      {
91          return getAttachments().values();
92      }
93  
94      public Set<Entry<String, DataHandler>> entrySet()
95      {
96          return getAttachments().entrySet();
97      }
98  
99      //TODO Could optimise this to cache if no writes are made
100     private Map<String, DataHandler> getAttachments()
101     {
102         Map<String, DataHandler> props = new HashMap<String, DataHandler>();
103         for (String s : message.getInboundAttachmentNames())
104         {
105             props.put(s, message.getInboundAttachment(s));
106         }
107         return props;
108     }
109 }