This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new c882f1794b4 CAMEL-18368: camel-jslt - Add support for byte[] and a few
other types.
c882f1794b4 is described below
commit c882f1794b43d4dc9c1335a7f07b63121b666183
Author: Claus Ibsen <[email protected]>
AuthorDate: Wed Aug 10 14:55:42 2022 +0200
CAMEL-18368: camel-jslt - Add support for byte[] and a few other types.
---
.../apache/camel/component/jslt/JsltEndpoint.java | 28 +++++++++++++++-------
.../apache/camel/component/jslt/JsltBasicTest.java | 19 +++++++++++++++
2 files changed, 39 insertions(+), 8 deletions(-)
diff --git
a/components/camel-jslt/src/main/java/org/apache/camel/component/jslt/JsltEndpoint.java
b/components/camel-jslt/src/main/java/org/apache/camel/component/jslt/JsltEndpoint.java
index 6f5f88ea15a..686f42ac836 100644
---
a/components/camel-jslt/src/main/java/org/apache/camel/component/jslt/JsltEndpoint.java
+++
b/components/camel-jslt/src/main/java/org/apache/camel/component/jslt/JsltEndpoint.java
@@ -16,6 +16,7 @@
*/
package org.apache.camel.component.jslt;
+import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
@@ -39,6 +40,7 @@ import org.apache.camel.Exchange;
import org.apache.camel.ExchangePattern;
import org.apache.camel.Message;
import org.apache.camel.ValidationException;
+import org.apache.camel.WrappedFile;
import org.apache.camel.component.ResourceEndpoint;
import org.apache.camel.spi.UriEndpoint;
import org.apache.camel.spi.UriParam;
@@ -159,20 +161,30 @@ public class JsltEndpoint extends ResourceEndpoint {
if (isMapBigDecimalAsFloats()) {
objectMapper.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS);
}
- if (exchange.getIn().getBody() instanceof String) {
- input =
objectMapper.readTree(exchange.getIn().getBody(String.class));
- } else if (exchange.getIn().getBody() instanceof InputStream) {
- input =
objectMapper.readTree(exchange.getIn().getBody(InputStream.class));
+
+ Object body = exchange.getIn().getBody();
+ if (body instanceof WrappedFile) {
+ body = ((WrappedFile<?>) body).getFile();
+ }
+ if (body instanceof String) {
+ input = objectMapper.readTree((String) body);
+ } else if (body instanceof Reader) {
+ input = objectMapper.readTree((Reader) body);
+ } else if (body instanceof File) {
+ input = objectMapper.readTree((File) body);
+ } else if (body instanceof byte[]) {
+ input = objectMapper.readTree((byte[]) body);
+ } else if (body instanceof InputStream) {
+ input = objectMapper.readTree((InputStream) body);
} else {
- log.debug("Body content is not String neither InputStream.");
- throw new ValidationException(exchange, "Allowed body types are
String or InputStream.");
+ throw new ValidationException(exchange, "Allowed body types are
String, Reader, File, byte[] or InputStream.");
}
Map<String, JsonNode> variables = extractVariables(exchange);
JsonNode output = getTransform(exchange.getMessage()).apply(variables,
input);
- Object body = isPrettyPrint() ? output.toPrettyString() :
output.toString();
- ExchangeHelper.setInOutBodyPatternAware(exchange, body);
+ String result = isPrettyPrint() ? output.toPrettyString() :
output.toString();
+ ExchangeHelper.setInOutBodyPatternAware(exchange, result);
}
/**
diff --git
a/components/camel-jslt/src/test/java/org/apache/camel/component/jslt/JsltBasicTest.java
b/components/camel-jslt/src/test/java/org/apache/camel/component/jslt/JsltBasicTest.java
index 532bd095328..44db7d36574 100644
---
a/components/camel-jslt/src/test/java/org/apache/camel/component/jslt/JsltBasicTest.java
+++
b/components/camel-jslt/src/test/java/org/apache/camel/component/jslt/JsltBasicTest.java
@@ -16,6 +16,7 @@
*/
package org.apache.camel.component.jslt;
+import java.nio.charset.StandardCharsets;
import java.util.Collections;
import org.apache.camel.builder.RouteBuilder;
@@ -73,6 +74,24 @@ public class JsltBasicTest extends CamelTestSupport {
assertMockEndpointsSatisfied();
}
+ @Test
+ public void testJsltAsByteArray() throws Exception {
+ getMockEndpoint("mock:result").expectedMinimumMessageCount(1);
+ getMockEndpoint("mock:result").expectedBodiesReceived(
+ IOHelper.loadText(
+ ResourceHelper.resolveMandatoryResourceAsInputStream(
+ context,
"org/apache/camel/component/jslt/demoPlayground/output.json"))
+ .trim() // Remove the last newline added by
IOHelper.loadText()
+ );
+
+ String text =
IOHelper.loadText(ResourceHelper.resolveMandatoryResourceAsInputStream(
+ context,
"org/apache/camel/component/jslt/demoPlayground/input.json"));
+
+ sendBody("direct://start", text.getBytes(StandardCharsets.UTF_8));
+
+ assertMockEndpointsSatisfied();
+ }
+
@Test
public void testJsltAsInputStreamPrettyPrint() throws Exception {
getMockEndpoint("mock:result").expectedMinimumMessageCount(1);