Skip to content

Commit

Permalink
Add function param support
Browse files Browse the repository at this point in the history
Add function param supprot and other improvements.
Related to wso2/product-micro-integrator/issues/3749
  • Loading branch information
GDLMadushanka committed Nov 29, 2024
1 parent 4ea7214 commit 1053e37
Show file tree
Hide file tree
Showing 29 changed files with 620 additions and 428 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -626,59 +626,4 @@ public enum ENDPOINT_TIMEOUT_TYPE { ENDPOINT_TIMEOUT, GLOBAL_TIMEOUT, HTTP_CONNE

public static final String ANALYTICS_METADATA = "ANALYTICS_METADATA";

// Constants related to Synapse Expressions
public static final String AND = "and";
public static final String OR = "or";
public static final String NOT = "not";
public static final String TO_LOWER = "toLower";
public static final String TO_UPPER = "toUpper";
public static final String LENGTH = "length";
public static final String SUBSTRING = "subString";
public static final String STARTS_WITH = "startsWith";
public static final String ENDS_WITH = "endsWith";
public static final String CONTAINS = "contains";
public static final String TRIM = "trim";
public static final String REPLACE = "replace";
public static final String SPLIT = "split";
public static final String INDEX_OF = "indexOf";
public static final String ABS = "abs";
public static final String CEIL = "ceil";
public static final String FLOOR = "floor";
public static final String SQRT = "sqrt";
public static final String LOG = "log";
public static final String POW = "pow";
public static final String B64ENCODE = "base64encode";
public static final String B64DECODE = "base64decode";
public static final String URL_ENCODE = "urlEncode";
public static final String URL_DECODE = "urlDecode";
public static final String IS_NUMBER = "isNumber";
public static final String IS_STRING = "isString";
public static final String IS_ARRAY = "isArray";
public static final String IS_OBJECT = "isObject";
public static final String OBJECT = "object";
public static final String ARRAY = "array";
public static final String REGISTRY = "registry";
public static final String EXISTS = "exists";
public static final String XPATH = "xpath";
public static final String SECRET = "secret";
public static final String NOW = "now";
public static final String FORMAT_DATE_TIME = "formatDateTime";
public static final String CHAR_AT = "charAt";

public static final String ROUND = "round";
public static final String INTEGER = "integer";
public static final String FLOAT = "float";
public static final String STRING = "string";
public static final String BOOLEAN = "boolean";

public static final String PAYLOAD = "payload";
public static final String PAYLOAD_$ = "$";
public static final String SYNAPSE_EXPRESSION_IDENTIFIER_START = "${";
public static final String SYNAPSE_EXPRESSION_IDENTIFIER_END = "}";
public static final String AXIS2 = "axis2";
public static final String QUERY_PARAM = "queryParams";
public static final String URI_PARAM = "uriParams";

public static final String UNKNOWN = "unknown";
public static final String VAULT_LOOKUP = "wso2:vault-lookup('";
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
import org.apache.axiom.om.OMAttribute;
import org.apache.axiom.om.OMElement;
import org.apache.synapse.Mediator;
import org.apache.synapse.SynapseConstants;
import org.apache.synapse.SynapseException;
import org.apache.synapse.mediators.Value;
import org.apache.synapse.mediators.builtin.PropertyMediator;
import org.apache.synapse.util.MediatorPropertyUtils;
import org.apache.synapse.util.synapse.expression.constants.ExpressionConstants;
import org.apache.synapse.util.xpath.SynapseJsonPath;
import org.apache.synapse.util.xpath.SynapseExpression;
import org.apache.synapse.util.xpath.SynapseXPath;
Expand Down Expand Up @@ -85,8 +85,8 @@ public Mediator createSpecificMediator(OMElement elem, Properties properties) {
String nameExpression = nameAttributeValue.substring(1, nameAttributeValue.length() - 1);
if(nameExpression.startsWith("json-eval(")) {
new SynapseJsonPath(nameExpression.substring(10, nameExpression.length() - 1));
} else if (nameExpression.startsWith(SynapseConstants.SYNAPSE_EXPRESSION_IDENTIFIER_START) &&
nameExpression.endsWith(SynapseConstants.SYNAPSE_EXPRESSION_IDENTIFIER_END)) {
} else if (nameExpression.startsWith(ExpressionConstants.SYNAPSE_EXPRESSION_IDENTIFIER_START) &&
nameExpression.endsWith(ExpressionConstants.SYNAPSE_EXPRESSION_IDENTIFIER_END)) {
new SynapseExpression(nameExpression.substring(2, nameExpression.length() - 1));
} else {
new SynapseXPath(nameExpression);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import org.apache.axiom.om.xpath.AXIOMXPath;
import org.apache.commons.logging.Log;
import org.apache.synapse.MessageContext;
import org.apache.synapse.SynapseConstants;
import org.apache.synapse.SynapseException;
import org.apache.synapse.transport.util.MessageHandlerProvider;
import org.apache.synapse.transport.passthru.PassThroughConstants;
import org.apache.synapse.transport.passthru.Pipe;
import org.apache.synapse.transport.passthru.config.PassThroughConfiguration;
import org.apache.synapse.util.streaming_xpath.custom.components.ParserComponent;
import org.apache.synapse.util.synapse.expression.constants.ExpressionConstants;
import org.apache.synapse.util.xpath.DOMSynapseXPathNamespaceMap;
import org.jaxen.JaxenException;

Expand Down Expand Up @@ -63,8 +63,8 @@ public SynapsePath(String path, String pathType, Log log) throws JaxenException
private String inferPathType(String expression) {
if (expression.startsWith("json-eval(")) {
return JSON_PATH;
} else if (expression.startsWith(SynapseConstants.SYNAPSE_EXPRESSION_IDENTIFIER_START)
&& expression.endsWith(SynapseConstants.SYNAPSE_EXPRESSION_IDENTIFIER_END)) {
} else if (expression.startsWith(ExpressionConstants.SYNAPSE_EXPRESSION_IDENTIFIER_START)
&& expression.endsWith(ExpressionConstants.SYNAPSE_EXPRESSION_IDENTIFIER_END)) {
return SYNAPSE_EXPRESSIONS_PATH;
} else {
return X_PATH;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.synapse.SynapseConstants;
import org.apache.synapse.SynapseException;
import org.apache.synapse.config.SynapsePropertiesLoader;
import org.apache.synapse.util.synapse.expression.constants.ExpressionConstants;
import org.apache.synapse.util.xpath.SynapseJsonPath;
import org.apache.synapse.util.xpath.SynapseExpression;
import org.apache.synapse.util.xpath.SynapseXPath;
Expand All @@ -50,8 +51,8 @@ public static org.apache.synapse.config.xml.SynapsePath getSynapsePath(OMElement

if(pathAttrib.getAttributeValue().startsWith("json-eval(")) {
path = new SynapseJsonPath(pathAttrib.getAttributeValue().substring(10, pathAttrib.getAttributeValue().length() - 1));
} else if (pathAttrib.getAttributeValue().startsWith(SynapseConstants.SYNAPSE_EXPRESSION_IDENTIFIER_START) &&
pathAttrib.getAttributeValue().endsWith(SynapseConstants.SYNAPSE_EXPRESSION_IDENTIFIER_END)) {
} else if (pathAttrib.getAttributeValue().startsWith(ExpressionConstants.SYNAPSE_EXPRESSION_IDENTIFIER_START) &&
pathAttrib.getAttributeValue().endsWith(ExpressionConstants.SYNAPSE_EXPRESSION_IDENTIFIER_END)) {
path = new SynapseExpression(pathAttrib.getAttributeValue().substring(2, pathAttrib.getAttributeValue().length() - 1));
} else {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.synapse.MessageContext;
import org.apache.synapse.SynapseConstants;
import org.apache.synapse.SynapseException;
import org.apache.synapse.config.xml.SynapsePath;
import org.apache.synapse.util.synapse.expression.constants.ExpressionConstants;
import org.apache.synapse.util.xpath.SynapseJsonPath;
import org.apache.synapse.util.xpath.SynapseXPath;
import org.apache.synapse.util.xpath.SynapseExpression;
Expand Down Expand Up @@ -103,8 +103,8 @@ public SynapsePath getExpression() {
new SynapseJsonPath(expressionString.substring(10, expressionString.length() - 1));
expression = expressionTypeKey;

} else if (expressionString.startsWith(SynapseConstants.SYNAPSE_EXPRESSION_IDENTIFIER_START) &&
expressionString.endsWith(SynapseConstants.SYNAPSE_EXPRESSION_IDENTIFIER_END)) {
} else if (expressionString.startsWith(ExpressionConstants.SYNAPSE_EXPRESSION_IDENTIFIER_START) &&
expressionString.endsWith(ExpressionConstants.SYNAPSE_EXPRESSION_IDENTIFIER_END)) {
expression = new SynapseExpression(
expressionString.substring(2, expressionString.length() - 1));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public List<ExpressionNode> getArguments() {
}

@Override
public ExpressionResult evaluate(EvaluationContext context) {
public ExpressionResult evaluate(EvaluationContext context, boolean isObjectValue) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ public ArrayIndexNode(ArgumentListNode arguments, char separator) {
}

@Override
public ExpressionResult evaluate(EvaluationContext context) {
public ExpressionResult evaluate(EvaluationContext context, boolean isObjectValue) {
List<String> indexList = new ArrayList<>();
for (ExpressionNode index : indexArray) {
if (index == null) {
indexList.add("");
continue;
}
ExpressionResult result = index.evaluate(context);
ExpressionResult result = index.evaluate(context, isObjectValue);
if (result != null) {
indexList.add(result.asString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ public BinaryOperationNode(ExpressionNode left, String operator, ExpressionNode
}

@Override
public ExpressionResult evaluate(EvaluationContext context) throws EvaluationException {
ExpressionResult leftValue = left.evaluate(context);
ExpressionResult rightValue = right.evaluate(context);
public ExpressionResult evaluate(EvaluationContext context, boolean isObjectValue) throws EvaluationException {
ExpressionResult leftValue = left.evaluate(context, isObjectValue);
ExpressionResult rightValue = right.evaluate(context, isObjectValue);
if ((leftValue == null || rightValue == null) &&
(operator != Operator.EQUALS && operator != Operator.NOT_EQUALS)) {
throw new EvaluationException("Null inputs for " + operator + " operation: " + leftValue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@ public ConditionalExpressionNode(ExpressionNode condition, ExpressionNode trueEx
}

@Override
public ExpressionResult evaluate(EvaluationContext context) {
ExpressionResult conditionResult = condition.evaluate(context);
public ExpressionResult evaluate(EvaluationContext context, boolean isObjectValue) throws EvaluationException {
ExpressionResult conditionResult = condition.evaluate(context, isObjectValue);
if (conditionResult == null || conditionResult.isNull()) {
throw new EvaluationException("Condition is null in conditional expression");
}
if (conditionResult.isBoolean()) {
return conditionResult.asBoolean() ? trueExpression.evaluate(context) : falseExpression.evaluate(context);
return conditionResult.asBoolean() ? trueExpression.evaluate(context, isObjectValue)
: falseExpression.evaluate(context, isObjectValue);
} else {
throw new EvaluationException("Condition is not a boolean in conditional expression");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@
* Represents a node in the AST.
*/
public interface ExpressionNode {
ExpressionResult evaluate(EvaluationContext context) throws EvaluationException;
ExpressionResult evaluate(EvaluationContext context, boolean isObjectValue) throws EvaluationException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@
import com.google.gson.JsonSyntaxException;
import com.google.gson.internal.LazilyParsedNumber;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMNode;
import org.apache.synapse.util.synapse.expression.exception.EvaluationException;

import java.util.List;

/**
* This class represents the result of an expression evaluation.
* It can hold values of different types such as String, Number, Boolean, JsonElement, and null.
Expand All @@ -44,7 +47,11 @@ public ExpressionResult(String value) {
this.value = value;
}

public ExpressionResult(OMElement value) {
public ExpressionResult(OMNode value) {
this.value = value;
}

public ExpressionResult(List value) {
this.value = value;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ public FilterExpressionNode(String expression, Map<String, ExpressionNode> argum
* Not evaluating here.
*/
@Override
public ExpressionResult evaluate(EvaluationContext context) {
public ExpressionResult evaluate(EvaluationContext context, boolean isObjectValue) {
for (Map.Entry<String, ExpressionNode> entry : arguments.entrySet()) {
if (entry.getValue() != null) {
ExpressionResult result = entry.getValue().evaluate(context);
ExpressionResult result = entry.getValue().evaluate(context, isObjectValue);
if (result != null) {
String regex = ExpressionUtils.escapeSpecialCharacters(entry.getKey());
String resultString = result.asString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.synapse.SynapseConstants;
import org.apache.synapse.commons.property.PropertyHolder;
import org.apache.synapse.commons.resolvers.ResolverException;
import org.apache.synapse.util.synapse.expression.constants.ExpressionConstants;
import org.apache.synapse.util.synapse.expression.context.EvaluationContext;
import org.apache.synapse.util.synapse.expression.exception.EvaluationException;

Expand All @@ -34,8 +35,7 @@ public enum Type {
HEADER,
PROPERTY,
CONFIG,
QUERY_PARAM,
PATH_PARAM
FUNCTION_PARAM
}

private final Type type;
Expand All @@ -56,9 +56,9 @@ public HeadersAndPropertiesAccessNode(ExpressionNode node, String scope) {
}

@Override
public ExpressionResult evaluate(EvaluationContext context) {
public ExpressionResult evaluate(EvaluationContext context, boolean isObjectValue) throws EvaluationException {
if (key != null) {
String name = key.evaluate(context).asString();
String name = key.evaluate(context, isObjectValue).asString();
Object value;
if (Type.HEADER.equals(type)) {
value = context.getHeader(name);
Expand All @@ -68,16 +68,22 @@ public ExpressionResult evaluate(EvaluationContext context) {
} catch (ResolverException e) {
throw new EvaluationException("The value of the key:[" + name + "] is null");
}
} else if (Type.FUNCTION_PARAM.equals(type)) {
value = context.getFunctionParam(name);
} else {
if (SynapseConstants.URI_PARAM.equals(scope)) {
if (ExpressionConstants.URI_PARAM.equals(scope)) {
value = context.getProperty("uri.var." + name, SynapseConstants.SYNAPSE);
} else if (SynapseConstants.QUERY_PARAM.equals(scope)) {
} else if (ExpressionConstants.QUERY_PARAM.equals(scope)) {
value = context.getProperty("query.param." + name, SynapseConstants.SYNAPSE);
} else {
value = context.getProperty(name, scope);
}
}
return new ExpressionResult(value != null ? value.toString() : null);
if (value != null) {
return new ExpressionResult(value.toString());
} else {
throw new EvaluationException("Could not fetch the value of the key: " + name);
}
}
throw new EvaluationException("Key cannot be null when accessing headers or properties");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public LiteralNode(ArgumentListNode value, Type type) {
}

@Override
public ExpressionResult evaluate(EvaluationContext context) {
public ExpressionResult evaluate(EvaluationContext context, boolean isObjectValue) {
switch (type) {
case NUMBER:
return parseNumber(value);
Expand All @@ -61,7 +61,7 @@ public ExpressionResult evaluate(EvaluationContext context) {
case NULL:
return null;
case ARRAY:
return parseArray(context);
return parseArray(context, isObjectValue);
default:
throw new IllegalArgumentException("Unsupported type: " + type);
}
Expand All @@ -83,10 +83,10 @@ private ExpressionResult parseNumber(String value) {
}
}

private ExpressionResult parseArray(EvaluationContext context) {
private ExpressionResult parseArray(EvaluationContext context, boolean isObjectValue) {
JsonArray jsonArray = new JsonArray();
for (ExpressionNode expressionNode : parameterList.getArguments()) {
ExpressionResult result = expressionNode.evaluate(context);
ExpressionResult result = expressionNode.evaluate(context, isObjectValue);
if (result.getType().equals(JsonElement.class)) {
jsonArray.add(result.asJsonElement());
} else if (result.isInteger()) {
Expand Down
Loading

0 comments on commit 1053e37

Please sign in to comment.