Skip to content

Commit

Permalink
Merge pull request #2264 from GDLMadushanka/master2
Browse files Browse the repository at this point in the history
Improve logging in Synapse Expressions
  • Loading branch information
GDLMadushanka authored Dec 13, 2024
2 parents 5174173 + ce97244 commit 9b9748f
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import org.apache.synapse.util.resolver.ResourceMap;
import org.apache.synapse.util.resolver.UserDefinedXmlSchemaURIResolver;
import org.apache.synapse.util.xpath.SourceXPathSupport;
import org.apache.synapse.util.xpath.SynapseExpression;
import org.apache.synapse.util.xpath.SynapseJsonPath;
import org.apache.synapse.util.xpath.SynapseXPath;
import org.xml.sax.SAXException;
Expand Down Expand Up @@ -279,10 +280,11 @@ public boolean mediate(MessageContext synCtx) {
String jsonPayload = null;
if (sourcePath != null) {
//evaluating
if (sourcePath instanceof SynapseJsonPath) {
if (sourcePath instanceof SynapseJsonPath || sourcePath instanceof SynapseExpression) {
jsonPayload = sourcePath.stringValueOf(synCtx);
} else {
handleException("Could not find the JSONPath evaluator for Source", synCtx);
handleException("Could not find JSONPath or Synapse Expression to extract the message " +
"to validate from the payload", synCtx);
}
} else {
jsonPayload = JsonUtil.jsonPayloadToString(a2mc);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,12 @@
*/
package org.apache.synapse.util.synapse.expression.ast;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.synapse.util.synapse.expression.context.EvaluationContext;

/**
* Represents a node in the AST that holds a signed expression. ex: ( -var.num1 )
*/
public class SignedExpressionNode implements ExpressionNode {
private static final Log log = LogFactory.getLog(SignedExpressionNode.class);
private final ExpressionNode expression;
private final boolean signed;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
package org.apache.synapse.util.synapse.expression.visitor;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.synapse.SynapseConstants;
import org.apache.synapse.util.synapse.expression.ast.*;
import org.apache.synapse.util.synapse.expression.constants.ExpressionConstants;
Expand All @@ -35,8 +37,13 @@
public class ExpressionVisitor extends ExpressionParserBaseVisitor<ExpressionNode>
implements ExpressionParserVisitor<ExpressionNode> {

private static final Log log = LogFactory.getLog(ExpressionVisitor.class);

@Override
public ExpressionNode visitExpression(ExpressionParser.ExpressionContext ctx) {
if (log.isDebugEnabled()) {
log.debug("Visiting expression: " + ctx.getText());
}
if (ctx.comparisonExpression() != null) {
return visitComparisonExpression(ctx.comparisonExpression());
} else if (ctx.conditionalExpression() != null) {
Expand All @@ -47,6 +54,9 @@ public ExpressionNode visitExpression(ExpressionParser.ExpressionContext ctx) {

@Override
public ExpressionNode visitComparisonExpression(ExpressionParser.ComparisonExpressionContext ctx) {
if (log.isDebugEnabled()) {
log.debug("Visiting comparison expression: " + ctx.getText());
}
if (ctx.logicalExpression() != null) {
if (ctx.logicalExpression().size() == 1) {
return visitLogicalExpression(ctx.logicalExpression().get(0));
Expand All @@ -64,6 +74,9 @@ public ExpressionNode visitComparisonExpression(ExpressionParser.ComparisonExpre

@Override
public ExpressionNode visitLogicalExpression(ExpressionParser.LogicalExpressionContext ctx) {
if (log.isDebugEnabled()) {
log.debug("Visiting logical expression: " + ctx.getText());
}
if (ctx.arithmeticExpression() != null) {
ExpressionNode left = visit(ctx.arithmeticExpression());
if (ctx.logicalExpression() != null && ctx.getChild(1) != null) {
Expand All @@ -77,6 +90,9 @@ public ExpressionNode visitLogicalExpression(ExpressionParser.LogicalExpressionC

@Override
public ExpressionNode visitArithmeticExpression(ExpressionParser.ArithmeticExpressionContext ctx) {
if (log.isDebugEnabled()) {
log.debug("Visiting arithmetic expression: " + ctx.getText());
}
if (ctx.term() != null) {
if (ctx.term().size() == 1) {
return visit(ctx.term().get(0));
Expand All @@ -94,6 +110,9 @@ public ExpressionNode visitArithmeticExpression(ExpressionParser.ArithmeticExpre

@Override
public ExpressionNode visitTerm(ExpressionParser.TermContext ctx) {
if (log.isDebugEnabled()) {
log.debug("Visiting term: " + ctx.getText());
}
if (ctx.factor() != null) {
if (ctx.factor().size() == 1) {
return visit(ctx.factor().get(0));
Expand All @@ -111,6 +130,9 @@ public ExpressionNode visitTerm(ExpressionParser.TermContext ctx) {

@Override
public ExpressionNode visitFactor(ExpressionParser.FactorContext ctx) {
if (log.isDebugEnabled()) {
log.debug("Visiting factor: " + ctx.getText());
}
if (ctx.literal() != null) {
return visit(ctx.literal());
} else if (ctx.functionCall() != null) {
Expand All @@ -135,6 +157,9 @@ public ExpressionNode visitFactor(ExpressionParser.FactorContext ctx) {

@Override
public ExpressionNode visitFunctionCall(ExpressionParser.FunctionCallContext ctx) {
if (log.isDebugEnabled()) {
log.debug("Visiting function call: " + ctx.getText());
}
ArgumentListNode parameterList = new ArgumentListNode();
if (ctx.expression() != null) {
for (ExpressionParser.ExpressionContext expressionContext : ctx.expression()) {
Expand Down Expand Up @@ -263,6 +288,9 @@ public ExpressionNode visitFunctionCall(ExpressionParser.FunctionCallContext ctx

@Override
public ExpressionNode visitLiteral(ExpressionParser.LiteralContext ctx) {
if (log.isDebugEnabled()) {
log.debug("Visiting literal: " + ctx.getText());
}
if (ctx.NUMBER() != null) {
return new LiteralNode(ctx.NUMBER().getText(), LiteralNode.Type.NUMBER);
} else if (ctx.BOOLEAN_LITERAL() != null) {
Expand All @@ -279,6 +307,9 @@ public ExpressionNode visitLiteral(ExpressionParser.LiteralContext ctx) {

@Override
public ExpressionNode visitArrayLiteral(ExpressionParser.ArrayLiteralContext ctx) {
if (log.isDebugEnabled()) {
log.debug("Visiting array literal: " + ctx.getText());
}
if (ctx.expression() != null) {
ArgumentListNode parameterList = new ArgumentListNode();
for (ExpressionParser.ExpressionContext expressionContext : ctx.expression()) {
Expand All @@ -291,6 +322,9 @@ public ExpressionNode visitArrayLiteral(ExpressionParser.ArrayLiteralContext ctx

@Override
public ExpressionNode visitVariableAccess(ExpressionParser.VariableAccessContext ctx) {
if (log.isDebugEnabled()) {
log.debug("Visiting variable access: " + ctx.getText());
}
Map<String, ExpressionNode> expressionNodeMap = new HashMap<>();
if (ctx.jsonPathExpression() != null) {
expressionNodeMap = visitJsonPath(ctx.jsonPathExpression());
Expand All @@ -300,6 +334,9 @@ public ExpressionNode visitVariableAccess(ExpressionParser.VariableAccessContext

@Override
public ExpressionNode visitPayloadAccess(ExpressionParser.PayloadAccessContext ctx) {
if (log.isDebugEnabled()) {
log.debug("Visiting payload access: " + ctx.getText());
}
Map<String, ExpressionNode> expressionNodeMap = new HashMap<>();
if (ctx.jsonPathExpression() != null) {
expressionNodeMap = visitJsonPath(ctx.jsonPathExpression());
Expand All @@ -308,6 +345,9 @@ public ExpressionNode visitPayloadAccess(ExpressionParser.PayloadAccessContext c
}

public Map<String, ExpressionNode> visitJsonPath(ExpressionParser.JsonPathExpressionContext ctx) {
if (log.isDebugEnabled()) {
log.debug("Visiting json path: " + ctx.getText());
}
Map<String, ExpressionNode> expressionNodeMap = new HashMap<>();
if (ctx.arrayIndex() != null) {
for (ExpressionParser.ArrayIndexContext expressionContext : ctx.arrayIndex()) {
Expand All @@ -319,6 +359,9 @@ public Map<String, ExpressionNode> visitJsonPath(ExpressionParser.JsonPathExpres

public ExpressionNode visitJsonPathAfterPayload(ExpressionParser.JsonPathExpressionContext ctx,
PredefinedFunctionNode functionNode, PayloadAccessNode.Type type) {
if (log.isDebugEnabled()) {
log.debug("Visiting json path after payload: " + ctx.getText());
}
Map<String, ExpressionNode> expressionNodeMap = new HashMap<>();
if (ctx.arrayIndex() != null) {
for (ExpressionParser.ArrayIndexContext expressionContext : ctx.arrayIndex()) {
Expand All @@ -330,6 +373,9 @@ public ExpressionNode visitJsonPathAfterPayload(ExpressionParser.JsonPathExpress

@Override
public ExpressionNode visitArrayIndex(ExpressionParser.ArrayIndexContext ctx) {
if (log.isDebugEnabled()) {
log.debug("Visiting array index: " + ctx.getText());
}
if (ctx.NUMBER() != null) {
return new LiteralNode(ctx.NUMBER().getText(), LiteralNode.Type.NUMBER);
} else if (ctx.STRING_LITERAL() != null) {
Expand All @@ -352,6 +398,9 @@ public ExpressionNode visitArrayIndex(ExpressionParser.ArrayIndexContext ctx) {

@Override
public ExpressionNode visitMultipleArrayIndices(ExpressionParser.MultipleArrayIndicesContext ctx) {
if (log.isDebugEnabled()) {
log.debug("Visiting multiple array indices: " + ctx.getText());
}
if (ctx.expression() != null) {
ArgumentListNode expressionNodes = new ArgumentListNode();
for (ExpressionParser.ExpressionContext expressionContext : ctx.expression()) {
Expand All @@ -364,6 +413,9 @@ public ExpressionNode visitMultipleArrayIndices(ExpressionParser.MultipleArrayIn

@Override
public ExpressionNode visitSliceArrayIndex(ExpressionParser.SliceArrayIndexContext ctx) {
if (log.isDebugEnabled()) {
log.debug("Visiting slice array index: " + ctx.getText());
}
if (ctx.signedExpressions() != null) {
ArgumentListNode expressionNodes = new ArgumentListNode();
if (ctx.getChildCount() == 2 && ctx.getChild(0).getText().equals(":")) {
Expand All @@ -382,6 +434,9 @@ public ExpressionNode visitSliceArrayIndex(ExpressionParser.SliceArrayIndexConte

@Override
public ExpressionNode visitSignedExpressions(ExpressionParser.SignedExpressionsContext ctx) {
if (log.isDebugEnabled()) {
log.debug("Visiting signed expressions: " + ctx.getText());
}
if (ctx.expression() != null) {
if (ctx.MINUS() != null) {
return new SignedExpressionNode(visit(ctx.expression()), true);
Expand All @@ -394,6 +449,9 @@ public ExpressionNode visitSignedExpressions(ExpressionParser.SignedExpressionsC

@Override
public ExpressionNode visitFilterExpression(ExpressionParser.FilterExpressionContext ctx) {
if (log.isDebugEnabled()) {
log.debug("Visiting filter expression: " + ctx.getText());
}
Map<String, ExpressionNode> expressionNodeMap = new HashMap<>();
if (ctx.filterComponent() != null) {
for (ExpressionParser.FilterComponentContext filterExpressionContext : ctx.filterComponent()) {
Expand All @@ -405,6 +463,9 @@ public ExpressionNode visitFilterExpression(ExpressionParser.FilterExpressionCon

@Override
public ExpressionNode visitFilterComponent(ExpressionParser.FilterComponentContext ctx) {
if (log.isDebugEnabled()) {
log.debug("Visiting filter component: " + ctx.getText());
}
if (ctx.payloadAccess() != null) {
return visit(ctx.payloadAccess());
} else if (ctx.stringOrOperator() != null) {
Expand All @@ -427,6 +488,9 @@ public ExpressionNode visitFilterComponent(ExpressionParser.FilterComponentConte

@Override
public ExpressionNode visitHeaderAccess(ExpressionParser.HeaderAccessContext ctx) {
if (log.isDebugEnabled()) {
log.debug("Visiting header access: " + ctx.getText());
}
if (ctx.propertyName() != null) {
return new HeadersAndPropertiesAccessNode(visit(ctx.propertyName()),
HeadersAndPropertiesAccessNode.Type.HEADER);
Expand All @@ -436,6 +500,9 @@ public ExpressionNode visitHeaderAccess(ExpressionParser.HeaderAccessContext ctx

@Override
public ExpressionNode visitPropertyName(ExpressionParser.PropertyNameContext ctx) {
if (log.isDebugEnabled()) {
log.debug("Visiting property name: " + ctx.getText());
}
if (ctx.ID() != null) {
return new LiteralNode(ctx.ID().getText(), LiteralNode.Type.STRING);
} else if (ctx.STRING_LITERAL() != null) {
Expand All @@ -446,6 +513,9 @@ public ExpressionNode visitPropertyName(ExpressionParser.PropertyNameContext ctx

@Override
public ExpressionNode visitConfigAccess(ExpressionParser.ConfigAccessContext ctx) {
if (log.isDebugEnabled()) {
log.debug("Visiting config access: " + ctx.getText());
}
if (ctx.propertyName() != null) {
return new HeadersAndPropertiesAccessNode(visit(ctx.propertyName()),
HeadersAndPropertiesAccessNode.Type.CONFIG);
Expand All @@ -455,6 +525,9 @@ public ExpressionNode visitConfigAccess(ExpressionParser.ConfigAccessContext ctx

@Override
public ExpressionNode visitPropertyAccess(ExpressionParser.PropertyAccessContext ctx) {
if (log.isDebugEnabled()) {
log.debug("Visiting property access: " + ctx.getText());
}
if (ctx.propertyName() != null) {
if (ctx.ID() != null) {
String scope = ctx.ID().getText();
Expand All @@ -471,6 +544,9 @@ public ExpressionNode visitPropertyAccess(ExpressionParser.PropertyAccessContext

@Override
public ExpressionNode visitParameterAccess(ExpressionParser.ParameterAccessContext ctx) {
if (log.isDebugEnabled()) {
log.debug("Visiting parameter access: " + ctx.getText());
}
if (ctx.propertyName() != null) {
if (ctx.ID() != null) {
String scope = ctx.ID().getText();
Expand All @@ -489,9 +565,11 @@ public ExpressionNode visitParameterAccess(ExpressionParser.ParameterAccessConte
return null;
}


@Override
public ExpressionNode visitConditionalExpression(ExpressionParser.ConditionalExpressionContext ctx) {
if (log.isDebugEnabled()) {
log.debug("Visiting conditional expression: " + ctx.getText());
}
ExpressionNode condition = null;
if (ctx.comparisonExpression() != null) {
condition = visit(ctx.comparisonExpression());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Represents a Synapse Expression
Expand All @@ -51,7 +49,7 @@ public class SynapseExpression extends SynapsePath {
private static final Log log = LogFactory.getLog(SynapseExpression.class);
private final ExpressionNode expressionNode;
private final Map<String, String> namespaceMap = new HashMap<>();
private boolean isContentAware = false;
private final boolean isContentAware;

public SynapseExpression(String synapseExpression) throws JaxenException {
super(synapseExpression, org.apache.synapse.config.xml.SynapsePath.JSON_PATH, log);
Expand Down Expand Up @@ -79,6 +77,9 @@ public SynapseExpression(String synapseExpression) throws JaxenException {

@Override
public String stringValueOf(MessageContext synCtx) {
if (log.isDebugEnabled()) {
log.debug("Evaluating expression (stringValueOf): " + expression);
}
EvaluationContext context = new EvaluationContext();
context.setNamespaceMap(namespaceMap);
context.setSynCtx(synCtx);
Expand All @@ -88,11 +89,14 @@ public String stringValueOf(MessageContext synCtx) {

@Override
public Object objectValueOf(MessageContext synCtx) {
if (log.isDebugEnabled()) {
log.debug("Evaluating expression (objectValueOf): " + expression);
}
EvaluationContext context = new EvaluationContext();
context.setNamespaceMap(namespaceMap);
context.setSynCtx(synCtx);
ExpressionResult result = evaluateExpression(context, true);
return result != null ? result.getValue() : null;
return result != null ? result.getValue() : "";
}

private ExpressionResult evaluateExpression(EvaluationContext context, boolean isObjectValue) {
Expand Down

0 comments on commit 9b9748f

Please sign in to comment.