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