Coverage Report - org.mule.util.expression.MessageAttachmentsExpressionEvaluator
 
Classes in this File Line Coverage Branch Coverage Complexity
MessageAttachmentsExpressionEvaluator
83%
19/23
83%
10/12
4.333
 
 1  
 /*
 2  
  * $Id: MessageAttachmentsExpressionEvaluator.java 11433 2008-03-20 03:43:57Z dirk.olmes $
 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.HashMap;
 17  
 import java.util.Map;
 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.Map} of key value pairs is returned.
 23  
  *
 24  
  * @see MessageAttachmentsListExpressionEvaluator
 25  
  * @see MessageAttachmentExpressionEvaluator
 26  
  * @see ExpressionEvaluator
 27  
  * @see ExpressionEvaluatorManager
 28  
  */
 29  1148
 public class MessageAttachmentsExpressionEvaluator implements ExpressionEvaluator
 30  
 {
 31  
     public static final String NAME = "attachments";
 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  8
             StringTokenizer tokenizer = new StringTokenizer(expression, DELIM);
 48  8
             Map result = new HashMap(tokenizer.countTokens());
 49  20
             while(tokenizer.hasMoreTokens())
 50  
             {
 51  12
                 String s = tokenizer.nextToken();
 52  12
                 s = s.trim();
 53  12
                 Object val = ((MessageAdapter) message).getAttachment(s);
 54  12
                 if (val != null)
 55  
                 {
 56  8
                     result.put(s, val);
 57  
                 }
 58  4
                 else if(required)
 59  
                 {
 60  0
                     throw new ExpressionRuntimeException(CoreMessages.expressionEvaluatorReturnedNull(NAME, expression));
 61  
                 }
 62  12
             }
 63  8
             if (result.size() == 0)
 64  
             {
 65  4
                 return null;
 66  
             }
 67  
             else
 68  
             {
 69  4
                 return result;
 70  
             }
 71  
         }
 72  4
         return null;
 73  
     }
 74  
 
 75  
     /**
 76  
      * {@inheritDoc}
 77  
      */
 78  
     public String getName()
 79  
     {
 80  1146
         return NAME;
 81  
     }
 82  
 
 83  
     /**
 84  
      * {@inheritDoc}
 85  
      */
 86  
     public void setName(String name)
 87  
     {
 88  0
         throw new UnsupportedOperationException("setName");
 89  
     }
 90  
 }