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