View Javadoc

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  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          boolean required = false;
37  
38          //This is a bit of a hack to manage required headers
39          if(expression.endsWith("required"))
40          {
41              required = true;
42              expression = expression.substring(0, expression.length() - 8);
43          }
44          
45          if (message instanceof MessageAdapter)
46          {
47  
48              StringTokenizer tokenizer = new StringTokenizer(expression, DELIM);
49              List result = new ArrayList(tokenizer.countTokens());
50              while(tokenizer.hasMoreTokens())
51              {
52                  String s = tokenizer.nextToken();
53                  s = s.trim();
54                  Object val = ((MessageAdapter) message).getAttachment(s);
55                  if (val != null)
56                  {
57                      result.add(val);
58                  }
59                  else if(required)
60                  {
61                      throw new ExpressionRuntimeException(CoreMessages.expressionEvaluatorReturnedNull(NAME, expression));
62                  }
63              }
64              if(result.size()==0)
65              {
66                  return null;
67              }
68              else
69              {
70                  return result;
71              }
72          }
73          return null;
74      }
75  
76      /** {@inheritDoc} */
77      public String getName()
78      {
79          return NAME;
80      }
81  
82      /** {@inheritDoc} */
83      public void setName(String name)
84      {
85          throw new UnsupportedOperationException("setName");
86      }
87  }