Browse Source

add check value "$"

pull/7101/head
Yuriy Lytvynchuk 4 years ago
parent
commit
6d46ea724a
  1. 28
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/transform/TbJsonPathNode.java

28
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/transform/TbJsonPathNode.java

@ -41,7 +41,7 @@ import java.util.concurrent.ExecutionException;
nodeDescription = "Extracts json element or set of elements from a message by JSONPath expression.", nodeDescription = "Extracts json element or set of elements from a message by JSONPath expression.",
nodeDetails = "JSONPath expression specifies a path to an element or a set of elements in a JSON structure. <br/>" nodeDetails = "JSONPath expression specifies a path to an element or a set of elements in a JSON structure. <br/>"
+ "<b>'$'</b> represents the root object or array. If JSONPath expression evaluation failed, incoming " + "<b>'$'</b> represents the root object or array. If JSONPath expression evaluation failed, incoming "
+ "age routes via <code>Failure</code> chain, otherwise <code>Success</code> chain is used.", + "message routes via <code>Failure</code> chain, otherwise <code>Success</code> chain is used.",
uiResources = {"static/rulenode/rulenode-core-config.js"}, uiResources = {"static/rulenode/rulenode-core-config.js"},
icon = "functions", icon = "functions",
configDirective = "tbTransformationNodeJsonPathConfig" configDirective = "tbTransformationNodeJsonPathConfig"
@ -51,23 +51,31 @@ public class TbJsonPathNode implements TbNode {
TbJsonPathNodeConfiguration config; TbJsonPathNodeConfiguration config;
Configuration configurationJsonPath; Configuration configurationJsonPath;
JsonPath jsonPath; JsonPath jsonPath;
String jsonPathValue;
@Override @Override
public void init(TbContext ctx, TbNodeConfiguration configuration) throws TbNodeException { public void init(TbContext ctx, TbNodeConfiguration configuration) throws TbNodeException {
this.config = TbNodeUtils.convert(configuration, TbJsonPathNodeConfiguration.class); this.config = TbNodeUtils.convert(configuration, TbJsonPathNodeConfiguration.class);
this.configurationJsonPath = Configuration.builder() this.jsonPathValue = config.getJsonPath();
.jsonProvider(new JacksonJsonNodeJsonProvider()) if (this.jsonPathValue != "$") {
.build(); this.configurationJsonPath = Configuration.builder()
this.jsonPath = JsonPath.compile(config.getJsonPath()); .jsonProvider(new JacksonJsonNodeJsonProvider())
.build();
this.jsonPath = JsonPath.compile(config.getJsonPath());
}
} }
@Override @Override
public void onMsg(TbContext ctx, TbMsg msg) throws ExecutionException, InterruptedException, TbNodeException { public void onMsg(TbContext ctx, TbMsg msg) throws ExecutionException, InterruptedException, TbNodeException {
try { if (jsonPathValue != "$") {
JsonNode jsonPathData = jsonPath.read(msg.getData(), this.configurationJsonPath); try {
ctx.tellSuccess(TbMsg.transformMsg(msg, msg.getType(), msg.getOriginator(), msg.getMetaData(), JacksonUtil.toString(jsonPathData))); JsonNode jsonPathData = jsonPath.read(msg.getData(), this.configurationJsonPath);
} catch (PathNotFoundException e) { ctx.tellSuccess(TbMsg.transformMsg(msg, msg.getType(), msg.getOriginator(), msg.getMetaData(), JacksonUtil.toString(jsonPathData)));
ctx.tellFailure(msg, e); } catch (PathNotFoundException e) {
ctx.tellFailure(msg, e);
}
} else {
ctx.tellSuccess(msg);
} }
} }

Loading…
Cancel
Save