Skip to content

Commit

Permalink
Remove msgCtx in Resolvers after the XPath2.0 evaluation
Browse files Browse the repository at this point in the history
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: wso2/api-manager#1977
  • Loading branch information
malakaganga committed Jul 12, 2023
1 parent fe943a4 commit 0326b60
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down

0 comments on commit 0326b60

Please sign in to comment.