View Javadoc

1   /*
2    * $Id: MessageExpressionEvaluator.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.ExceptionPayload;
14  import org.mule.api.MuleMessage;
15  import org.mule.api.expression.ExpressionEvaluator;
16  import org.mule.util.StringUtils;
17  
18  import org.apache.commons.logging.Log;
19  import org.apache.commons.logging.LogFactory;
20  
21  /**
22   * Returns properties on the MuleMessage itself. The supported expressions map directly to methods on the message.
23   * For example:
24   * <ul>
25   * <li>id - returns the value of {@link MuleMessage#getUniqueId()}</li>
26   * <li>correlationId - returns the value of {@link MuleMessage#getCorrelationId()}</li>
27   * <li>correlationGroupSize - returns the value of {@link MuleMessage#getCorrelationGroupSize()}</li>
28   * <li>correlationSequence - returns the value of {@link MuleMessage#getCorrelationSequence()}</li>
29   * <li>replyTo - returns the value of {@link MuleMessage#getReplyTo()}</li>
30   * <li>payload - returns the value of {@link MuleMessage#getPayload()}</li>
31   * <li>encoding - returns the value of {@link MuleMessage#getEncoding()}</li>
32   * <li>exception - returns the value of <code>MuleMessage.getExceptionPayload().getException()</code> or null if there is no exception payload</li>
33   * </ul>
34   * If no expression is set the MuleMessage itself will be returned.
35   * <p/>
36   * If the object passed in is not a MuleMessage, the same object will be returned.
37   *
38   * @see ExpressionEvaluator
39   * @see DefaultExpressionManager
40   */
41  public class MessageExpressionEvaluator implements ExpressionEvaluator
42  {
43      public static final String NAME = "message";
44  
45      /**
46       * logger used by this class
47       */
48      protected transient final Log logger = LogFactory.getLog(MessagePayloadExpressionEvaluator.class);
49  
50      public Object evaluate(String expression, MuleMessage message)
51      {
52          if (StringUtils.isEmpty(expression) || message==null)
53          {
54              return message;
55          }
56          else
57          {
58              if (expression.equals("id"))
59              {
60                  return message.getUniqueId();
61              }
62              else if (expression.equals("correlationId"))
63              {
64                  return message.getCorrelationId();
65              }
66              else if (expression.equals("correlationSequence"))
67              {
68                  return message.getCorrelationSequence();
69              }
70              else if (expression.equals("correlationGroupSize"))
71              {
72                  return message.getCorrelationGroupSize();
73              }
74              else if (expression.equals("replyTo"))
75              {
76                  return message.getReplyTo();
77              }
78              else if (expression.equals("payload"))
79              {
80                  return message.getPayload();
81              }
82              else if (expression.equals("encoding"))
83              {
84                  return message.getEncoding();
85              }
86              else if (expression.equals("exception"))
87              {
88                  ExceptionPayload ep = message.getExceptionPayload();
89                  if (ep != null)
90                  {
91                      return ep.getException();
92                  }
93                  return null;
94              }
95              else
96              {
97                  throw new IllegalArgumentException(expression);
98              }
99  
100         }
101     }
102 
103 
104     /**
105      * {@inheritDoc}
106      */
107     public String getName()
108     {
109         return NAME;
110     }
111 
112     /**
113      * {@inheritDoc}
114      */
115     public void setName(String name)
116     {
117         throw new UnsupportedOperationException();
118     }
119 }