diff --git a/modules/core/src/main/java/org/apache/synapse/config/xml/EnrichMediatorFactory.java b/modules/core/src/main/java/org/apache/synapse/config/xml/EnrichMediatorFactory.java index dd6b4f0a6e..a715333e03 100644 --- a/modules/core/src/main/java/org/apache/synapse/config/xml/EnrichMediatorFactory.java +++ b/modules/core/src/main/java/org/apache/synapse/config/xml/EnrichMediatorFactory.java @@ -124,7 +124,7 @@ protected Mediator createSpecificMediator(OMElement elem, Properties properties) } if (!StringUtils.isEmpty(inlineString)) { - enrich.setContainsInlineExpressions(InlineExpressionUtil.checkForInlineExpressions(inlineString)); + source.setContainsInlineExpressions(InlineExpressionUtil.checkForInlineExpressions(inlineString)); } } diff --git a/modules/core/src/main/java/org/apache/synapse/mediators/elementary/EnrichMediator.java b/modules/core/src/main/java/org/apache/synapse/mediators/elementary/EnrichMediator.java index 94e74ddc21..77674864c7 100644 --- a/modules/core/src/main/java/org/apache/synapse/mediators/elementary/EnrichMediator.java +++ b/modules/core/src/main/java/org/apache/synapse/mediators/elementary/EnrichMediator.java @@ -91,8 +91,6 @@ public class EnrichMediator extends AbstractMediator { private boolean isNativeJsonSupportEnabled = false; - private boolean containsInlineExpressions = false; - public static final String ACTION_REMOVE = "remove"; public boolean mediate(MessageContext synCtx) { @@ -179,18 +177,31 @@ public boolean mediate(MessageContext synCtx) { // If the inline text contains expressions, we need to evaluate the values // and decide on the type (whether XML or JSON) boolean isInlineTextXML = !isNativeJsonSupportEnabled; + OMNode inlineOMNodeWithValues = null; + String inlineString = null; - if (containsInlineExpressions()) { + if (source.containsInlineExpressions()) { OMNode inlineOMNode = source.getInlineOMNode(); - source.setInitialInlineOMNode(inlineOMNode); if (inlineOMNode != null) { - if (inlineOMNode instanceof OMText) { - // If the node type is text, it can either be a JSON string or an expression - // If it is an expression we must check again after resolving the expressions - // whether it is XML or JSON - isInlineTextXML = setDynamicValuesInNode(synCtx, ((OMTextImpl) inlineOMNode).getText()); - } else if (inlineOMNode instanceof OMElement) { - isInlineTextXML = setDynamicValuesInNode(synCtx, inlineOMNode.toString()); + try { + if (inlineOMNode instanceof OMText) { + // If the node type is text, it can either be a JSON string or an expression + // If it is an expression we must check again after resolving the expressions + // whether it is XML or JSON + inlineString = InlineExpressionUtil.replaceDynamicValues(synCtx, + ((OMTextImpl) inlineOMNode).getText()); + // After the expressions in the inline text is replaced with the value, + // the string must be parsed again to identify whether it has changed to a XML + inlineOMNodeWithValues = AXIOMUtil.stringToOM(inlineString); + isInlineTextXML = true; + } else if (inlineOMNode instanceof OMElement) { + inlineString = InlineExpressionUtil.replaceDynamicValues(synCtx, inlineOMNode.toString()); + inlineOMNodeWithValues = AXIOMUtil.stringToOM(inlineString); + isInlineTextXML = true; + } + } catch (XMLStreamException | OMException e){ + // The string is considered as a text / JSON + inlineOMNodeWithValues = OMAbstractFactory.getOMFactory().createOMText(inlineString); } } } @@ -218,7 +229,7 @@ public boolean mediate(MessageContext synCtx) { } else { Object sourceNode; try { - sourceNode = source.evaluateJson(synCtx, synLog, sourcePropertyJson); + sourceNode = source.evaluateJson(synCtx, synLog, sourcePropertyJson, inlineOMNodeWithValues); if (sourceNode == null) { handleException("Failed to get the source for Enriching : ", synCtx); } else if (target.getTargetType() == KEY) { @@ -252,7 +263,7 @@ public boolean mediate(MessageContext synCtx) { // TODO implement target action "remove" for XML ArrayList sourceNodeList; try { - sourceNodeList = source.evaluate(synCtx, synLog); + sourceNodeList = source.evaluate(synCtx, synLog, inlineOMNodeWithValues); if (sourceNodeList == null) { handleException("Failed to get the source for Enriching : ", synCtx); } else { @@ -324,13 +335,4 @@ public void setNativeJsonSupportEnabled(boolean nativeJsonSupportEnabled) { isNativeJsonSupportEnabled = nativeJsonSupportEnabled; } - public boolean containsInlineExpressions() { - - return containsInlineExpressions; - } - - public void setContainsInlineExpressions(boolean containsInlineExpressions) { - - this.containsInlineExpressions = containsInlineExpressions; - } } diff --git a/modules/core/src/main/java/org/apache/synapse/mediators/elementary/Source.java b/modules/core/src/main/java/org/apache/synapse/mediators/elementary/Source.java index a43f516570..7e0a5a58b2 100644 --- a/modules/core/src/main/java/org/apache/synapse/mediators/elementary/Source.java +++ b/modules/core/src/main/java/org/apache/synapse/mediators/elementary/Source.java @@ -75,14 +75,18 @@ public class Source { private OMNode inlineOMNode = null; - private OMNode initialInlineOMNode = null; - private String inlineKey = null; private static final Log log = LogFactory.getLog(Source.class); + private boolean containsInlineExpressions = false; + public ArrayList evaluate(MessageContext synCtx, SynapseLog synLog) throws JaxenException { + return evaluate(synCtx, synLog, null); + } + public ArrayList evaluate(MessageContext synCtx, SynapseLog synLog, OMNode inlineOMNodeWithValues) + throws JaxenException { ArrayList sourceNodeList = new ArrayList(); @@ -196,7 +200,12 @@ public ArrayList evaluate(MessageContext synCtx, SynapseLog synLog) } } else if (sourceType == EnrichMediator.INLINE) { if (inlineOMNode instanceof OMElement) { - OMElement inlineOMElement = (OMElement) inlineOMNode; + OMElement inlineOMElement; + if (containsInlineExpressions && inlineOMNodeWithValues != null) { + inlineOMElement = (OMElement) inlineOMNodeWithValues; + } else { + inlineOMElement = (OMElement) inlineOMNode; + } if (inlineOMElement.getQName().getLocalPart().equals("Envelope")) { SOAPEnvelope soapEnvelope = getSOAPEnvFromOM(inlineOMElement); if (soapEnvelope != null) { @@ -208,7 +217,11 @@ public ArrayList evaluate(MessageContext synCtx, SynapseLog synLog) sourceNodeList.add(inlineOMElement.cloneOMElement()); } } else if (inlineOMNode instanceof OMText) { - sourceNodeList.add(inlineOMNode); + if (containsInlineExpressions && inlineOMNodeWithValues != null) { + sourceNodeList.add(inlineOMNodeWithValues); + } else { + sourceNodeList.add(inlineOMNode); + } } else if (inlineKey != null) { Object inlineObj = synCtx.getEntry(inlineKey); if (inlineObj instanceof OMElement) { @@ -233,12 +246,6 @@ public ArrayList evaluate(MessageContext synCtx, SynapseLog synLog) } else { synLog.error("Inline Source Content is not valid."); } - // If the initialInlineOMNode is not null, it means that inline OM Node has been overridden with the - // inline string containing resolved dynamic values. Therefore, we should set the initial OM Node back - // which contains the original inline value - if (initialInlineOMNode != null) { - this.inlineOMNode = initialInlineOMNode; - } } return sourceNodeList; } @@ -257,18 +264,24 @@ private SOAPEnvelope getSOAPEnvFromOM(OMElement inlineElement) { return builder.getSOAPEnvelope(); } + public JsonElement evaluateJson(MessageContext synCtx, SynapseLog synLog, + JsonElement sourcePropertyJson) throws JaxenException { + return evaluateJson(synCtx, synLog, sourcePropertyJson, null); + } /** * This method will evaluate a specified source json element. * * @param synCtx - Current Message Context * @param synLog - Default Logger for the package * @param sourcePropertyJson Parsed Json element + * @param inlineOMNodeWithValues Inline OM Node with values if there are dynamic expressions * @return A HashMap with the following keys:
* [1] "errorsExistInSrcTag" - holds either true or false
* [2] "evaluatedSrcJsonElement" - holds the evaluated Json Element as an Object */ public JsonElement evaluateJson(MessageContext synCtx, SynapseLog synLog, - JsonElement sourcePropertyJson) throws JaxenException { + JsonElement sourcePropertyJson, OMNode inlineOMNodeWithValues) + throws JaxenException{ JsonElement object = null; String jsonPath = null; JsonParser parser = new JsonParser(); @@ -322,7 +335,11 @@ public JsonElement evaluateJson(MessageContext synCtx, SynapseLog synLog, assert inlineOMNode != null || inlineKey != null : "inlineJSONNode or key shouldn't be null when type is INLINE"; if (inlineOMNode instanceof OMText) { - object = JsonPath.parse(((OMTextImpl) inlineOMNode).getText()).json(); + if (containsInlineExpressions() && inlineOMNodeWithValues != null) { + object = JsonPath.parse(((OMTextImpl) inlineOMNodeWithValues).getText()).json(); + } else { + object = JsonPath.parse(((OMTextImpl) inlineOMNode).getText()).json(); + } } else if (inlineKey != null && !inlineKey.trim().equals("")) { Object inlineObj = synCtx.getEntry(inlineKey); if ((inlineObj instanceof String) && !(((String) inlineObj).trim().equals(""))) { @@ -334,12 +351,6 @@ public JsonElement evaluateJson(MessageContext synCtx, SynapseLog synLog, synLog.error("Source failed to get inline JSON" + "inlineJSONNode=" + inlineOMNode + ", inlineKey=" + inlineKey); } - // If the initialInlineOMNode is not null, it means that inline OM Node has been overridden with the - // inline string containing resolved dynamic values. Therefore, we should set the initial OM Node back - // which contains the original inline value - if (initialInlineOMNode != null) { - this.inlineOMNode = initialInlineOMNode; - } break; } case EnrichMediator.PROPERTY: { @@ -407,14 +418,14 @@ public void setInlineKey(String inlineKey) { this.inlineKey = inlineKey; } - public OMNode getInitialInlineOMNode() { + public boolean containsInlineExpressions() { - return initialInlineOMNode; + return containsInlineExpressions; } - public void setInitialInlineOMNode(OMNode inlineOMNodeWithExpressions) { + public void setContainsInlineExpressions(boolean containsInlineExpressions) { - this.initialInlineOMNode = inlineOMNodeWithExpressions; + this.containsInlineExpressions = containsInlineExpressions; } } 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 8b6962c861..e362b54557 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 @@ -47,7 +47,7 @@ public final class InlineExpressionUtil { private static final String EXPRESSION_JSON_EVAL = "json-eval("; // Regex to identify expressions in inline text - private static final Pattern EXPRESSION_PATTERN = Pattern.compile("(\\{[^\\s\"][^,<>\n}\\]]*})"); + private static final Pattern EXPRESSION_PATTERN = Pattern.compile("(\\{[^\\s\",<>}\\]]+})"); private InlineExpressionUtil() {