This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
commit e9e8ed2057f0122f30664eb123ca00775a69c538 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Wed Mar 24 18:38:35 2021 +0100 CAMEL-16397: camel-jsonpath - Evaluate jsonpath result using the practice that the jsonpath project itself suggests. This allows to use their JSonPath cache for thread safety. --- .../org/apache/camel/jsonpath/JsonPathEngine.java | 43 ++++++++++------------ 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathEngine.java b/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathEngine.java index d64eca0f..e62cb7d 100644 --- a/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathEngine.java +++ b/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathEngine.java @@ -54,8 +54,8 @@ public class JsonPathEngine { private final String expression; private final boolean writeAsString; private final String headerName; - private final JsonPath path; private final Configuration configuration; + private final boolean hasSimple; private JsonPathAdapter adapter; private volatile boolean initJsonAdapter; @@ -81,33 +81,28 @@ public class JsonPathEngine { } this.configuration = builder.build(); - boolean hasSimple = false; + boolean simpleInUse = false; if (allowSimple) { // is simple language embedded Matcher matcher = SIMPLE_PATTERN.matcher(expression); if (matcher.find()) { - hasSimple = true; + simpleInUse = true; } } - if (hasSimple) { - this.path = null; - } else { - this.path = JsonPath.compile(expression); - LOG.debug("Compiled static JsonPath: {}", expression); - } + this.hasSimple = simpleInUse; } @SuppressWarnings("unchecked") public Object read(Exchange exchange) throws Exception { Object answer; - if (path == null) { + if (hasSimple) { + // need to compile every time Expression exp = exchange.getContext().resolveLanguage("simple").createExpression(expression); String text = exp.evaluate(exchange, String.class); - JsonPath path = JsonPath.compile(text); - LOG.debug("Compiled dynamic JsonPath: {}", expression); - answer = doRead(path, exchange); + LOG.debug("Compiled dynamic JsonPath: {}", text); + answer = doRead(text, exchange); } else { - answer = doRead(path, exchange); + answer = doRead(expression, exchange); } if (writeAsString) { @@ -158,7 +153,7 @@ public class JsonPathEngine { return answer; } - private Object doRead(JsonPath path, Exchange exchange) throws IOException, CamelExchangeException { + private Object doRead(String path, Exchange exchange) throws IOException, CamelExchangeException { Object json = headerName != null ? exchange.getIn().getHeader(headerName) : exchange.getIn().getBody(); if (json instanceof InputStream) { @@ -169,22 +164,22 @@ public class JsonPathEngine { if (genericFile.getCharset() != null) { // special treatment for generic file with charset InputStream inputStream = new FileInputStream((File) genericFile.getFile()); - return path.read(inputStream, genericFile.getCharset(), configuration); + return JsonPath.using(configuration).parse(inputStream, genericFile.getCharset()).read(path); } } if (json instanceof String) { LOG.trace("JSonPath: {} is read as String: {}", path, json); String str = (String) json; - return path.read(str, configuration); + return JsonPath.using(configuration).parse(str).read(path); } else if (json instanceof Map) { LOG.trace("JSonPath: {} is read as Map: {}", path, json); Map map = (Map) json; - return path.read(map, configuration); + return JsonPath.using(configuration).parse(map).read(path); } else if (json instanceof List) { LOG.trace("JSonPath: {} is read as List: {}", path, json); List list = (List) json; - return path.read(list, configuration); + return JsonPath.using(configuration).parse(list).read(path); } else { // can we find an adapter which can read the message body/header Object answer = readWithAdapter(path, exchange); @@ -214,7 +209,7 @@ public class JsonPathEngine { } } - private Object readWithInputStream(JsonPath path, Exchange exchange) throws IOException { + private Object readWithInputStream(String path, Exchange exchange) throws IOException { Object json = headerName != null ? exchange.getIn().getHeader(headerName) : exchange.getIn().getBody(); LOG.trace("JSonPath: {} is read as InputStream: {}", path, json); @@ -228,19 +223,19 @@ public class JsonPathEngine { String jsonEncoding = exchange.getIn().getHeader(JsonPathConstants.HEADER_JSON_ENCODING, String.class); if (jsonEncoding != null) { // json encoding specified in header - return path.read(is, jsonEncoding, configuration); + return JsonPath.using(configuration).parse(is, jsonEncoding).read(path); } else { // No json encoding specified --> assume json encoding is unicode and determine the specific unicode encoding according to RFC-4627. // This is a temporary solution, it can be removed as soon as jsonpath offers the encoding detection JsonStream jsonStream = new JsonStream(is); - return path.read(jsonStream, jsonStream.getEncoding().name(), configuration); + return JsonPath.using(configuration).parse(jsonStream, jsonStream.getEncoding().name()).read(path); } } return null; } - private Object readWithAdapter(JsonPath path, Exchange exchange) { + private Object readWithAdapter(String path, Exchange exchange) { Object json = headerName != null ? exchange.getIn().getHeader(headerName) : exchange.getIn().getBody(); LOG.trace("JSonPath: {} is read with adapter: {}", path, json); @@ -259,7 +254,7 @@ public class JsonPathEngine { LOG.debug("JacksonJsonAdapter converted object from: {} to: java.util.Map", ObjectHelper.classCanonicalName(json)); } - return path.read(map, configuration); + return JsonPath.using(configuration).parse(map).read(path); } }