Skip to content

Commit

Permalink
Merge pull request #2246 from GDLMadushanka/siel3
Browse files Browse the repository at this point in the history
Modify synapse expression return object for JSON
  • Loading branch information
GDLMadushanka authored Dec 2, 2024
2 parents c17a92f + a2b8dcb commit 80e83df
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.commons.logging.LogFactory;
import org.apache.synapse.SynapseException;
import org.apache.synapse.mediators.Value;
import org.apache.synapse.util.xpath.SynapseExpression;
import org.apache.synapse.util.xpath.SynapseJsonPath;
import org.apache.synapse.util.xpath.SynapseXPath;
import org.jaxen.JaxenException;
Expand Down Expand Up @@ -65,6 +66,10 @@ public Value createValue(String name, OMElement elem) {
attributeValue = attributeValue.substring(1, attributeValue.length() - 1);
SynapseJsonPath synJsonPath = createSynJsonPath(attributeValue);
key = new Value(synJsonPath);
} else if (attributeValue.startsWith("{${") && attributeValue.endsWith("}}")) {
attributeValue = attributeValue.substring(1, attributeValue.length() - 1);
SynapseExpression synapseExpression = createSynapseExpression(attributeValue);
key = new Value(synapseExpression);
} else {
SynapseXPath synXpath = createSynXpath(elem, attributeValue);
key = new Value(synXpath);
Expand Down Expand Up @@ -220,6 +225,17 @@ public SynapseJsonPath createSynJsonPath(String key) {
return synapseJsonPath;
}

private SynapseExpression createSynapseExpression(String key) {
String expression = key.trim().substring(2, key.length() - 1);
SynapseExpression synapseExpression = null;
try {
synapseExpression = new SynapseExpression(expression);
} catch (JaxenException e) {
handleException("Can not create SynapseExpression from given: " + key);
}
return synapseExpression;
}

/**
* Handle exceptions
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,16 @@ public ExpressionResult evaluate(EvaluationContext context, boolean isObjectValu
switch (type) {
case PAYLOAD:
try {
result = context.getJSONResult(isObjectValue, expression);
result = context.getJSONResult(expression);
} catch (PathNotFoundException e) {
// convert jsonPath error to native one
throw new EvaluationException(e.getMessage());
} catch (IOException e) {
throw new EvaluationException("Error while parsing payload");
} catch (JaxenException e) {
throw new EvaluationException("Error while retrieving payload");
} catch (EvaluationException e) {
throw new EvaluationException("Error while fetching the payload, " + e.getMessage());
}
break;
case VARIABLE:
Expand All @@ -135,17 +137,10 @@ public ExpressionResult evaluate(EvaluationContext context, boolean isObjectValu
+ " on non-JSON variable value");
}
try {
if (isObjectValue) {
SynapseJsonPath jsonPath = new SynapseJsonPath(expressionToEvaluate);
result = jsonPath.evaluate(variable.toString());
} else {
result = JsonPath.parse(variable.toString()).read(expressionToEvaluate);
}
result = JsonPath.parse(variable.toString()).read(expressionToEvaluate);
} catch (PathNotFoundException e) {
// convert jsonPath error to native one
throw new EvaluationException(e.getMessage());
} catch (JaxenException e) {
throw new EvaluationException("Error while parsing the expression: " + expressionToEvaluate);
}
}
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@
import org.apache.axiom.om.OMText;
import org.apache.axiom.om.impl.llom.OMTextImpl;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.synapse.MessageContext;
import org.apache.synapse.SynapseConstants;
import org.apache.synapse.commons.json.JsonUtil;
import org.apache.synapse.core.axis2.Axis2MessageContext;
import org.apache.synapse.mediators.template.TemplateContext;
import org.apache.synapse.registry.Registry;
import org.apache.synapse.util.synapse.expression.constants.ExpressionConstants;
import org.apache.synapse.util.synapse.expression.exception.EvaluationException;
import org.apache.synapse.util.xpath.SynapseJsonPath;
import org.apache.synapse.util.xpath.SynapseXPath;
import org.jaxen.JaxenException;
Expand Down Expand Up @@ -73,7 +75,7 @@ public Object getVariable(String name) {
}

// Payload methods
public Object getJSONResult(boolean isObjectValue, String expression) throws IOException, JaxenException {
public Object getJSONResult(String expression) throws IOException, JaxenException {
if (payload == null) {
if (JsonUtil.hasAJsonPayload(((Axis2MessageContext) synCtx).getAxis2MessageContext())) {
payload = IOUtils.toString(Objects.requireNonNull(JsonUtil.getJsonPayload(((Axis2MessageContext) synCtx)
Expand All @@ -84,13 +86,11 @@ public Object getJSONResult(boolean isObjectValue, String expression) throws IOE
SynapseJsonPath jsonPath = new SynapseJsonPath("$.");
payload = jsonPath.stringValueOf(synCtx);
}
if (StringUtils.isEmpty(payload)) {
throw new EvaluationException("Payload is empty");
}
}
if (isObjectValue) {
SynapseJsonPath jsonPath = new SynapseJsonPath(expression);
return jsonPath.evaluate(payload);
} else {
return JsonPath.parse(payload).read(expression);
}
return JsonPath.parse(payload).read(expression);
}

public Object getHeader(String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public String stringValueOf(MessageContext synCtx) {
context.setNamespaceMap(namespaceMap);
context.setSynCtx(synCtx);
ExpressionResult result = evaluateExpression(context, false);
return result != null ? result.asString() : null;
return result != null ? result.asString() : "";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ public void testIfElse() {
Assert.assertEquals("true", TestUtils.evaluateExpression("true ? false ? false : true : false"));
Assert.assertEquals("true", TestUtils.evaluateExpression(" 5 > 4 ? (false ? false : true) : false"));
Assert.assertEquals("5", TestUtils.evaluateExpression(" 5.2 > 5 ? 5 : 4"));
Assert.assertNull(TestUtils.evaluateExpression("\"bla\"? true : 123"));
Assert.assertNull(TestUtils.evaluateExpression("45 == ( 5 + 34 ? true : 456)"));
Assert.assertNull(TestUtils.evaluateExpressionWithPayload("45 == ( $[\"null\"] ? true : 456)", 1));
Assert.assertEquals("", TestUtils.evaluateExpression("\"bla\"? true : 123"));
Assert.assertEquals("", TestUtils.evaluateExpression("45 == ( 5 + 34 ? true : 456)"));
Assert.assertEquals("", TestUtils.evaluateExpressionWithPayload("45 == ( $[\"null\"] ? true : 456)", 1));
Assert.assertEquals("[22.99]", TestUtils.evaluateExpressionWithPayloadAndVariables(
"var.num1 > var.num2 ? $..book[?(@.author =~ /.*Tolkien/i)].price " +
": $..book[(@.\"length\"-1)].title", 2,1));
Assert.assertNull(TestUtils.evaluateExpression("null == $[\"null\"] ? 123 : 456"));
Assert.assertNull(TestUtils.evaluateExpressionWithPayload(
Assert.assertEquals("", TestUtils.evaluateExpression("null == $[\"null\"] ? 123 : 456"));
Assert.assertEquals("", TestUtils.evaluateExpressionWithPayload(
"$[\"null\"] ? 123 : 456", 1));
Assert.assertEquals("123", TestUtils.evaluateExpressionWithPayload(
"$[\"null\"] == null ? 123 : 456",1));
Assert.assertNull(TestUtils.evaluateExpressionWithPayload("null ? 4 : 5", 1));
Assert.assertEquals("", TestUtils.evaluateExpressionWithPayload("null ? 4 : 5", 1));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public void testTransportHeaderWithSpecialChar() throws Exception {
@Test
public void testTransportHeaderNotExist() throws Exception {
SynapseExpression testPath = new SynapseExpression("headers[\"toUpper2\"]");
Assert.assertNull(testPath.stringValueOf(synCtx));
Assert.assertEquals("", testPath.stringValueOf(synCtx));
}

@Test
Expand Down Expand Up @@ -187,9 +187,9 @@ public void testSynapseHeaderInFilter() throws Exception {
@Test
public void testNonExistingEmptyAndNull() throws Exception {
SynapseExpression testPath = new SynapseExpression("props.synapse.nonExisting");
Assert.assertNull(testPath.stringValueOf(synCtx));
Assert.assertEquals("", testPath.stringValueOf(synCtx));
testPath = new SynapseExpression("props.synapse.[\"null\"]");
Assert.assertNull(testPath.stringValueOf(synCtx));
Assert.assertEquals("", testPath.stringValueOf(synCtx));
testPath = new SynapseExpression("props.synapse[\"empty\"]");
Assert.assertEquals("", testPath.stringValueOf(synCtx));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ public class PayloadAndVariableAccessTest {
@Test
public void testPayloadAccess() {
TestUtils.clearMessageContext();
Assert.assertNull(TestUtils.evaluateExpression("payload.name"));
Assert.assertEquals("", TestUtils.evaluateExpression("payload.name"));
Assert.assertEquals("John", TestUtils.evaluateExpressionWithPayload("payload.name", 1));
Assert.assertEquals("John", TestUtils.evaluateExpressionWithPayload("payload[\"name\"]", 1));
Assert.assertEquals("BMW", TestUtils.evaluateExpressionWithPayload("$.cars[1]", 1));
Assert.assertNull(TestUtils.evaluateExpressionWithPayload("$.cars[10]", 1));
Assert.assertEquals("", TestUtils.evaluateExpressionWithPayload("$.cars[10]", 1));
Assert.assertEquals("BMW", TestUtils.evaluateExpressionWithPayload("$.cars[$.index]", 1));
Assert.assertEquals("[\"BMW\",\"Lexus\"]", TestUtils.evaluateExpressionWithPayload(
"$.cars[$.index,4]", 1));
Expand All @@ -46,7 +46,7 @@ public void testPayloadAccess() {
TestUtils.evaluateExpressionWithPayload("payload.[:4]", 3));
Assert.assertEquals("[\"Forget\",\"the\",\"wrong\",\"that\",\"I've\",\"done\"]",
TestUtils.evaluateExpressionWithPayload("payload[4:]", 3));
Assert.assertNull(TestUtils.evaluateExpressionWithPayloadAndVariables(
Assert.assertEquals("", TestUtils.evaluateExpressionWithPayloadAndVariables(
"payload.random", 1, 1));
}

Expand Down Expand Up @@ -143,9 +143,9 @@ public void testVariableAccess() {
Assert.assertEquals("[\"The Lord of the Rings\",\"To Kill a Mockingbird\"]",
TestUtils.evaluateExpressionWithPayloadAndVariables(
"var.[\"json2\"].store.[\"book\"][?(@.price > payload.expensive)].title", 2, 2));
Assert.assertNull(TestUtils.evaluateExpressionWithPayloadAndVariables(
Assert.assertEquals("", TestUtils.evaluateExpressionWithPayloadAndVariables(
"var.random", 0, 1));
Assert.assertNull(TestUtils.evaluateExpressionWithPayloadAndVariables(
Assert.assertEquals("", TestUtils.evaluateExpressionWithPayloadAndVariables(
"var.num1[0]", 0, 1));
}
}
Loading

0 comments on commit 80e83df

Please sign in to comment.