View Javadoc

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