Skip to content

Commit

Permalink
Move Synapse expression content check to util and use in log mediator
Browse files Browse the repository at this point in the history
  • Loading branch information
SanojPunchihewa committed Dec 3, 2024
1 parent 781686c commit 2463b2c
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public Mediator createSpecificMediator(OMElement elem, Properties properties) {
}

logMediator.addAllProperties(MediatorPropertyFactory.getMediatorProperties(elem));

logMediator.processTemplateAndSetContentAware();
addAllCommentChildrenToList(elem, logMediator.getCommentsList());

return logMediator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public class LogMediator extends AbstractMediator {
private final List<MediatorProperty> properties = new ArrayList<MediatorProperty>();

private String messageTemplate = "";
private boolean isContentAware = false;

/**
* Logs the current message according to the supplied semantics
Expand Down Expand Up @@ -331,14 +332,22 @@ private void processMessageTemplate(StringBuffer stringBuffer, MessageContext sy

@Override
public boolean isContentAware() {

return isContentAware;
}

public void processTemplateAndSetContentAware() {

if (logLevel == MESSAGE_TEMPLATE || logLevel == CUSTOM) {
for (MediatorProperty property : properties) {
if (property.getExpression() != null && property.getExpression().isContentAware()) {
return true;
isContentAware = true;
return;
}
}
return InlineExpressionUtil.isInlineSynapseExpressionsContentAware(messageTemplate);
isContentAware = InlineExpressionUtil.isInlineSynapseExpressionsContentAware(messageTemplate);
} else {
isContentAware = true;
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
Expand All @@ -30,8 +29,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.SynapseExpressionUtils;
import org.apache.synapse.util.xpath.SynapseJsonPath;
import org.apache.synapse.util.xpath.SynapseXPath;
import org.jaxen.JaxenException;
Expand Down Expand Up @@ -216,8 +215,8 @@ 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("$.")) {
String expression = matcher.group(1);
if (SynapseExpressionUtils.isSynapseExpressionContentAware(expression)) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,33 +74,7 @@ public SynapseExpression(String synapseExpression) throws JaxenException {
}
throw new JaxenException(errorMessage.toString());
}

// TODO : Need to improve the content aware detection logic
if (synapseExpression.equals("payload") || synapseExpression.equals("$")
|| synapseExpression.contains("payload.") || synapseExpression.contains("$.")) {
isContentAware = true;
} else if (synapseExpression.contains("xpath(")) {
// TODO change the regex to support xpath + variable syntax
Pattern pattern = Pattern.compile("xpath\\(['\"](.*?)['\"]\\s*(,\\s*['\"](.*?)['\"])?\\)?");
Matcher matcher = pattern.matcher(synapseExpression);
// Find all matches
while (matcher.find()) {
if (matcher.group(2) != null) {
// evaluating xpath on a variable so not content aware
continue;
}
String xpath = matcher.group(1);
try {
SynapseXPath synapseXPath = new SynapseXPath(xpath);
if (synapseXPath.isContentAware()) {
isContentAware = true;
break;
}
} catch (JaxenException e) {
// Ignore the exception and continue
}
}
}
isContentAware = SynapseExpressionUtils.isSynapseExpressionContentAware(synapseExpression);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.synapse.util.xpath;

import org.jaxen.JaxenException;

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

/**
* Utility class for Synapse Expressions
*/
public class SynapseExpressionUtils {

/**
* Checks whether the synapse expression is content aware
*
* @param synapseExpression synapse expression string
* @return true if the synapse expression is content aware, false otherwise
*/
public static boolean isSynapseExpressionContentAware(String synapseExpression) {

// TODO : Need to improve the content aware detection logic
if (synapseExpression.equals("payload") || synapseExpression.equals("$")
|| synapseExpression.contains("payload.") || synapseExpression.contains("$.")) {
return true;
} else if (synapseExpression.contains("xpath(")) {
// TODO change the regex to support xpath + variable syntax
Pattern pattern = Pattern.compile("xpath\\(['\"](.*?)['\"]\\s*(,\\s*['\"](.*?)['\"])?\\)?");
Matcher matcher = pattern.matcher(synapseExpression);
// Find all matches
while (matcher.find()) {
if (matcher.group(2) != null) {
// evaluating xpath on a variable so not content aware
continue;
}
String xpath = matcher.group(1);
try {
SynapseXPath synapseXPath = new SynapseXPath(xpath);
if (synapseXPath.isContentAware()) {
return true;
}
} catch (JaxenException e) {
// Ignore the exception and continue
}
}
}
return false;
}
}

0 comments on commit 2463b2c

Please sign in to comment.