From 9b6624911050357b33b5e1a02155f29402280cfc Mon Sep 17 00:00:00 2001 From: Sanoj Punchihewa Date: Thu, 14 Nov 2024 10:51:47 +0530 Subject: [PATCH] Add synapse expression support for the log mediator --- .../mediators/builtin/LogMediator.java | 19 +++++-- .../synapse/util/InlineExpressionUtil.java | 49 ++++++++++++------- 2 files changed, 45 insertions(+), 23 deletions(-) diff --git a/modules/core/src/main/java/org/apache/synapse/mediators/builtin/LogMediator.java b/modules/core/src/main/java/org/apache/synapse/mediators/builtin/LogMediator.java index 671c26be0b..b313e4fc03 100644 --- a/modules/core/src/main/java/org/apache/synapse/mediators/builtin/LogMediator.java +++ b/modules/core/src/main/java/org/apache/synapse/mediators/builtin/LogMediator.java @@ -29,17 +29,15 @@ import org.apache.synapse.SynapseLog; import org.apache.synapse.commons.json.JsonUtil; import org.apache.synapse.commons.CorrelationConstants; -import org.apache.synapse.config.xml.SynapsePath; import org.apache.synapse.core.axis2.Axis2MessageContext; import org.apache.synapse.mediators.AbstractMediator; import org.apache.synapse.mediators.MediatorProperty; import org.apache.synapse.util.InlineExpressionUtil; +import org.jaxen.JaxenException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; -import java.util.regex.Matcher; -import java.util.regex.Pattern; /** * Logs the specified message into the configured logger. The log levels specify @@ -319,14 +317,25 @@ private String trimLeadingSeparator(StringBuffer sb) { private String processMessageTemplate(MessageContext synCtx, String template) { StringBuffer result = new StringBuffer(); - result.append(InlineExpressionUtil.processInLineTemplate(synCtx, template)); + try { + result.append(InlineExpressionUtil.processInLineSynapseExpressionTemplate(synCtx, template)); + } catch (JaxenException e) { + handleException("Failed to process the message template : " + template, e, synCtx); + } setCustomProperties(result, synCtx); return result.toString(); } @Override public boolean isContentAware() { - // TODO Use the new simplified expression model to check if the log mediator is content aware + if (logLevel == MESSAGE_TEMPLATE) { + for (MediatorProperty property : properties) { + if (property.getExpression() != null && property.getExpression().isContentAware()) { + return true; + } + } + return InlineExpressionUtil.isInlineSynapseExpressionsContentAware(messageTemplate); + } if (logLevel == CUSTOM) { for (MediatorProperty property : properties) { if (property.getExpression() != null && property.getExpression().isContentAware()) { diff --git a/modules/core/src/main/java/org/apache/synapse/util/InlineExpressionUtil.java b/modules/core/src/main/java/org/apache/synapse/util/InlineExpressionUtil.java index c7a7548482..616d44c510 100644 --- a/modules/core/src/main/java/org/apache/synapse/util/InlineExpressionUtil.java +++ b/modules/core/src/main/java/org/apache/synapse/util/InlineExpressionUtil.java @@ -30,6 +30,8 @@ import org.apache.synapse.MessageContext; import org.apache.synapse.SynapseException; import org.apache.synapse.config.xml.SynapsePath; +import org.apache.synapse.config.xml.SynapsePathFactory; +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; @@ -49,8 +51,8 @@ public final class InlineExpressionUtil { // Regex to identify expressions in inline text private static final Pattern EXPRESSION_PATTERN = Pattern.compile("(\\{[^\\s\",<>}\\]]+})"); - // Regex to identify synapse expressions #[expression] in inline text - private static final Pattern EXPRESSION_PLACEHOLDER_PATTERN = Pattern.compile("#\\[(.+?)\\]"); + // Regex to identify synapse expressions ${expression} in inline text + private static final Pattern SYNAPSE_EXPRESSION_PLACEHOLDER_PATTERN = Pattern.compile("\\$\\{(.+?)}"); private InlineExpressionUtil() { @@ -201,6 +203,27 @@ private static boolean isValidXML(String stringToValidate) { return false; } + /** + * Checks whether inline text contains synapse expressions + * Inline expressions will be denoted inside ${} + * e.g.: ${var.var1}, ${payload.element.id} + * + * @param inlineText Inline text string + * @return true if the string contains inline synapse expressions, false otherwise + */ + public static boolean isInlineSynapseExpressionsContentAware(String inlineText) { + + Matcher matcher = SYNAPSE_EXPRESSION_PLACEHOLDER_PATTERN.matcher(inlineText); + while (matcher.find()) { + // Extract the expression inside ${...} + String placeholder = matcher.group(1); + if (placeholder.contains("xpath(") || placeholder.contains("payload.") || placeholder.contains("$.")) { + return true; + } + } + return false; + } + /** * Process the inline template and replace the synapse expressions with the resolved values * @@ -208,29 +231,19 @@ private static boolean isValidXML(String stringToValidate) { * @param template Inline template * @return Processed inline template */ - public static String processInLineTemplate(MessageContext synCtx, String template) { + public static String processInLineSynapseExpressionTemplate(MessageContext synCtx, String template) + throws JaxenException { - Matcher matcher = EXPRESSION_PLACEHOLDER_PATTERN.matcher(template); + Matcher matcher = SYNAPSE_EXPRESSION_PLACEHOLDER_PATTERN.matcher(template); StringBuffer result = new StringBuffer(); while (matcher.find()) { - // Extract the expression inside #[...] + // Extract the expression inside ${...} String placeholder = matcher.group(1); - // Dummy resolver for expressions to test the Log mediator - // TODO update the #getDynamicValue method with synapse expressions and replace this - String replacement = ExpressionResolver.resolve(placeholder, synCtx); + SynapseExpression expression = new SynapseExpression(placeholder); + String replacement = expression.stringValueOf(synCtx); matcher.appendReplacement(result, Matcher.quoteReplacement(replacement)); } matcher.appendTail(result); return result.toString(); } - - static class ExpressionResolver { - public static String resolve(String expression, MessageContext synCtx) { - String variableName = expression.substring(5); - if (synCtx.getVariable(variableName) != null) { - return synCtx.getVariable(variableName).toString(); - } - return expression; - } - } }