Skip to content

Commit

Permalink
Add synapse expression support for the log mediator
Browse files Browse the repository at this point in the history
  • Loading branch information
SanojPunchihewa committed Nov 14, 2024
1 parent 396b6a3 commit 9b66249
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {

Expand Down Expand Up @@ -201,36 +203,47 @@ 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
*
* @param synCtx Message Context
* @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;
}
}
}

0 comments on commit 9b66249

Please sign in to comment.