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 0654622 commit 9732938
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 9 deletions.
Expand Up @@ -333,7 +333,21 @@ public T javaScript(String text) {
* @return the builder to continue processing the DSL
*/
public T jsonpath(String text) {
return expression(new JsonPathExpression(text));
return jsonpath(text, false);
}

/**
* Evaluates a <a href="http://camel.apache.org/jsonpath.html">Json Path
* expression</a>
*
* @param text the expression to be evaluated
* @param suppressExceptions whether to suppress exceptions such as PathNotFoundException
* @return the builder to continue processing the DSL
*/
public T jsonpath(String text, boolean suppressExceptions) {
JsonPathExpression expression = new JsonPathExpression(text);
expression.setSuppressExceptions(suppressExceptions);
return expression(expression);
}

/**
Expand All @@ -351,6 +365,23 @@ public T jsonpath(String text, Class<?> resultType) {
return result;
}

/**
* Evaluates a <a href="http://camel.apache.org/jsonpath.html">Json Path
* expression</a>
*
* @param text the expression to be evaluated
* @param suppressExceptions whether to suppress exceptions such as PathNotFoundException
* @param resultType the return type expected by the expression
* @return the builder to continue processing the DSL
*/
public T jsonpath(String text, boolean suppressExceptions, Class<?> resultType) {
JsonPathExpression expression = new JsonPathExpression(text);
expression.setSuppressExceptions(suppressExceptions);
expression.setResultType(resultType);
setExpressionType(expression);
return result;
}

/**
* Evaluates a <a href="http://commons.apache.org/jxpath/">JXPath expression</a>
*
Expand Down
Expand Up @@ -42,6 +42,8 @@ public class JsonPathExpression extends ExpressionDefinition {
private String resultTypeName;
@XmlTransient
private Class<?> resultType;
@XmlAttribute @Metadata(defaultValue = "false")
private Boolean suppressExceptions;

public JsonPathExpression() {
}
Expand Down Expand Up @@ -72,6 +74,17 @@ public void setResultType(Class<?> resultType) {
this.resultType = resultType;
}

public Boolean getSuppressExceptions() {
return suppressExceptions;
}

/**
* Whether to suppress exceptions such as PathNotFoundException.
*/
public void setSuppressExceptions(Boolean suppressExceptions) {
this.suppressExceptions = suppressExceptions;
}

public String getLanguage() {
return "jsonpath";
}
Expand All @@ -93,6 +106,9 @@ protected void configureExpression(CamelContext camelContext, Expression express
if (resultType != null) {
setProperty(expression, "resultType", resultType);
}
if (suppressExceptions != null) {
setProperty(expression, "suppressExceptions", suppressExceptions);
}
super.configureExpression(camelContext, expression);
}

Expand All @@ -101,6 +117,9 @@ protected void configurePredicate(CamelContext camelContext, Predicate predicate
if (resultType != null) {
setProperty(predicate, "resultType", resultType);
}
if (suppressExceptions != null) {
setProperty(predicate, "suppressExceptions", suppressExceptions);
}
super.configurePredicate(camelContext, predicate);
}

Expand Down
Expand Up @@ -40,6 +40,11 @@

String value();

/**
* Whether to suppress exceptions such as PathNotFoundException
*/
boolean suppressExceptions() default false;

/**
* To configure the json path options to use
*/
Expand Down
Expand Up @@ -39,6 +39,9 @@ public Expression createExpression(CamelContext camelContext, Annotation annotat

if (annotation instanceof JsonPath) {
JsonPath jsonPathAnnotation = (JsonPath) annotation;

answer.setSuppressExceptions(jsonPathAnnotation.suppressExceptions());

Option[] options = jsonPathAnnotation.options();
answer.setOptions(options);
}
Expand Down
Expand Up @@ -40,15 +40,23 @@ public class JsonPathEngine {
private final Configuration configuration;

public JsonPathEngine(String expression) {
this(expression, null);
this(expression, false, null);
}

public JsonPathEngine(String expression, Option[] options) {
public JsonPathEngine(String expression, boolean suppressExceptions, Option[] options) {
Defaults defaults = DefaultsImpl.INSTANCE;
if (options != null) {
this.configuration = Configuration.builder().jsonProvider(defaults.jsonProvider()).options(options).build();
Configuration.ConfigurationBuilder builder = Configuration.builder().jsonProvider(defaults.jsonProvider()).options(options);
if (suppressExceptions) {
builder.options(Option.SUPPRESS_EXCEPTIONS);
}
this.configuration = builder.build();
} else {
this.configuration = Configuration.builder().jsonProvider(defaults.jsonProvider()).build();
Configuration.ConfigurationBuilder builder = Configuration.builder().jsonProvider(defaults.jsonProvider());
if (suppressExceptions) {
builder.options(Option.SUPPRESS_EXCEPTIONS);
}
this.configuration = builder.build();
}
this.path = JsonPath.compile(expression);
}
Expand Down
Expand Up @@ -28,6 +28,7 @@ public class JsonPathExpression extends ExpressionAdapter {
private JsonPathEngine engine;

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

public JsonPathExpression(String expression) {
Expand All @@ -36,7 +37,7 @@ public JsonPathExpression(String expression) {

public void init() {
try {
engine = new JsonPathEngine(expression, options);
engine = new JsonPathEngine(expression, suppressExceptions, options);
} catch (Exception e) {
throw new ExpressionIllegalSyntaxException(expression, e);
}
Expand All @@ -46,14 +47,31 @@ public Class<?> getResultType() {
return resultType;
}

/**
* To configure the result type to use
*/
public void setResultType(Class<?> resultType) {
this.resultType = resultType;
}

public boolean isSuppressExceptions() {
return suppressExceptions;
}

/**
* Whether to suppress exceptions such as PathNotFoundException
*/
public void setSuppressExceptions(boolean suppressExceptions) {
this.suppressExceptions = suppressExceptions;
}

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

/**
* To configure the json path options to use
*/
public void setOptions(Option[] options) {
this.options = options;
}
Expand Down
Expand Up @@ -16,12 +16,11 @@
*/
package org.apache.camel.jsonpath;

import com.jayway.jsonpath.Option;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;

public class JsonPathBeanOptionTest extends CamelTestSupport {
public class JsonPathBeanSuppressExceptionsTest extends CamelTestSupport {

@Test
public void testFullName() throws Exception {
Expand Down Expand Up @@ -52,7 +51,7 @@ public void configure() {
protected static class FullNameBean {
// middle name is optional
public static String getName(@JsonPath("person.firstname") String first,
@JsonPath(value = "person.middlename", options = Option.SUPPRESS_EXCEPTIONS) String middle,
@JsonPath(value = "person.middlename", suppressExceptions = true) String middle,
@JsonPath("person.lastname") String last) {
if (middle != null) {
return first + " " + middle + " " + last;
Expand Down

0 comments on commit 9732938

Please sign in to comment.