Skip to content

Commit

Permalink
Fix: Sequence Backend storing issue in APICTL project
Browse files Browse the repository at this point in the history
  • Loading branch information
BLasan committed Sep 20, 2024
1 parent 4823c10 commit ee05101
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,8 @@ private void unDeployAPI(APIGatewayAdmin apiGatewayAdmin, DeployAPIInGatewayEven
gatewayAPIDTO);
GatewayUtils.setEndpointsToBeRemoved(apiProductIdentifier, associatedApi.getUuid(),
gatewayAPIDTO);
GatewayUtils.setCustomBackendToBeRemoved(apiProductIdentifier, associatedApi.getUuid(),
gatewayAPIDTO);
}
} else {
API api = new API(new APIIdentifier(gatewayEvent.getProvider(), gatewayEvent.getName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10029,7 +10029,10 @@ public static void loadCommonOperationPolicies(String organization) {
*/
public static InputStream getCustomBackendSequence(String extractedFolderPath, String customBackendFileName,
String fileExtension) throws APIManagementException {
String fileName = extractedFolderPath + File.separator + customBackendFileName + fileExtension;
if (!StringUtils.isEmpty(customBackendFileName) && !customBackendFileName.contains(fileExtension)) {
customBackendFileName = customBackendFileName + fileExtension;
}
String fileName = extractedFolderPath + File.separator + customBackendFileName;
InputStream inputStream = null;
if (checkFileExistence(fileName)) {
try {
Expand Down Expand Up @@ -10091,7 +10094,10 @@ public static String getCustomBackendSequenceFromFile(String extractedFolderPath

String customBackendContent = null;
try {
String fileName = extractedFolderPath + File.separator + sequenceName + fileExtension;
if(!StringUtils.isEmpty(sequenceName) && !sequenceName.contains(".xml")) {
sequenceName = sequenceName + fileExtension;
}
String fileName = extractedFolderPath + File.separator + sequenceName;
if (checkFileExistence(fileName)) {
if (log.isDebugEnabled()) {
log.debug("Found policy definition file " + fileName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ public static void setCustomBackendToBeRemoved(GatewayAPIDTO gatewayAPIDTO) {
addStringToList(productionBackend, gatewayAPIDTO.getSequencesToBeRemove()));
}

public static void setCustomBackendToBeRemoved(APIProductIdentifier apiProductIdentifier, String apiUUID,
GatewayAPIDTO gatewayAPIDTO) {
String sandBoxBackend = APIUtil.getCustomBackendName(apiProductIdentifier.getUUID().concat("-" + apiUUID),
APIConstants.API_KEY_TYPE_SANDBOX);
gatewayAPIDTO.setSequencesToBeRemove(addStringToList(sandBoxBackend, gatewayAPIDTO.getSequencesToBeRemove()));
String productionBackend = APIUtil.getCustomBackendName(apiProductIdentifier.getUUID().concat("-" + apiUUID),
APIConstants.API_KEY_TYPE_PRODUCTION);
gatewayAPIDTO.setSequencesToBeRemove(
addStringToList(productionBackend, gatewayAPIDTO.getSequencesToBeRemove()));
}

public static String[] addStringToList(String key, String[] keys) {

if (keys == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,30 @@ public static String generatePolicySequenceForUriTemplateSet(Set<URITemplate> ur
}
}

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

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);
if (sanitizedSequence == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@
import org.wso2.carbon.apimgt.api.model.APIProductResource;
import org.wso2.carbon.apimgt.api.model.CORSConfiguration;
import org.wso2.carbon.apimgt.api.model.Environment;
import org.wso2.carbon.apimgt.api.model.SequenceBackendData;
import org.wso2.carbon.apimgt.api.model.URITemplate;
import org.wso2.carbon.apimgt.api.model.WebSocketTopicMappingConfiguration;
import org.wso2.carbon.apimgt.common.gateway.graphql.GraphQLSchemaDefinitionUtil;
import org.wso2.carbon.apimgt.impl.APIConstants;
import org.wso2.carbon.apimgt.impl.certificatemgt.exceptions.CertificateManagementException;
import org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO;
import org.wso2.carbon.apimgt.impl.definitions.GraphQLSchemaDefinition;
import org.wso2.carbon.apimgt.impl.dto.SoapToRestMediationDto;
import org.wso2.carbon.apimgt.impl.importexport.ImportExportConstants;
Expand Down Expand Up @@ -91,6 +93,7 @@ public class TemplateBuilderUtil {
private static final String ENDPOINT_SANDBOX = "_SANDBOX_";

private static final Log log = LogFactory.getLog(TemplateBuilderUtil.class);
private static final ApiMgtDAO apiMgtDAO = ApiMgtDAO.getInstance();

public static APITemplateBuilderImpl getAPITemplateBuilder(API api, String tenantDomain,
List<ClientCertificateDTO> clientCertificateDTOSProduction,
Expand Down Expand Up @@ -522,14 +525,30 @@ public static GatewayAPIDTO retrieveGatewayAPIDto(API api, Environment environme
String policyDirectory =
extractedFolderPath + File.separator + ImportExportConstants.CUSTOM_BACKEND_DIRECTORY;
String seqName = APIUtil.getCustomBackendName(api.getUuid(), APIConstants.API_KEY_TYPE_SANDBOX);
if (APIUtil.checkFileExistence(policyDirectory + File.separator + seqName
+ APIConstants.SYNAPSE_POLICY_DEFINITION_EXTENSION_XML)) {
endpointConfig.put("sandbox", seqName);
SequenceBackendData seqData = apiMgtDAO.getCustomBackendByAPIUUID(api.getUuid(),
APIConstants.API_KEY_TYPE_SANDBOX);
if (seqData != null) {
String name = seqData.getName();
if (!StringUtils.isEmpty(name) && !name.contains(
APIConstants.SYNAPSE_POLICY_DEFINITION_EXTENSION_XML)) {
name = name + APIConstants.SYNAPSE_POLICY_DEFINITION_EXTENSION_XML;
}
if (APIUtil.checkFileExistence(policyDirectory + File.separator + name)) {
endpointConfig.put("sandbox", seqName);
}
}

seqName = APIUtil.getCustomBackendName(api.getUuid(), APIConstants.API_KEY_TYPE_PRODUCTION);
if (APIUtil.checkFileExistence(policyDirectory + File.separator + seqName
+ APIConstants.SYNAPSE_POLICY_DEFINITION_EXTENSION_XML)) {
endpointConfig.put("production", seqName);
seqData = apiMgtDAO.getCustomBackendByAPIUUID(api.getUuid(), APIConstants.API_KEY_TYPE_PRODUCTION);
if (seqData != null) {
String name = seqData.getName();
if (!StringUtils.isEmpty(name) && !name.contains(
APIConstants.SYNAPSE_POLICY_DEFINITION_EXTENSION_XML)) {
name = name + APIConstants.SYNAPSE_POLICY_DEFINITION_EXTENSION_XML;
}
if (APIUtil.checkFileExistence(policyDirectory + File.separator + name)) {
endpointConfig.put("production", seqName);
}
}
api.setEndpointConfig(objectMapper.writeValueAsString(endpointConfig));
}
Expand Down Expand Up @@ -729,16 +748,15 @@ private static void setCustomSequencesToBeAdded(APIProduct apiProduct, API api,

JsonObject endpointConfigMap = JsonParser.parseString(api.getEndpointConfig()).getAsJsonObject();
if (endpointConfigMap != null && APIConstants.ENDPOINT_TYPE_SEQUENCE.equals(
endpointConfigMap.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE).getAsString()) && (
APIConstants.API_TYPE_HTTP.equals(api.getType()) || APIConstants.API_TYPE_SOAPTOREST.equals(
api.getType()))) {
GatewayContentDTO gatewayCustomBackendSequenceDTO = retrieveCustomBackendSequence(api,
APIConstants.API_KEY_TYPE_SANDBOX, extractedPath);
endpointConfigMap.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE).getAsString())
&& APIConstants.API_TYPE_HTTP.equals(api.getType())) {
GatewayContentDTO gatewayCustomBackendSequenceDTO = retrieveSequenceBackendForAPIProduct(api,
apiProduct, APIConstants.API_KEY_TYPE_SANDBOX, extractedPath);
if (gatewayCustomBackendSequenceDTO != null) {
gatewayAPIDTO.setSequenceToBeAdd(addGatewayContentToList(gatewayCustomBackendSequenceDTO,
gatewayAPIDTO.getSequenceToBeAdd()));
}
gatewayCustomBackendSequenceDTO = retrieveCustomBackendSequence(api,
gatewayCustomBackendSequenceDTO = retrieveSequenceBackendForAPIProduct(api, apiProduct,
APIConstants.API_KEY_TYPE_PRODUCTION, extractedPath);
if (gatewayCustomBackendSequenceDTO != null) {
gatewayAPIDTO.setSequenceToBeAdd(addGatewayContentToList(gatewayCustomBackendSequenceDTO,
Expand Down Expand Up @@ -1442,32 +1460,74 @@ private static GatewayContentDTO retrieveOperationPolicySequence(String pathToAc
return null;
}

private static GatewayContentDTO retrieveCustomBackendSequence(API api, String endpointType, String pathToAchieve)
throws APIManagementException {
private static GatewayContentDTO retrieveSequenceBackendForAPIProduct(API api, APIProduct apiProduct,
String endpointType, String pathToAchieve) throws APIManagementException {
GatewayContentDTO customBackendSequenceContentDto = new GatewayContentDTO();

String customSequence = null;
String seqExt = APIUtil.getCustomBackendName(api.getUuid(), endpointType);
try {
customSequence = SynapsePolicyAggregator.generateBackendSequenceForCustomSequence(seqExt, pathToAchieve,
SequenceBackendData data = apiMgtDAO.getCustomBackendByAPIUUID(api.getUuid(), endpointType);
if (data != null) {
String seqExt = data.getName();
if (!StringUtils.isEmpty(seqExt) && seqExt.contains(".xml")) {
seqExt = seqExt + ".xml";
}
String prodSeqExt = APIUtil.getCustomBackendName(apiProduct.getUuid().concat("-" + api.getUuid()),
endpointType);
} catch (IOException e) {
throw new APIManagementException(e);
try {
customSequence = SynapsePolicyAggregator.generateSequenceBackendForAPIProducts(seqExt, prodSeqExt,
pathToAchieve, endpointType);
} catch (IOException e) {
throw new APIManagementException(e);
}

if (StringUtils.isNotEmpty(customSequence)) {
try {
OMElement omElement = APIUtil.buildOMElement(new ByteArrayInputStream(customSequence.getBytes()));
if (omElement != null) {
if (omElement.getAttribute(new QName("name")) != null) {
omElement.getAttribute(new QName("name")).setAttributeValue(prodSeqExt);
}
customBackendSequenceContentDto.setName(prodSeqExt);
customBackendSequenceContentDto.setContent(APIUtil.convertOMtoString(omElement));
return customBackendSequenceContentDto;
}
} catch (Exception e) {
throw new APIManagementException(e);
}
}
}

if (StringUtils.isNotEmpty(customSequence)) {
return null;
}

private static GatewayContentDTO retrieveCustomBackendSequence(API api, String endpointType, String pathToAchieve)
throws APIManagementException {
GatewayContentDTO customBackendSequenceContentDto = new GatewayContentDTO();
String customSequence = null;
SequenceBackendData data = apiMgtDAO.getCustomBackendByAPIUUID(api.getUuid(), endpointType);
if (data != null) {
String seqExt = data.getName();
String apiSeqName = APIUtil.getCustomBackendName(api.getUuid(), endpointType);
try {
OMElement omElement = APIUtil.buildOMElement(new ByteArrayInputStream(customSequence.getBytes()));
if (omElement != null) {
if (omElement.getAttribute(new QName("name")) != null) {
omElement.getAttribute(new QName("name")).setAttributeValue(seqExt);
customSequence = SynapsePolicyAggregator.generateBackendSequenceForCustomSequence(seqExt, pathToAchieve,
endpointType, apiSeqName);
} catch (IOException e) {
throw new APIManagementException(e);
}

if (StringUtils.isNotEmpty(customSequence)) {
try {
OMElement omElement = APIUtil.buildOMElement(new ByteArrayInputStream(customSequence.getBytes()));
if (omElement != null) {
if (omElement.getAttribute(new QName("name")) != null) {
omElement.getAttribute(new QName("name")).setAttributeValue(apiSeqName);
}
customBackendSequenceContentDto.setName(apiSeqName);
customBackendSequenceContentDto.setContent(APIUtil.convertOMtoString(omElement));
return customBackendSequenceContentDto;
}
customBackendSequenceContentDto.setName(seqExt);
customBackendSequenceContentDto.setContent(APIUtil.convertOMtoString(omElement));
return customBackendSequenceContentDto;
} catch (Exception e) {
throw new APIManagementException(e);
}
} catch (Exception e) {
throw new APIManagementException(e);
}
}
return null;
Expand Down
Loading

0 comments on commit ee05101

Please sign in to comment.