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.MuleContext;
10  import org.mule.api.MuleMessage;
11  import org.mule.api.MuleRuntimeException;
12  import org.mule.api.context.MuleContextAware;
13  import org.mule.api.expression.ExpressionEvaluator;
14  import org.mule.api.transformer.DataType;
15  import org.mule.api.transformer.TransformerException;
16  import org.mule.config.i18n.CoreMessages;
17  import org.mule.transformer.types.DataTypeFactory;
18  import org.mule.util.ClassUtils;
19  import org.mule.util.StringUtils;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  
24  /**
25   * Returns the message payload.  If the expression is set to a class name then Mule will attempt to transform the payload by
26   * discovering the correct transformer(s) in the registry. This is only suited for simple transformations between common types.
27   * <p/>
28   * <code>
29   * #[payload:byte[]]
30   * </code>
31   * <p/>
32   * or
33   * <p/>
34   * <code>
35   * #[payload:org.mule.api.OutputHandler]
36   * </code>
37   * <p/>
38   * If the object passed in is not a MuleMessage, the same object will be returned.
39   *
40   * @see org.mule.api.expression.ExpressionEvaluator
41   * @see DefaultExpressionManager
42   */
43  public class MessagePayloadExpressionEvaluator implements ExpressionEvaluator, MuleContextAware
44  {
45      public static final String NAME = "payload";
46      public static final String BYTE_ARRAY = "byte[]";
47  
48      /**
49       * logger used by this class
50       */
51      protected transient final Log logger = LogFactory.getLog(MessagePayloadExpressionEvaluator.class);
52  
53      protected MuleContext muleContext;
54  
55      public void setMuleContext(MuleContext context)
56      {
57          this.muleContext = context;
58      }
59  
60      public Object evaluate(String expression, MuleMessage message)
61      {
62          if(message==null) return null;
63          
64          if (StringUtils.isEmpty(expression))
65          {
66              return message.getPayload();
67          }
68          else
69          {
70              try
71              {
72                  if (expression.equals(BYTE_ARRAY))
73                  {
74                      return message.getPayload(DataType.BYTE_ARRAY_DATA_TYPE);
75                  }
76                  else
77                  {
78                      return message.getPayload(DataTypeFactory.create(ClassUtils.loadClass(expression, getClass())));
79                  }
80              }
81              catch (TransformerException e)
82              {
83                  throw new MuleRuntimeException(CoreMessages.failedToProcessExtractorFunction(expression), e);
84              }
85              catch (ClassNotFoundException e)
86              {
87                  throw new MuleRuntimeException(CoreMessages.failedToProcessExtractorFunction(expression), e);
88              }
89          }
90      }
91  
92      /**
93       * {@inheritDoc}
94       */
95      public String getName()
96      {
97          return NAME;
98      }
99  
100     /**
101      * {@inheritDoc}
102      */
103     public void setName(String name)
104     {
105         throw new UnsupportedOperationException();
106     }
107 }