Skip to content

Wrap value with special characters in CDATA for connectors #402

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ private SynapseConfigResponse generateConnectorSynapseConfig(STNode node, String
protected Map<String, Object> processConnectorParameter(Object data) {

Map<String, Object> dataValue = new HashMap<>();
boolean isExpressionField = false;
if (data instanceof String) {
dataValue.put(Constant.VALUE, String.format("%s", data));
} else if (data instanceof Boolean) {
Expand All @@ -205,7 +206,9 @@ protected Map<String, Object> processConnectorParameter(Object data) {
Object isExpressionObj = dataValue.get(Constant.IS_EXPRESSION);
boolean isExpression = isExpressionObj == null ? false : (boolean) isExpressionObj;
if (isExpression) {
isExpressionField = true;
dataValue.put(Constant.VALUE, String.format("{%s}", dataValue.get(Constant.VALUE)));
dataValue.put(Constant.IS_EXPRESSION, true);
} else {
if (dataValue.get(Constant.VALUE).toString().startsWith("{") &&
dataValue.get(Constant.VALUE).toString().endsWith("}")) {
Expand All @@ -232,16 +235,28 @@ protected Map<String, Object> processConnectorParameter(Object data) {
dataValue.put(Constant.VALUE, String.format("%s", dataValueStr));
}
if (dataValue.get(Constant.VALUE) != null && dataValue.get(Constant.VALUE).toString().startsWith("<![CDATA[")) {
dataValue.put("isCDATA", true);
dataValue.put(Constant.IS_CDATA, true);
String value = dataValue.get(Constant.VALUE).toString().substring(9); // Remove <![CDATA[
if (value.endsWith("]]>")) {
value = value.substring(0, value.length() - 3); // Remove ]]>
}
dataValue.put(Constant.VALUE, value);
}
if (!isExpressionField && hasSpecialXmlCharacter(dataValue.get(Constant.VALUE))) {
dataValue.put(Constant.IS_CDATA, true);
}
return dataValue;
}

private boolean hasSpecialXmlCharacter(Object o) {

if (!(o instanceof String)) {
return false;
}
String value = (String) o;
return value.contains("&") || value.contains("<") || value.contains(">");
}

protected ConnectorAction getConnectorOperation(STNode node, String mediator) {

String connectorName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,8 @@ private void addConnectorParameters(Connector connector, DOMElement element) {
if (isExpression) {
parameter.setExpression(inline);
} else {
parameter.setValue(inline);
parameter.setValue(Utils.removeCDATATag(inline));
}
parameter.setValue(Utils.getInlineString(childElement.getFirstChild()));
parameters.add(parameter);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,7 @@ public class Constant {
public static final String REQUESTS = "requests";
public static final String DEFAULT_REQUEST = "defaultRequest";
public static final String CONTENT = "content";
public static final String IS_CDATA = "isCDATA";

static {
// AI Connection to Display Name bi-Mapping
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.lemminx.customservice.synapse.connectors.entity.Connection;
import org.eclipse.lemminx.customservice.synapse.mediatorService.MediatorUtils;
import org.eclipse.lemminx.customservice.synapse.mediatorService.pojo.Namespace;
Expand Down Expand Up @@ -85,6 +86,9 @@ private static void processElement(JsonObject elementData, JsonObject elementObj
currentValue.getAsString().substring(1, currentValue.getAsString().length() - 1));
} else if (isCheckBox(value)) {
currentValue = new JsonPrimitive(currentValue.getAsBoolean());
} else if (currentValue.isJsonPrimitive()) {
String sanitizedValue = Utils.removeCDATATag(currentValue.getAsString());
currentValue = new JsonPrimitive(sanitizedValue);
}
value.add("currentValue", currentValue);
}
Expand All @@ -100,8 +104,9 @@ private static boolean isCheckBox(JsonObject value) {
return false;
}

private static JsonArray generateTableDataForConnector(String tableFieldValue) {
private static JsonArray generateTableDataForConnector(String tableFieldCDATA) {

String tableFieldValue = Utils.removeCDATATag(tableFieldCDATA);
JsonArray result = new JsonArray();
JSONArray tableValues = new JSONArray(tableFieldValue);
for (int i = 0; i < tableValues.length(); i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1391,4 +1391,22 @@ public static boolean isValidProject(String projectRoot) {
}
return true;
}

/**
* Remove CDATA tags from the given xml string.
* <p>
* * Example:
* {@code <![CDATA[<xml/>]]>} will be converted to {@code <xml/>}
* </p>
*
* @param value the xml string
* @return the xml string without CDATA tags
*/
public static String removeCDATATag(String value) {

if (StringUtils.isEmpty(value)) {
return StringUtils.EMPTY;
}
return value.replaceAll("<!\\[CDATA\\[", "").replaceAll("]]>", "");
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<{{tag}} {{#configKey}}configKey="{{{configKey}}}"{{/configKey}}>
{{#parameters}}
<{{{name}}} {{#value}}{{#namespaces}} xmlns:{{{prefix}}}="{{{uri}}}"{{/namespaces}}>{{#isCDATA}}<![CDATA[{{{value}}}]]>{{/isCDATA}}{{^isCDATA}}{{value}}{{/isCDATA}}{{/value}}</{{{name}}}>
<{{{name}}} {{#value}}{{#namespaces}} xmlns:{{{prefix}}}="{{{uri}}}"{{/namespaces}}>{{#isCDATA}}<![CDATA[{{{value}}}]]>{{/isCDATA}}{{^isCDATA}}{{#isExpression}}{{value}}{{/isExpression}}{{^isExpression}}{{{value}}}{{/isExpression}}{{/isCDATA}}{{/value}}</{{{name}}}>
{{/parameters}}
</{{tag}}>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<{{tag}} {{#configKey}}configKey="{{{configKey}}}"{{/configKey}}>
{{#parameters}}
<{{{name}}} {{#value}}{{#namespaces}} xmlns:{{{prefix}}}="{{{uri}}}"{{/namespaces}}>{{#isCDATA}}<![CDATA[{{{value}}}]]>{{/isCDATA}}{{^isCDATA}}{{value}}{{/isCDATA}}{{/value}}</{{{name}}}>
<{{{name}}} {{#value}}{{#namespaces}} xmlns:{{{prefix}}}="{{{uri}}}"{{/namespaces}}>{{#isCDATA}}<![CDATA[{{{value}}}]]>{{/isCDATA}}{{^isCDATA}}{{#isExpression}}{{value}}{{/isExpression}}{{^isExpression}}{{{value}}}{{/isExpression}}{{/isCDATA}}{{/value}}</{{{name}}}>
{{/parameters}}
</{{tag}}>
Loading