diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/ApiMgtDAO.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/ApiMgtDAO.java index 92a3b9d2edd7..88fdfffc898b 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/ApiMgtDAO.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/ApiMgtDAO.java @@ -20902,14 +20902,15 @@ public void updateAPIPoliciesMapping(String apiUUID, Set uriTemplat } } - public void updateCustomBackend(String apiUUID, String sequenceName, InputStream sequence, String type, String backendUUID) throws APIManagementException { - String deleteCustomBackedQuery = SQLConstants.CustomBackendConstants.DELETE_CUSTOM_BACKEND; + public void updateCustomBackend(String apiUUID, String sequenceName, InputStream sequence, String type, + String backendUUID) throws APIManagementException { + // delete current working copy + String deleteCustomBackedQuery = SQLConstants.CustomBackendConstants.DELETE_CUSTOM_BACKEND_BY_API_AND_TYPE; try (Connection connection = APIMgtDBUtil.getConnection(); - PreparedStatement prepStmt = connection.prepareStatement(deleteCustomBackedQuery)) { + PreparedStatement prepStmt = connection.prepareStatement(deleteCustomBackedQuery)) { connection.setAutoCommit(false); prepStmt.setString(1, apiUUID); - prepStmt.setString(2, backendUUID); - prepStmt.setString(3, type); + prepStmt.setString(2, type); prepStmt.executeUpdate(); addCustomBackend(apiUUID, sequenceName, null, sequence, type, connection, backendUUID); connection.commit(); diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SQLConstants.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SQLConstants.java index 339dbdb72f13..535aa6fd132c 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SQLConstants.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SQLConstants.java @@ -4405,7 +4405,8 @@ public static class CustomBackendConstants { public static final String ADD_CUSTOM_BACKEND = "INSERT INTO AM_API_CUSTOM_BACKEND (ID,API_UUID,SEQUENCE,TYPE,REVISION_UUID,NAME) " + "VALUES (?,?,?,?,?,?)"; public static final String DELETE_WORKING_COPY_OF_CUSTOM_BACKEND = "DELETE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ? AND REVISION_UUID = '0'"; - public static final String DELETE_CUSTOM_BACKEND = "DELETE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ? AND ID = ? AND TYPE = ? AND REVISION_UUID = '0'"; + public static final String DELETE_CUSTOM_BACKEND = "DELETE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ? AND TYPE = ? AND ID = ? AND REVISION_UUID = '0'"; + public static final String DELETE_CUSTOM_BACKEND_BY_API_AND_TYPE = "DELETE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ? AND TYPE = ? AND REVISION_UUID = '0'"; public static final String DELETE_CUSTOM_BACKEND_BY_REVISION = "DELETE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ? AND REVISION_UUID = ?"; public static final String DELETE_CUSTOM_BACKEND_BY_API = "DELETE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ?"; public static final String GET_CUSTOM_BACKEND_OF_API_REVISION = "SELECT ID, NAME, SEQUENCE, TYPE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ? AND REVISION_UUID = ?"; diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java index 56639d21549a..379c62179473 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java @@ -9977,6 +9977,20 @@ 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; + InputStream inputStream = null; + if (checkFileExistence(fileName)) { + try { + inputStream = new FileInputStream(fileName); + } catch (IOException ex) { + handleException("Error reading Custom Backend " + customBackendFileName); + } + } + return inputStream; + } + /** * Read the operation policy definition from the provided path and return the definition object * diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/ImportUtils.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/ImportUtils.java index 18b478717da5..d5a419a188a0 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/ImportUtils.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/ImportUtils.java @@ -18,6 +18,7 @@ package org.wso2.carbon.apimgt.rest.api.publisher.v1.common.mappings; +import com.fasterxml.jackson.databind.ObjectMapper; import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonElement; @@ -125,6 +126,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import java.util.UUID; import javax.validation.constraints.NotNull; /** @@ -268,18 +270,25 @@ public static ImportedAPIDTO importApi(String extractedFolderPath, APIDTO import APIUtil.validateAPIEndpointConfig(importedApiDTO.getEndpointConfig(), importedApiDTO.getType().toString(), importedApiDTO.getName()); - Map endpointConfig = (Map) importedApiDTO.getEndpointConfig(); - - // if a valid one then update the sequence file path - if (endpointConfig != null && APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( - endpointConfig.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE))) { - if (endpointConfig.get("sequence_name") != null) { - String sequenceFileName = endpointConfig.get("sequence_name").toString(); - String path = extractedFolderPath + File.separator + sequenceFileName; - endpointConfig.put("sequence_path", path); - } - importedApiDTO.setEndpointConfig(endpointConfig); - } +// Map endpointConfig = (Map) importedApiDTO.getEndpointConfig(); +// +// // if a valid one then update the sequence file path +// if (endpointConfig != null && APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( +// endpointConfig.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE))) { +// if (endpointConfig.get("sandbox") != null) { +// Map sandboxConfig = (Map) endpointConfig.get("sandbox"); +// String seqName = sandboxConfig.get("sequence_name").toString(); +// String path = extractedFolderPath + File.separator + seqName; +// String id = sandboxConfig.get("sequence_id").toString(); +// +// } +// if (endpointConfig.get("sequence_name") != null) { +// String sequenceFileName = endpointConfig.get("sequence_name").toString(); +// String path = extractedFolderPath + File.separator + sequenceFileName; +// endpointConfig.put("sequence_path", path); +// } +// importedApiDTO.setEndpointConfig(endpointConfig); +// } API targetApi = retrieveApiToOverwrite(importedApiDTO.getName(), importedApiDTO.getVersion(), currentTenantDomain, apiProvider, Boolean.TRUE, organization); @@ -386,6 +395,13 @@ public static ImportedAPIDTO importApi(String extractedFolderPath, APIDTO import populateAPIWithPolicies(importedApi, apiProvider, extractedFolderPath, extractedPoliciesMap, extractedAPIPolicies, currentTenantDomain); + // Update Custom Backend Data if endpoint type is selected to "custom_backend" + Map endpointConf = (Map) importedApiDTO.getEndpointConfig(); + if (endpointConf != null && APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( + endpointConf.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE))) { + updateAPIWithCustomBackend(importedApi, extractedFolderPath, apiProvider); + } + API oldAPI = apiProvider.getAPIbyUUID(importedApi.getUuid(), importedApi.getOrganization()); apiProvider.updateAPI(importedApi, oldAPI); @@ -674,6 +690,46 @@ public static void validateAppliedPolicy(OperationPolicy appliedPolicy, } } + public static void updateAPIWithCustomBackend(API api, String extractedFolderPath, APIProvider apiProvider) + throws APIManagementException { + String customBackendDir = extractedFolderPath + File.separator + ImportExportConstants.CUSTOM_BACKEND_DIRECTORY; + JsonObject endpointConfig = JsonParser.parseString(api.getEndpointConfig()).getAsJsonObject(); + ObjectMapper objectMapper = new ObjectMapper(); + try { + if (endpointConfig != null) { + if (endpointConfig.get("sandbox") != null) { + JsonObject sandboxConf = endpointConfig.get("sandbox").getAsJsonObject(); + String seqFile = sandboxConf.get("sequence_file").getAsString(); + String type = sandboxConf.get("type").getAsString(); + String seqName = APIUtil.getCustomBackendName(api.getUuid(), type); + String seqId = UUID.randomUUID().toString(); + sandboxConf.addProperty("sequence_id", seqId); + sandboxConf.addProperty("sequence_name", seqName); + endpointConfig.add("sandbox", sandboxConf); + InputStream seq = APIUtil.getCustomBackendSequence(customBackendDir, seqFile, ".xml"); + apiProvider.updateCustomBackend(api, type, seq, seqName, seqId); + } + if (endpointConfig.get("production") != null) { + JsonObject prodConf = endpointConfig.get("production").getAsJsonObject(); + String seqFile = endpointConfig.get("sequence_file").getAsString(); + String type = endpointConfig.get("type").getAsString(); + String seqName = APIUtil.getCustomBackendName(api.getUuid(), type); + String seqId = UUID.randomUUID().toString(); + prodConf.addProperty("sequence_id", seqId); + prodConf.addProperty("sequence_name", seqName); + endpointConfig.add("production", prodConf); + InputStream seq = APIUtil.getCustomBackendSequence(customBackendDir, seqFile, ".xml"); + apiProvider.updateCustomBackend(api, type, seq, seqName, seqId); + } + Map endpointConfMap = new ObjectMapper().readValue(endpointConfig.toString(), + Map.class); + api.setEndpointConfig(objectMapper.writeValueAsString(endpointConfMap)); + } + } catch (IOException ex) { + throw new APIManagementException("Error when updating Endpoint Configuration of API: " + api.getUuid()); + } + } + /** * This method is used to populate uri template of the API with API Level Policies and Operation Level Policies. * diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/PublisherCommonUtils.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/PublisherCommonUtils.java index 61cb45405e82..dd7cbccb9a95 100755 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/PublisherCommonUtils.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/PublisherCommonUtils.java @@ -318,26 +318,6 @@ private static API prepareForUpdateApi(API originalAPI, APIDTO apiDtoToUpdate, A encryptEndpointSecurityOAuthCredentials(endpointConfig, cryptoUtil, oldProductionApiSecret, oldSandboxApiSecret, apiDtoToUpdate); - // update endpointConfig with the provided custom sequence - if (endpointConfig != null) { - if (APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( - endpointConfig.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE))) { - try { - if (endpointConfig.get("sequence_path") != null) { - String pathToSequence = endpointConfig.get("sequence_path").toString(); - String sequence = FileUtils.readFileToString(new File(pathToSequence), - Charset.defaultCharset()); - endpointConfig.put("sequence", sequence); - apiDtoToUpdate.setEndpointConfig(endpointConfig); - } - } catch (IOException ex) { - throw new APIManagementException( - "Error while reading Custom Sequence of API: " + apiDtoToUpdate.getId(), ex, - ExceptionCodes.ERROR_READING_CUSTOM_SEQUENCE); - } - } - } - // AWS Lambda: secret key encryption while updating the API if (apiDtoToUpdate.getEndpointConfig() != null) { if (endpointConfig.containsKey(APIConstants.AMZN_SECRET_KEY)) {