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