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