View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.expression;
8   
9   import org.mule.api.MuleMessage;
10  import org.mule.api.expression.ExpressionEvaluator;
11  import org.mule.api.expression.RequiredValueException;
12  import org.mule.config.i18n.CoreMessages;
13  import org.mule.routing.filters.WildcardFilter;
14  
15  import java.util.Collections;
16  import java.util.HashMap;
17  import java.util.Map;
18  import java.util.StringTokenizer;
19  
20  import javax.activation.DataHandler;
21  
22  import static org.mule.expression.ExpressionConstants.ALL_ARGUMENT;
23  import static org.mule.expression.ExpressionConstants.DELIM;
24  import static org.mule.expression.ExpressionConstants.OPTIONAL_ARGUMENT;
25  
26  /**
27   * Looks up the attachment(s) on the message using the expression given. The
28   * expression can contain a comma-separated list of header names to lookup. A
29   * <code>java.util.Map&lt;String, DataHandler&gt;</code> of key value pairs is
30   * returned.
31   *
32   * @see MessageAttachmentsListExpressionEvaluator
33   * @see MessageAttachmentExpressionEvaluator
34   * @see ExpressionEvaluator
35   * @see DefaultExpressionManager
36   */
37  public class MessageAttachmentsExpressionEvaluator implements ExpressionEvaluator
38  {
39      public static final String NAME = "attachments";
40  
41      public Object evaluate(String expression, MuleMessage message)
42      {
43          boolean required;
44  
45          Map<String, DataHandler> result;
46          //Enable wildcard matching
47          if (expression.contains(ALL_ARGUMENT))
48          {
49              WildcardFilter filter = new WildcardFilter(expression);
50              result = new HashMap<String, DataHandler>(message.getInboundAttachmentNames().size());
51              for (String name : message.getInboundAttachmentNames())
52              {
53                  if (filter.accept(name))
54                  {
55                      result.put(name, message.getInboundAttachment(name));
56                  }
57              }
58          }
59          else
60          {
61              StringTokenizer tokenizer = new StringTokenizer(expression, DELIM);
62              result = new HashMap<String, DataHandler>(tokenizer.countTokens());
63              while (tokenizer.hasMoreTokens())
64              {
65                  String s = tokenizer.nextToken();
66                  s = s.trim();
67                  if (s.endsWith(OPTIONAL_ARGUMENT))
68                  {
69                      s = s.substring(0, s.length() - OPTIONAL_ARGUMENT.length());
70                      required = false;
71                  }
72                  else
73                  {
74                      required = true;
75                  }
76                  DataHandler val = message.getInboundAttachment(s);
77                  if (val != null)
78                  {
79                      result.put(s, val);
80                  }
81                  else if (required)
82                  {
83                      throw new RequiredValueException(CoreMessages.expressionEvaluatorReturnedNull(NAME, expression));
84                  }
85              }
86          }
87          if (result.size() == 0)
88          {
89              return Collections.unmodifiableMap(Collections.<String, DataHandler>emptyMap());
90          }
91          else
92          {
93              return Collections.unmodifiableMap(result);
94          }
95      }
96  
97      /**
98       * {@inheritDoc}
99       */
100     public String getName()
101     {
102         return NAME;
103     }
104 
105     /**
106      * {@inheritDoc}
107      */
108     public void setName(String name)
109     {
110         throw new UnsupportedOperationException();
111     }
112 }