View Javadoc

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  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          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              StringTokenizer tokenizer = new StringTokenizer(expression, DELIM);
48              Map result = new HashMap(tokenizer.countTokens());
49              while(tokenizer.hasMoreTokens())
50              {
51                  String s = tokenizer.nextToken();
52                  s = s.trim();
53                  Object val = ((MessageAdapter) message).getAttachment(s);
54                  if (val != null)
55                  {
56                      result.put(s, val);
57                  }
58                  else if(required)
59                  {
60                      throw new ExpressionRuntimeException(CoreMessages.expressionEvaluatorReturnedNull(NAME, expression));
61                  }
62              }
63              if (result.size() == 0)
64              {
65                  return null;
66              }
67              else
68              {
69                  return result;
70              }
71          }
72          return null;
73      }
74  
75      /**
76       * {@inheritDoc}
77       */
78      public String getName()
79      {
80          return NAME;
81      }
82  
83      /**
84       * {@inheritDoc}
85       */
86      public void setName(String name)
87      {
88          throw new UnsupportedOperationException("setName");
89      }
90  }