From 0326b606d36ff9916bc1229a22ca028d8455daa5 Mon Sep 17 00:00:00 2001 From: malakaganga Date: Wed, 12 Jul 2023 12:56:09 +0530 Subject: [PATCH] Remove msgCtx in Resolvers after the XPath2.0 evaluation We are keeping a reference to the message context in both GetPropertyFunctionResolver and DOMSynapseXPathVariableResolver (hold by SynapseXpath ultimately). But the resolveFunction method should always have the new message context to be evaluated. Hence removing the msgCtx in Resolvers after the XPath2.0 evaluation. Fixes: https://github.com/wso2/api-manager/issues/1977 --- .../util/xpath/DOMSynapseXPathVariableResolver.java | 4 ++++ .../util/xpath/GetPropertyFunctionResolver.java | 5 +++++ .../org/apache/synapse/util/xpath/SynapseXPath.java | 11 +++++++++-- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/modules/core/src/main/java/org/apache/synapse/util/xpath/DOMSynapseXPathVariableResolver.java b/modules/core/src/main/java/org/apache/synapse/util/xpath/DOMSynapseXPathVariableResolver.java index 45f7ec72bd..48e2c90d98 100644 --- a/modules/core/src/main/java/org/apache/synapse/util/xpath/DOMSynapseXPathVariableResolver.java +++ b/modules/core/src/main/java/org/apache/synapse/util/xpath/DOMSynapseXPathVariableResolver.java @@ -31,4 +31,8 @@ public Object resolveVariable(QName variable) { throw new SynapseException("DOM Synapse XPATH variable resolution failed",e); } } + + public void setSynCtx(MessageContext synCtx) { + this.synCtx = synCtx; + } } diff --git a/modules/core/src/main/java/org/apache/synapse/util/xpath/GetPropertyFunctionResolver.java b/modules/core/src/main/java/org/apache/synapse/util/xpath/GetPropertyFunctionResolver.java index 0ac5186838..e0382eaee6 100644 --- a/modules/core/src/main/java/org/apache/synapse/util/xpath/GetPropertyFunctionResolver.java +++ b/modules/core/src/main/java/org/apache/synapse/util/xpath/GetPropertyFunctionResolver.java @@ -15,9 +15,14 @@ public GetPropertyFunctionResolver(MessageContext synCtx) { } public XPathFunction resolveFunction(QName functionName, int arity) { + if (SynapseXPathConstants.GET_PROPERTY_FUNCTION.equals(functionName.getLocalPart())) { return new GetPropertyFunction(synCtx); } return null; } + + public void setSynCtx(MessageContext synCtx) { + this.synCtx = synCtx; + } } diff --git a/modules/core/src/main/java/org/apache/synapse/util/xpath/SynapseXPath.java b/modules/core/src/main/java/org/apache/synapse/util/xpath/SynapseXPath.java index 1ed453173d..829c08476c 100755 --- a/modules/core/src/main/java/org/apache/synapse/util/xpath/SynapseXPath.java +++ b/modules/core/src/main/java/org/apache/synapse/util/xpath/SynapseXPath.java @@ -670,12 +670,19 @@ public synchronized String evaluateDOMXPath(MessageContext synCtx) throws XPathE doomElement = convertToDOOM(element); } domXpath.setNamespaceContext(domNamespaceMap); - domXpath.setXPathFunctionResolver(new GetPropertyFunctionResolver(synCtx)); - domXpath.setXPathVariableResolver(new DOMSynapseXPathVariableResolver(this.getVariableContext(), synCtx)); + GetPropertyFunctionResolver getPropertyFunctionResolver = new GetPropertyFunctionResolver(synCtx); + DOMSynapseXPathVariableResolver domSynapseXPathVariableResolver = + new DOMSynapseXPathVariableResolver(this.getVariableContext(), synCtx); + domXpath.setXPathFunctionResolver(getPropertyFunctionResolver); + domXpath.setXPathVariableResolver(domSynapseXPathVariableResolver); /* Compile the original expression again with Saxon to be evaluated with XPath 2.0 */ XPathExpression expr = domXpath.compile(getExpression()); Object result = expr.evaluate(doomElement); + // Set message contexts in resolvers to null to avoid memory leaks. Resolvers are initialized for each evaluation + domSynapseXPathVariableResolver.setSynCtx(null); + getPropertyFunctionResolver.setSynCtx(null); + if (result != null) { return result.toString(); }