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  
14  import javax.activation.DataHandler;
15  
16  import static org.mule.expression.ExpressionConstants.OPTIONAL_ARGUMENT;
17  
18  /**
19   * Looks up an attachment with the given name.
20   *
21   * @see MessageAttachmentsListExpressionEvaluator
22   * @see MessageAttachmentsExpressionEvaluator
23   * @see org.mule.api.expression.ExpressionEvaluator
24   * @see DefaultExpressionManager
25   */
26  public class MessageAttachmentExpressionEvaluator implements ExpressionEvaluator
27  {
28      public static final String NAME = "attachment";
29  
30      public Object evaluate(String expression, MuleMessage message)
31      {
32          if (expression == null)
33          {
34              return null;
35          }
36  
37          boolean required;
38          if (expression.endsWith(OPTIONAL_ARGUMENT))
39          {
40              expression = expression.substring(0, expression.length() - OPTIONAL_ARGUMENT.length());
41              required = false;
42          }
43          else
44          {
45              required = true;
46          }
47          DataHandler dh = message.getInboundAttachment(expression);
48  
49          if (dh == null && required)
50          {
51              throw new RequiredValueException(CoreMessages.expressionEvaluatorReturnedNull(NAME, expression));
52          }
53          return dh;
54      }
55  
56      /**
57       * {@inheritDoc}
58       */
59      public String getName()
60      {
61          return NAME;
62      }
63  
64      /**
65       * {@inheritDoc}
66       */
67      public void setName(String name)
68      {
69          throw new UnsupportedOperationException();
70      }
71  }