View Javadoc

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