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