Skip to content

Commit

Permalink
Merge pull request #2201 from GDLMadushanka/enrich
Browse files Browse the repository at this point in the history
Fix issue with synapse configuration get overridden
  • Loading branch information
GDLMadushanka authored Jul 11, 2024
2 parents 676bb92 + 59e954d commit 1fe6a57
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -252,7 +263,7 @@ public boolean mediate(MessageContext synCtx) {
// TODO implement target action "remove" for XML
ArrayList<OMNode> 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 {
Expand Down Expand Up @@ -324,13 +335,4 @@ public void setNativeJsonSupportEnabled(boolean nativeJsonSupportEnabled) {
isNativeJsonSupportEnabled = nativeJsonSupportEnabled;
}

public boolean containsInlineExpressions() {

return containsInlineExpressions;
}

public void setContainsInlineExpressions(boolean containsInlineExpressions) {

this.containsInlineExpressions = containsInlineExpressions;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<OMNode> evaluate(MessageContext synCtx, SynapseLog synLog)
throws JaxenException {
return evaluate(synCtx, synLog, null);
}
public ArrayList<OMNode> evaluate(MessageContext synCtx, SynapseLog synLog, OMNode inlineOMNodeWithValues)
throws JaxenException {

ArrayList<OMNode> sourceNodeList = new ArrayList<OMNode>();

Expand Down Expand Up @@ -196,7 +200,12 @@ public ArrayList<OMNode> 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) {
Expand All @@ -208,7 +217,11 @@ public ArrayList<OMNode> 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) {
Expand All @@ -233,12 +246,6 @@ public ArrayList<OMNode> 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;
}
Expand All @@ -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: <br/>
* [1] "errorsExistInSrcTag" - holds either true or false <br/>
* [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();
Expand Down Expand Up @@ -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(""))) {
Expand All @@ -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: {
Expand Down Expand Up @@ -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;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand Down

0 comments on commit 1fe6a57

Please sign in to comment.