This is an automated email from the ASF dual-hosted git repository. lburgazzoli pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel-k.git
commit 94305ddbb1e1dca841976a30a401a2239b48d26a Author: nferraro <ni.ferr...@gmail.com> AuthorDate: Mon Dec 24 12:12:42 2018 +0100 Adding JSON auto-conversion option --- .../camel/component/knative/KnativeComponent.java | 10 ++++++++ .../knative/KnativeConversionProcessor.java | 28 ++++++++++++++++++++++ .../camel/component/knative/KnativeEndpoint.java | 20 ++++++++++------ 3 files changed, 51 insertions(+), 7 deletions(-) diff --git a/runtime/camel-knative/src/main/java/org/apache/camel/component/knative/KnativeComponent.java b/runtime/camel-knative/src/main/java/org/apache/camel/component/knative/KnativeComponent.java index 0767fda..264f644 100644 --- a/runtime/camel-knative/src/main/java/org/apache/camel/component/knative/KnativeComponent.java +++ b/runtime/camel-knative/src/main/java/org/apache/camel/component/knative/KnativeComponent.java @@ -30,6 +30,8 @@ public class KnativeComponent extends DefaultComponent { private final KnativeConfiguration configuration; private String environmentPath; + private boolean jsonSerializationEnabled; + public KnativeComponent() { this(null); } @@ -116,4 +118,12 @@ public class KnativeComponent extends DefaultComponent { return conf; } + + public boolean isJsonSerializationEnabled() { + return jsonSerializationEnabled; + } + + public void setJsonSerializationEnabled(boolean jsonSerializationEnabled) { + this.jsonSerializationEnabled = jsonSerializationEnabled; + } } diff --git a/runtime/camel-knative/src/main/java/org/apache/camel/component/knative/KnativeConversionProcessor.java b/runtime/camel-knative/src/main/java/org/apache/camel/component/knative/KnativeConversionProcessor.java new file mode 100644 index 0000000..6ff5830 --- /dev/null +++ b/runtime/camel-knative/src/main/java/org/apache/camel/component/knative/KnativeConversionProcessor.java @@ -0,0 +1,28 @@ +package org.apache.camel.component.knative; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; + +/** + * Converts objects prior to serializing them to external endpoints or channels + */ +public class KnativeConversionProcessor implements Processor { + + private boolean enabled; + + public KnativeConversionProcessor(boolean enabled) { + this.enabled = enabled; + } + + @Override + public void process(Exchange exchange) throws Exception { + if (enabled) { + Object body = exchange.getIn().getBody(); + if (body != null) { + byte[] newBody = Knative.MAPPER.writeValueAsBytes(body); + exchange.getIn().setBody(newBody); + exchange.getIn().setHeader("CE-ContentType", "application/json"); + } + } + } +} diff --git a/runtime/camel-knative/src/main/java/org/apache/camel/component/knative/KnativeEndpoint.java b/runtime/camel-knative/src/main/java/org/apache/camel/component/knative/KnativeEndpoint.java index 10f264c..89c7f3b 100644 --- a/runtime/camel-knative/src/main/java/org/apache/camel/component/knative/KnativeEndpoint.java +++ b/runtime/camel-knative/src/main/java/org/apache/camel/component/knative/KnativeEndpoint.java @@ -16,13 +16,6 @@ */ package org.apache.camel.component.knative; -import java.io.InputStream; -import java.time.ZoneId; -import java.time.ZonedDateTime; -import java.time.format.DateTimeFormatter; -import java.util.HashMap; -import java.util.Map; - import org.apache.camel.CamelContext; import org.apache.camel.Consumer; import org.apache.camel.DelegateEndpoint; @@ -44,6 +37,13 @@ import org.apache.camel.util.StringHelper; import org.apache.camel.util.URISupport; import org.apache.commons.lang3.StringUtils; +import java.io.InputStream; +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.util.HashMap; +import java.util.Map; + import static org.apache.camel.util.ObjectHelper.ifNotEmpty; @@ -101,6 +101,11 @@ public class KnativeEndpoint extends DefaultEndpoint implements DelegateEndpoint } @Override + public KnativeComponent getComponent() { + return (KnativeComponent) super.getComponent(); + } + + @Override public Producer createProducer() throws Exception { return new KnativeProducer( this, @@ -121,6 +126,7 @@ public class KnativeEndpoint extends DefaultEndpoint implements DelegateEndpoint // Always remove host so it's always computed from the URL and not inherited from the exchange headers.remove("Host"); }, + new KnativeConversionProcessor(getComponent().isJsonSerializationEnabled()), endpoint.createProducer() ); }