View Javadoc

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