View Javadoc

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