Coverage Report - org.mule.expression.MessageAttachmentsExpressionEvaluator
 
Classes in this File Line Coverage Branch Coverage Complexity
MessageAttachmentsExpressionEvaluator
0%
0/28
0%
0/16
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  
 import org.mule.api.expression.ExpressionEvaluator;
 11  
 import org.mule.api.expression.RequiredValueException;
 12  
 import org.mule.config.i18n.CoreMessages;
 13  
 import org.mule.routing.filters.WildcardFilter;
 14  
 
 15  
 import java.util.Collections;
 16  
 import java.util.HashMap;
 17  
 import java.util.Map;
 18  
 import java.util.StringTokenizer;
 19  
 
 20  
 import javax.activation.DataHandler;
 21  
 
 22  
 import static org.mule.expression.ExpressionConstants.ALL_ARGUMENT;
 23  
 import static org.mule.expression.ExpressionConstants.DELIM;
 24  
 import static org.mule.expression.ExpressionConstants.OPTIONAL_ARGUMENT;
 25  
 
 26  
 /**
 27  
  * Looks up the attachment(s) on the message using the expression given. The
 28  
  * expression can contain a comma-separated list of header names to lookup. A
 29  
  * <code>java.util.Map&lt;String, DataHandler&gt;</code> of key value pairs is
 30  
  * returned.
 31  
  *
 32  
  * @see MessageAttachmentsListExpressionEvaluator
 33  
  * @see MessageAttachmentExpressionEvaluator
 34  
  * @see ExpressionEvaluator
 35  
  * @see DefaultExpressionManager
 36  
  */
 37  0
 public class MessageAttachmentsExpressionEvaluator implements ExpressionEvaluator
 38  
 {
 39  
     public static final String NAME = "attachments";
 40  
 
 41  
     public Object evaluate(String expression, MuleMessage message)
 42  
     {
 43  
         boolean required;
 44  
 
 45  
         Map<String, DataHandler> result;
 46  
         //Enable wildcard matching
 47  0
         if (expression.contains(ALL_ARGUMENT))
 48  
         {
 49  0
             WildcardFilter filter = new WildcardFilter(expression);
 50  0
             result = new HashMap<String, DataHandler>(message.getInboundAttachmentNames().size());
 51  0
             for (String name : message.getInboundAttachmentNames())
 52  
             {
 53  0
                 if (filter.accept(name))
 54  
                 {
 55  0
                     result.put(name, message.getInboundAttachment(name));
 56  
                 }
 57  
             }
 58  0
         }
 59  
         else
 60  
         {
 61  0
             StringTokenizer tokenizer = new StringTokenizer(expression, DELIM);
 62  0
             result = new HashMap<String, DataHandler>(tokenizer.countTokens());
 63  0
             while (tokenizer.hasMoreTokens())
 64  
             {
 65  0
                 String s = tokenizer.nextToken();
 66  0
                 s = s.trim();
 67  0
                 if (s.endsWith(OPTIONAL_ARGUMENT))
 68  
                 {
 69  0
                     s = s.substring(0, s.length() - OPTIONAL_ARGUMENT.length());
 70  0
                     required = false;
 71  
                 }
 72  
                 else
 73  
                 {
 74  0
                     required = true;
 75  
                 }
 76  0
                 DataHandler val = message.getInboundAttachment(s);
 77  0
                 if (val != null)
 78  
                 {
 79  0
                     result.put(s, val);
 80  
                 }
 81  0
                 else if (required)
 82  
                 {
 83  0
                     throw new RequiredValueException(CoreMessages.expressionEvaluatorReturnedNull(NAME, expression));
 84  
                 }
 85  0
             }
 86  
         }
 87  0
         if (result.size() == 0)
 88  
         {
 89  0
             return Collections.unmodifiableMap(Collections.<String, DataHandler>emptyMap());
 90  
         }
 91  
         else
 92  
         {
 93  0
             return Collections.unmodifiableMap(result);
 94  
         }
 95  
     }
 96  
 
 97  
     /**
 98  
      * {@inheritDoc}
 99  
      */
 100  
     public String getName()
 101  
     {
 102  0
         return NAME;
 103  
     }
 104  
 
 105  
     /**
 106  
      * {@inheritDoc}
 107  
      */
 108  
     public void setName(String name)
 109  
     {
 110  0
         throw new UnsupportedOperationException();
 111  
     }
 112  
 }