Skip to content

Commit

Permalink
CAMEL-8799: Make it possible for JsonPath to suppress PathNotFoundExc…
Browse files Browse the repository at this point in the history
…eption
  • Loading branch information
davsclaus committed May 30, 2015
1 parent e6fb2d5 commit 0654622
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 6 deletions.
Expand Up @@ -40,14 +40,16 @@ public class JsonPathEngine {
private final Configuration configuration;

public JsonPathEngine(String expression) {
Defaults defaults = DefaultsImpl.INSTANCE;
this.configuration = Configuration.builder().jsonProvider(defaults.jsonProvider()).build();
this.path = JsonPath.compile(expression);
this(expression, null);
}

public JsonPathEngine(String expression, Option[] options) {
Defaults defaults = DefaultsImpl.INSTANCE;
this.configuration = Configuration.builder().jsonProvider(defaults.jsonProvider()).options(options).build();
if (options != null) {
this.configuration = Configuration.builder().jsonProvider(defaults.jsonProvider()).options(options).build();
} else {
this.configuration = Configuration.builder().jsonProvider(defaults.jsonProvider()).build();
}
this.path = JsonPath.compile(expression);
}

Expand Down
Expand Up @@ -16,24 +16,57 @@
*/
package org.apache.camel.jsonpath;

import com.jayway.jsonpath.Option;
import org.apache.camel.Expression;
import org.apache.camel.Predicate;
import org.apache.camel.support.LanguageSupport;

public class JsonPathLanguage extends LanguageSupport {

private Class<?> resultType;
private Option[] options;

public Class<?> getResultType() {
return resultType;
}

public void setResultType(Class<?> resultType) {
this.resultType = resultType;
}

public Option[] getOptions() {
return options;
}

public void setOption(Option option) {
this.options = new Option[]{option};
}

public void setOptions(Option[] options) {
this.options = options;
}

@Override
public Predicate createPredicate(final String predicate) {
JsonPathExpression answer = new JsonPathExpression(predicate);
answer.setResultType(resultType);
answer.setOptions(options);
answer.init();
return answer;
}

@Override
public Expression createExpression(final String expression) {
JsonPathExpression answer = new JsonPathExpression(expression);
answer.setResultType(resultType);
answer.setOptions(options);
answer.init();
return answer;
}

@Override
public boolean isSingleton() {
// cannot be singleton due options
return false;
}
}
Expand Up @@ -31,6 +31,16 @@ public void testFullName() throws Exception {
assertMockEndpointsSatisfied();
}

@Test
public void testFullNameTwo() throws Exception {
String json = "{\"person\" : {\"firstname\" : \"foo\", \"middlename\" : \"foo2\", \"lastname\" : \"bar\"}}";
String json2 = "{\"person\" : {\"firstname\" : \"bar\", \"middlename\" : \"bar2\", \"lastname\" : \"foo\"}}";
getMockEndpoint("mock:result").expectedBodiesReceived("foo foo2 bar", "bar bar2 foo");
template.sendBody("direct:start", json);
template.sendBody("direct:start", json2);
assertMockEndpointsSatisfied();
}

@Test
public void testFirstAndLastName() throws Exception {
String json = "{\"person\" : {\"firstname\" : \"foo\", \"lastname\" : \"bar\"}}";
Expand Down
Expand Up @@ -68,7 +68,6 @@ public void testCheapBicycle() throws Exception {
sendMessageToBicycleRoute("direct:bicycle");
resetMocks();
sendMessageToBicycleRoute("direct:bicycle2");

}

private void sendMessageToBicycleRoute(String startPoint) throws Exception {
Expand Down
Expand Up @@ -19,6 +19,7 @@
import java.io.File;
import java.util.List;

import com.jayway.jsonpath.Option;
import org.apache.camel.Exchange;
import org.apache.camel.Expression;
import org.apache.camel.Predicate;
Expand Down Expand Up @@ -93,7 +94,9 @@ public void testSuppressException() throws Exception {
Exchange exchange = new DefaultExchange(context);
exchange.getIn().setBody(new File("src/test/resources/type.json"));

Language lan = context.resolveLanguage("jsonpath");
JsonPathLanguage lan = (JsonPathLanguage) context.resolveLanguage("jsonpath");
lan.setOption(Option.SUPPRESS_EXCEPTIONS);

Expression exp = lan.createExpression("$.foo");
String nofoo = exp.evaluate(exchange, String.class);

Expand Down

0 comments on commit 0654622

Please sign in to comment.