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 9d2041992 CAMEL-17046 Implemented DatasonnetBuilder (#6220)
9d2041992 is described below
commit 9d20419923f3191a2c75092ae7b8f30e49019b50
Author: henka-rl <[email protected]>
AuthorDate: Thu Oct 7 07:14:26 2021 +0200
CAMEL-17046 Implemented DatasonnetBuilder (#6220)
---
.../datasonnet/CamelDatasonnetJavaDslTest.java | 111 +++++++++++++++
.../org/apache/camel/builder/BuilderSupport.java | 12 +-
.../apache/camel/builder/DatasonnetBuilder.java | 149 +++++++++++++++++++++
3 files changed, 264 insertions(+), 8 deletions(-)
diff --git
a/components/camel-datasonnet/src/test/java/org/apache/camel/language/datasonnet/CamelDatasonnetJavaDslTest.java
b/components/camel-datasonnet/src/test/java/org/apache/camel/language/datasonnet/CamelDatasonnetJavaDslTest.java
new file mode 100644
index 0000000..6f6a768
--- /dev/null
+++
b/components/camel-datasonnet/src/test/java/org/apache/camel/language/datasonnet/CamelDatasonnetJavaDslTest.java
@@ -0,0 +1,111 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.language.datasonnet;
+
+import java.io.InputStream;
+import java.nio.charset.Charset;
+
+import com.datasonnet.document.Document;
+import com.datasonnet.document.MediaTypes;
+import org.apache.camel.Exchange;
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.support.ExchangeHelper;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.apache.commons.io.IOUtils;
+import org.junit.jupiter.api.Test;
+import org.skyscreamer.jsonassert.JSONAssert;
+
+public class CamelDatasonnetJavaDslTest extends CamelTestSupport {
+ private MockEndpoint mock;
+
+ @Test
+ public void testTransform() throws Exception {
+ runCamelTest(loadResourceAsString("simpleMapping_payload.json"),
+ loadResourceAsString("simpleMapping_result.json"),
+ "direct:basicTransform");
+ }
+
+ @Test
+ public void testTransformXML() throws Exception {
+ runCamelTest(loadResourceAsString("payload.xml"),
+ loadResourceAsString("readXMLExtTest.json"),
+ "direct:transformXML");
+ }
+
+ @Test
+ public void testTransformCSV() throws Exception {
+ runCamelTest(loadResourceAsString("payload.csv"),
+ "{\"account\":\"123\"}",
+ "direct:transformCSV");
+ }
+
+ @Override
+ protected RoutesBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("direct:basicTransform")
+ .routeId("basicTransform")
+ .setProperty("test", constant("HelloWorld"))
+ .setProperty("count",
simple("1").resultType(Integer.class))
+ .setProperty("isActive",
simple("true").resultType(Boolean.class))
+ .setProperty("1. Full Name", constant("DataSonnet"))
+
.transform(datasonnet("resource:classpath:simpleMapping.ds", String.class)
+
.bodyMediaType(MediaTypes.APPLICATION_JSON_VALUE)
+
.outputMediaType(MediaTypes.APPLICATION_JSON_VALUE))
+ .to("mock:direct:end");
+
+ from("direct:transformXML")
+ .routeId("transformXML")
+
.transform(datasonnet("resource:classpath:readXMLExtTest.ds", String.class)
+
.bodyMediaType(MediaTypes.APPLICATION_XML_VALUE)
+
.outputMediaType(MediaTypes.APPLICATION_JSON_VALUE))
+ .to("mock:direct:end");
+
+ from("direct:transformCSV")
+ .routeId("transformCSV")
+
.transform(datasonnet("resource:classpath:readCSVTest.ds", String.class)
+
.bodyMediaType(MediaTypes.APPLICATION_CSV_VALUE)
+
.outputMediaType(MediaTypes.APPLICATION_JSON_VALUE))
+ .to("mock:direct:end");
+
+ }
+ };
+ }
+
+ private void runCamelTest(Object payload, String expectedJson, String uri)
throws Exception {
+ template.sendBody(uri, payload);
+ mock = getMockEndpoint("mock:direct:end");
+ Exchange exchange =
mock.assertExchangeReceived(mock.getReceivedCounter() - 1);
+ Object body = exchange.getMessage().getBody();
+ String response;
+ if (body instanceof Document) {
+ response = ExchangeHelper.convertToMandatoryType(exchange,
String.class, ((Document<?>) body).getContent());
+ } else {
+ response = exchange.getMessage().getBody(String.class);
+
+ }
+ JSONAssert.assertEquals(expectedJson, response, true);
+ }
+
+ private String loadResourceAsString(String name) throws Exception {
+ InputStream is = getClass().getClassLoader().getResourceAsStream(name);
+ return IOUtils.toString(is, Charset.defaultCharset());
+ }
+}
diff --git
a/core/camel-core-model/src/main/java/org/apache/camel/builder/BuilderSupport.java
b/core/camel-core-model/src/main/java/org/apache/camel/builder/BuilderSupport.java
index a75da04..97ea58d 100644
---
a/core/camel-core-model/src/main/java/org/apache/camel/builder/BuilderSupport.java
+++
b/core/camel-core-model/src/main/java/org/apache/camel/builder/BuilderSupport.java
@@ -26,7 +26,6 @@ import org.apache.camel.Endpoint;
import org.apache.camel.Expression;
import org.apache.camel.NoSuchEndpointException;
import org.apache.camel.RuntimeCamelException;
-import org.apache.camel.model.language.DatasonnetExpression;
import org.apache.camel.model.language.ExchangePropertyExpression;
import org.apache.camel.model.language.HeaderExpression;
import org.apache.camel.model.language.XPathExpression;
@@ -164,18 +163,15 @@ public abstract class BuilderSupport implements
CamelContextAware {
/**
* Returns a Datasonnet expression value builder
*/
- public ValueBuilder datasonnet(String value) {
- DatasonnetExpression exp = new DatasonnetExpression(value);
- return new ValueBuilder(exp);
+ public DatasonnetBuilder datasonnet(String value) {
+ return DatasonnetBuilder.datasonnet(value);
}
/**
* Returns a Datasonnet expression value builder
*/
- public ValueBuilder datasonnet(String value, Class<?> resultType) {
- DatasonnetExpression exp = new DatasonnetExpression(value);
- exp.setResultType(resultType);
- return new ValueBuilder(exp);
+ public DatasonnetBuilder datasonnet(String value, Class<?> resultType) {
+ return DatasonnetBuilder.datasonnet(value, resultType);
}
/**
diff --git
a/core/camel-core-model/src/main/java/org/apache/camel/builder/DatasonnetBuilder.java
b/core/camel-core-model/src/main/java/org/apache/camel/builder/DatasonnetBuilder.java
new file mode 100644
index 0000000..4d08264
--- /dev/null
+++
b/core/camel-core-model/src/main/java/org/apache/camel/builder/DatasonnetBuilder.java
@@ -0,0 +1,149 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.builder;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.Expression;
+import org.apache.camel.Predicate;
+import org.apache.camel.spi.ExpressionResultTypeAware;
+import org.apache.camel.spi.Language;
+
+/**
+ * Creates an Simple language builder.
+ * <p/>
+ * This builder is available in the Java DSL from the {@link RouteBuilder}
which means that using simple language for
+ * {@link Expression}s or {@link Predicate}s is very easy with the help of
this builder.
+ */
+public class DatasonnetBuilder implements Predicate, Expression,
ExpressionResultTypeAware {
+
+ private String text;
+ private Class<?> resultType;
+ private String bodyMediaType;
+ private String outputMediaType;
+
+ // cache the expression/predicate
+ private Language datasonnet;
+ private volatile Expression expression;
+ private volatile Predicate predicate;
+
+ public DatasonnetBuilder(String text) {
+ this.text = text;
+ }
+
+ public static DatasonnetBuilder datasonnet(String text) {
+ return new DatasonnetBuilder(text);
+ }
+
+ public static DatasonnetBuilder datasonnet(String text, Class<?>
resultType) {
+ DatasonnetBuilder answer = datasonnet(text);
+ answer.setResultType(resultType);
+ return answer;
+ }
+
+ public String getText() {
+ return text;
+ }
+
+ @Override
+ public String getExpressionText() {
+ return getText();
+ }
+
+ @Override
+ public Class<?> getResultType() {
+ return resultType;
+ }
+
+ public void setResultType(Class<?> resultType) {
+ this.resultType = resultType;
+ }
+
+ public String getBodyMediaType() {
+ return bodyMediaType;
+ }
+
+ public void setBodyMediaType(String bodyMediaType) {
+ this.bodyMediaType = bodyMediaType;
+ }
+
+ public String getOutputMediaType() {
+ return outputMediaType;
+ }
+
+ public void setOutputMediaType(String outputMediaType) {
+ this.outputMediaType = outputMediaType;
+ }
+
+ public DatasonnetBuilder resultType(Class<?> resultType) {
+ setResultType(resultType);
+ return this;
+ }
+
+ public DatasonnetBuilder bodyMediaType(String bodyMediaType) {
+ setBodyMediaType(bodyMediaType);
+ return this;
+ }
+
+ public DatasonnetBuilder outputMediaType(String outputMediaType) {
+ setOutputMediaType(outputMediaType);
+ return this;
+ }
+
+ @Override
+ public String toString() {
+ return text;
+ }
+
+ @Override
+ public <T> T evaluate(Exchange exchange, Class<T> type) {
+ if (expression == null) {
+ if (datasonnet == null) {
+ init(exchange.getContext());
+ }
+ if (resultType != null) {
+ Object[] properties = new Object[3];
+ properties[0] = resultType;
+ properties[1] = bodyMediaType;
+ properties[2] = outputMediaType;
+ expression = datasonnet.createExpression(text, properties);
+ } else {
+ expression = datasonnet.createExpression(text);
+ }
+ expression.init(exchange.getContext());
+ }
+ return expression.evaluate(exchange, type);
+ }
+
+ @Override
+ public boolean matches(Exchange exchange) {
+ if (predicate == null) {
+ if (datasonnet == null) {
+ init(exchange.getContext());
+ }
+ predicate = datasonnet.createPredicate(text);
+ predicate.init(exchange.getContext());
+ }
+ return predicate.matches(exchange);
+ }
+
+ @Override
+ public void init(CamelContext context) {
+ datasonnet = context.resolveLanguage("datasonnet");
+ text = context.resolvePropertyPlaceholders(text);
+ }
+}