diff --git a/modules/core/src/main/java/org/apache/synapse/config/xml/LogMediatorFactory.java b/modules/core/src/main/java/org/apache/synapse/config/xml/LogMediatorFactory.java index e54d889a69..77609db7a4 100644 --- a/modules/core/src/main/java/org/apache/synapse/config/xml/LogMediatorFactory.java +++ b/modules/core/src/main/java/org/apache/synapse/config/xml/LogMediatorFactory.java @@ -126,7 +126,7 @@ public Mediator createSpecificMediator(OMElement elem, Properties properties) { } logMediator.addAllProperties(MediatorPropertyFactory.getMediatorProperties(elem)); - + logMediator.processTemplateAndSetContentAware(); addAllCommentChildrenToList(elem, logMediator.getCommentsList()); return logMediator; 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 6bed8cbfbf..3f4f67c1c8 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 @@ -79,6 +79,7 @@ public class LogMediator extends AbstractMediator { private final List properties = new ArrayList(); private String messageTemplate = ""; + private boolean isContentAware = false; /** * Logs the current message according to the supplied semantics @@ -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; } } 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 9dae242813..ff2301e5bc 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 @@ -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; @@ -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; @@ -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; } } diff --git a/modules/core/src/main/java/org/apache/synapse/util/xpath/SynapseExpression.java b/modules/core/src/main/java/org/apache/synapse/util/xpath/SynapseExpression.java index effe9ba1ff..20a5ba3ede 100644 --- a/modules/core/src/main/java/org/apache/synapse/util/xpath/SynapseExpression.java +++ b/modules/core/src/main/java/org/apache/synapse/util/xpath/SynapseExpression.java @@ -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 diff --git a/modules/core/src/main/java/org/apache/synapse/util/xpath/SynapseExpressionUtils.java b/modules/core/src/main/java/org/apache/synapse/util/xpath/SynapseExpressionUtils.java new file mode 100644 index 0000000000..b26b509a8a --- /dev/null +++ b/modules/core/src/main/java/org/apache/synapse/util/xpath/SynapseExpressionUtils.java @@ -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; + } +}