Skip to content

Commit

Permalink
CAMEL-12464 Add support for transform with context
Browse files Browse the repository at this point in the history
  • Loading branch information
rockoder authored and onderson committed May 7, 2018
1 parent ec26fab commit d8934d4
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 14 deletions.
Expand Up @@ -17,21 +17,17 @@
package org.apache.camel.component.jolt; package org.apache.camel.component.jolt;


import java.io.InputStream; import java.io.InputStream;
import java.util.Map;


import com.bazaarvoice.jolt.Chainr; import com.bazaarvoice.jolt.*;
import com.bazaarvoice.jolt.Defaultr;
import com.bazaarvoice.jolt.JsonUtils;
import com.bazaarvoice.jolt.Removr;
import com.bazaarvoice.jolt.Shiftr;
import com.bazaarvoice.jolt.Sortr;
import com.bazaarvoice.jolt.Transform;


import org.apache.camel.Exchange; import org.apache.camel.Exchange;
import org.apache.camel.ExchangePattern; import org.apache.camel.ExchangePattern;
import org.apache.camel.Message; import org.apache.camel.Message;
import org.apache.camel.component.ResourceEndpoint; import org.apache.camel.component.ResourceEndpoint;
import org.apache.camel.spi.UriEndpoint; import org.apache.camel.spi.UriEndpoint;
import org.apache.camel.spi.UriParam; import org.apache.camel.spi.UriParam;
import org.apache.camel.util.ExchangeHelper;
import org.apache.camel.util.ObjectHelper; import org.apache.camel.util.ObjectHelper;


/** /**
Expand All @@ -40,8 +36,8 @@
@UriEndpoint(firstVersion = "2.16.0", scheme = "jolt", title = "JOLT", syntax = "jolt:resourceUri", producerOnly = true, label = "transformation") @UriEndpoint(firstVersion = "2.16.0", scheme = "jolt", title = "JOLT", syntax = "jolt:resourceUri", producerOnly = true, label = "transformation")
public class JoltEndpoint extends ResourceEndpoint { public class JoltEndpoint extends ResourceEndpoint {


private Transform transform; private JoltTransform transform;

@UriParam(defaultValue = "Hydrated") @UriParam(defaultValue = "Hydrated")
private JoltInputOutputType outputType; private JoltInputOutputType outputType;


Expand Down Expand Up @@ -73,7 +69,7 @@ protected String createEndpointUri() {
return "jolt:" + getResourceUri(); return "jolt:" + getResourceUri();
} }


private synchronized Transform getTransform() throws Exception { private synchronized JoltTransform getTransform() throws Exception {
if (transform == null) { if (transform == null) {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
String path = getResourceUri(); String path = getResourceUri();
Expand Down Expand Up @@ -110,7 +106,7 @@ private synchronized Transform getTransform() throws Exception {
/** /**
* Sets the Transform to use. If not set a Transform specified by the transformDsl will be created * Sets the Transform to use. If not set a Transform specified by the transformDsl will be created
*/ */
public void setTransform(Transform transform) { public void setTransform(JoltTransform transform) {
this.transform = transform; this.transform = transform;
} }


Expand Down Expand Up @@ -167,14 +163,24 @@ protected void onExchange(Exchange exchange) throws Exception {
newEndpoint.onExchange(exchange); newEndpoint.onExchange(exchange);
return; return;
} }

Object input; Object input;
if (getInputType() == JoltInputOutputType.JsonString) { if (getInputType() == JoltInputOutputType.JsonString) {
input = JsonUtils.jsonToObject(exchange.getIn().getBody(InputStream.class)); input = JsonUtils.jsonToObject(exchange.getIn().getBody(InputStream.class));
} else { } else {
input = exchange.getIn().getBody(); input = exchange.getIn().getBody();
} }
Object output = getTransform().transform(input);

Object output;

@SuppressWarnings("unchecked")
Map<String, Object> inputContextMap = exchange.getIn().getHeader(JoltConstants.JOLT_CONTEXT, Map.class);
if (inputContextMap != null) {
output = ((ContextualTransform)getTransform()).transform(input, inputContextMap);
} else {
output = ((Transform)getTransform()).transform(input);
}

// now lets output the results to the exchange // now lets output the results to the exchange
Message out = exchange.getOut(); Message out = exchange.getOut();
if (getOutputType() == JoltInputOutputType.JsonString) { if (getOutputType() == JoltInputOutputType.JsonString) {
Expand Down
Expand Up @@ -16,12 +16,17 @@
*/ */
package org.apache.camel.component.jolt; package org.apache.camel.component.jolt;


import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder; import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.test.junit4.CamelTestSupport; import org.apache.camel.test.junit4.CamelTestSupport;
import org.apache.camel.util.IOHelper; import org.apache.camel.util.IOHelper;
import org.apache.camel.util.ResourceHelper; import org.apache.camel.util.ResourceHelper;
import org.junit.Test; import org.junit.Test;


import java.util.HashMap;
import java.util.Map;

/** /**
* Unit test based on the first sample test from the JOLT project. * Unit test based on the first sample test from the JOLT project.
*/ */
Expand All @@ -46,9 +51,19 @@ public void testFirstSampleJolt() throws Exception {


@Override @Override
protected RouteBuilder createRouteBuilder() throws Exception { protected RouteBuilder createRouteBuilder() throws Exception {
final Processor processor = new Processor() {
public void process(Exchange exchange) {
Map<String, String> contextMap = new HashMap<>();
contextMap.put("contextB", "bb");

exchange.getIn().setHeader(JoltConstants.JOLT_CONTEXT, contextMap);
}
};

return new RouteBuilder() { return new RouteBuilder() {
public void configure() { public void configure() {
from("direct://start") from("direct://start")
.process(processor)
.to("jolt:org/apache/camel/component/jolt/firstSample/spec.json?inputType=JsonString&outputType=JsonString") .to("jolt:org/apache/camel/component/jolt/firstSample/spec.json?inputType=JsonString&outputType=JsonString")
.to("mock:result"); .to("mock:result");
} }
Expand Down
@@ -1 +1 @@
{"~a":"aa","~id":"id","~z":"zz","Rating":3,"RatingRange":5,"SecondaryRatings":{"quality":{"Id":"quality","Range":7,"Value":3},"sharpness":{"Id":"sharpness","Range":5,"Value":5}},"zz":"zz"} {"~a":"aa","~id":"id","~z":"zz","Rating":3,"RatingRange":5,"SecondaryRatings":{"quality":{"Id":"quality","Range":7,"Value":3},"sharpness":{"Id":"sharpness","Range":5,"Value":5}},"b":"bb","zz":"zz"}
Expand Up @@ -34,6 +34,12 @@
"~deleteme": "" "~deleteme": ""
} }
}, },
{
"operation": "modify-overwrite-beta",
"spec": {
"b": "^contextB"
}
},
{ {
// last operation is to sort the JSON // last operation is to sort the JSON
"operation": "sort" "operation": "sort"
Expand Down

0 comments on commit d8934d4

Please sign in to comment.