Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
JsonExpressionEvaluator |
|
| 0.0;0 |
1 | /* | |
2 | * $Id: JsonExpressionEvaluator.java 20383 2010-11-29 19:44:13Z 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.module.json; | |
12 | ||
13 | import org.mule.api.MuleMessage; | |
14 | import org.mule.api.MuleRuntimeException; | |
15 | import org.mule.api.expression.ExpressionEvaluator; | |
16 | import org.mule.config.i18n.CoreMessages; | |
17 | import org.mule.util.NumberUtils; | |
18 | ||
19 | import java.util.ArrayList; | |
20 | import java.util.Iterator; | |
21 | import java.util.List; | |
22 | ||
23 | import org.apache.commons.logging.Log; | |
24 | import org.apache.commons.logging.LogFactory; | |
25 | import org.codehaus.jackson.JsonNode; | |
26 | import org.codehaus.jackson.node.ArrayNode; | |
27 | import org.codehaus.jackson.node.ObjectNode; | |
28 | import org.codehaus.jackson.node.ValueNode; | |
29 | ||
30 | /** | |
31 | * An expression evaluator to allow users to define json expressions in their mule configuration, i.e. | |
32 | * <code> | |
33 | * #[json:person/addresses[0]/postcode] | |
34 | * </code> | |
35 | * <p/> | |
36 | * See the {@link org.mule.module.json.JsonData} object for mor information about the query syntax. | |
37 | * <p/> | |
38 | * It is also possible to use this evaluator in {@link org.mule.routing.filters.ExpressionFilter} objects. For example | |
39 | * a filter could be defined as - | |
40 | * <p/> | |
41 | * <code> | |
42 | * #[json:person/registered] | |
43 | * </code> | |
44 | * <p/> | |
45 | * Where 'registered' is a boolean value. It is also possible to filter on the existence of a value i.e. | |
46 | * <p/> | |
47 | * <code> | |
48 | * #[json:person/favouriteColour] | |
49 | * </code> | |
50 | * <p/> | |
51 | * Which would return true if 'favouriteColour' has been set. This evaluator also dds two logic operators you can use | |
52 | * to create more sophisticated boolean expressions; equals and not equals - | |
53 | * <p/> | |
54 | * <code> | |
55 | * #[json:person/favouriteColour = red] | |
56 | * </code> | |
57 | * <p/> | |
58 | * or | |
59 | * <p/> | |
60 | * <code> | |
61 | * #[json:person/favouriteColour != brown] | |
62 | * </code> | |
63 | * | |
64 | * @see org.mule.module.json.JsonData | |
65 | */ | |
66 | 0 | public class JsonExpressionEvaluator implements ExpressionEvaluator |
67 | { | |
68 | /** | |
69 | * logger used by this class | |
70 | */ | |
71 | 0 | protected transient final Log logger = LogFactory.getLog(JsonExpressionEvaluator.class); |
72 | ||
73 | public Object evaluate(String expression, MuleMessage message) | |
74 | { | |
75 | 0 | String compareTo = null; |
76 | 0 | boolean not = false; |
77 | 0 | int start = expression.lastIndexOf("/"); |
78 | 0 | if (start == -1) |
79 | { | |
80 | 0 | start = 0; |
81 | } | |
82 | 0 | int i=0; |
83 | 0 | if ((i = expression.indexOf("!=", start)) > -1) |
84 | { | |
85 | 0 | compareTo = expression.substring(i + 2, expression.length()).trim(); |
86 | 0 | expression = expression.substring(0, i).trim(); |
87 | 0 | not = true; |
88 | } | |
89 | 0 | else if ((i = expression.indexOf("=", start)) > -1) |
90 | { | |
91 | 0 | compareTo = expression.substring(i + 1, expression.length()).trim(); |
92 | 0 | expression = expression.substring(0, i).trim(); |
93 | } | |
94 | ||
95 | try | |
96 | { | |
97 | 0 | String json = message.getPayloadAsString(); |
98 | 0 | JsonData data = new JsonData(json); |
99 | try | |
100 | { | |
101 | 0 | JsonNode resultNode = data.get(expression); |
102 | 0 | if (compareTo != null) |
103 | { | |
104 | 0 | Object resultValue = resultNode.isValueNode() ? resultNode.getValueAsText() : resultNode; |
105 | 0 | if (compareTo.equalsIgnoreCase("null")) |
106 | { | |
107 | 0 | boolean answer = resultValue == null; |
108 | 0 | return (not ? !answer : answer); |
109 | } | |
110 | 0 | else if (resultValue instanceof Number && NumberUtils.isDigits(compareTo)) |
111 | { | |
112 | 0 | boolean answer = NumberUtils.createNumber(compareTo).equals(resultValue); |
113 | 0 | return (not ? !answer : answer); |
114 | } | |
115 | 0 | else if (resultValue instanceof Boolean |
116 | && (compareTo.equalsIgnoreCase("true") || compareTo.equalsIgnoreCase("false"))) | |
117 | { | |
118 | 0 | boolean answer = resultValue.equals(Boolean.valueOf(compareTo)); |
119 | 0 | return (not ? !answer : answer); |
120 | } | |
121 | else | |
122 | { | |
123 | 0 | boolean answer = compareTo.equals(resultValue); |
124 | 0 | return (not ? !answer : answer); |
125 | } | |
126 | } | |
127 | else | |
128 | { | |
129 | 0 | return extractResultFromNode(resultNode); |
130 | } | |
131 | } | |
132 | 0 | catch (IllegalArgumentException e) |
133 | { | |
134 | 0 | if (compareTo == null) |
135 | { | |
136 | 0 | logger.debug("returning null for json expression: " + expression + ": " + e.getMessage()); |
137 | 0 | return null; |
138 | } | |
139 | //If the element does not exist but is matching against 'null' return true, otherwise false | |
140 | 0 | return (compareTo.equalsIgnoreCase("null")) & !not; |
141 | } | |
142 | } | |
143 | 0 | catch (Exception e) |
144 | { | |
145 | 0 | throw new MuleRuntimeException(CoreMessages.failedToProcessExtractorFunction(getName() + ":" + expression), e); |
146 | } | |
147 | } | |
148 | ||
149 | protected Object extractResultFromNode(JsonNode result) | |
150 | { | |
151 | 0 | if (result instanceof ValueNode) |
152 | { | |
153 | 0 | return result.getValueAsText(); |
154 | } | |
155 | 0 | if (result instanceof ObjectNode) |
156 | { | |
157 | 0 | return ((ObjectNode) result).toString(); |
158 | } | |
159 | 0 | else if (result instanceof ArrayNode) |
160 | { | |
161 | 0 | List parts = new ArrayList(); |
162 | 0 | for (Iterator<JsonNode> i = ((JsonNode) result).getElements(); i.hasNext();) |
163 | { | |
164 | 0 | JsonNode arrayNode = i.next(); |
165 | 0 | parts.add(extractResultFromNode(arrayNode)); |
166 | 0 | } |
167 | 0 | return parts; |
168 | } | |
169 | else | |
170 | { | |
171 | 0 | return result; |
172 | } | |
173 | } | |
174 | ||
175 | public void setName(String name) | |
176 | { | |
177 | 0 | throw new UnsupportedOperationException("setName"); |
178 | } | |
179 | ||
180 | public String getName() | |
181 | { | |
182 | 0 | return "json"; |
183 | } | |
184 | } |