Coverage Report - org.mule.util.expression.MessageAttachmentsListExpressionEvaluator
 
Classes in this File Line Coverage Branch Coverage Complexity
MessageAttachmentsListExpressionEvaluator
83%
19/23
83%
10/12
4.333
 
 1  
 /*
 2  
  * $Id: MessageAttachmentsListExpressionEvaluator.java 11297 2008-03-09 20:11:02Z rossmason $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.util.expression;
 12  
 
 13  
 import org.mule.api.transport.MessageAdapter;
 14  
 import org.mule.config.i18n.CoreMessages;
 15  
 
 16  
 import java.util.ArrayList;
 17  
 import java.util.List;
 18  
 import java.util.StringTokenizer;
 19  
 
 20  
 /**
 21  
  * Looks up the attachment(s) on the message using the expression given. The expression can contain a comma-separated list
 22  
  * of header names to lookup. A {@link java.util.List} of values is returned.
 23  
  *
 24  
  * @see MessageAttachmentsExpressionEvaluator
 25  
  * @see MessageAttachmentExpressionEvaluator
 26  
  * @see ExpressionEvaluator
 27  
  * @see ExpressionEvaluatorManager
 28  
  */
 29  1148
 public class MessageAttachmentsListExpressionEvaluator implements ExpressionEvaluator
 30  
 {
 31  
     public static final String NAME = "attachments-list";
 32  
     public static final String DELIM = ",";
 33  
 
 34  
     public Object evaluate(String expression, Object message)
 35  
     {
 36  12
         boolean required = false;
 37  
 
 38  
         //This is a bit of a hack to manage required headers
 39  12
         if(expression.endsWith("required"))
 40  
         {
 41  0
             required = true;
 42  0
             expression = expression.substring(0, expression.length() - 8);
 43  
         }
 44  
         
 45  12
         if (message instanceof MessageAdapter)
 46  
         {
 47  
 
 48  8
             StringTokenizer tokenizer = new StringTokenizer(expression, DELIM);
 49  8
             List result = new ArrayList(tokenizer.countTokens());
 50  20
             while(tokenizer.hasMoreTokens())
 51  
             {
 52  12
                 String s = tokenizer.nextToken();
 53  12
                 s = s.trim();
 54  12
                 Object val = ((MessageAdapter) message).getAttachment(s);
 55  12
                 if (val != null)
 56  
                 {
 57  8
                     result.add(val);
 58  
                 }
 59  4
                 else if(required)
 60  
                 {
 61  0
                     throw new ExpressionRuntimeException(CoreMessages.expressionEvaluatorReturnedNull(NAME, expression));
 62  
                 }
 63  12
             }
 64  8
             if(result.size()==0)
 65  
             {
 66  4
                 return null;
 67  
             }
 68  
             else
 69  
             {
 70  4
                 return result;
 71  
             }
 72  
         }
 73  4
         return null;
 74  
     }
 75  
 
 76  
     /** {@inheritDoc} */
 77  
     public String getName()
 78  
     {
 79  1146
         return NAME;
 80  
     }
 81  
 
 82  
     /** {@inheritDoc} */
 83  
     public void setName(String name)
 84  
     {
 85  0
         throw new UnsupportedOperationException("setName");
 86  
     }
 87  
 }