View Javadoc

1   /*
2    * $Id: MessageAttachmentsListExpressionEvaluator.java 19191 2010-08-25 21:05:23Z tcarlson $
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.ArrayList;
20  import java.util.Collections;
21  import java.util.List;
22  import java.util.StringTokenizer;
23  
24  import javax.activation.DataHandler;
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, ExpressionConstants
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 }