Skip to content
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

Fix: Sequence Template Issue deployed in gateway #12825

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -18,6 +18,7 @@
package org.wso2.carbon.apimgt.rest.api.publisher.v1.common;

import com.hubspot.jinjava.Jinjava;
import org.apache.axiom.om.OMAttribute;
import org.apache.axiom.om.OMElement;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.logging.Log;
Expand Down Expand Up @@ -47,6 +48,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.xml.namespace.QName;

/**
* This class used to generate Synapse Artifact.
Expand Down Expand Up @@ -114,7 +116,7 @@ public static String generateSequenceBackendForAPIProducts(String seqName, Strin
.replace("\\", "");
// change sequence name from the upper function
configMap.put("sequence_name", prodSeq);
String sanitizedSequence = renderCustomBackendSequence(seqName, pathToArchive);
String sanitizedSequence = renderCustomBackendSequence(seqName, pathToArchive, prodSeq);
if (sanitizedSequence == null) {
return null;
}
Expand All @@ -125,18 +127,11 @@ public static String generateSequenceBackendForAPIProducts(String seqName, Strin

public static String generateBackendSequenceForCustomSequence(String fileName, String pathToArchive,
String endpointType, String apiSeqName) throws APIManagementException, IOException {
Map<String, Object> configMap = new HashMap<>();
String customBackendTemplate = FileUtil.readFileToString(CUSTOM_BACKEND_SEQUENCE_TEMPLATE_LOCATION)
.replace("\\", "");
// change sequence name from the upper function
configMap.put("sequence_name", apiSeqName);
String sanitizedSequence = renderCustomBackendSequence(fileName, pathToArchive);
String sanitizedSequence = renderCustomBackendSequence(fileName, pathToArchive, apiSeqName);
if (sanitizedSequence == null) {
return null;
}
configMap.put("custom_sequence", sanitizedSequence);
configMap.put("endpoint_type", endpointType);
return renderPolicyTemplate(customBackendTemplate, configMap);
return sanitizedSequence;
}

/**
Expand Down Expand Up @@ -237,15 +232,20 @@ private static List<String> renderPolicyMapping(List<OperationPolicy> policyList
return renderedPolicyMappingList;
}

private static String renderCustomBackendSequence(String sequenceName, String pathToArchive)
private static String renderCustomBackendSequence(String fileName, String pathToArchive, String sequenceName)
throws APIManagementException {
String policyDirectory = pathToArchive + File.separator + ImportExportConstants.CUSTOM_BACKEND_DIRECTORY;
String sequence = APIUtil.getCustomBackendSequenceFromFile(policyDirectory, sequenceName,
String sequence = APIUtil.getCustomBackendSequenceFromFile(policyDirectory, fileName,
APIConstants.SYNAPSE_POLICY_DEFINITION_EXTENSION_XML);
if (sequence == null) {
return null;
}
return renderPolicyTemplate(sequence, new HashMap<>());
try {
sequence = updateSequenceName(sequence, sequenceName);
} catch (Exception ex) {
throw new APIManagementException("Error when updating the sequence name");
}
return sequence;
}

/**
Expand All @@ -267,6 +267,64 @@ private static String sanitizeOMElementWithSuperParentNode(String xmlString) thr
return filteredTemplate.toString();
}

/**
* Update the Sequence name of the xml if provided
*
* @param xmlString Sequence content
* @param name Sequence name
* @return Updated XML as a string
* @throws Exception If an error occurs
*/
private static String updateSequenceName(String xmlString, String name) throws Exception {
String updatedXmlString = "<root>" + xmlString + "</root>";
OMElement sanitizedPolicyElement = APIUtil.buildSecuredOMElement(
new ByteArrayInputStream(updatedXmlString.getBytes()));
StringBuilder filteredTemplate = new StringBuilder();
boolean isFound = false;
int count = 0;
for (Iterator childElements = sanitizedPolicyElement.getChildElements(); childElements.hasNext(); ) {
OMElement element = (OMElement) childElements.next();
if (element != null && "sequence".equals(element.getLocalName())) {
isFound = true;
OMAttribute nameAttribute = element.getAttribute(new QName("name"));
if (nameAttribute != null) {
nameAttribute.setAttributeValue(name);
} else {
element.addAttribute("name", name, null);
}
}

// break the loop if there's no <sequence> tag exists
if (count == 0 && !isFound) {
break;
}
filteredTemplate.append(element.toString());
count += 1;
}

// if <sequence> tag is not found, then attach it and build the OMElement
if (!isFound) {
updatedXmlString =
"<root><sequence xmlns=\"http://ws.apache.org/ns/synapse\">\n" + xmlString + "\n</sequence></root>";
sanitizedPolicyElement = APIUtil.buildSecuredOMElement(
new ByteArrayInputStream(updatedXmlString.getBytes()));
filteredTemplate = new StringBuilder();
for (Iterator childElements = sanitizedPolicyElement.getChildElements(); childElements.hasNext(); ) {
OMElement element = (OMElement) childElements.next();
if (element != null && "sequence".equals(element.getLocalName())) {
OMAttribute nameAttribute = element.getAttribute(new QName("name"));
if (nameAttribute != null) {
nameAttribute.setAttributeValue(name);
} else {
element.addAttribute("name", name, null);
}
}
filteredTemplate.append(element.toString());
}
}
return filteredTemplate.toString();
}

public static String renderPolicyTemplate(String template, Map<String, Object> configMap) {

Jinjava jinjava = new Jinjava();
Expand Down
Loading