From 5c4b8b98fbd17047809878668ef89637ec79c6d0 Mon Sep 17 00:00:00 2001 From: BLasan Date: Thu, 1 Aug 2024 11:18:03 +0530 Subject: [PATCH 01/56] Init Commit --- .../org/wso2/carbon/apimgt/api/model/API.java | 9 ++++++++ .../wso2/carbon/apimgt/impl/APIConstants.java | 3 +++ .../src/main/resources/publisher-api.yaml | 3 +++ .../rest/api/publisher/v1/dto/APIDTO.java | 22 ++++++++++++++++++- .../v1/common/SynapseArtifactGenerator.java | 19 ++++++++++++++++ .../v1/common/TemplateBuilderUtil.java | 3 ++- .../v1/common/mappings/APIMappingUtil.java | 4 ++++ .../v1/common/mappings/ImportUtils.java | 5 +++++ .../src/main/resources/publisher-api.yaml | 3 +++ .../src/main/resources/publisher-api.yaml | 3 +++ 10 files changed, 72 insertions(+), 2 deletions(-) diff --git a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/API.java b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/API.java index cc2489b9b635..a65e8e9aff6e 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/API.java +++ b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/API.java @@ -86,6 +86,7 @@ public class API implements Serializable { private boolean apiResourcePatternsChanged; private String status; + private String sequence; private String technicalOwner; private String technicalOwnerEmail; @@ -786,6 +787,14 @@ public void setRating(float rating) { this.rating = rating; } + public void setSequence(String sequence) { + this.sequence = sequence; + } + + public String getSequence() { + return sequence; + } + public void setLatest(boolean latest) { isLatest = latest; } diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConstants.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConstants.java index d37671ad8fc2..074727ce8d0a 100755 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConstants.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConstants.java @@ -1783,6 +1783,9 @@ private ConfigParameters() { public static final String ENDPOINT_TYPE_SERVICE = "service"; public static final String ENDPOINT_TYPE_ADDRESS = "address"; public static final String ENDPOINT_TYPE_AWSLAMBDA = "awslambda"; + public static final String ENDPOINT_TYPE_SEQUENCE = "custom_sequence"; + + public static final String SEQUENCE_DATA = "sequence"; public static final String ENDPOINT_PRODUCTION_FAILOVERS = "production_failovers"; public static final String ENDPOINT_SANDBOX_FAILOVERS = "sandbox_failovers"; public static final String ENDPOINT_PRODUCTION_ENDPOINTS = "production_endpoints"; diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml b/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml index d25b133d4465..f6c24924cc54 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml @@ -9831,6 +9831,9 @@ components: enableSubscriberVerification: type: boolean example: false + sequence: + type: string + example: in sequence type: type: string description: The api creation type to be used. Accepted values are HTTP, diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/dto/APIDTO.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/dto/APIDTO.java index 81d415d20ffa..f67451bca1da 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/dto/APIDTO.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/dto/APIDTO.java @@ -63,6 +63,7 @@ public class APIDTO { private Integer revisionId = null; private Boolean enableSchemaValidation = null; private Boolean enableSubscriberVerification = null; + private String sequence = null; @XmlType(name="TypeEnum") @XmlEnum(String.class) @@ -639,6 +640,23 @@ public void setEnableSubscriberVerification(Boolean enableSubscriberVerification this.enableSubscriberVerification = enableSubscriberVerification; } + /** + **/ + public APIDTO sequence(String sequence) { + this.sequence = sequence; + return this; + } + + + @ApiModelProperty(example = "in sequence", value = "") + @JsonProperty("sequence") + public String getSequence() { + return sequence; + } + public void setSequence(String sequence) { + this.sequence = sequence; + } + /** * The api creation type to be used. Accepted values are HTTP, WS, SOAPTOREST, GRAPHQL, WEBSUB, SSE, WEBHOOK, ASYNC **/ @@ -1470,6 +1488,7 @@ public boolean equals(java.lang.Object o) { Objects.equals(revisionId, API.revisionId) && Objects.equals(enableSchemaValidation, API.enableSchemaValidation) && Objects.equals(enableSubscriberVerification, API.enableSubscriberVerification) && + Objects.equals(sequence, API.sequence) && Objects.equals(type, API.type) && Objects.equals(subtype, API.subtype) && Objects.equals(audience, API.audience) && @@ -1519,7 +1538,7 @@ public boolean equals(java.lang.Object o) { @Override public int hashCode() { - return Objects.hash(id, name, description, context, version, provider, lifeCycleStatus, wsdlInfo, wsdlUrl, responseCachingEnabled, cacheTimeout, hasThumbnail, isDefaultVersion, isRevision, revisionedApiId, revisionId, enableSchemaValidation, enableSubscriberVerification, type, subtype, audience, audiences, transport, tags, policies, apiThrottlingPolicy, authorizationHeader, apiKeyHeader, securityScheme, maxTps, visibility, visibleRoles, visibleTenants, mediationPolicies, apiPolicies, subscriptionAvailability, subscriptionAvailableTenants, additionalProperties, additionalPropertiesMap, monetization, accessControl, accessControlRoles, businessInformation, corsConfiguration, websubSubscriptionConfiguration, workflowStatus, createdTime, lastUpdatedTimestamp, lastUpdatedTime, endpointConfig, endpointImplementationType, aiConfiguration, scopes, operations, threatProtectionPolicies, categories, keyManagers, serviceInfo, advertiseInfo, gatewayVendor, gatewayType, asyncTransportProtocols, egress); + return Objects.hash(id, name, description, context, version, sequence, provider, lifeCycleStatus, wsdlInfo, wsdlUrl, responseCachingEnabled, cacheTimeout, hasThumbnail, isDefaultVersion, isRevision, revisionedApiId, revisionId, enableSchemaValidation, enableSubscriberVerification, type, subtype, audience, audiences, transport, tags, policies, apiThrottlingPolicy, authorizationHeader, apiKeyHeader, securityScheme, maxTps, visibility, visibleRoles, visibleTenants, mediationPolicies, apiPolicies, subscriptionAvailability, subscriptionAvailableTenants, additionalProperties, additionalPropertiesMap, monetization, accessControl, accessControlRoles, businessInformation, corsConfiguration, websubSubscriptionConfiguration, workflowStatus, createdTime, lastUpdatedTimestamp, lastUpdatedTime, endpointConfig, endpointImplementationType, aiConfiguration, scopes, operations, threatProtectionPolicies, categories, keyManagers, serviceInfo, advertiseInfo, gatewayVendor, gatewayType, asyncTransportProtocols, egress); } @Override @@ -1545,6 +1564,7 @@ public String toString() { sb.append(" revisionId: ").append(toIndentedString(revisionId)).append("\n"); sb.append(" enableSchemaValidation: ").append(toIndentedString(enableSchemaValidation)).append("\n"); sb.append(" enableSubscriberVerification: ").append(toIndentedString(enableSubscriberVerification)).append("\n"); + sb.append(" sequence: ").append(toIndentedString(sequence)).append("\n"); sb.append(" type: ").append(toIndentedString(type)).append("\n"); sb.append(" subtype: ").append(toIndentedString(subtype)).append("\n"); sb.append(" audience: ").append(toIndentedString(audience)).append("\n"); 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/SynapseArtifactGenerator.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/SynapseArtifactGenerator.java index b05f7e4ed571..8851aeb1733f 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/SynapseArtifactGenerator.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/SynapseArtifactGenerator.java @@ -23,6 +23,7 @@ import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.json.JSONObject; import org.osgi.service.component.annotations.Component; import org.wso2.carbon.apimgt.api.APIDefinition; import org.wso2.carbon.apimgt.api.APIDefinitionValidationResponse; @@ -105,6 +106,22 @@ public RuntimeArtifactDto generateGatewayArtifact(List ap tenantDomain, extractedFolderPath); } else { APIDTO apidto = ImportUtils.retrievedAPIDto(extractedFolderPath); + if (apidto.getEndpointConfig() != null) { + // convert sequence to string + JSONObject endpointObject = (JSONObject) apidto.getEndpointConfig(); + if (APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( + endpointObject.get(APIConstants.ENDPOINT_TYPE_SEQUENCE))) { + try (InputStream sequence = (InputStream) endpointObject.get( + APIConstants.SEQUENCE_DATA)) { + File dir = CommonUtil.createTempDirectory(null); + String path = ImportUtils.getArchivePathOfExtractedDirectory( + dir.getAbsolutePath(), sequence); + String content = ImportUtils.retrieveXMLContent(path); + endpointObject.put("sequence", content); + apidto.setEndpointConfig(endpointObject); + } + } + } API api = APIMappingUtil.fromDTOtoAPI(apidto, apidto.getProvider()); api.setUUID(apidto.getId()); if (APIConstants.APITransportType.GRAPHQL.toString().equals(api.getType())) { @@ -125,6 +142,8 @@ public RuntimeArtifactDto generateGatewayArtifact(List ap api.setGraphQLSchema(graphqlSchema); gatewayAPIDTO = TemplateBuilderUtil.retrieveGatewayAPIDto(api, environment, tenantDomain, apidto, extractedFolderPath); +// // if sequence is passed, then override the existing config +// gatewayAPIDTO.setApiDefinition(api.getAsyncApiDefinition()); } else if (api.getType() != null && (APIConstants.APITransportType.HTTP.toString().equals(api.getType()) || APIConstants.API_TYPE_SOAP.equals(api.getType()) 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/TemplateBuilderUtil.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/TemplateBuilderUtil.java index bd35cd8fae60..9bf90bbaaf10 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/TemplateBuilderUtil.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/TemplateBuilderUtil.java @@ -859,7 +859,8 @@ private static GatewayAPIDTO createAPIGatewayDTOtoPublishAPI(Environment environ String apiConfig = builder.getConfigStringForTemplate(environment); gatewayAPIDTO.setApiDefinition(apiConfig); if (endpointConfig != null && !endpointConfig.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE) - .equals(APIConstants.ENDPOINT_TYPE_AWSLAMBDA)) { + .equals(APIConstants.ENDPOINT_TYPE_AWSLAMBDA) && !endpointConfig.get( + APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE).equals(APIConstants.ENDPOINT_TYPE_SEQUENCE)) { if (!isWsApi) { addEndpoints(api, builder, gatewayAPIDTO); } 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/APIMappingUtil.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/APIMappingUtil.java index 5804fb64cb86..10612dfb8fad 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/APIMappingUtil.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/APIMappingUtil.java @@ -194,6 +194,10 @@ public static API fromDTOtoAPI(APIDTO dto, String provider) throws APIManagement model.setContext(context); model.setDescription(dto.getDescription()); + if (dto != null && dto.getSequence() != null) { + model.setSequence(dto.getSequence()); + } + Object endpointConfig = dto.getEndpointConfig(); if (endpointConfig != null) { ObjectMapper mapper = new ObjectMapper(); 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 71646c2243fc..90c9c06c57fe 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 @@ -111,6 +111,7 @@ import java.io.IOException; import java.io.InputStream; import java.net.URLConnection; +import java.nio.charset.Charset; import java.nio.file.DirectoryIteratorException; import java.nio.file.DirectoryStream; import java.nio.file.Files; @@ -1307,6 +1308,10 @@ public static APIProductDTO retrieveAPIProductDto(String pathToArchive) throws I return new Gson().fromJson(jsonObject, APIProductDTO.class); } + public static String retrieveXMLContent(String pathToArchive) throws IOException, APIManagementException { + return FileUtils.readFileToString(new File(pathToArchive), Charset.defaultCharset()); + } + /** * This function will preprocess endpoint config security. * diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/resources/publisher-api.yaml b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/resources/publisher-api.yaml index d25b133d4465..f6c24924cc54 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/resources/publisher-api.yaml +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/resources/publisher-api.yaml @@ -9831,6 +9831,9 @@ components: enableSubscriberVerification: type: boolean example: false + sequence: + type: string + example: in sequence type: type: string description: The api creation type to be used. Accepted values are HTTP, diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.util/src/main/resources/publisher-api.yaml b/components/apimgt/org.wso2.carbon.apimgt.rest.api.util/src/main/resources/publisher-api.yaml index ec8be349b52e..c202a51c4687 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.util/src/main/resources/publisher-api.yaml +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.util/src/main/resources/publisher-api.yaml @@ -8539,6 +8539,9 @@ components: isDefaultVersion: type: boolean example: false + sequence: + type: string + example: in sequence isRevision: type: boolean example: false From c5c91493b49aae9eb156ff47cd9741fc1dd81f9f Mon Sep 17 00:00:00 2001 From: BLasan Date: Mon, 12 Aug 2024 11:48:42 +0530 Subject: [PATCH 02/56] Add: New REST API to upload Custom Sequence --- .../src/main/resources/publisher-api.yaml | 77 +++++++++++++++++++ .../src/main/resources/publisher-api.yaml | 77 +++++++++++++++++++ 2 files changed, 154 insertions(+) diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml b/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml index f6c24924cc54..56f7d0911927 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml @@ -487,6 +487,83 @@ paths: -H "Content-Type: application/json" -d @data.json "https://127.0.0.1:9443/api/am/publisher/v4/apis/7a2298c4-c905-403f-8fac-38c73301631f/topics"' + /apis/{apiId}/custom-sequence: + put: + tags: + - APIs + summary: Upload Custom Sequence as the Endpoint of the API + description: This operation can be used to change the endpoint of the API to Custom Sequence + operationId: customSequenceUpdate + parameters: + - $ref: '#/components/parameters/apiId' + - $ref: '#/components/parameters/If-Match' + requestBody: + content: + multipart/form-data: + schema: + properties: + sequence: + type: string + description: The sequence that needs to be uploaded. + format: binary + type: + type: string + description: Type of the Endpoint + apiData: + type: string + description: API Data to be updated + responses: + 200: + description: | + OK. + Successful response with updated API object + headers: + ETag: + description: | + Entity Tag of the response resource. Used by caches, or in conditional requests (Will be supported in future). + schema: + type: string + Last-Modified: + description: | + Date and time the resource has been modifed the last time. + Used by caches, or in conditional requests (Will be supported in future). + schema: + type: string + Location: + description: | + The URL of the newly created resource. + schema: + type: string + Content-Type: + description: | + The content type of the body. + schema: + type: string + content: + application/json: + schema: + $ref: '#/components/schemas/API' + 400: + $ref: '#/components/responses/BadRequest' + 403: + $ref: '#/components/responses/Forbidden' + 404: + $ref: '#/components/responses/NotFound' + 409: + $ref: '#/components/responses/Conflict' + 412: + $ref: '#/components/responses/PreconditionFailed' + security: + - OAuth2Security: + - apim:api_create + - apim:api_manage + - apim:api_publish + x-code-samples: + - lang: Curl + source: 'curl -k -X PUT -H "Authorization: Bearer ae4eae22-3f65-387b-a171-d37eaa366fa8" + -H "Content-Type: application/json" -d @data.json "https://127.0.0.1:9443/api/am/publisher/v4/apis/7a2298c4-c905-403f-8fac-38c73301631f"' + + /apis/{apiId}/reimport-service: put: tags: diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/resources/publisher-api.yaml b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/resources/publisher-api.yaml index f6c24924cc54..56f7d0911927 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/resources/publisher-api.yaml +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/resources/publisher-api.yaml @@ -487,6 +487,83 @@ paths: -H "Content-Type: application/json" -d @data.json "https://127.0.0.1:9443/api/am/publisher/v4/apis/7a2298c4-c905-403f-8fac-38c73301631f/topics"' + /apis/{apiId}/custom-sequence: + put: + tags: + - APIs + summary: Upload Custom Sequence as the Endpoint of the API + description: This operation can be used to change the endpoint of the API to Custom Sequence + operationId: customSequenceUpdate + parameters: + - $ref: '#/components/parameters/apiId' + - $ref: '#/components/parameters/If-Match' + requestBody: + content: + multipart/form-data: + schema: + properties: + sequence: + type: string + description: The sequence that needs to be uploaded. + format: binary + type: + type: string + description: Type of the Endpoint + apiData: + type: string + description: API Data to be updated + responses: + 200: + description: | + OK. + Successful response with updated API object + headers: + ETag: + description: | + Entity Tag of the response resource. Used by caches, or in conditional requests (Will be supported in future). + schema: + type: string + Last-Modified: + description: | + Date and time the resource has been modifed the last time. + Used by caches, or in conditional requests (Will be supported in future). + schema: + type: string + Location: + description: | + The URL of the newly created resource. + schema: + type: string + Content-Type: + description: | + The content type of the body. + schema: + type: string + content: + application/json: + schema: + $ref: '#/components/schemas/API' + 400: + $ref: '#/components/responses/BadRequest' + 403: + $ref: '#/components/responses/Forbidden' + 404: + $ref: '#/components/responses/NotFound' + 409: + $ref: '#/components/responses/Conflict' + 412: + $ref: '#/components/responses/PreconditionFailed' + security: + - OAuth2Security: + - apim:api_create + - apim:api_manage + - apim:api_publish + x-code-samples: + - lang: Curl + source: 'curl -k -X PUT -H "Authorization: Bearer ae4eae22-3f65-387b-a171-d37eaa366fa8" + -H "Content-Type: application/json" -d @data.json "https://127.0.0.1:9443/api/am/publisher/v4/apis/7a2298c4-c905-403f-8fac-38c73301631f"' + + /apis/{apiId}/reimport-service: put: tags: From b55dabbf33f4153ae6b510bc8463da22d8fdb43c Mon Sep 17 00:00:00 2001 From: BLasan Date: Thu, 15 Aug 2024 17:48:19 +0530 Subject: [PATCH 03/56] Add: Custom Sequence support for the APIS --- .../carbon/apimgt/api/ExceptionCodes.java | 3 ++ .../wso2/carbon/apimgt/impl/APIConstants.java | 1 - .../carbon/apimgt/impl/utils/APIUtil.java | 11 +++++ .../v1/common/SynapseArtifactGenerator.java | 30 ++++++-------- .../v1/common/mappings/ImportUtils.java | 29 ++++++++++++-- .../common/mappings/PublisherCommonUtils.java | 40 ++++++++++++++++++- .../apimgt/rest/api/publisher/v1/ApisApi.java | 22 ++++++++++ .../rest/api/publisher/v1/ApisApiService.java | 1 + .../publisher/v1/impl/ApisApiServiceImpl.java | 37 +++++++++++++++++ 9 files changed, 151 insertions(+), 23 deletions(-) diff --git a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/ExceptionCodes.java b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/ExceptionCodes.java index a5033939ff01..739468645392 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/ExceptionCodes.java +++ b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/ExceptionCodes.java @@ -482,6 +482,9 @@ public enum ExceptionCodes implements ErrorHandler { // API import/export related codes ERROR_READING_META_DATA(900907, "Error while reading meta information from the definition", 400, "Error while reading meta information from the definition"), + + ERROR_READING_CUSTOM_SEQUENCE(900908, "Error while reading Custom Sequence from the API Endpoint Configuration", 400, + "Error while reading Custom Sequence from the API Endpoint Configuration"), ERROR_READING_PARAMS_FILE(900908, "Error while reading meta information from the params file", 400, "Error while reading meta information from the params file"), ERROR_FETCHING_DEFINITION_FILE(900909, "Cannot find the definition file of the project", 400, diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConstants.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConstants.java index 074727ce8d0a..4ec1b295ab42 100755 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConstants.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConstants.java @@ -1784,7 +1784,6 @@ private ConfigParameters() { public static final String ENDPOINT_TYPE_ADDRESS = "address"; public static final String ENDPOINT_TYPE_AWSLAMBDA = "awslambda"; public static final String ENDPOINT_TYPE_SEQUENCE = "custom_sequence"; - public static final String SEQUENCE_DATA = "sequence"; public static final String ENDPOINT_PRODUCTION_FAILOVERS = "production_failovers"; public static final String ENDPOINT_SANDBOX_FAILOVERS = "sandbox_failovers"; 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 4af0ebda42c3..583985e21581 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 @@ -2921,6 +2921,17 @@ public static void validateAPIContext(String context, String apiName) throws API } } + public static void validateAPIEndpointConfig(Object endpointConfigObject, String apiType, String apiName) + throws APIManagementException { + Map endpointConfigMap = (Map) endpointConfigObject; + if (endpointConfigMap.containsKey("endpoint_type") && APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( + endpointConfigMap.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE)) + && !APIConstants.APITransportType.GRAPHQL.toString().equalsIgnoreCase(apiType) + && !APIConstants.API_TYPE_HTTP.equalsIgnoreCase(apiType)) { + throw new APIManagementException("Invalid endpoint configuration provided for the API " + apiName); + } + } + /** * Check whether the parentheses are balanced * 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/SynapseArtifactGenerator.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/SynapseArtifactGenerator.java index 8851aeb1733f..c066efddd2f0 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/SynapseArtifactGenerator.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/SynapseArtifactGenerator.java @@ -23,7 +23,6 @@ import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.json.JSONObject; import org.osgi.service.component.annotations.Component; import org.wso2.carbon.apimgt.api.APIDefinition; import org.wso2.carbon.apimgt.api.APIDefinitionValidationResponse; @@ -106,22 +105,17 @@ public RuntimeArtifactDto generateGatewayArtifact(List ap tenantDomain, extractedFolderPath); } else { APIDTO apidto = ImportUtils.retrievedAPIDto(extractedFolderPath); - if (apidto.getEndpointConfig() != null) { - // convert sequence to string - JSONObject endpointObject = (JSONObject) apidto.getEndpointConfig(); - if (APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( - endpointObject.get(APIConstants.ENDPOINT_TYPE_SEQUENCE))) { - try (InputStream sequence = (InputStream) endpointObject.get( - APIConstants.SEQUENCE_DATA)) { - File dir = CommonUtil.createTempDirectory(null); - String path = ImportUtils.getArchivePathOfExtractedDirectory( - dir.getAbsolutePath(), sequence); - String content = ImportUtils.retrieveXMLContent(path); - endpointObject.put("sequence", content); - apidto.setEndpointConfig(endpointObject); - } - } - } +// if (apidto.getEndpointConfig() != null) { +// org.json.JSONObject endpointConfig = new org.json.JSONObject( +// new Gson().toJson(apidto.getEndpointConfig())); +// if (APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( +// endpointConfig.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE))) { +// String content = ImportUtils.retrieveXMLContent( +// (InputStream) endpointConfig.get(APIConstants.SEQUENCE_DATA)); +// endpointConfig.put("sequence", content); +// } +// apidto.setEndpointConfig(endpointConfig); +// } API api = APIMappingUtil.fromDTOtoAPI(apidto, apidto.getProvider()); api.setUUID(apidto.getId()); if (APIConstants.APITransportType.GRAPHQL.toString().equals(api.getType())) { @@ -145,7 +139,7 @@ public RuntimeArtifactDto generateGatewayArtifact(List ap // // if sequence is passed, then override the existing config // gatewayAPIDTO.setApiDefinition(api.getAsyncApiDefinition()); } else if (api.getType() != null && - (APIConstants.APITransportType.HTTP.toString().equals(api.getType()) + (APIConstants.APITransportType.HTTP.toString ().equals(api.getType()) || APIConstants.API_TYPE_SOAP.equals(api.getType()) || APIConstants.API_TYPE_SOAPTOREST.equals(api.getType()) || APIConstants.APITransportType.WEBHOOK.toString() 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 90c9c06c57fe..fe0eeaad7775 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 @@ -103,6 +103,7 @@ import org.wso2.carbon.utils.multitenancy.MultitenantConstants; import org.wso2.carbon.utils.multitenancy.MultitenantUtils; +import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; @@ -110,8 +111,8 @@ import java.io.FileReader; import java.io.IOException; import java.io.InputStream; +import java.io.InputStreamReader; import java.net.URLConnection; -import java.nio.charset.Charset; import java.nio.file.DirectoryIteratorException; import java.nio.file.DirectoryStream; import java.nio.file.Files; @@ -264,6 +265,21 @@ public static ImportedAPIDTO importApi(String extractedFolderPath, APIDTO import // validate the API context APIUtil.validateAPIContext(importedApiDTO.getContext(), importedApiDTO.getName()); + // Get the endpoint config object updated + 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))) { + String sequenceFileName = endpointConfig.get("sequence").toString(); + String path = extractedFolderPath + File.separator + sequenceFileName; + endpointConfig.put("sequence", path); + importedApiDTO.setEndpointConfig(endpointConfig); + } + API targetApi = retrieveApiToOverwrite(importedApiDTO.getName(), importedApiDTO.getVersion(), currentTenantDomain, apiProvider, Boolean.TRUE, organization); @@ -1308,8 +1324,15 @@ public static APIProductDTO retrieveAPIProductDto(String pathToArchive) throws I return new Gson().fromJson(jsonObject, APIProductDTO.class); } - public static String retrieveXMLContent(String pathToArchive) throws IOException, APIManagementException { - return FileUtils.readFileToString(new File(pathToArchive), Charset.defaultCharset()); + public static String retrieveXMLContent(InputStream sequenceInputStream) throws IOException { + StringBuilder result = new StringBuilder(); + try (BufferedReader reader = new BufferedReader(new InputStreamReader(sequenceInputStream))) { + String line; + while ((line = reader.readLine()) != null) { + result.append(line).append("\n"); + } + } + return result.toString(); } /** 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 8931bc46fa37..c30827fb8d0d 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 @@ -29,6 +29,7 @@ import graphql.schema.validation.SchemaValidationError; import graphql.schema.validation.SchemaValidator; import io.swagger.v3.parser.ObjectMapperFactory; +import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; @@ -98,9 +99,11 @@ import org.wso2.carbon.core.util.CryptoUtil; import org.wso2.carbon.utils.multitenancy.MultitenantConstants; +import java.io.File; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Field; +import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -294,6 +297,25 @@ private static API prepareForUpdateApi(API originalAPI, APIDTO apiDtoToUpdate, A encryptEndpointSecurityApiKeyCredentials(endpointConfig, cryptoUtil, oldProductionApiKeyValue, oldSandboxApiKeyValue, 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") != null) { + String pathToSequence = endpointConfig.get("sequence").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) { @@ -1072,9 +1094,25 @@ public static API addAPIWithGeneratedSwaggerDefinition(APIDTO apiDto, String oas encryptEndpointSecurityApiKeyCredentials(endpointConfig, cryptoUtil, StringUtils.EMPTY, StringUtils.EMPTY, apiDto); + // update endpointConfig with the provided custom sequence + if (endpointConfig != null) { + endpointConfig.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE))) { + try { + if (endpointConfig.get("sequence") != null) { + String pathToSequence = endpointConfig.get("sequence").toString(); + String sequence = FileUtils.readFileToString(new File(pathToSequence), + Charset.defaultCharset()); + endpointConfig.put("sequence", sequence); + apiDto.setEndpointConfig(endpointConfig); + } + } catch (IOException ex) { + throw new APIManagementException("Error while reading Custom Sequence of API: " + apiDto.getId(), + ex, ExceptionCodes.ERROR_READING_CUSTOM_SEQUENCE); + } + } + } // AWS Lambda: secret key encryption while creating the API - if (apiDto.getEndpointConfig() != null) { if (endpointConfig.containsKey(APIConstants.AMZN_SECRET_KEY)) { String secretKey = (String) endpointConfig.get(APIConstants.AMZN_SECRET_KEY); if (!StringUtils.isEmpty(secretKey)) { diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApi.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApi.java index 95ec977cfc81..50c82bdb3cf2 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApi.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApi.java @@ -377,6 +377,28 @@ public Response createNewAPIVersion( @NotNull @Size(max=30) @ApiParam(value = "V return delegate.createNewAPIVersion(newVersion, apiId, defaultVersion, serviceVersion, securityContext); } + @PUT + @Path("/{apiId}/custom-sequence") + @Consumes({ "multipart/form-data" }) + @Produces({ "application/json" }) + @ApiOperation(value = "Upload Custom Sequence as the Endpoint of the API", notes = "This operation can be used to change the endpoint of the API to Custom Sequence", response = APIDTO.class, authorizations = { + @Authorization(value = "OAuth2Security", scopes = { + @AuthorizationScope(scope = "apim:api_create", description = "Create API"), + @AuthorizationScope(scope = "apim:api_manage", description = "Manage all API related operations"), + @AuthorizationScope(scope = "apim:api_publish", description = "Publish API") + }) + }, tags={ "APIs", }) + @ApiResponses(value = { + @ApiResponse(code = 200, message = "OK. Successful response with updated API object ", response = APIDTO.class), + @ApiResponse(code = 400, message = "Bad Request. Invalid request or validation error.", response = ErrorDTO.class), + @ApiResponse(code = 403, message = "Forbidden. The request must be conditional but no condition has been specified.", response = ErrorDTO.class), + @ApiResponse(code = 404, message = "Not Found. The specified resource does not exist.", response = ErrorDTO.class), + @ApiResponse(code = 409, message = "Conflict. Specified resource already exists.", response = ErrorDTO.class), + @ApiResponse(code = 412, message = "Precondition Failed. The request has not been performed because one of the preconditions is not met.", response = ErrorDTO.class) }) + public Response customSequenceUpdate(@ApiParam(value = "**API ID** consisting of the **UUID** of the API. ",required=true) @PathParam("apiId") String apiId, @ApiParam(value = "Validator for conditional requests; based on ETag. " )@HeaderParam("If-Match") String ifMatch, @Multipart(value = "sequence", required = false) InputStream sequenceInputStream, @Multipart(value = "sequence" , required = false) Attachment sequenceDetail, @Multipart(value = "type", required = false) String type, @Multipart(value = "apiData", required = false) String apiData) throws APIManagementException{ + return delegate.customSequenceUpdate(apiId, ifMatch, sequenceInputStream, sequenceDetail, type, apiData, securityContext); + } + @DELETE @Path("/{apiId}") diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApiService.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApiService.java index e6b1fd08e701..019c6471b7c7 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApiService.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApiService.java @@ -79,6 +79,7 @@ public interface ApisApiService { public Response createAPI(APIDTO APIDTO, String openAPIVersion, MessageContext messageContext) throws APIManagementException; public Response createAPIRevision(String apiId, APIRevisionDTO apIRevisionDTO, MessageContext messageContext) throws APIManagementException; public Response createNewAPIVersion(String newVersion, String apiId, Boolean defaultVersion, String serviceVersion, MessageContext messageContext) throws APIManagementException; + public Response customSequenceUpdate(String apiId, String ifMatch, InputStream sequenceInputStream, Attachment sequenceDetail, String type, String apiData, MessageContext messageContext) throws APIManagementException; public Response deleteAPI(String apiId, String ifMatch, MessageContext messageContext) throws APIManagementException; public Response deleteAPIClientCertificateByAlias(String alias, String apiId, MessageContext messageContext) throws APIManagementException; public Response deleteAPIClientCertificateByKeyTypeAndAlias(String keyType, String alias, String apiId, MessageContext messageContext) throws APIManagementException; diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java index 90f3ca802aa2..d98db322105d 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java @@ -21,6 +21,10 @@ import com.amazonaws.SdkClientException; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.gson.Gson; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import com.google.gson.internal.LinkedTreeMap; import org.apache.commons.collections.MapUtils; import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.IOUtils; @@ -202,6 +206,31 @@ public Response getAPI(String apiId, String xWSO2Tenant, String ifNoneMatch, return Response.ok().entity(apiToReturn).build(); } + @Override + public Response customSequenceUpdate(String apiId, String ifMatch, InputStream sequenceInputStream, + Attachment sequenceDetail, String type, String apiData, MessageContext messageContext) + throws APIManagementException { + APIDTO apidto = new Gson().fromJson(apiData, APIDTO.class); + // Get the endpoint config object updated + APIUtil.validateAPIEndpointConfig(apidto.getEndpointConfig(), apidto.getType().toString(), + apidto.getName()); + if (apidto.getEndpointConfig() != null) { + org.json.JSONObject endpointConfig = new org.json.JSONObject(new Gson().toJson(apidto.getEndpointConfig())); + if (APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( + endpointConfig.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE))) { + try { + String content = ImportUtils.retrieveXMLContent(sequenceInputStream); + endpointConfig.put("sequence", content); + } catch (IOException ex) { + RestApiUtil.handleInternalServerError("Failed to read Custom Sequence of " + apiId, ex, log); + } + } + apidto.setEndpointConfig(new Gson().fromJson(endpointConfig.toString(), Object.class)); + } + updateAPI(apiId, apidto, ifMatch, messageContext); + return Response.ok().entity(null).build(); + } + @Override public Response addCommentToAPI(String apiId, PostRequestBodyDTO postRequestBodyDTO, String replyTo, MessageContext messageContext) throws APIManagementException { @@ -3892,6 +3921,14 @@ public Response deployAPIRevision(String apiId, String revisionId, //validate whether the API is advertise only APIDTO apiDto = getAPIByID(apiId, apiProvider, organization); + + // Cannot deploy an API with custom sequence to the APK gateway + Map endpointConfigMap = (Map) apiDto.getEndpointConfig(); + if (APIConstants.WSO2_APK_GATEWAY.equals(apiDto.getGatewayType()) && APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( + endpointConfigMap.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE))) { + return Response.status(Response.Status.BAD_REQUEST) + .entity("Cannot Deploy an API with a Custom Sequence to APK Gateway: " + apiId).build(); + } // Reject the request if API lifecycle is 'RETIRED'. if (apiDto.getLifeCycleStatus().equals(APIConstants.RETIRED)) { String errorMessage = "Deploying API Revisions is not supported for retired APIs. ApiId: " + apiId; From 74443fbf6701730ffa98c61dce18a29d5e4c1b07 Mon Sep 17 00:00:00 2001 From: BLasan Date: Fri, 16 Aug 2024 17:14:26 +0530 Subject: [PATCH 04/56] Fix: Analytics Type conversion issue --- .../handlers/common/APIMgtLatencyStatsHandler.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/common/APIMgtLatencyStatsHandler.java b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/common/APIMgtLatencyStatsHandler.java index 2d14790fca61..4b06dc73f491 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/common/APIMgtLatencyStatsHandler.java +++ b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/common/APIMgtLatencyStatsHandler.java @@ -109,11 +109,12 @@ public boolean handleResponse(MessageContext messageContext) { org.apache.axis2.context.MessageContext.setCurrentMessageContext(axis2MC); if (messageContext.getProperty(APIMgtGatewayConstants.BACKEND_REQUEST_END_TIME) == null) { messageContext.setProperty(APIMgtGatewayConstants.BACKEND_REQUEST_END_TIME, System.currentTimeMillis()); - if (APIUtil.isAnalyticsEnabled()) { - long executionStartTime = Long.parseLong((String) messageContext.getProperty(APIMgtGatewayConstants - .BACKEND_REQUEST_START_TIME)); - messageContext.setProperty(APIMgtGatewayConstants.BACKEND_LATENCY, System.currentTimeMillis() - - executionStartTime); + if (APIUtil.isAnalyticsEnabled() + && messageContext.getProperty(APIMgtGatewayConstants.BACKEND_REQUEST_START_TIME) != null) { + long executionStartTime = Long.parseLong( + (String) messageContext.getProperty(APIMgtGatewayConstants.BACKEND_REQUEST_START_TIME)); + messageContext.setProperty(APIMgtGatewayConstants.BACKEND_LATENCY, + System.currentTimeMillis() - executionStartTime); } } return true; From 65628a452e517296c424f70726bef42717cb9ccf Mon Sep 17 00:00:00 2001 From: BLasan Date: Sun, 18 Aug 2024 01:14:02 +0530 Subject: [PATCH 05/56] Revamp: Endpoint Config update for API import via APICTL and REST --- .../java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java | 3 ++- .../api/publisher/v1/common/mappings/ImportUtils.java | 4 ++-- .../v1/common/mappings/PublisherCommonUtils.java | 8 ++++---- 3 files changed, 8 insertions(+), 7 deletions(-) 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 583985e21581..4395318c8c67 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 @@ -2927,7 +2927,8 @@ public static void validateAPIEndpointConfig(Object endpointConfigObject, String if (endpointConfigMap.containsKey("endpoint_type") && APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( endpointConfigMap.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE)) && !APIConstants.APITransportType.GRAPHQL.toString().equalsIgnoreCase(apiType) - && !APIConstants.API_TYPE_HTTP.equalsIgnoreCase(apiType)) { + && !APIConstants.API_TYPE_HTTP.equalsIgnoreCase(apiType) + && !APIConstants.API_TYPE_SOAPTOREST.equalsIgnoreCase(apiType)) { throw new APIManagementException("Invalid endpoint configuration provided for the API " + apiName); } } 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 fe0eeaad7775..119cfc8c4fdf 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 @@ -274,9 +274,9 @@ public static ImportedAPIDTO importApi(String extractedFolderPath, APIDTO import // 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))) { - String sequenceFileName = endpointConfig.get("sequence").toString(); + String sequenceFileName = endpointConfig.get("sequence_path").toString(); String path = extractedFolderPath + File.separator + sequenceFileName; - endpointConfig.put("sequence", path); + endpointConfig.put("sequence_path", path); importedApiDTO.setEndpointConfig(endpointConfig); } 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 c30827fb8d0d..af0514e912e5 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 @@ -302,8 +302,8 @@ private static API prepareForUpdateApi(API originalAPI, APIDTO apiDtoToUpdate, A if (APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( endpointConfig.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE))) { try { - if (endpointConfig.get("sequence") != null) { - String pathToSequence = endpointConfig.get("sequence").toString(); + 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); @@ -1098,8 +1098,8 @@ public static API addAPIWithGeneratedSwaggerDefinition(APIDTO apiDto, String oas if (endpointConfig != null) { endpointConfig.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE))) { try { - if (endpointConfig.get("sequence") != null) { - String pathToSequence = endpointConfig.get("sequence").toString(); + 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); From e7f8f2f7aa4aa5228c585c9f5fd8b94a34f1dec9 Mon Sep 17 00:00:00 2001 From: BLasan Date: Sun, 25 Aug 2024 18:38:17 +0530 Subject: [PATCH 06/56] Fix: Sequence generating Issue --- .../carbon/apimgt/impl/utils/APIUtil.java | 4 +- .../v1/common/SynapseArtifactGenerator.java | 13 ---- .../v1/common/SynapsePolicyAggregator.java | 31 ++++++++++ .../v1/common/TemplateBuilderUtil.java | 62 ++++++++++++++++--- .../v1/common/mappings/APIMappingUtil.java | 12 ++++ .../v1/common/mappings/ImportUtils.java | 8 ++- .../publisher/v1/impl/ApisApiServiceImpl.java | 3 +- 7 files changed, 105 insertions(+), 28 deletions(-) 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 4395318c8c67..facaa270ac65 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 @@ -2924,7 +2924,9 @@ public static void validateAPIContext(String context, String apiName) throws API public static void validateAPIEndpointConfig(Object endpointConfigObject, String apiType, String apiName) throws APIManagementException { Map endpointConfigMap = (Map) endpointConfigObject; - if (endpointConfigMap.containsKey("endpoint_type") && APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( + + if (endpointConfigMap.containsKey("endpoint_type") && endpointConfigMap.containsKey("sequence_name") + && APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( endpointConfigMap.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE)) && !APIConstants.APITransportType.GRAPHQL.toString().equalsIgnoreCase(apiType) && !APIConstants.API_TYPE_HTTP.equalsIgnoreCase(apiType) 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/SynapseArtifactGenerator.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/SynapseArtifactGenerator.java index c066efddd2f0..d81e65a4c43b 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/SynapseArtifactGenerator.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/SynapseArtifactGenerator.java @@ -105,17 +105,6 @@ public RuntimeArtifactDto generateGatewayArtifact(List ap tenantDomain, extractedFolderPath); } else { APIDTO apidto = ImportUtils.retrievedAPIDto(extractedFolderPath); -// if (apidto.getEndpointConfig() != null) { -// org.json.JSONObject endpointConfig = new org.json.JSONObject( -// new Gson().toJson(apidto.getEndpointConfig())); -// if (APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( -// endpointConfig.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE))) { -// String content = ImportUtils.retrieveXMLContent( -// (InputStream) endpointConfig.get(APIConstants.SEQUENCE_DATA)); -// endpointConfig.put("sequence", content); -// } -// apidto.setEndpointConfig(endpointConfig); -// } API api = APIMappingUtil.fromDTOtoAPI(apidto, apidto.getProvider()); api.setUUID(apidto.getId()); if (APIConstants.APITransportType.GRAPHQL.toString().equals(api.getType())) { @@ -136,8 +125,6 @@ public RuntimeArtifactDto generateGatewayArtifact(List ap api.setGraphQLSchema(graphqlSchema); gatewayAPIDTO = TemplateBuilderUtil.retrieveGatewayAPIDto(api, environment, tenantDomain, apidto, extractedFolderPath); -// // if sequence is passed, then override the existing config -// gatewayAPIDTO.setApiDefinition(api.getAsyncApiDefinition()); } else if (api.getType() != null && (APIConstants.APITransportType.HTTP.toString ().equals(api.getType()) || APIConstants.API_TYPE_SOAP.equals(api.getType()) 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/SynapsePolicyAggregator.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/SynapsePolicyAggregator.java index 3936c0170a02..534f0a01e923 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/SynapsePolicyAggregator.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/SynapsePolicyAggregator.java @@ -57,6 +57,9 @@ public class SynapsePolicyAggregator { private static final String POLICY_SEQUENCE_TEMPLATE_LOCATION = CarbonUtils.getCarbonHome() + File.separator + "repository" + File.separator + "resources" + File.separator + "api_templates" + File.separator + "operation_policy_template.j2"; + private static final String CUSTOM_BACKEND_SEQUENCE_TEMPLATE_LOCATION = CarbonUtils.getCarbonHome() + File.separator + + "repository" + File.separator + "resources" + File.separator + "api_templates" + File.separator + + "custom_backend_sequence_template.j2"; private static final String GATEWAY_POLICY_SEQUENCE_TEMPLATE_LOCATION = CarbonUtils.getCarbonHome() + File.separator + "repository" + File.separator + "resources" + File.separator + "templates" + File.separator + "gateway_policy_template.j2"; @@ -104,6 +107,27 @@ public static String generatePolicySequenceForUriTemplateSet(Set ur } } + public static String generateBackendSequenceForCustomSequence(API api, String sequenceName, String flow, + String sequence) throws APIManagementException, IOException { + Map configMap = new HashMap<>(); + boolean render = false; + String customBackendTemplate = FileUtil.readFileToString(CUSTOM_BACKEND_SEQUENCE_TEMPLATE_LOCATION) + .replace("\\", ""); + configMap.put("sequence_name", sequenceName); + if (sequence != null || sequence.isEmpty()) { + String sanitizedSequence = renderCustomBackendSequence(sequence, sequenceName); + if (sanitizedSequence == null) { + throw new APIManagementException("Error when preparing the sequence: " + sequenceName); + } + configMap.put("custom_sequence", sanitizedSequence); + render = true; + } + if (render) { + return renderPolicyTemplate(customBackendTemplate, configMap); + } + return ""; + } + /** * This method will populate the operation policy case list. * @@ -202,6 +226,13 @@ private static List renderPolicyMapping(List policyList return renderedPolicyMappingList; } + private static String renderCustomBackendSequence(String sequence, String sequenceName) { + if (sequence != null) { + return renderPolicyTemplate(sequence, new HashMap<>()); + } + return null; + } + /** * As there can be multiple child elements without a root element, for sanitization, we will * first wrap the template with a dummy root element and build the OM element. 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/TemplateBuilderUtil.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/TemplateBuilderUtil.java index 9bf90bbaaf10..edbd48b093ab 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/TemplateBuilderUtil.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/TemplateBuilderUtil.java @@ -968,6 +968,18 @@ private static void setCustomSequencesToBeAdded(API api, GatewayAPIDTO gatewayAP addGatewayContentToList(gatewayFaultContentDTO, gatewayAPIDTO.getSequenceToBeAdd())); } } + Map endpointConfigMap = (Map) apidto.getEndpointConfig(); + + if (APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( + endpointConfigMap.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE)) + && endpointConfigMap.get("sequence") != null) { + GatewayContentDTO gatewayCustomBackendSequenceDTO = retrieveCustomBackendSequence(api, + APIConstants.OPERATION_SEQUENCE_TYPE_REQUEST, endpointConfigMap.get("sequence").toString()); + if (gatewayCustomBackendSequenceDTO != null) { + gatewayAPIDTO.setSequenceToBeAdd( + addGatewayContentToList(gatewayCustomBackendSequenceDTO, gatewayAPIDTO.getSequenceToBeAdd())); + } + } } private static void setAPIFaultSequencesToBeAdded(API api, GatewayAPIDTO gatewayAPIDTO, String extractedPath, @@ -1396,15 +1408,15 @@ private static GatewayContentDTO retrieveOperationPolicySequence(String pathToAc operationPolicySequenceContentDto.setName(seqExt); operationPolicySequenceContentDto.setContent(APIUtil.convertOMtoString(omElement)); switch (flow) { - case APIConstants.OPERATION_SEQUENCE_TYPE_REQUEST: - api.setInSequence(seqExt); - break; - case APIConstants.OPERATION_SEQUENCE_TYPE_RESPONSE: - api.setOutSequence(seqExt); - break; - case APIConstants.OPERATION_SEQUENCE_TYPE_FAULT: - api.setFaultSequence(seqExt); - break; + case APIConstants.OPERATION_SEQUENCE_TYPE_REQUEST: + api.setInSequence(seqExt); + break; + case APIConstants.OPERATION_SEQUENCE_TYPE_RESPONSE: + api.setOutSequence(seqExt); + break; + case APIConstants.OPERATION_SEQUENCE_TYPE_FAULT: + api.setFaultSequence(seqExt); + break; } return operationPolicySequenceContentDto; } @@ -1415,6 +1427,38 @@ private static GatewayContentDTO retrieveOperationPolicySequence(String pathToAc return null; } + private static GatewayContentDTO retrieveCustomBackendSequence(API api, String flow, String sequence) + throws APIManagementException { + GatewayContentDTO customBackendSequenceContentDto = new GatewayContentDTO(); + + String customSequence = null; + String seqExt = APIUtil.getSequenceExtensionName(api) + SynapsePolicyAggregator.getSequenceExtensionFlow(flow) + + "-Custom-Backend"; + try { + customSequence = SynapsePolicyAggregator.generateBackendSequenceForCustomSequence(api, seqExt, flow, + sequence); + } 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(seqExt); + } + customBackendSequenceContentDto.setName(seqExt); + customBackendSequenceContentDto.setContent(APIUtil.convertOMtoString(omElement)); + return customBackendSequenceContentDto; + } + } catch (Exception e) { + throw new APIManagementException(e); + } + } + return null; + } + private static GatewayContentDTO retrieveOperationPolicySequenceForProducts(APIProduct apiProduct, API api, String extractedLocation, String flow) throws APIManagementException { 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/APIMappingUtil.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/APIMappingUtil.java index 10612dfb8fad..eccf9c98fed8 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/APIMappingUtil.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/APIMappingUtil.java @@ -70,6 +70,7 @@ import org.wso2.carbon.apimgt.rest.api.common.RestApiCommonUtil; import org.wso2.carbon.apimgt.rest.api.common.RestApiConstants; import org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO; +import org.wso2.carbon.apimgt.rest.api.publisher.v1.common.SynapsePolicyAggregator; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIBusinessInformationDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APICorsConfigurationDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIDTO; @@ -199,12 +200,23 @@ public static API fromDTOtoAPI(APIDTO dto, String provider) throws APIManagement } Object endpointConfig = dto.getEndpointConfig(); + + // update the sequence name generated if (endpointConfig != null) { ObjectMapper mapper = new ObjectMapper(); try { if (endpointConfig instanceof LinkedHashMap) { ((LinkedHashMap) endpointConfig).remove(APIConstants.IMPLEMENTATION_STATUS); } + if (endpointConfig instanceof HashMap) { + if (APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( + ((HashMap) endpointConfig).get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE))) { + String seqExt = APIUtil.getSequenceExtensionName(dto.getName(), dto.getVersion()) + + SynapsePolicyAggregator.getSequenceExtensionFlow( + APIConstants.OPERATION_SEQUENCE_TYPE_REQUEST) + "-Custom-Backend"; + ((HashMap) endpointConfig).put("sequence_name", seqExt); + } + } model.setEndpointConfig(mapper.writeValueAsString(endpointConfig)); } catch (IOException e) { handleException("Error while converting endpointConfig to json", e); 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 119cfc8c4fdf..ca3420a7ed8a 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 @@ -274,9 +274,11 @@ public static ImportedAPIDTO importApi(String extractedFolderPath, APIDTO import // 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))) { - String sequenceFileName = endpointConfig.get("sequence_path").toString(); - String path = extractedFolderPath + File.separator + sequenceFileName; - endpointConfig.put("sequence_path", path); + 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); } diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java index d98db322105d..d1cce0543442 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java @@ -227,8 +227,7 @@ public Response customSequenceUpdate(String apiId, String ifMatch, InputStream s } apidto.setEndpointConfig(new Gson().fromJson(endpointConfig.toString(), Object.class)); } - updateAPI(apiId, apidto, ifMatch, messageContext); - return Response.ok().entity(null).build(); + return updateAPI(apiId, apidto, ifMatch, messageContext); } @Override From ab62a2779fadb5418955cf7fe45bc90d22554af9 Mon Sep 17 00:00:00 2001 From: BLasan Date: Tue, 3 Sep 2024 14:58:29 +0530 Subject: [PATCH 07/56] Changed: REST API of Custom Backend update --- .../wso2/carbon/apimgt/api/APIProvider.java | 4 + .../apimgt/gateway/InMemoryAPIDeployer.java | 1 + .../wso2/carbon/apimgt/impl/APIConstants.java | 2 + .../carbon/apimgt/impl/APIProviderImpl.java | 16 +++ .../carbon/apimgt/impl/dao/ApiMgtDAO.java | 110 ++++++++++++++++++ .../impl/dao/constants/SQLConstants.java | 9 ++ .../apimgt/impl/utils/GatewayUtils.java | 5 + .../src/main/resources/publisher-api.yaml | 9 +- .../v1/common/SynapseArtifactGenerator.java | 34 ++++++ .../v1/common/SynapsePolicyAggregator.java | 3 +- .../v1/common/TemplateBuilderUtil.java | 10 +- .../v1/common/mappings/APIMappingUtil.java | 1 + .../common/mappings/PublisherCommonUtils.java | 33 ++++++ .../apimgt/rest/api/publisher/v1/ApisApi.java | 6 +- .../rest/api/publisher/v1/ApisApiService.java | 2 +- .../publisher/v1/impl/ApisApiServiceImpl.java | 38 +++--- .../src/main/resources/publisher-api.yaml | 9 +- 17 files changed, 249 insertions(+), 43 deletions(-) diff --git a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java index 028fd054b660..3ae2202891ca 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java +++ b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java @@ -33,6 +33,7 @@ import org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy; import java.io.ByteArrayInputStream; +import java.io.InputStream; import java.util.List; import java.util.Map; import java.util.Set; @@ -315,6 +316,9 @@ List getSubscriptionsOfAPI(String apiName, String apiVersion, Str */ API updateAPI(API api, API existingAPI) throws APIManagementException, FaultGatewaysException; + void updateCustomBackend(API api,String type, InputStream sequence, String fileName) throws APIManagementException; + void updateCustomBackendByRevisionID(API api, String type, InputStream sequence, String revision, String fileName) throws APIManagementException; + /** * Create a new version of the api, with version newVersion * diff --git a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/InMemoryAPIDeployer.java b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/InMemoryAPIDeployer.java index dbb9d0ecac21..8e3c5251426f 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/InMemoryAPIDeployer.java +++ b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/InMemoryAPIDeployer.java @@ -350,6 +350,7 @@ private void unDeployAPI(APIGatewayAdmin apiGatewayAdmin, DeployAPIInGatewayEven } GatewayUtils.setCustomSequencesToBeRemoved(api, gatewayAPIDTO); + GatewayUtils.setCustomBackendToBeRemoved(api, gatewayAPIDTO); } gatewayAPIDTO.setLocalEntriesToBeRemove( GatewayUtils diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConstants.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConstants.java index 4ec1b295ab42..235625f64bfe 100755 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConstants.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConstants.java @@ -1561,6 +1561,8 @@ private OAuthConstants() { public static final String MEDIATION_SEQUENCE_ELEM = "sequence"; public static final String MEDIATION_CONFIG_EXT = ".xml"; public static final String API_CUSTOM_SEQ_IN_EXT = "--In"; + + public static final String API_CUSTOM_BACKEND_SEQ_EXT = "--Custom_Backend"; public static final String API_CUSTOM_SEQ_OUT_EXT = "--Out"; public static final String API_CUSTOM_SEQ_FAULT_EXT = "--Fault"; diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java index c0292daa29cd..2fe164a09a76 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java @@ -1167,6 +1167,18 @@ private void updateAPIPolicies(API api, String tenantDomain) throws APIManagemen apiMgtDAO.updateAPIPoliciesMapping(api.getUuid(), api.getUriTemplates(), api.getApiPolicies(), tenantDomain); } + @Override + public void updateCustomBackend(API api, String type, InputStream sequence, String fileName) + throws APIManagementException { + apiMgtDAO.updateCustomBackend(api.getUuid(), fileName, sequence, type); + } + + @Override + public void updateCustomBackendByRevisionID(API api, String type, InputStream sequence, + String revision, String fileName) throws APIManagementException { + apiMgtDAO.updateCustomBackendByRevision(api.getUuid(), fileName, sequence, type, revision); + } + private void validateKeyManagers(API api) throws APIManagementException { Map tenantKeyManagers = KeyManagerHolder.getGlobalAndTenantKeyManagers(tenantDomain); @@ -6302,6 +6314,9 @@ public void deployAPIRevision(String apiId, String apiRevisionUUID, } } apiMgtDAO.addAPIRevisionDeployment(apiRevisionUUID, apiRevisionDeployments); + + // update AM_API_CUSTOM_BACKEND table + WorkflowExecutor revisionDeploymentWFExecutor = getWorkflowExecutor( WorkflowConstants.WF_TYPE_AM_REVISION_DEPLOYMENT); @@ -6685,6 +6700,7 @@ public void deleteAPIRevision(String apiId, String apiRevisionId, String organiz ERROR_DELETING_API_REVISION,apiRevision.getApiUUID())); } apiMgtDAO.deleteAPIRevision(apiRevision); + apiMgtDAO.deleteCustomBackend(apiId, apiRevisionId); gatewayArtifactsMgtDAO.deleteGatewayArtifact(apiRevision.getApiUUID(), apiRevision.getRevisionUUID()); if (artifactSaver != null) { try { 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 ae8849691258..9d68c7631130 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 @@ -11213,6 +11213,58 @@ public List retrieveSavedEmailList(String userName, String stakeHolder) } + /** + * + * @param apiUUID UUID of API + * @param revisionUUID Revision ID of the API + * @return A HashMap with Custom Backend data + * @throws APIManagementException + */ + public Map retrieveCustomBackendOfAPIRevision(String apiUUID, String revisionUUID) throws APIManagementException { + String sqlQuery = SQLConstants.CustomBackendConstants.GET_CUSTOM_BACKEND_OF_API_REVISION; + Map map = new HashMap<>(); + ResultSet resultSet = null; + try (Connection connection = APIMgtDBUtil.getConnection(); + PreparedStatement ps = connection.prepareStatement(sqlQuery)) { + ps.setString(1, apiUUID); + ps.setString(2, revisionUUID); + resultSet = ps.executeQuery(); + while (resultSet.next()) { + map.put("sequence", resultSet.getString("SEQUENCE")); + map.put("endpoint_type", resultSet.getString("TYPE")); + map.put("sequence_name", resultSet.getString("NAME")); + } + } catch (SQLException ex) { + handleException("Error retrieving Custom Backend of an API: " + apiUUID, ex); + } + return map; + } + + /** + * + * @param apiUUID API UUID + * @return HashMap with Custom Backend data + * @throws APIManagementException + */ + public Map retrieveCustomBackendOfAPI(String apiUUID) throws APIManagementException { + String sqlQuery = SQLConstants.CustomBackendConstants.GET_CUSTOM_BACKEND_OF_API_DEFAULT_REVISION; + Map map = new HashMap<>(); + ResultSet resultSet = null; + try (Connection connection = APIMgtDBUtil.getConnection(); + PreparedStatement ps = connection.prepareStatement(sqlQuery)) { + ps.setString(1, apiUUID); + resultSet = ps.executeQuery(); + while (resultSet.next()) { + map.put("sequence", resultSet.getString("SEQUENCE")); + map.put("endpoint_type", resultSet.getString("TYPE")); + map.put("sequence_name", resultSet.getString("NAME")); + } + } catch (SQLException ex) { + handleException("Error retrieving Custom Backend of an API: " + apiUUID, ex); + } + return map; + } + /** * This method will delete all email alert subscriptions details from tables * @@ -21329,6 +21381,64 @@ public void updateAPIPoliciesMapping(String apiUUID, Set uriTemplat } } + public void updateCustomBackend(String apiUUID, String sequenceName, InputStream sequence, String type) throws APIManagementException { + String deleteCustomBackedQuery = SQLConstants.CustomBackendConstants.DELETE_CUSTOM_BACKEND; + try (Connection connection = APIMgtDBUtil.getConnection(); + PreparedStatement prepStmt = connection.prepareStatement(deleteCustomBackedQuery)) { + connection.setAutoCommit(false); + prepStmt.setString(1, apiUUID); + prepStmt.executeUpdate(); + addCustomBackend(apiUUID, sequenceName, null, sequence, type, connection); + connection.commit(); + } catch (SQLException e) { + handleException("Error while adding Custom Backend for API : " + apiUUID, e); + } + } + + public void deleteCustomBackend(String apiUUID, String revisionId) throws APIManagementException { + String deleteCustomBackedQuery = SQLConstants.CustomBackendConstants.DELETE_CUSTOM_BACKEND_BY_REVISION; + try (Connection connection = APIMgtDBUtil.getConnection(); + PreparedStatement prepStmt = connection.prepareStatement(deleteCustomBackedQuery)) { + connection.setAutoCommit(false); + prepStmt.setString(1, apiUUID); + prepStmt.setString(2, revisionId); + prepStmt.executeUpdate(); + connection.commit(); + } catch (SQLException e) { + handleException("Error while deleting Custom Backend for API : " + apiUUID, e); + } + } + + public void updateCustomBackendByRevision(String apiUUID, String sequenceName, InputStream sequence, String type, String revision) throws APIManagementException { + String deleteCustomBackedQuery = SQLConstants.CustomBackendConstants.DELETE_CUSTOM_BACKEND_BY_REVISION; + try (Connection connection = APIMgtDBUtil.getConnection(); + PreparedStatement prepStmt = connection.prepareStatement(deleteCustomBackedQuery)) { + connection.setAutoCommit(false); + prepStmt.setString(1, apiUUID); + prepStmt.setString(2, revision); + prepStmt.executeUpdate(); + addCustomBackend(apiUUID, sequenceName, revision, sequence, type, connection); + connection.commit(); + } catch (SQLException e) { + handleException("Error while adding Custom Backend for API : " + apiUUID, e); + } + } + + public void addCustomBackend(String apiUUID, String sequenceName, String revision, InputStream sequence, String type, Connection connection) throws APIManagementException { + String insertCustomBackendQuery = SQLConstants.CustomBackendConstants.ADD_CUSTOM_BACKEND; + try (PreparedStatement prepStmt = connection.prepareStatement(insertCustomBackendQuery)) { + connection.setAutoCommit(false); + prepStmt.setString(1, apiUUID); + prepStmt.setBinaryStream(2, sequence); + prepStmt.setString(3, type); + prepStmt.setString(4, revision); + prepStmt.setString(5, sequenceName); + prepStmt.executeUpdate(); + } catch (SQLException e) { + handleException("Error while adding Custom Backend for API : " + apiUUID, e); + } + } + /** * This method will add API level policy mappings to the database. * 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 5c2be2aa2e72..72f9f1e64194 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 @@ -4474,5 +4474,14 @@ public static class TransactionCountConstants { public static final String INSERT_TRANSACTION_COUNT = "INSERT INTO AM_TRANSACTION_RECORDS " + "(ID, HOST, SERVER_ID, SERVER_TYPE, COUNT, RECORDED_TIME) " + "VALUES (?,?,?,?,?,?)"; public static final String GET_TRANSACTION_COUNT = "SELECT SUM(COUNT) FROM AM_TRANSACTION_RECORDS " + "WHERE RECORDED_TIME >= ? AND RECORDED_TIME <= ?"; } + public static class CustomBackendConstants { + public static final String ADD_CUSTOM_BACKEND = + "INSERT INTO AM_API_CUSTOM_BACKEND (API_UUID,SEQUENCE,TYPE,REVISION_UUID,NAME) " + "VALUES (?,?,?,?,?)"; + public static final String DELETE_CUSTOM_BACKEND = "DELETE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ? AND REVISION_UUID IS NULL"; + 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 NAME, SEQUENCE, TYPE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ? AND REVISION_UUID = ?"; + public static final String GET_CUSTOM_BACKEND_OF_API_DEFAULT_REVISION = "SELECT NAME, SEQUENCE, TYPE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ? AND REVISION_UUID IS NULL"; + } } diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/GatewayUtils.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/GatewayUtils.java index 633844789fc2..f82916f353aa 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/GatewayUtils.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/GatewayUtils.java @@ -46,6 +46,11 @@ public static void setCustomSequencesToBeRemoved(API api, GatewayAPIDTO gatewayA gatewayAPIDTO.setSequencesToBeRemove(addStringToList(faultSequence, gatewayAPIDTO.getSequencesToBeRemove())); } + public static void setCustomBackendToBeRemoved(API api, GatewayAPIDTO gatewayAPIDTO) { + String sequence = APIUtil.getSequenceExtensionName(api) + APIConstants.API_CUSTOM_SEQ_IN_EXT + APIConstants.API_CUSTOM_BACKEND_SEQ_EXT; + gatewayAPIDTO.setSequencesToBeRemove(addStringToList(sequence, gatewayAPIDTO.getSequencesToBeRemove())); + } + public static String[] addStringToList(String key, String[] keys) { if (keys == null) { diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml b/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml index 56f7d0911927..e4e4d8cad1c7 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml @@ -487,13 +487,13 @@ paths: -H "Content-Type: application/json" -d @data.json "https://127.0.0.1:9443/api/am/publisher/v4/apis/7a2298c4-c905-403f-8fac-38c73301631f/topics"' - /apis/{apiId}/custom-sequence: + /apis/{apiId}/custom-backend: put: tags: - APIs summary: Upload Custom Sequence as the Endpoint of the API description: This operation can be used to change the endpoint of the API to Custom Sequence - operationId: customSequenceUpdate + operationId: customBackendUpdate parameters: - $ref: '#/components/parameters/apiId' - $ref: '#/components/parameters/If-Match' @@ -509,9 +509,6 @@ paths: type: type: string description: Type of the Endpoint - apiData: - type: string - description: API Data to be updated responses: 200: description: | @@ -561,7 +558,7 @@ paths: x-code-samples: - lang: Curl source: 'curl -k -X PUT -H "Authorization: Bearer ae4eae22-3f65-387b-a171-d37eaa366fa8" - -H "Content-Type: application/json" -d @data.json "https://127.0.0.1:9443/api/am/publisher/v4/apis/7a2298c4-c905-403f-8fac-38c73301631f"' + -H "Content-Type: application/json" -d @data.json "https://127.0.0.1:9443/api/am/publisher/v4/apis/7a2298c4-c905-403f-8fac-38c73301631f/custom-backend"' /apis/{apiId}/reimport-service: 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/SynapseArtifactGenerator.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/SynapseArtifactGenerator.java index d81e65a4c43b..54615455d838 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/SynapseArtifactGenerator.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/SynapseArtifactGenerator.java @@ -17,6 +17,7 @@ */ package org.wso2.carbon.apimgt.rest.api.publisher.v1.common; +import com.fasterxml.jackson.databind.ObjectMapper; import com.google.gson.Gson; import org.apache.axiom.om.OMElement; import org.apache.commons.io.FileUtils; @@ -38,6 +39,7 @@ import org.wso2.carbon.apimgt.api.model.SwaggerData; import org.wso2.carbon.apimgt.api.model.graphql.queryanalysis.GraphqlComplexityInfo; import org.wso2.carbon.apimgt.impl.APIConstants; +import org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO; import org.wso2.carbon.apimgt.impl.definitions.GraphQLSchemaDefinition; import org.wso2.carbon.apimgt.impl.definitions.OAS3Parser; import org.wso2.carbon.apimgt.impl.dto.APIRuntimeArtifactDto; @@ -46,6 +48,7 @@ import org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.GatewayArtifactGenerator; import org.wso2.carbon.apimgt.impl.importexport.utils.CommonUtil; import org.wso2.carbon.apimgt.impl.utils.APIUtil; +import org.wso2.carbon.apimgt.impl.utils.GatewayUtils; import org.wso2.carbon.apimgt.rest.api.publisher.v1.common.mappings.APIMappingUtil; import org.wso2.carbon.apimgt.rest.api.publisher.v1.common.mappings.ImportUtils; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIDTO; @@ -57,10 +60,15 @@ import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; import javax.xml.namespace.QName; import javax.xml.stream.XMLStreamException; +import static org.wso2.carbon.apimgt.impl.utils.APIUtil.handleException; + /** * This class used to generate Synapse Artifact. */ @@ -73,6 +81,7 @@ public class SynapseArtifactGenerator implements GatewayArtifactGenerator { private static final Log log = LogFactory.getLog(SynapseArtifactGenerator.class); private static final String GATEWAY_EXT_SEQUENCE_PREFIX = "WSO2AMGW--Ext"; + private static final ApiMgtDAO apiMgtDao = ApiMgtDAO.getInstance(); @Override public RuntimeArtifactDto generateGatewayArtifact(List apiRuntimeArtifactDtoList) @@ -105,7 +114,10 @@ public RuntimeArtifactDto generateGatewayArtifact(List ap tenantDomain, extractedFolderPath); } else { APIDTO apidto = ImportUtils.retrievedAPIDto(extractedFolderPath); + // Update the EndpointConfig if it's a Custom Backend + updateCustomBackendOfAPI(apidto, runTimeArtifact.getRevision()); API api = APIMappingUtil.fromDTOtoAPI(apidto, apidto.getProvider()); + api.setUUID(apidto.getId()); if (APIConstants.APITransportType.GRAPHQL.toString().equals(api.getType())) { APIDefinition parser = new OAS3Parser(); @@ -170,6 +182,28 @@ public RuntimeArtifactDto generateGatewayArtifact(List ap return runtimeArtifactDto; } + private void updateCustomBackendOfAPI(APIDTO apidto, String revisionID) throws APIManagementException { + Object endpointConfig = apidto.getEndpointConfig(); + // update the sequence name generated + if (endpointConfig != null) { + if (endpointConfig instanceof HashMap) { + if (APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( + ((HashMap) endpointConfig).get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE))) { + // Get Endpoint Configurations from the DB + Map conf = apiMgtDao.retrieveCustomBackendOfAPIRevision(apidto.getId(), revisionID); + if (conf == null) { + throw new APIManagementException( + "Cannot find the Custom Backend for the API: " + apidto.getId() + " , Revision: " + + revisionID); + } + ((HashMap) endpointConfig).put("sequence", conf.get("sequence")); + ((HashMap) endpointConfig).put("type", conf.get("type")); + } + } + apidto.setEndpointConfig(endpointConfig); + } + } + /** * Generate gateway policy artifact. * 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/SynapsePolicyAggregator.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/SynapsePolicyAggregator.java index 534f0a01e923..0bdc79ec7534 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/SynapsePolicyAggregator.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/SynapsePolicyAggregator.java @@ -108,7 +108,7 @@ public static String generatePolicySequenceForUriTemplateSet(Set ur } public static String generateBackendSequenceForCustomSequence(API api, String sequenceName, String flow, - String sequence) throws APIManagementException, IOException { + String sequence, String endpointType) throws APIManagementException, IOException { Map configMap = new HashMap<>(); boolean render = false; String customBackendTemplate = FileUtil.readFileToString(CUSTOM_BACKEND_SEQUENCE_TEMPLATE_LOCATION) @@ -120,6 +120,7 @@ public static String generateBackendSequenceForCustomSequence(API api, String se throw new APIManagementException("Error when preparing the sequence: " + sequenceName); } configMap.put("custom_sequence", sanitizedSequence); + configMap.put("endpoint_type", endpointType); render = true; } if (render) { 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/TemplateBuilderUtil.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/TemplateBuilderUtil.java index edbd48b093ab..f91d447788f5 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/TemplateBuilderUtil.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/TemplateBuilderUtil.java @@ -690,6 +690,7 @@ private static GatewayAPIDTO createAPIGatewayDTOtoPublishAPI(Environment environ api.setUuid(apidto.getId()); GatewayUtils.setCustomSequencesToBeRemoved(apiProduct.getId(), api.getUuid(), productAPIDto); APITemplateBuilder apiTemplateBuilder = new APITemplateBuilderImpl(api, apiProduct); + // check the endpoint type addEndpoints(api, apiTemplateBuilder, productAPIDto); setCustomSequencesToBeAdded(apiProduct, api, productAPIDto, apiExtractedPath, apidto); setAPIFaultSequencesToBeAdded(api, productAPIDto, apiExtractedPath, apidto); @@ -974,7 +975,8 @@ private static void setCustomSequencesToBeAdded(API api, GatewayAPIDTO gatewayAP endpointConfigMap.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE)) && endpointConfigMap.get("sequence") != null) { GatewayContentDTO gatewayCustomBackendSequenceDTO = retrieveCustomBackendSequence(api, - APIConstants.OPERATION_SEQUENCE_TYPE_REQUEST, endpointConfigMap.get("sequence").toString()); + APIConstants.OPERATION_SEQUENCE_TYPE_REQUEST, endpointConfigMap.get("sequence").toString(), + endpointConfigMap.get("type").toString()); if (gatewayCustomBackendSequenceDTO != null) { gatewayAPIDTO.setSequenceToBeAdd( addGatewayContentToList(gatewayCustomBackendSequenceDTO, gatewayAPIDTO.getSequenceToBeAdd())); @@ -1427,8 +1429,8 @@ private static GatewayContentDTO retrieveOperationPolicySequence(String pathToAc return null; } - private static GatewayContentDTO retrieveCustomBackendSequence(API api, String flow, String sequence) - throws APIManagementException { + private static GatewayContentDTO retrieveCustomBackendSequence(API api, String flow, String sequence, + String endpointType) throws APIManagementException { GatewayContentDTO customBackendSequenceContentDto = new GatewayContentDTO(); String customSequence = null; @@ -1436,7 +1438,7 @@ private static GatewayContentDTO retrieveCustomBackendSequence(API api, String f + "-Custom-Backend"; try { customSequence = SynapsePolicyAggregator.generateBackendSequenceForCustomSequence(api, seqExt, flow, - sequence); + sequence, endpointType); } catch (IOException e) { throw new APIManagementException(e); } 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/APIMappingUtil.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/APIMappingUtil.java index eccf9c98fed8..d86b98621c04 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/APIMappingUtil.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/APIMappingUtil.java @@ -60,6 +60,7 @@ import org.wso2.carbon.apimgt.api.model.WebsubSubscriptionConfiguration; import org.wso2.carbon.apimgt.impl.APIConstants; import org.wso2.carbon.apimgt.impl.ServiceCatalogImpl; +import org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO; import org.wso2.carbon.apimgt.impl.definitions.AsyncApiParser; import org.wso2.carbon.apimgt.impl.definitions.OASParserUtil; import org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder; 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 af0514e912e5..f6278c12c449 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 @@ -197,6 +197,39 @@ public static API updateApi(API originalAPI, APIDTO apiDtoToUpdate, APIProvider // TODO use returend api } + /** + * @param api API of the Custom Backend + * @param apiProvider API Provider + * @param endpointType Endpoint Type of the Custom Backend (SANDBOX, PRODUCTION) + * @param customBackend Custom Backend + * @param contentDecomp Header Content of the Request + * @return Custom Backend File Name + * @throws APIManagementException If an error occurs while updating the API and API definition + */ + public static String updateCustomBackend(API api, APIProvider apiProvider, String endpointType, + InputStream customBackend, String contentDecomp) throws APIManagementException { + String fileName = getFileNameFromContentDisposition(contentDecomp); + if (fileName == null) + throw new APIManagementException( + "Error when retrieving Custom Backend file name of API: " + api.getId().getApiName()); + apiProvider.updateCustomBackend(api, endpointType, customBackend, fileName); + return fileName; + } + + private static String getFileNameFromContentDisposition(String contentDisposition) { + // Split the Content-Disposition header to get the file name + String[] parts = contentDisposition.split(";"); + for (String part : parts) { + if (part.trim().startsWith("filename")) { + // Extract the file name value + return part.split("=")[1].trim().replace("\"", ""); + } + } + return null; + } + + + /** * Prepare for API object before updating the API. * diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApi.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApi.java index 50c82bdb3cf2..bdc559b0a5f5 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApi.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApi.java @@ -378,7 +378,7 @@ public Response createNewAPIVersion( @NotNull @Size(max=30) @ApiParam(value = "V } @PUT - @Path("/{apiId}/custom-sequence") + @Path("/{apiId}/custom-backend") @Consumes({ "multipart/form-data" }) @Produces({ "application/json" }) @ApiOperation(value = "Upload Custom Sequence as the Endpoint of the API", notes = "This operation can be used to change the endpoint of the API to Custom Sequence", response = APIDTO.class, authorizations = { @@ -395,8 +395,8 @@ public Response createNewAPIVersion( @NotNull @Size(max=30) @ApiParam(value = "V @ApiResponse(code = 404, message = "Not Found. The specified resource does not exist.", response = ErrorDTO.class), @ApiResponse(code = 409, message = "Conflict. Specified resource already exists.", response = ErrorDTO.class), @ApiResponse(code = 412, message = "Precondition Failed. The request has not been performed because one of the preconditions is not met.", response = ErrorDTO.class) }) - public Response customSequenceUpdate(@ApiParam(value = "**API ID** consisting of the **UUID** of the API. ",required=true) @PathParam("apiId") String apiId, @ApiParam(value = "Validator for conditional requests; based on ETag. " )@HeaderParam("If-Match") String ifMatch, @Multipart(value = "sequence", required = false) InputStream sequenceInputStream, @Multipart(value = "sequence" , required = false) Attachment sequenceDetail, @Multipart(value = "type", required = false) String type, @Multipart(value = "apiData", required = false) String apiData) throws APIManagementException{ - return delegate.customSequenceUpdate(apiId, ifMatch, sequenceInputStream, sequenceDetail, type, apiData, securityContext); + public Response customBackendUpdate(@ApiParam(value = "**API ID** consisting of the **UUID** of the API. ",required=true) @PathParam("apiId") String apiId, @ApiParam(value = "Validator for conditional requests; based on ETag. " )@HeaderParam("If-Match") String ifMatch, @Multipart(value = "sequence", required = false) InputStream sequenceInputStream, @Multipart(value = "sequence" , required = false) Attachment sequenceDetail, @Multipart(value = "type", required = false) String type) throws APIManagementException{ + return delegate.customBackendUpdate(apiId, ifMatch, sequenceInputStream, sequenceDetail, type, securityContext); } @DELETE diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApiService.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApiService.java index 019c6471b7c7..17dfc5c9c574 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApiService.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApiService.java @@ -79,7 +79,7 @@ public interface ApisApiService { public Response createAPI(APIDTO APIDTO, String openAPIVersion, MessageContext messageContext) throws APIManagementException; public Response createAPIRevision(String apiId, APIRevisionDTO apIRevisionDTO, MessageContext messageContext) throws APIManagementException; public Response createNewAPIVersion(String newVersion, String apiId, Boolean defaultVersion, String serviceVersion, MessageContext messageContext) throws APIManagementException; - public Response customSequenceUpdate(String apiId, String ifMatch, InputStream sequenceInputStream, Attachment sequenceDetail, String type, String apiData, MessageContext messageContext) throws APIManagementException; + public Response customBackendUpdate(String apiId, String ifMatch, InputStream sequenceInputStream, Attachment sequenceDetail, String type, MessageContext messageContext) throws APIManagementException; public Response deleteAPI(String apiId, String ifMatch, MessageContext messageContext) throws APIManagementException; public Response deleteAPIClientCertificateByAlias(String alias, String apiId, MessageContext messageContext) throws APIManagementException; public Response deleteAPIClientCertificateByKeyTypeAndAlias(String keyType, String alias, String apiId, MessageContext messageContext) throws APIManagementException; diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java index d1cce0543442..a269e4d41fac 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java @@ -86,6 +86,7 @@ import org.wso2.carbon.utils.multitenancy.MultitenantConstants; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.Response; import javax.ws.rs.core.StreamingOutput; import java.io.*; @@ -206,28 +207,21 @@ public Response getAPI(String apiId, String xWSO2Tenant, String ifNoneMatch, return Response.ok().entity(apiToReturn).build(); } - @Override - public Response customSequenceUpdate(String apiId, String ifMatch, InputStream sequenceInputStream, - Attachment sequenceDetail, String type, String apiData, MessageContext messageContext) - throws APIManagementException { - APIDTO apidto = new Gson().fromJson(apiData, APIDTO.class); - // Get the endpoint config object updated - APIUtil.validateAPIEndpointConfig(apidto.getEndpointConfig(), apidto.getType().toString(), - apidto.getName()); - if (apidto.getEndpointConfig() != null) { - org.json.JSONObject endpointConfig = new org.json.JSONObject(new Gson().toJson(apidto.getEndpointConfig())); - if (APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( - endpointConfig.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE))) { - try { - String content = ImportUtils.retrieveXMLContent(sequenceInputStream); - endpointConfig.put("sequence", content); - } catch (IOException ex) { - RestApiUtil.handleInternalServerError("Failed to read Custom Sequence of " + apiId, ex, log); - } - } - apidto.setEndpointConfig(new Gson().fromJson(endpointConfig.toString(), Object.class)); - } - return updateAPI(apiId, apidto, ifMatch, messageContext); + @Override public Response customBackendUpdate(String apiId, String ifMatch, InputStream sequenceInputStream, + Attachment sequenceDetail, String type, MessageContext messageContext) throws APIManagementException { + String username = RestApiCommonUtil.getLoggedInUsername(); + APIProvider apiProvider = RestApiCommonUtil.getProvider(username); + String organization = RestApiUtil.getValidatedOrganization(messageContext); + API api = apiProvider.getAPIbyUUID(apiId, organization); + api.setOrganization(organization); + MultivaluedMap headers = sequenceDetail.getHeaders(); + String contentDecomp = headers.getFirst("Content-Disposition"); + + String sequenceName = PublisherCommonUtils.updateCustomBackend(api, apiProvider, type, sequenceInputStream, + contentDecomp); + JSONObject obj = new JSONObject(); + obj.put("sequenceName", sequenceName); + return Response.ok().entity(obj).build(); } @Override diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/resources/publisher-api.yaml b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/resources/publisher-api.yaml index 56f7d0911927..e4e4d8cad1c7 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/resources/publisher-api.yaml +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/resources/publisher-api.yaml @@ -487,13 +487,13 @@ paths: -H "Content-Type: application/json" -d @data.json "https://127.0.0.1:9443/api/am/publisher/v4/apis/7a2298c4-c905-403f-8fac-38c73301631f/topics"' - /apis/{apiId}/custom-sequence: + /apis/{apiId}/custom-backend: put: tags: - APIs summary: Upload Custom Sequence as the Endpoint of the API description: This operation can be used to change the endpoint of the API to Custom Sequence - operationId: customSequenceUpdate + operationId: customBackendUpdate parameters: - $ref: '#/components/parameters/apiId' - $ref: '#/components/parameters/If-Match' @@ -509,9 +509,6 @@ paths: type: type: string description: Type of the Endpoint - apiData: - type: string - description: API Data to be updated responses: 200: description: | @@ -561,7 +558,7 @@ paths: x-code-samples: - lang: Curl source: 'curl -k -X PUT -H "Authorization: Bearer ae4eae22-3f65-387b-a171-d37eaa366fa8" - -H "Content-Type: application/json" -d @data.json "https://127.0.0.1:9443/api/am/publisher/v4/apis/7a2298c4-c905-403f-8fac-38c73301631f"' + -H "Content-Type: application/json" -d @data.json "https://127.0.0.1:9443/api/am/publisher/v4/apis/7a2298c4-c905-403f-8fac-38c73301631f/custom-backend"' /apis/{apiId}/reimport-service: From 64fb6e571b940b1b7cb4be01b86ddceeee48cdbd Mon Sep 17 00:00:00 2001 From: BLasan Date: Wed, 4 Sep 2024 19:26:16 +0530 Subject: [PATCH 08/56] Add: Custom Backend to APICTL file created on revision deployment --- .../wso2/carbon/apimgt/api/APIProvider.java | 10 +- .../apimgt/gateway/InMemoryAPIDeployer.java | 4 +- .../wso2/carbon/apimgt/impl/APIConstants.java | 2 - .../carbon/apimgt/impl/APIProviderImpl.java | 44 +++++- .../carbon/apimgt/impl/dao/ApiMgtDAO.java | 115 ++++++++++++-- .../impl/dao/constants/SQLConstants.java | 12 +- .../importexport/ImportExportConstants.java | 1 + .../carbon/apimgt/impl/utils/APIUtil.java | 29 ++++ .../apimgt/impl/utils/GatewayUtils.java | 5 - .../src/main/resources/publisher-api.yaml | 118 +++++++++++++++ .../publisher/v1/dto/CustomBackendDTO.java | 142 ++++++++++++++++++ .../v1/common/SynapseArtifactGenerator.java | 2 +- .../v1/common/SynapsePolicyAggregator.java | 31 ++-- .../v1/common/TemplateBuilderUtil.java | 17 +-- .../v1/common/mappings/APIMappingUtil.java | 9 -- .../v1/common/mappings/ExportUtils.java | 33 ++++ .../common/mappings/PublisherCommonUtils.java | 15 +- .../publisher/v1/impl/ApisApiServiceImpl.java | 24 ++- .../src/main/resources/publisher-api.yaml | 118 +++++++++++++++ .../src/main/resources/sql/mysql.sql | 11 ++ 20 files changed, 657 insertions(+), 85 deletions(-) create mode 100644 components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/dto/CustomBackendDTO.java diff --git a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java index 3ae2202891ca..ff9a56bb79b9 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java +++ b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java @@ -316,8 +316,14 @@ List getSubscriptionsOfAPI(String apiName, String apiVersion, Str */ API updateAPI(API api, API existingAPI) throws APIManagementException, FaultGatewaysException; - void updateCustomBackend(API api,String type, InputStream sequence, String fileName) throws APIManagementException; - void updateCustomBackendByRevisionID(API api, String type, InputStream sequence, String revision, String fileName) throws APIManagementException; + void updateCustomBackend(API api,String type, InputStream sequence, String fileName, String customBackendUUID) throws APIManagementException; + Map getCustomBackendOfAPIByUUID(String customBackendUUID, String apiUUID, boolean isInfoOnly) throws APIManagementException; + + void updateCustomBackendByRevisionID(String apiUUID, String type, String revision, + String seqName, String backendUUID) throws APIManagementException; + + void addNewCustomBackendForRevision(String revisionUUID, String updatedBackendUUID, String apiUUID, + Map config) throws APIManagementException; /** * Create a new version of the api, with version newVersion diff --git a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/InMemoryAPIDeployer.java b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/InMemoryAPIDeployer.java index 8e3c5251426f..c37d7ef97d57 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/InMemoryAPIDeployer.java +++ b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/InMemoryAPIDeployer.java @@ -33,7 +33,6 @@ import org.apache.synapse.transport.dynamicconfigurations.DynamicProfileReloaderHolder; import org.wso2.carbon.apimgt.api.APIManagementException; import org.wso2.carbon.apimgt.api.ExceptionCodes; -import org.wso2.carbon.apimgt.api.APIManagementException; import org.wso2.carbon.apimgt.api.gateway.GatewayAPIDTO; import org.wso2.carbon.apimgt.api.gateway.GatewayContentDTO; import org.wso2.carbon.apimgt.api.gateway.GraphQLSchemaDTO; @@ -350,7 +349,8 @@ private void unDeployAPI(APIGatewayAdmin apiGatewayAdmin, DeployAPIInGatewayEven } GatewayUtils.setCustomSequencesToBeRemoved(api, gatewayAPIDTO); - GatewayUtils.setCustomBackendToBeRemoved(api, gatewayAPIDTO); + // GatewayUtils.setCustomBackendToBeRemoved(api, gatewayAPIDTO); + } gatewayAPIDTO.setLocalEntriesToBeRemove( GatewayUtils diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConstants.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConstants.java index 235625f64bfe..4ec1b295ab42 100755 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConstants.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConstants.java @@ -1561,8 +1561,6 @@ private OAuthConstants() { public static final String MEDIATION_SEQUENCE_ELEM = "sequence"; public static final String MEDIATION_CONFIG_EXT = ".xml"; public static final String API_CUSTOM_SEQ_IN_EXT = "--In"; - - public static final String API_CUSTOM_BACKEND_SEQ_EXT = "--Custom_Backend"; public static final String API_CUSTOM_SEQ_OUT_EXT = "--Out"; public static final String API_CUSTOM_SEQ_FAULT_EXT = "--Fault"; diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java index 2fe164a09a76..b5da0d1cbc0a 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java @@ -21,6 +21,8 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.gson.Gson; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; import org.apache.axiom.om.OMElement; import org.apache.axiom.om.util.AXIOMUtil; import org.apache.axis2.Constants; @@ -1168,15 +1170,29 @@ private void updateAPIPolicies(API api, String tenantDomain) throws APIManagemen } @Override - public void updateCustomBackend(API api, String type, InputStream sequence, String fileName) + public void updateCustomBackend(API api, String type, InputStream sequence, String seqName, String customBackendUUID) throws APIManagementException { - apiMgtDAO.updateCustomBackend(api.getUuid(), fileName, sequence, type); + apiMgtDAO.updateCustomBackend(api.getUuid(), seqName, sequence, type, customBackendUUID); } @Override - public void updateCustomBackendByRevisionID(API api, String type, InputStream sequence, - String revision, String fileName) throws APIManagementException { - apiMgtDAO.updateCustomBackendByRevision(api.getUuid(), fileName, sequence, type, revision); + public Map getCustomBackendOfAPIByUUID(String customBackendUUID, String apiUUID, boolean isInfoOnly) throws APIManagementException { + return apiMgtDAO.getCustomBackendOfAPIByUUID(customBackendUUID, apiUUID, isInfoOnly); + } + + @Override + public void updateCustomBackendByRevisionID(String apiUUID, String type, String revision, + String seqName, String backendUUID) throws APIManagementException { + String customBackendUUID = UUID.randomUUID().toString(); + InputStream sequence = apiMgtDAO.getCustomBackendSequenceOfAPIByUUID(backendUUID, apiUUID); + apiMgtDAO.updateCustomBackendByRevision(apiUUID, seqName, sequence, type, revision, customBackendUUID); + } + + @Override public void addNewCustomBackendForRevision(String revisionUUID, String updatedBackendUUID, String apiUUID, + Map config) throws APIManagementException { + InputStream sequence = apiMgtDAO.getCustomBackendSequenceOfAPIByUUID(updatedBackendUUID, apiUUID); + config.put("sequence", sequence); + apiMgtDAO.addNewCustomBackendForAPIRevision(apiUUID, revisionUUID, config); } private void validateKeyManagers(API api) throws APIManagementException { @@ -5395,6 +5411,7 @@ public API getAPIbyUUID(String uuid, String organization) throws APIManagementEx if (APIConstants.API_SUBTYPE_AI_API.equals(api.getSubtype())) { populateAiConfiguration(api); } + if (APIUtil.isSequenceDefined(api.getInSequence()) || APIUtil.isSequenceDefined(api.getOutSequence()) || APIUtil.isSequenceDefined(api.getFaultSequence())) { if (migrationEnabled == null) { @@ -5468,6 +5485,23 @@ private void populateAPILevelPolicies(API api) throws APIManagementException { } } + private void populateCustomBackend(API api) throws APIManagementException { + JsonObject endpointConfig = JsonParser.parseString(api.getEndpointConfig()).getAsJsonObject(); + if(endpointConfig != null) { + if(endpointConfig.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE) != null && + APIConstants.ENDPOINT_TYPE_SEQUENCE.equals(endpointConfig.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE).getAsString())) { + ObjectMapper mapper = new ObjectMapper(); + // TODO: Check DB Queries. Following fetches data by API UUID and Revision ID = null + Map conf = apiMgtDAO.retrieveCustomBackendOfAPI(api.getUuid()); + try { + api.setEndpointConfig(mapper.writeValueAsString(conf)); + } catch (IOException ex) { + handleException("Error while converting endpointConfig to json", ex); + } + } + } + } + @Override public APISearchResult searchPaginatedAPIsByFQDN(String endpoint, String tenantDomain, int offset, int limit) throws APIManagementException { 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 9d68c7631130..805ebfe4589f 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 @@ -20,6 +20,7 @@ import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; +import jdk.internal.util.xml.impl.Input; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.tuple.Pair; @@ -2796,9 +2797,8 @@ public List getSubscriptionsOfAPI(String apiName, String apiVersi } /** - * @param apiName Name of the API - * @param apiVersion Version of the API - * @param provider Name of API creator + * @param apiUUID UUID of the API + * @param organization Organization of the API * @return All subscriptions of a given API * @throws org.wso2.carbon.apimgt.api.APIManagementException */ @@ -11240,6 +11240,66 @@ public Map retrieveCustomBackendOfAPIRevision(String apiUUID, St return map; } + public InputStream getCustomBackendSequenceOfAPIByUUID(String backendUUID, String apiUUID) throws APIManagementException { + String sqlQuery = SQLConstants.CustomBackendConstants.GET_API_SPECIFIC_CUSTOM_BACKEND_FROM_SEQUENCE_ID; + ResultSet rs = null; + InputStream sequence = null; + try (Connection con = APIMgtDBUtil.getConnection(); + PreparedStatement ps = con.prepareStatement(sqlQuery)) { + ps.setString(1, backendUUID); + ps.setString(2, apiUUID); + rs = ps.executeQuery(); + while(rs.next()) { + try (InputStream in = rs.getBinaryStream("SEQUENCE")) { + sequence = in; + } catch (IOException ex) { + handleException("Error reading the sequence of Custom Backend: "+ backendUUID + ", API: " + apiUUID, ex); + } + } + } catch (SQLException ex) { + handleException("Error when fetching Custom Backend data: "+ backendUUID + " of API: " + apiUUID, ex); + } + + if(sequence == null) { + throw new APIManagementException("Custom Backend Content cannot be empty"); + } + return sequence; + } + + public Map getCustomBackendOfAPIByUUID(String backendUUID, String apiUUID, boolean isInfo) throws APIManagementException { + String sqlQuery; + Map endpointConfig = new HashMap<>(); + boolean isRevisioned = checkAPIUUIDIsARevisionUUID(apiUUID) != null; + if(isRevisioned) { + sqlQuery = SQLConstants.CustomBackendConstants.GET_REVISION_SPECIFIC_CUSTOM_BACKEND_FROM_SEQUENCE_ID; + } else { + sqlQuery = SQLConstants.CustomBackendConstants.GET_API_SPECIFIC_CUSTOM_BACKEND_FROM_SEQUENCE_ID; + } + ResultSet resultSet = null; + try (Connection connection = APIMgtDBUtil.getConnection(); + PreparedStatement ps = connection.prepareStatement(sqlQuery)) { + ps.setString(1, backendUUID); + ps.setString(2, apiUUID); + resultSet = ps.executeQuery(); + while (resultSet.next()) { + if(!isInfo) { + try (InputStream in = resultSet.getBinaryStream("SEQUENCE")) { + endpointConfig.put("sequence", IOUtils.toString(in)); + } catch (IOException ex) { + handleException("Error reading Sequence Content of Custom Backend: " + backendUUID + " API: " + apiUUID, ex); + } + } + endpointConfig.put("type", resultSet.getString("TYPE")); + endpointConfig.put("sequence_name", resultSet.getString("NAME")); + endpointConfig.put("endpoint_type", "custom_backend"); + endpointConfig.put("sequence_id", resultSet.getString("ID")); + } + } catch (SQLException ex) { + handleException("Error when retrieving Custom Backend of API: " + apiUUID, ex); + } + return endpointConfig; + } + /** * * @param apiUUID API UUID @@ -11255,9 +11315,9 @@ public Map retrieveCustomBackendOfAPI(String apiUUID) throws API ps.setString(1, apiUUID); resultSet = ps.executeQuery(); while (resultSet.next()) { - map.put("sequence", resultSet.getString("SEQUENCE")); - map.put("endpoint_type", resultSet.getString("TYPE")); + map.put("type", resultSet.getString("TYPE")); map.put("sequence_name", resultSet.getString("NAME")); + map.put("endpoint_type", "custom_backend"); } } catch (SQLException ex) { handleException("Error retrieving Custom Backend of an API: " + apiUUID, ex); @@ -21381,14 +21441,15 @@ public void updateAPIPoliciesMapping(String apiUUID, Set uriTemplat } } - public void updateCustomBackend(String apiUUID, String sequenceName, InputStream sequence, String type) throws APIManagementException { + public void updateCustomBackend(String apiUUID, String sequenceName, InputStream sequence, String type, String backendUUID) throws APIManagementException { String deleteCustomBackedQuery = SQLConstants.CustomBackendConstants.DELETE_CUSTOM_BACKEND; try (Connection connection = APIMgtDBUtil.getConnection(); PreparedStatement prepStmt = connection.prepareStatement(deleteCustomBackedQuery)) { connection.setAutoCommit(false); prepStmt.setString(1, apiUUID); + prepStmt.setString(2, backendUUID); prepStmt.executeUpdate(); - addCustomBackend(apiUUID, sequenceName, null, sequence, type, connection); + addCustomBackend(apiUUID, sequenceName, null, sequence, type, connection, backendUUID); connection.commit(); } catch (SQLException e) { handleException("Error while adding Custom Backend for API : " + apiUUID, e); @@ -21409,7 +21470,8 @@ public void deleteCustomBackend(String apiUUID, String revisionId) throws APIMan } } - public void updateCustomBackendByRevision(String apiUUID, String sequenceName, InputStream sequence, String type, String revision) throws APIManagementException { + public void updateCustomBackendByRevision(String apiUUID, String sequenceName, InputStream sequence, String type, + String revision, String backendUUID) throws APIManagementException { String deleteCustomBackedQuery = SQLConstants.CustomBackendConstants.DELETE_CUSTOM_BACKEND_BY_REVISION; try (Connection connection = APIMgtDBUtil.getConnection(); PreparedStatement prepStmt = connection.prepareStatement(deleteCustomBackedQuery)) { @@ -21417,22 +21479,45 @@ public void updateCustomBackendByRevision(String apiUUID, String sequenceName, I prepStmt.setString(1, apiUUID); prepStmt.setString(2, revision); prepStmt.executeUpdate(); - addCustomBackend(apiUUID, sequenceName, revision, sequence, type, connection); + addCustomBackend(apiUUID, sequenceName, revision, sequence, type, connection, backendUUID); connection.commit(); } catch (SQLException e) { handleException("Error while adding Custom Backend for API : " + apiUUID, e); } } - public void addCustomBackend(String apiUUID, String sequenceName, String revision, InputStream sequence, String type, Connection connection) throws APIManagementException { + public void addNewCustomBackendForAPIRevision(String apiUUID, String revisionID, Map endpointConfig) throws APIManagementException { + String addSqlQuery = SQLConstants.CustomBackendConstants.ADD_CUSTOM_BACKEND; + try (Connection con = APIMgtDBUtil.getConnection(); + PreparedStatement ps = con.prepareStatement(addSqlQuery)) { + String backendUUID = endpointConfig.get("sequence_id").toString(); + InputStream sequence = (InputStream) endpointConfig.get("sequence"); + String type = endpointConfig.get("type").toString(); + String sequenceName = endpointConfig.get("sequence_name").toString(); + con.setAutoCommit(false); + ps.setString(1, backendUUID); + ps.setString(2, apiUUID); + ps.setBinaryStream(3, sequence); + ps.setString(4, type); + ps.setString(5, revisionID); + ps.setString(6, sequenceName); + ps.executeUpdate(); + } catch (SQLException ex) { + handleException("Error when inserting Custom Backend data for API: " + apiUUID, ex); + } + } + + public void addCustomBackend(String apiUUID, String sequenceName, String revision, InputStream sequence, + String type, Connection connection, String backendUUID) throws APIManagementException { String insertCustomBackendQuery = SQLConstants.CustomBackendConstants.ADD_CUSTOM_BACKEND; try (PreparedStatement prepStmt = connection.prepareStatement(insertCustomBackendQuery)) { connection.setAutoCommit(false); - prepStmt.setString(1, apiUUID); - prepStmt.setBinaryStream(2, sequence); - prepStmt.setString(3, type); - prepStmt.setString(4, revision); - prepStmt.setString(5, sequenceName); + prepStmt.setString(1, backendUUID); + prepStmt.setString(2, apiUUID); + prepStmt.setBinaryStream(3, sequence); + prepStmt.setString(4, type); + prepStmt.setString(5, revision); + prepStmt.setString(6, sequenceName); prepStmt.executeUpdate(); } catch (SQLException e) { handleException("Error while adding Custom Backend for API : " + apiUUID, e); 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 72f9f1e64194..c6c501701a62 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 @@ -4476,12 +4476,18 @@ public static class TransactionCountConstants { } public static class CustomBackendConstants { public static final String ADD_CUSTOM_BACKEND = - "INSERT INTO AM_API_CUSTOM_BACKEND (API_UUID,SEQUENCE,TYPE,REVISION_UUID,NAME) " + "VALUES (?,?,?,?,?)"; - public static final String DELETE_CUSTOM_BACKEND = "DELETE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ? AND REVISION_UUID IS NULL"; + "INSERT INTO AM_API_CUSTOM_BACKEND (ID,API_UUID,SEQUENCE,TYPE,REVISION_UUID,NAME) " + "VALUES (?,?,?,?,?,?)"; + public static final String DELETE_CUSTOM_BACKEND = "DELETE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ? AND ID = ? AND REVISION_UUID IS NULL"; 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 NAME, SEQUENCE, TYPE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ? AND REVISION_UUID = ?"; - public static final String GET_CUSTOM_BACKEND_OF_API_DEFAULT_REVISION = "SELECT NAME, SEQUENCE, TYPE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ? AND REVISION_UUID IS NULL"; + public static final String GET_CUSTOM_BACKEND_OF_API_DEFAULT_REVISION = "SELECT ACB.NAME, ACB.TYPE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ? AND REVISION_UUID IS NULL"; + public static final String GET_REVISION_SPECIFIC_CUSTOM_BACKEND_FROM_SEQUENCE_ID = "SELECT ACB.ID, ACB.NAME, ACB.SEQUENCE, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.ID = ? AND ACB.REVISION_UUID = ?"; + public static final String GET_API_SPECIFIC_CUSTOM_BACKEND_FROM_SEQUENCE_ID = "SELECT ACB.ID, ACB.NAME, ACB.SEQUENCE, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.ID = ? AND API_UUID = ? AND REVISION_UUID IS NULL"; + public static final String GET_REVISION_SPECIFIC_CUSTOM_BACKEND_META_DATA_FROM_SEQUENCE_ID = "SELECT ACB.ID, ACB.NAME, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.ID = ? AND ACB.REVISION_UUID = ?"; + public static final String GET_API_SPECIFIC_CUSTOM_BACKEND_META_DATA_FROM_SEQUENCE_ID = "SELECT ACB.ID, ACB.NAME, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.ID = ? AND API_UUID = ?"; + public static final String GET_API_SPECIFIC_CUSTOM_BACKEND_SEQUENCE_FROM_SEQUENCE_ID = "SELECT ACB.SEQUENCE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.ID = ? AND API_UUID = ?"; + public static final String GET_REVISION_SPECIFIC_CUSTOM_BACKEND_SEQUENCE_FROM_SEQUENCE_ID = "SELECT ACB.SEQUENCE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.ID = ? AND ACB.REVISION_UUID = ?"; } } diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/importexport/ImportExportConstants.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/importexport/ImportExportConstants.java index 908592a483fc..885fc5c595e7 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/importexport/ImportExportConstants.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/importexport/ImportExportConstants.java @@ -309,6 +309,7 @@ public final class ImportExportConstants { public static final String DISPLAY_ON_DEVPORTAL_OPTION = "displayOnDevportal"; public static final String POLICIES_DIRECTORY = "Policies"; + public static final String CUSTOM_BACKEND_DIRECTORY = "Custom-Backend"; public static final String SWAGGER_X_WSO2_APICTL_INIT = "x-wso2-apictl-init"; public static final String EXPORT_POLICY_TYPE_YAML = "YAML"; 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 facaa270ac65..c9d50b4b7f5e 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 @@ -4302,6 +4302,10 @@ public static String getSequenceExtensionName(API api) { return api.getId().getApiName() + ":v" + api.getId().getVersion(); } + public static String getCustomBackendName(String apiUUID, String type) { + return apiUUID + "-" + type; + } + /** * Return the sequence extension name. * eg: admin--testAPi--v1.00 @@ -10061,6 +10065,27 @@ public static OperationPolicyDefinition getOperationPolicyDefinitionFromFile(Str return policyDefinition; } + public static String getOperationCustomBackendSequenceFromFile(String extractedFolderPath, + String sequenceName, + String fileExtension) + throws APIManagementException { + + String customBackendContent = null; + try { + String fileName = extractedFolderPath + File.separator + sequenceName + fileExtension; + if (checkFileExistence(fileName)) { + if (log.isDebugEnabled()) { + log.debug("Found policy definition file " + fileName); + } + customBackendContent = FileUtils.readFileToString(new File(fileName)); + } + } catch (IOException e) { + throw new APIManagementException("Error while reading Custom Backend from path: " + + extractedFolderPath, e, ExceptionCodes.ERROR_READING_META_DATA); + } + return customBackendContent; + } + /** * Check whether there exists a file for the provided location * @@ -10260,6 +10285,10 @@ public static String getOperationPolicyFileName(String policyName, String policy return policyName + "_" + policyVersion; } + public static String getCustomBackendFileName(String apiUUID, String endpointType) { + return apiUUID + "-" + endpointType; + } + public static void initializeVelocityContext(VelocityEngine velocityEngine){ velocityEngine.setProperty(RuntimeConstants.OLD_CHECK_EMPTY_OBJECTS, false); velocityEngine.setProperty(DeprecatedRuntimeConstants.OLD_SPACE_GOBBLING,"bc"); diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/GatewayUtils.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/GatewayUtils.java index f82916f353aa..633844789fc2 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/GatewayUtils.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/GatewayUtils.java @@ -46,11 +46,6 @@ public static void setCustomSequencesToBeRemoved(API api, GatewayAPIDTO gatewayA gatewayAPIDTO.setSequencesToBeRemove(addStringToList(faultSequence, gatewayAPIDTO.getSequencesToBeRemove())); } - public static void setCustomBackendToBeRemoved(API api, GatewayAPIDTO gatewayAPIDTO) { - String sequence = APIUtil.getSequenceExtensionName(api) + APIConstants.API_CUSTOM_SEQ_IN_EXT + APIConstants.API_CUSTOM_BACKEND_SEQ_EXT; - gatewayAPIDTO.setSequencesToBeRemove(addStringToList(sequence, gatewayAPIDTO.getSequencesToBeRemove())); - } - public static String[] addStringToList(String key, String[] keys) { if (keys == null) { diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml b/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml index e4e4d8cad1c7..667346746d68 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml @@ -487,6 +487,96 @@ paths: -H "Content-Type: application/json" -d @data.json "https://127.0.0.1:9443/api/am/publisher/v4/apis/7a2298c4-c905-403f-8fac-38c73301631f/topics"' + + /apis/{apiId}/custom-backend/{customBackendId}: + get: + tags: + - APIs + summary: Get Custom Backend of the API + description: This operation can be used to get Custom Backend data of the API + operationId: getCustomBackendData + parameters: + - $ref: '#/components/parameters/customBackendId' + - $ref: '#/components/parameters/apiId' + responses: + 200: + description: | + OK. + Requested API Custom Backend is returned + headers: + ETag: + description: | + Entity Tag of the response resource. Used by caches, or in conditional requests (Will be supported in future). + schema: + type: string + Last-Modified: + description: | + Date and time the resource has been modifed the last time. + Used by caches, or in conditional requests (Will be supported in future). + schema: + type: string + Content-Type: + description: | + The content type of the body. + schema: + type: string + content: + application/json: + schema: + $ref: '#/components/schemas/CustomBackend' + 304: + description: | + Not Modified. + Empty body because the client has already the latest version of the requested resource (Will be supported in future). + content: {} + 404: + $ref: '#/components/responses/NotFound' + 406: + $ref: '#/components/responses/NotAcceptable' + security: + - OAuth2Security: + - apim:api_view + - apim:api_manage + - apim:api_import_export + - apim:api_product_import_export + x-code-samples: + - lang: Curl + source: 'curl -k -H "Authorization: Bearer ae4eae22-3f65-387b-a171-d37eaa366fa8" + "https://127.0.0.1:9443/api/am/publisher/v4/apis/7a2298c4-c905-403f-8fac-38c73301631f/custom-backend/7a2298c4-c915-483f-8fac-38c73301631f"' + delete: + tags: + - APIs + summary: Delete Custom Backend of the API + description: This operation can be used to remove the Custom Backend of the API + operationId: customBackendDelete + parameters: + - $ref: '#/components/parameters/apiId' + - $ref: '#/components/parameters/If-Match' + - $ref: '#/components/parameters/customBackendId' + responses: + 200: + description: | + OK. + Resource successfully deleted. + content: { } + 403: + $ref: '#/components/responses/Forbidden' + 404: + $ref: '#/components/responses/NotFound' + 409: + $ref: '#/components/responses/Conflict' + 412: + $ref: '#/components/responses/PreconditionFailed' + security: + - OAuth2Security: + - apim:api_delete + - apim:api_manage + - apim:api_import_export + x-code-samples: + - lang: Curl + source: 'curl -k -X DELETE -H "Authorization: Bearer ae4eae22-3f65-387b-a171-d37eaa366fa8" + "https://127.0.0.1:9443/api/am/publisher/v4/apis/7a2298c4-c905-403f-8fac-38c73301631f/custom-backend/7a2298c4-c915-483f-8fac-38c73301631f"' + /apis/{apiId}/custom-backend: put: tags: @@ -9432,6 +9522,26 @@ paths: -H "Content-Type: application/json" -d @data.json "https://127.0.0.1:9443/api/am/admin/v4/workflows/update-workflow-status?workflowReferenceId=56e3a170-a7a7-45f8-b051-7e43a58a67e1"' components: schemas: + CustomBackend: + title: Custom Backend + type: object + properties: + sequenceId: + type: string + readOnly: true + example: 943d3002-000c-42d3-a1b9-d6559f8a4d49 + sequenceName: + type: string + readOnly: true + example: 943d3002-000c-42d3-a1b9-d6559f8a4d49-SANDBOX + sequenceType: + type: string + readOnly: true + example: SANDBOX + sequence: + type: string + format: binary + readOnly: true Comment: title: Comment required: @@ -13763,6 +13873,14 @@ components: schema: type: integer default: 0 + customBackendId: + name: customBackendId + in: path + description: | + Custom Backend ID + required: true + schema: + type: string commentId: name: commentId in: path diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/dto/CustomBackendDTO.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/dto/CustomBackendDTO.java new file mode 100644 index 000000000000..395cb3e890e2 --- /dev/null +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/dto/CustomBackendDTO.java @@ -0,0 +1,142 @@ +package org.wso2.carbon.apimgt.rest.api.publisher.v1.dto; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonCreator; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import java.io.File; +import javax.validation.constraints.*; + + +import io.swagger.annotations.*; +import java.util.Objects; + +import javax.xml.bind.annotation.*; +import org.wso2.carbon.apimgt.rest.api.common.annotations.Scope; +import com.fasterxml.jackson.annotation.JsonCreator; + +import javax.validation.Valid; + + + +public class CustomBackendDTO { + + private String sequenceId = null; + private String sequenceName = null; + private String sequenceType = null; + private File sequence = null; + + /** + **/ + public CustomBackendDTO sequenceId(String sequenceId) { + this.sequenceId = sequenceId; + return this; + } + + + @ApiModelProperty(example = "943d3002-000c-42d3-a1b9-d6559f8a4d49", value = "") + @JsonProperty("sequenceId") + public String getSequenceId() { + return sequenceId; + } + public void setSequenceId(String sequenceId) { + this.sequenceId = sequenceId; + } + + /** + **/ + public CustomBackendDTO sequenceName(String sequenceName) { + this.sequenceName = sequenceName; + return this; + } + + + @ApiModelProperty(example = "943d3002-000c-42d3-a1b9-d6559f8a4d49-SANDBOX", value = "") + @JsonProperty("sequenceName") + public String getSequenceName() { + return sequenceName; + } + public void setSequenceName(String sequenceName) { + this.sequenceName = sequenceName; + } + + /** + **/ + public CustomBackendDTO sequenceType(String sequenceType) { + this.sequenceType = sequenceType; + return this; + } + + + @ApiModelProperty(example = "SANDBOX", value = "") + @JsonProperty("sequenceType") + public String getSequenceType() { + return sequenceType; + } + public void setSequenceType(String sequenceType) { + this.sequenceType = sequenceType; + } + + /** + **/ + public CustomBackendDTO sequence(File sequence) { + this.sequence = sequence; + return this; + } + + + @ApiModelProperty(value = "") + @JsonProperty("sequence") + public File getSequence() { + return sequence; + } + public void setSequence(File sequence) { + this.sequence = sequence; + } + + + @Override + public boolean equals(java.lang.Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + CustomBackendDTO customBackend = (CustomBackendDTO) o; + return Objects.equals(sequenceId, customBackend.sequenceId) && + Objects.equals(sequenceName, customBackend.sequenceName) && + Objects.equals(sequenceType, customBackend.sequenceType) && + Objects.equals(sequence, customBackend.sequence); + } + + @Override + public int hashCode() { + return Objects.hash(sequenceId, sequenceName, sequenceType, sequence); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("class CustomBackendDTO {\n"); + + sb.append(" sequenceId: ").append(toIndentedString(sequenceId)).append("\n"); + sb.append(" sequenceName: ").append(toIndentedString(sequenceName)).append("\n"); + sb.append(" sequenceType: ").append(toIndentedString(sequenceType)).append("\n"); + sb.append(" sequence: ").append(toIndentedString(sequence)).append("\n"); + sb.append("}"); + return sb.toString(); + } + + /** + * Convert the given object to string with each line indented by 4 spaces + * (except the first line). + */ + private String toIndentedString(java.lang.Object o) { + if (o == null) { + return "null"; + } + return o.toString().replace("\n", "\n "); + } +} + 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/SynapseArtifactGenerator.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/SynapseArtifactGenerator.java index 54615455d838..abe5153019e0 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/SynapseArtifactGenerator.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/SynapseArtifactGenerator.java @@ -115,7 +115,7 @@ public RuntimeArtifactDto generateGatewayArtifact(List ap } else { APIDTO apidto = ImportUtils.retrievedAPIDto(extractedFolderPath); // Update the EndpointConfig if it's a Custom Backend - updateCustomBackendOfAPI(apidto, runTimeArtifact.getRevision()); + // updateCustomBackendOfAPI(apidto, runTimeArtifact.getRevision()); API api = APIMappingUtil.fromDTOtoAPI(apidto, apidto.getProvider()); api.setUUID(apidto.getId()); 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/SynapsePolicyAggregator.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/SynapsePolicyAggregator.java index 0bdc79ec7534..ae350198d250 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/SynapsePolicyAggregator.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/SynapsePolicyAggregator.java @@ -107,26 +107,18 @@ public static String generatePolicySequenceForUriTemplateSet(Set ur } } - public static String generateBackendSequenceForCustomSequence(API api, String sequenceName, String flow, - String sequence, String endpointType) throws APIManagementException, IOException { + public static String generateBackendSequenceForCustomSequence(String sequenceName, String pathToArchive, String endpointType) throws APIManagementException, IOException { Map configMap = new HashMap<>(); - boolean render = false; String customBackendTemplate = FileUtil.readFileToString(CUSTOM_BACKEND_SEQUENCE_TEMPLATE_LOCATION) .replace("\\", ""); configMap.put("sequence_name", sequenceName); - if (sequence != null || sequence.isEmpty()) { - String sanitizedSequence = renderCustomBackendSequence(sequence, sequenceName); - if (sanitizedSequence == null) { - throw new APIManagementException("Error when preparing the sequence: " + sequenceName); - } - configMap.put("custom_sequence", sanitizedSequence); - configMap.put("endpoint_type", endpointType); - render = true; + String sanitizedSequence = renderCustomBackendSequence(sequenceName, pathToArchive); + if (sanitizedSequence == null) { + throw new APIManagementException("Error when preparing the sequence: " + sequenceName); } - if (render) { - return renderPolicyTemplate(customBackendTemplate, configMap); - } - return ""; + configMap.put("custom_sequence", sanitizedSequence); + configMap.put("endpoint_type", endpointType); + return renderPolicyTemplate(customBackendTemplate, configMap); } /** @@ -227,11 +219,10 @@ private static List renderPolicyMapping(List policyList return renderedPolicyMappingList; } - private static String renderCustomBackendSequence(String sequence, String sequenceName) { - if (sequence != null) { - return renderPolicyTemplate(sequence, new HashMap<>()); - } - return null; + private static String renderCustomBackendSequence(String sequenceName, String pathToArchive) throws APIManagementException { + String policyDirectory = pathToArchive + File.separator + ImportExportConstants.CUSTOM_BACKEND_DIRECTORY; + String sequence = APIUtil.getOperationCustomBackendSequenceFromFile(policyDirectory, sequenceName, ".xml"); + return renderPolicyTemplate(sequence, new HashMap<>()); } /** 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/TemplateBuilderUtil.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/TemplateBuilderUtil.java index f91d447788f5..fb54940d6143 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/TemplateBuilderUtil.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/TemplateBuilderUtil.java @@ -972,11 +972,9 @@ private static void setCustomSequencesToBeAdded(API api, GatewayAPIDTO gatewayAP Map endpointConfigMap = (Map) apidto.getEndpointConfig(); if (APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( - endpointConfigMap.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE)) - && endpointConfigMap.get("sequence") != null) { + endpointConfigMap.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE))) { GatewayContentDTO gatewayCustomBackendSequenceDTO = retrieveCustomBackendSequence(api, - APIConstants.OPERATION_SEQUENCE_TYPE_REQUEST, endpointConfigMap.get("sequence").toString(), - endpointConfigMap.get("type").toString()); + endpointConfigMap.get("type").toString(), extractedPath); if (gatewayCustomBackendSequenceDTO != null) { gatewayAPIDTO.setSequenceToBeAdd( addGatewayContentToList(gatewayCustomBackendSequenceDTO, gatewayAPIDTO.getSequenceToBeAdd())); @@ -1429,16 +1427,15 @@ private static GatewayContentDTO retrieveOperationPolicySequence(String pathToAc return null; } - private static GatewayContentDTO retrieveCustomBackendSequence(API api, String flow, String sequence, - String endpointType) throws APIManagementException { + private static GatewayContentDTO retrieveCustomBackendSequence(API api, String endpointType, String pathToAchieve) + throws APIManagementException { GatewayContentDTO customBackendSequenceContentDto = new GatewayContentDTO(); String customSequence = null; - String seqExt = APIUtil.getSequenceExtensionName(api) + SynapsePolicyAggregator.getSequenceExtensionFlow(flow) - + "-Custom-Backend"; + String seqExt = APIUtil.getCustomBackendName(api.getUuid(), endpointType); try { - customSequence = SynapsePolicyAggregator.generateBackendSequenceForCustomSequence(api, seqExt, flow, - sequence, endpointType); + customSequence = SynapsePolicyAggregator.generateBackendSequenceForCustomSequence(seqExt, + pathToAchieve, endpointType); } catch (IOException e) { throw new APIManagementException(e); } 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/APIMappingUtil.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/APIMappingUtil.java index d86b98621c04..1bd8a94d54ed 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/APIMappingUtil.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/APIMappingUtil.java @@ -209,15 +209,6 @@ public static API fromDTOtoAPI(APIDTO dto, String provider) throws APIManagement if (endpointConfig instanceof LinkedHashMap) { ((LinkedHashMap) endpointConfig).remove(APIConstants.IMPLEMENTATION_STATUS); } - if (endpointConfig instanceof HashMap) { - if (APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( - ((HashMap) endpointConfig).get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE))) { - String seqExt = APIUtil.getSequenceExtensionName(dto.getName(), dto.getVersion()) - + SynapsePolicyAggregator.getSequenceExtensionFlow( - APIConstants.OPERATION_SEQUENCE_TYPE_REQUEST) + "-Custom-Backend"; - ((HashMap) endpointConfig).put("sequence_name", seqExt); - } - } model.setEndpointConfig(mapper.writeValueAsString(endpointConfig)); } catch (IOException e) { handleException("Error while converting endpointConfig to json", e); 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/ExportUtils.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/ExportUtils.java index 1c89b95b2cc1..1779c954992d 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/ExportUtils.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/ExportUtils.java @@ -86,6 +86,7 @@ import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Set; import static org.wso2.carbon.apimgt.impl.APIConstants.API_DATA_PRODUCTION_ENDPOINTS; @@ -216,6 +217,13 @@ public static File exportApi(APIProvider apiProvider, APIIdentifier apiIdentifie String tenantDomain = APIUtil.getTenantDomainFromTenantId(tenantId); addOperationPoliciesToArchive(archivePath, tenantDomain, exportFormat, apiProvider, api, currentApiUuid); + + // TODO: Add Custom Backend to the Archive + JsonObject endpointConfig = JsonParser.parseString(api.getEndpointConfig()).getAsJsonObject(); + if(APIConstants.ENDPOINT_TYPE_SEQUENCE.equals(endpointConfig.get(API_ENDPOINT_CONFIG_PROTOCOL_TYPE).getAsString())) { + addCustomBackendToAPI(archivePath, apiProvider, api, currentApiUuid, endpointConfig); + } + addGatewayEnvironmentsToArchive(archivePath, apiDtoToReturn.getId(), exportFormat, apiProvider); if (migrationEnabled != null) { @@ -628,6 +636,23 @@ public static void addEndpointCertificatesToArchive(String archivePath, APIDTO a } } + public static void addCustomBackendToAPI(String archivePath, + APIProvider apiProvider, API api, String currentApiUuid, JsonObject endpointConfig) throws APIManagementException { + String exportedCustomBackend = null; + try { + String backendUUID = endpointConfig.get("sequence_id").getAsString(); + CommonUtil.createDirectory(archivePath + File.separator + ImportExportConstants.CUSTOM_BACKEND_DIRECTORY); + String customBackendName = APIUtil.getCustomBackendName(api.getUuid(), endpointConfig.get("type").getAsString()); + Map endpointConfigMap = apiProvider.getCustomBackendOfAPIByUUID(backendUUID, currentApiUuid, false); + if(endpointConfigMap != null) { + exportCustomBackend(customBackendName, endpointConfigMap, archivePath); + } + + } catch (IOException | APIImportExportException ex) { + + } + } + /** * Retrieve the operation policies and store those in the archive directory. * @@ -736,6 +761,14 @@ public static void exportPolicyData(String policyFileName, OperationPolicyData p } } + public static void exportCustomBackend(String customBackendFileName, Map endpointConfig, String archivePath) throws APIImportExportException, IOException { + String customBackendName = archivePath + File.separator + ImportExportConstants.CUSTOM_BACKEND_DIRECTORY + File.separator + customBackendFileName; + if(endpointConfig != null && endpointConfig.get(API_ENDPOINT_CONFIG_PROTOCOL_TYPE) != null && APIConstants.ENDPOINT_TYPE_SEQUENCE.equals(endpointConfig.get( + API_ENDPOINT_CONFIG_PROTOCOL_TYPE))) { + CommonUtil.writeFile(customBackendName + APIConstants.SYNAPSE_POLICY_DEFINITION_EXTENSION_XML, endpointConfig.get("sequence")); + } + } + /** * Retrieve the deployed gateway environments and store those in the archive directory. * 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 f6278c12c449..fb938092a9d8 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 @@ -113,6 +113,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import java.util.UUID; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -206,14 +207,22 @@ public static API updateApi(API originalAPI, APIDTO apiDtoToUpdate, APIProvider * @return Custom Backend File Name * @throws APIManagementException If an error occurs while updating the API and API definition */ - public static String updateCustomBackend(API api, APIProvider apiProvider, String endpointType, + public static JSONObject updateCustomBackend(API api, APIProvider apiProvider, String endpointType, InputStream customBackend, String contentDecomp) throws APIManagementException { String fileName = getFileNameFromContentDisposition(contentDecomp); if (fileName == null) throw new APIManagementException( "Error when retrieving Custom Backend file name of API: " + api.getId().getApiName()); - apiProvider.updateCustomBackend(api, endpointType, customBackend, fileName); - return fileName; + String seqName = APIUtil.getCustomBackendName(api.getUuid(), endpointType); + String customBackendUUID = UUID.randomUUID().toString(); + apiProvider.updateCustomBackend(api, endpointType, customBackend, seqName, customBackendUUID); + JSONObject obj = new JSONObject(); + obj.put("sequence_name", seqName); + obj.put("sequence_file", fileName); + obj.put("sequence_id", customBackendUUID); + obj.put("type", endpointType); + obj.put("endpoint_type", "custom_backend"); + return obj; } private static String getFileNameFromContentDisposition(String contentDisposition) { diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java index a269e4d41fac..bf369c2e08ce 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java @@ -21,10 +21,6 @@ import com.amazonaws.SdkClientException; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.gson.Gson; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; -import com.google.gson.internal.LinkedTreeMap; import org.apache.commons.collections.MapUtils; import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.IOUtils; @@ -217,11 +213,9 @@ public Response getAPI(String apiId, String xWSO2Tenant, String ifNoneMatch, MultivaluedMap headers = sequenceDetail.getHeaders(); String contentDecomp = headers.getFirst("Content-Disposition"); - String sequenceName = PublisherCommonUtils.updateCustomBackend(api, apiProvider, type, sequenceInputStream, + JSONObject resp = PublisherCommonUtils.updateCustomBackend(api, apiProvider, type, sequenceInputStream, contentDecomp); - JSONObject obj = new JSONObject(); - obj.put("sequenceName", sequenceName); - return Response.ok().entity(obj).build(); + return Response.ok().entity(resp).build(); } @Override @@ -3818,6 +3812,20 @@ public Response createAPIRevision(String apiId, APIRevisionDTO apIRevisionDTO, M //adding the api revision String revisionId = apiProvider.addAPIRevision(apiRevision, organization); + Object endpointConfig = apiDto.getEndpointConfig(); + + if (endpointConfig != null && endpointConfig instanceof HashMap) { + String endpointType = ((HashMap) endpointConfig).get("endpoint_type").toString(); + if (endpointType != null && APIConstants.ENDPOINT_TYPE_SEQUENCE.equals(endpointType)) { + String sequenceName = ((HashMap) endpointConfig).get("sequence_name").toString(); + String sequenceEndpointType = ((HashMap) endpointConfig).get("sequence_type").toString(); + String backendUUID = ((HashMap) endpointConfig).get("sequence_id").toString(); + // Update Custom Backend table + apiProvider.addNewCustomBackendForRevision(revisionId, backendUUID, apiId, + (HashMap) endpointConfig); + } + } + //Retrieve the newly added APIRevision to send in the response payload APIRevision createdApiRevision = apiProvider.getAPIRevision(revisionId); APIRevisionDTO createdApiRevisionDTO = APIMappingUtil.fromAPIRevisiontoDTO(createdApiRevision); diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/resources/publisher-api.yaml b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/resources/publisher-api.yaml index e4e4d8cad1c7..667346746d68 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/resources/publisher-api.yaml +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/resources/publisher-api.yaml @@ -487,6 +487,96 @@ paths: -H "Content-Type: application/json" -d @data.json "https://127.0.0.1:9443/api/am/publisher/v4/apis/7a2298c4-c905-403f-8fac-38c73301631f/topics"' + + /apis/{apiId}/custom-backend/{customBackendId}: + get: + tags: + - APIs + summary: Get Custom Backend of the API + description: This operation can be used to get Custom Backend data of the API + operationId: getCustomBackendData + parameters: + - $ref: '#/components/parameters/customBackendId' + - $ref: '#/components/parameters/apiId' + responses: + 200: + description: | + OK. + Requested API Custom Backend is returned + headers: + ETag: + description: | + Entity Tag of the response resource. Used by caches, or in conditional requests (Will be supported in future). + schema: + type: string + Last-Modified: + description: | + Date and time the resource has been modifed the last time. + Used by caches, or in conditional requests (Will be supported in future). + schema: + type: string + Content-Type: + description: | + The content type of the body. + schema: + type: string + content: + application/json: + schema: + $ref: '#/components/schemas/CustomBackend' + 304: + description: | + Not Modified. + Empty body because the client has already the latest version of the requested resource (Will be supported in future). + content: {} + 404: + $ref: '#/components/responses/NotFound' + 406: + $ref: '#/components/responses/NotAcceptable' + security: + - OAuth2Security: + - apim:api_view + - apim:api_manage + - apim:api_import_export + - apim:api_product_import_export + x-code-samples: + - lang: Curl + source: 'curl -k -H "Authorization: Bearer ae4eae22-3f65-387b-a171-d37eaa366fa8" + "https://127.0.0.1:9443/api/am/publisher/v4/apis/7a2298c4-c905-403f-8fac-38c73301631f/custom-backend/7a2298c4-c915-483f-8fac-38c73301631f"' + delete: + tags: + - APIs + summary: Delete Custom Backend of the API + description: This operation can be used to remove the Custom Backend of the API + operationId: customBackendDelete + parameters: + - $ref: '#/components/parameters/apiId' + - $ref: '#/components/parameters/If-Match' + - $ref: '#/components/parameters/customBackendId' + responses: + 200: + description: | + OK. + Resource successfully deleted. + content: { } + 403: + $ref: '#/components/responses/Forbidden' + 404: + $ref: '#/components/responses/NotFound' + 409: + $ref: '#/components/responses/Conflict' + 412: + $ref: '#/components/responses/PreconditionFailed' + security: + - OAuth2Security: + - apim:api_delete + - apim:api_manage + - apim:api_import_export + x-code-samples: + - lang: Curl + source: 'curl -k -X DELETE -H "Authorization: Bearer ae4eae22-3f65-387b-a171-d37eaa366fa8" + "https://127.0.0.1:9443/api/am/publisher/v4/apis/7a2298c4-c905-403f-8fac-38c73301631f/custom-backend/7a2298c4-c915-483f-8fac-38c73301631f"' + /apis/{apiId}/custom-backend: put: tags: @@ -9432,6 +9522,26 @@ paths: -H "Content-Type: application/json" -d @data.json "https://127.0.0.1:9443/api/am/admin/v4/workflows/update-workflow-status?workflowReferenceId=56e3a170-a7a7-45f8-b051-7e43a58a67e1"' components: schemas: + CustomBackend: + title: Custom Backend + type: object + properties: + sequenceId: + type: string + readOnly: true + example: 943d3002-000c-42d3-a1b9-d6559f8a4d49 + sequenceName: + type: string + readOnly: true + example: 943d3002-000c-42d3-a1b9-d6559f8a4d49-SANDBOX + sequenceType: + type: string + readOnly: true + example: SANDBOX + sequence: + type: string + format: binary + readOnly: true Comment: title: Comment required: @@ -13763,6 +13873,14 @@ components: schema: type: integer default: 0 + customBackendId: + name: customBackendId + in: path + description: | + Custom Backend ID + required: true + schema: + type: string commentId: name: commentId in: path diff --git a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/mysql.sql b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/mysql.sql index a279934e9286..2355c6e3b07b 100644 --- a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/mysql.sql +++ b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/mysql.sql @@ -1524,6 +1524,17 @@ CREATE TABLE IF NOT EXISTS AM_API ( UNIQUE (API_UUID) )ENGINE INNODB; +CREATE TABLE IF NOT EXISTS AM_API_CUSTOM_BACKEND ( + ID VARCHAR(60) NOT NULL, + API_UUID VARCHAR(256) NOT NULL, + REVISION_UUID VARCHAR(256), + SEQUENCE LONGBLOB, + NAME VARCHAR(256) NOT NULL, + TYPE VARCHAR(120) NOT NULL, + PRIMARY KEY (ID, API_UUID, REVISION_UUID), + FOREIGN KEY (API_UUID) REFERENCES AM_API(API_UUID) ON DELETE CASCADE +)ENGINE INNODB; + CREATE TABLE IF NOT EXISTS AM_GRAPHQL_COMPLEXITY ( UUID VARCHAR(256), API_ID INTEGER NOT NULL, From f16e7561a46b9c63c72a75d5a479ce6167055dcd Mon Sep 17 00:00:00 2001 From: BLasan Date: Thu, 5 Sep 2024 09:07:44 +0530 Subject: [PATCH 09/56] Remove: Unnecessary Codes --- .../wso2/carbon/apimgt/api/APIProvider.java | 4 +- .../carbon/apimgt/impl/APIProviderImpl.java | 48 ++++++++-- .../carbon/apimgt/impl/dao/ApiMgtDAO.java | 91 +++++++++++++++---- .../impl/dao/constants/SQLConstants.java | 9 +- .../src/main/resources/publisher-api.yaml | 28 ++++++ .../v1/common/SynapseArtifactGenerator.java | 27 +----- .../v1/common/mappings/ExportUtils.java | 34 ++++--- .../apimgt/rest/api/publisher/v1/ApisApi.java | 43 +++++++++ .../rest/api/publisher/v1/ApisApiService.java | 3 + .../publisher/v1/impl/ApisApiServiceImpl.java | 42 ++++++--- .../src/main/resources/sql/mysql.sql | 4 +- 11 files changed, 244 insertions(+), 89 deletions(-) diff --git a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java index ff9a56bb79b9..7ca1d7a2394a 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java +++ b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java @@ -65,6 +65,7 @@ public interface APIProvider extends APIManager { */ Comment getComment(ApiTypeWrapper apiTypeWrapper, String commentId, Integer replyLimit, Integer replyOffset) throws APIManagementException; + void deleteCustomBackendByID(String backendUUID, String apiUUID, String type) throws APIManagementException; /** * @param apiTypeWrapper Api type wrapper @@ -317,7 +318,8 @@ List getSubscriptionsOfAPI(String apiName, String apiVersion, Str API updateAPI(API api, API existingAPI) throws APIManagementException, FaultGatewaysException; void updateCustomBackend(API api,String type, InputStream sequence, String fileName, String customBackendUUID) throws APIManagementException; - Map getCustomBackendOfAPIByUUID(String customBackendUUID, String apiUUID, boolean isInfoOnly) throws APIManagementException; + Map getCustomBackendOfAPIByUUID(String customBackendUUID, String apiUUID, String type, boolean isInfoOnly) throws APIManagementException; + InputStream getCustomBackendSequenceOfAPIByUUID(String apiUUID, String backendUUID, String type) throws APIManagementException; void updateCustomBackendByRevisionID(String apiUUID, String type, String revision, String seqName, String backendUUID) throws APIManagementException; diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java index b5da0d1cbc0a..43e40d18a88d 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java @@ -34,6 +34,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.solr.client.solrj.util.ClientUtils; +import org.apache.solr.common.util.Hash; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; @@ -1176,23 +1177,50 @@ public void updateCustomBackend(API api, String type, InputStream sequence, Stri } @Override - public Map getCustomBackendOfAPIByUUID(String customBackendUUID, String apiUUID, boolean isInfoOnly) throws APIManagementException { - return apiMgtDAO.getCustomBackendOfAPIByUUID(customBackendUUID, apiUUID, isInfoOnly); + public Map getCustomBackendOfAPIByUUID(String customBackendUUID, String apiUUID, String type, boolean isInfoOnly) throws APIManagementException { + return apiMgtDAO.getCustomBackendOfAPIByUUID(customBackendUUID, apiUUID, type, isInfoOnly); } - @Override - public void updateCustomBackendByRevisionID(String apiUUID, String type, String revision, - String seqName, String backendUUID) throws APIManagementException { + @Override public void updateCustomBackendByRevisionID(String apiUUID, String type, String revision, String seqName, + String backendUUID) throws APIManagementException { String customBackendUUID = UUID.randomUUID().toString(); - InputStream sequence = apiMgtDAO.getCustomBackendSequenceOfAPIByUUID(backendUUID, apiUUID); + InputStream sequence = apiMgtDAO.getCustomBackendSequenceOfAPIByUUID(backendUUID, apiUUID, type); apiMgtDAO.updateCustomBackendByRevision(apiUUID, seqName, sequence, type, revision, customBackendUUID); } + @Override public InputStream getCustomBackendSequenceOfAPIByUUID(String apiUUID, String backendUUID, String type) + throws APIManagementException { + return apiMgtDAO.getCustomBackendSequenceOfAPIByUUID(backendUUID, apiUUID, type); + } + @Override public void addNewCustomBackendForRevision(String revisionUUID, String updatedBackendUUID, String apiUUID, Map config) throws APIManagementException { - InputStream sequence = apiMgtDAO.getCustomBackendSequenceOfAPIByUUID(updatedBackendUUID, apiUUID); - config.put("sequence", sequence); - apiMgtDAO.addNewCustomBackendForAPIRevision(apiUUID, revisionUUID, config); + if (config.get("production") != null) { + InputStream sequence = apiMgtDAO.getCustomBackendSequenceOfAPIByUUID(updatedBackendUUID, apiUUID, + "PRODUCTION"); + if (config.get("production") instanceof HashMap) { + String seqName = ((HashMap) config.get("production")).get("sequence_name").toString(); + String backendUUID = ((HashMap) config.get("production")).get("sequence_id").toString(); + apiMgtDAO.addNewCustomBackendForAPIRevision(apiUUID, revisionUUID, "PRODUCTION", seqName, sequence, + backendUUID); + } + } + + if (config.get("SANDBOX") != null) { + InputStream sequence = apiMgtDAO.getCustomBackendSequenceOfAPIByUUID(updatedBackendUUID, apiUUID, + "SANDBOX"); + if (config.get("sandbox") instanceof HashMap) { + String seqName = ((HashMap) config.get("sandbox")).get("sequence_name").toString(); + String backendUUID = ((HashMap) config.get("sandbox")).get("sequence_id").toString(); + apiMgtDAO.addNewCustomBackendForAPIRevision(apiUUID, revisionUUID, "SANDBOX", seqName, sequence, + backendUUID); + } + } + } + + @Override public void deleteCustomBackendByID(String backendUUID, String apiUUID, String type) + throws APIManagementException { + apiMgtDAO.deleteCustomBackend(apiUUID, backendUUID, type); } private void validateKeyManagers(API api) throws APIManagementException { @@ -6734,7 +6762,7 @@ public void deleteAPIRevision(String apiId, String apiRevisionId, String organiz ERROR_DELETING_API_REVISION,apiRevision.getApiUUID())); } apiMgtDAO.deleteAPIRevision(apiRevision); - apiMgtDAO.deleteCustomBackend(apiId, apiRevisionId); + apiMgtDAO.deleteCustomBackendByRevision(apiId, apiRevisionId); gatewayArtifactsMgtDAO.deleteGatewayArtifact(apiRevision.getApiUUID(), apiRevision.getRevisionUUID()); if (artifactSaver != null) { try { 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 805ebfe4589f..370d039bba74 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 @@ -20,7 +20,6 @@ import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; -import jdk.internal.util.xml.impl.Input; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.tuple.Pair; @@ -11240,7 +11239,7 @@ public Map retrieveCustomBackendOfAPIRevision(String apiUUID, St return map; } - public InputStream getCustomBackendSequenceOfAPIByUUID(String backendUUID, String apiUUID) throws APIManagementException { + public InputStream getCustomBackendSequenceOfAPIByUUID(String backendUUID, String apiUUID, String type) throws APIManagementException { String sqlQuery = SQLConstants.CustomBackendConstants.GET_API_SPECIFIC_CUSTOM_BACKEND_FROM_SEQUENCE_ID; ResultSet rs = null; InputStream sequence = null; @@ -11266,27 +11265,31 @@ public InputStream getCustomBackendSequenceOfAPIByUUID(String backendUUID, Strin return sequence; } - public Map getCustomBackendOfAPIByUUID(String backendUUID, String apiUUID, boolean isInfo) throws APIManagementException { + public Map getCustomBackendOfAPIByUUID(String backendUUID, String apiUUID, String type, + boolean isInfo) throws APIManagementException { String sqlQuery; - Map endpointConfig = new HashMap<>(); + Map endpointConfig = new HashMap<>(); boolean isRevisioned = checkAPIUUIDIsARevisionUUID(apiUUID) != null; - if(isRevisioned) { + if (isRevisioned) { sqlQuery = SQLConstants.CustomBackendConstants.GET_REVISION_SPECIFIC_CUSTOM_BACKEND_FROM_SEQUENCE_ID; } else { sqlQuery = SQLConstants.CustomBackendConstants.GET_API_SPECIFIC_CUSTOM_BACKEND_FROM_SEQUENCE_ID; } ResultSet resultSet = null; try (Connection connection = APIMgtDBUtil.getConnection(); - PreparedStatement ps = connection.prepareStatement(sqlQuery)) { + PreparedStatement ps = connection.prepareStatement(sqlQuery)) { ps.setString(1, backendUUID); ps.setString(2, apiUUID); + ps.setString(3, type); resultSet = ps.executeQuery(); while (resultSet.next()) { - if(!isInfo) { + if (!isInfo) { try (InputStream in = resultSet.getBinaryStream("SEQUENCE")) { - endpointConfig.put("sequence", IOUtils.toString(in)); + endpointConfig.put("sequence", in); } catch (IOException ex) { - handleException("Error reading Sequence Content of Custom Backend: " + backendUUID + " API: " + apiUUID, ex); + handleException( + "Error reading Sequence Content of Custom Backend: " + backendUUID + " API: " + apiUUID, + ex); } } endpointConfig.put("type", resultSet.getString("TYPE")); @@ -17515,6 +17518,9 @@ public void addAPIRevision(APIRevision apiRevision) throws APIManagementExceptio // Retrieve API ID APIIdentifier apiIdentifier = APIUtil.getAPIIdentifierFromUUID(apiRevision.getApiUUID()); + + // Insert Custom Backend if Provided + int apiId = getAPIID(apiRevision.getApiUUID(), connection); int tenantId = APIUtil.getTenantId(APIUtil.replaceEmailDomainBack(apiIdentifier.getProviderName())); String tenantDomain = APIUtil.getTenantDomainFromTenantId(tenantId); @@ -17643,6 +17649,8 @@ public void addAPIRevision(APIRevision apiRevision) throws APIManagementExceptio insertScopeResourceMappingStatement.executeBatch(); insertProductResourceMappingStatement.executeBatch(); revisionAPIPolicies(apiRevision, tenantDomain, uriTemplateMap, connection); + // Add Custom Backend + revisionCustomBackend(apiRevision, connection); // Adding to AM_API_CLIENT_CERTIFICATE String getClientCertificatesQuery = SQLConstants.APIRevisionSqlConstants.GET_CLIENT_CERTIFICATES_OF_KEY_TYPE; @@ -21448,6 +21456,7 @@ public void updateCustomBackend(String apiUUID, String sequenceName, InputStream connection.setAutoCommit(false); prepStmt.setString(1, apiUUID); prepStmt.setString(2, backendUUID); + prepStmt.setString(3, type); prepStmt.executeUpdate(); addCustomBackend(apiUUID, sequenceName, null, sequence, type, connection, backendUUID); connection.commit(); @@ -21456,13 +21465,14 @@ public void updateCustomBackend(String apiUUID, String sequenceName, InputStream } } - public void deleteCustomBackend(String apiUUID, String revisionId) throws APIManagementException { - String deleteCustomBackedQuery = SQLConstants.CustomBackendConstants.DELETE_CUSTOM_BACKEND_BY_REVISION; + public void deleteCustomBackend(String apiUUID, String type, String backendUUID) throws APIManagementException { + String deleteCustomBackedQuery = SQLConstants.CustomBackendConstants.DELETE_CUSTOM_BACKEND; try (Connection connection = APIMgtDBUtil.getConnection(); PreparedStatement prepStmt = connection.prepareStatement(deleteCustomBackedQuery)) { connection.setAutoCommit(false); prepStmt.setString(1, apiUUID); - prepStmt.setString(2, revisionId); + prepStmt.setString(2, backendUUID); + prepStmt.setString(3, type); prepStmt.executeUpdate(); connection.commit(); } catch (SQLException e) { @@ -21470,6 +21480,24 @@ public void deleteCustomBackend(String apiUUID, String revisionId) throws APIMan } } + public void deleteCustomBackendByRevision(String apiUUID, String revisionUUID) throws APIManagementException { + boolean isRevisioned = checkAPIUUIDIsARevisionUUID(apiUUID) != null; + String deleteSqlQuery = SQLConstants.CustomBackendConstants.DELETE_CUSTOM_BACKEND_BY_REVISION; + if(isRevisioned) { + try (Connection con = APIMgtDBUtil.getConnection(); + PreparedStatement ps = con.prepareStatement(deleteSqlQuery)) { + con.setAutoCommit(false); + ps.setString(1, apiUUID); + ps.executeUpdate(); + con.commit(); + } catch (SQLException ex) { + handleException("Error deleting Custom Backend for Revision: "+ apiUUID, ex); + } + } else { + throw new APIManagementException("Invalid Revision Id: "+ apiUUID); + } + } + public void updateCustomBackendByRevision(String apiUUID, String sequenceName, InputStream sequence, String type, String revision, String backendUUID) throws APIManagementException { String deleteCustomBackedQuery = SQLConstants.CustomBackendConstants.DELETE_CUSTOM_BACKEND_BY_REVISION; @@ -21486,22 +21514,19 @@ public void updateCustomBackendByRevision(String apiUUID, String sequenceName, I } } - public void addNewCustomBackendForAPIRevision(String apiUUID, String revisionID, Map endpointConfig) throws APIManagementException { + public void addNewCustomBackendForAPIRevision(String apiUUID, String revisionID, String type, String seqName, InputStream sequence, String backendUUID) throws APIManagementException { String addSqlQuery = SQLConstants.CustomBackendConstants.ADD_CUSTOM_BACKEND; try (Connection con = APIMgtDBUtil.getConnection(); PreparedStatement ps = con.prepareStatement(addSqlQuery)) { - String backendUUID = endpointConfig.get("sequence_id").toString(); - InputStream sequence = (InputStream) endpointConfig.get("sequence"); - String type = endpointConfig.get("type").toString(); - String sequenceName = endpointConfig.get("sequence_name").toString(); con.setAutoCommit(false); ps.setString(1, backendUUID); ps.setString(2, apiUUID); ps.setBinaryStream(3, sequence); ps.setString(4, type); ps.setString(5, revisionID); - ps.setString(6, sequenceName); + ps.setString(6, seqName); ps.executeUpdate(); + con.commit(); } catch (SQLException ex) { handleException("Error when inserting Custom Backend data for API: " + apiUUID, ex); } @@ -21516,6 +21541,9 @@ public void addCustomBackend(String apiUUID, String sequenceName, String revisio prepStmt.setString(2, apiUUID); prepStmt.setBinaryStream(3, sequence); prepStmt.setString(4, type); + if (revision == null) { + revision = "0"; + } prepStmt.setString(5, revision); prepStmt.setString(6, sequenceName); prepStmt.executeUpdate(); @@ -21708,6 +21736,33 @@ private List getAPIPolicyMapping(String apiUUID, String revisio return policyList; } + private void revisionCustomBackend(APIRevision apiRevision, Connection connection) + throws SQLException, APIManagementException { + String addCBSqlQuery = SQLConstants.CustomBackendConstants.ADD_CUSTOM_BACKEND; + String getCBSQLQuery = SQLConstants.CustomBackendConstants.GET_ALL_API_SPECIFIC_CUSTOM_BACKENDS; + ResultSet rs = null; + try (Connection con = APIMgtDBUtil.getConnection(); + PreparedStatement getPstmt = con.prepareStatement(getCBSQLQuery); + PreparedStatement addPstmt = con.prepareStatement(addCBSqlQuery)) { + con.setAutoCommit(false); + getPstmt.setString(1, apiRevision.getApiUUID()); + rs = getPstmt.executeQuery(); + while (rs.next()) { + addPstmt.setString(1, rs.getString("ID")); + addPstmt.setString(2, apiRevision.getApiUUID()); + addPstmt.setBinaryStream(3, rs.getBinaryStream("SEQUENCE")); + addPstmt.setString(4, rs.getString("TYPE")); + addPstmt.setString(5, apiRevision.getRevisionUUID()); + addPstmt.setString(6, rs.getString("NAME")); + addPstmt.addBatch(); + } + addPstmt.executeBatch(); + } catch (SQLException ex) { + handleException("Error while adding Custom Backends to the database of API: " + apiRevision.getApiUUID(), + ex); + } + } + /** * Create a revision of API policis. This will clone the policy and policy mapping with each revision * 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 c6c501701a62..28de7e20349b 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 @@ -4477,13 +4477,14 @@ public static class TransactionCountConstants { 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_CUSTOM_BACKEND = "DELETE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ? AND ID = ? AND REVISION_UUID IS NULL"; - 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 = "DELETE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ? AND ID = ? AND TYPE = ? AND REVISION_UUID = 0"; + public static final String DELETE_CUSTOM_BACKEND_BY_REVISION = "DELETE FROM AM_API_CUSTOM_BACKEND WHERE 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 NAME, SEQUENCE, TYPE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ? AND REVISION_UUID = ?"; public static final String GET_CUSTOM_BACKEND_OF_API_DEFAULT_REVISION = "SELECT ACB.NAME, ACB.TYPE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ? AND REVISION_UUID IS NULL"; - public static final String GET_REVISION_SPECIFIC_CUSTOM_BACKEND_FROM_SEQUENCE_ID = "SELECT ACB.ID, ACB.NAME, ACB.SEQUENCE, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.ID = ? AND ACB.REVISION_UUID = ?"; - public static final String GET_API_SPECIFIC_CUSTOM_BACKEND_FROM_SEQUENCE_ID = "SELECT ACB.ID, ACB.NAME, ACB.SEQUENCE, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.ID = ? AND API_UUID = ? AND REVISION_UUID IS NULL"; + public static final String GET_REVISION_SPECIFIC_CUSTOM_BACKEND_FROM_SEQUENCE_ID = "SELECT ACB.ID, ACB.NAME, ACB.SEQUENCE, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.ID = ? AND ACB.REVISION_UUID = ? AND ACB.TYPE = ?"; + public static final String GET_API_SPECIFIC_CUSTOM_BACKEND_FROM_SEQUENCE_ID = "SELECT ACB.ID, ACB.NAME, ACB.SEQUENCE, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.ID = ? AND ACB.API_UUID = ? AND ACB.REVISION_UUID = 0 AND ACB.TYPE = ?"; + public static final String GET_ALL_API_SPECIFIC_CUSTOM_BACKENDS = "SELECT ACB.ID, ACB.NAME, ACB.SEQUENCE, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.API_UUID = ? AND ACB.REVISION_UUID = 0"; public static final String GET_REVISION_SPECIFIC_CUSTOM_BACKEND_META_DATA_FROM_SEQUENCE_ID = "SELECT ACB.ID, ACB.NAME, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.ID = ? AND ACB.REVISION_UUID = ?"; public static final String GET_API_SPECIFIC_CUSTOM_BACKEND_META_DATA_FROM_SEQUENCE_ID = "SELECT ACB.ID, ACB.NAME, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.ID = ? AND API_UUID = ?"; public static final String GET_API_SPECIFIC_CUSTOM_BACKEND_SEQUENCE_FROM_SEQUENCE_ID = "SELECT ACB.SEQUENCE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.ID = ? AND API_UUID = ?"; diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml b/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml index 667346746d68..83688d162bc2 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml @@ -577,6 +577,34 @@ paths: source: 'curl -k -X DELETE -H "Authorization: Bearer ae4eae22-3f65-387b-a171-d37eaa366fa8" "https://127.0.0.1:9443/api/am/publisher/v4/apis/7a2298c4-c905-403f-8fac-38c73301631f/custom-backend/7a2298c4-c915-483f-8fac-38c73301631f"' + /apis/{apiId}/custom-backend/{customBackendId}/content: + get: + tags: + - API Custom Backend Sequence + summary: Download an API Specific Custom Backend + description: | + This operation can be used to download a particular API specific Custom Backend. + operationId: getAPISpecificOperationPolicyContentByPolicyId + parameters: + - $ref: '#/components/parameters/apiId' + - $ref: '#/components/parameters/customBackendId' + responses: + 200: + description: | + OK. + Custom Backend returned. + headers: + Content-Type: + description: | + The content type of the body. + schema: + type: string + content: + application/zip: + schema: + type: string + format: binary + /apis/{apiId}/custom-backend: put: tags: 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/SynapseArtifactGenerator.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/SynapseArtifactGenerator.java index abe5153019e0..e6aaea5f17ff 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/SynapseArtifactGenerator.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/SynapseArtifactGenerator.java @@ -114,10 +114,7 @@ public RuntimeArtifactDto generateGatewayArtifact(List ap tenantDomain, extractedFolderPath); } else { APIDTO apidto = ImportUtils.retrievedAPIDto(extractedFolderPath); - // Update the EndpointConfig if it's a Custom Backend - // updateCustomBackendOfAPI(apidto, runTimeArtifact.getRevision()); API api = APIMappingUtil.fromDTOtoAPI(apidto, apidto.getProvider()); - api.setUUID(apidto.getId()); if (APIConstants.APITransportType.GRAPHQL.toString().equals(api.getType())) { APIDefinition parser = new OAS3Parser(); @@ -138,7 +135,7 @@ public RuntimeArtifactDto generateGatewayArtifact(List ap gatewayAPIDTO = TemplateBuilderUtil.retrieveGatewayAPIDto(api, environment, tenantDomain, apidto, extractedFolderPath); } else if (api.getType() != null && - (APIConstants.APITransportType.HTTP.toString ().equals(api.getType()) + (APIConstants.APITransportType.HTTP.toString().equals(api.getType()) || APIConstants.API_TYPE_SOAP.equals(api.getType()) || APIConstants.API_TYPE_SOAPTOREST.equals(api.getType()) || APIConstants.APITransportType.WEBHOOK.toString() @@ -182,28 +179,6 @@ public RuntimeArtifactDto generateGatewayArtifact(List ap return runtimeArtifactDto; } - private void updateCustomBackendOfAPI(APIDTO apidto, String revisionID) throws APIManagementException { - Object endpointConfig = apidto.getEndpointConfig(); - // update the sequence name generated - if (endpointConfig != null) { - if (endpointConfig instanceof HashMap) { - if (APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( - ((HashMap) endpointConfig).get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE))) { - // Get Endpoint Configurations from the DB - Map conf = apiMgtDao.retrieveCustomBackendOfAPIRevision(apidto.getId(), revisionID); - if (conf == null) { - throw new APIManagementException( - "Cannot find the Custom Backend for the API: " + apidto.getId() + " , Revision: " - + revisionID); - } - ((HashMap) endpointConfig).put("sequence", conf.get("sequence")); - ((HashMap) endpointConfig).put("type", conf.get("type")); - } - } - apidto.setEndpointConfig(endpointConfig); - } - } - /** * Generate gateway policy artifact. * 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/ExportUtils.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/ExportUtils.java index 1779c954992d..8a1a416e5847 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/ExportUtils.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/ExportUtils.java @@ -30,6 +30,7 @@ import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.eclipse.jetty.util.IO; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -69,6 +70,7 @@ import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIProductDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.AdvertiseInfoDTO; +import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CustomBackendDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.GraphQLQueryComplexityInfoDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ProductAPIDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ResourcePolicyInfoDTO; @@ -221,7 +223,7 @@ public static File exportApi(APIProvider apiProvider, APIIdentifier apiIdentifie // TODO: Add Custom Backend to the Archive JsonObject endpointConfig = JsonParser.parseString(api.getEndpointConfig()).getAsJsonObject(); if(APIConstants.ENDPOINT_TYPE_SEQUENCE.equals(endpointConfig.get(API_ENDPOINT_CONFIG_PROTOCOL_TYPE).getAsString())) { - addCustomBackendToAPI(archivePath, apiProvider, api, currentApiUuid, endpointConfig); + addCustomBackendToArchive(archivePath, apiProvider, api, currentApiUuid, endpointConfig); } addGatewayEnvironmentsToArchive(archivePath, apiDtoToReturn.getId(), exportFormat, apiProvider); @@ -636,20 +638,27 @@ public static void addEndpointCertificatesToArchive(String archivePath, APIDTO a } } - public static void addCustomBackendToAPI(String archivePath, + public static void addCustomBackendToArchive(String archivePath, APIProvider apiProvider, API api, String currentApiUuid, JsonObject endpointConfig) throws APIManagementException { - String exportedCustomBackend = null; try { - String backendUUID = endpointConfig.get("sequence_id").getAsString(); + JsonElement prodElement = endpointConfig.get("production"); CommonUtil.createDirectory(archivePath + File.separator + ImportExportConstants.CUSTOM_BACKEND_DIRECTORY); - String customBackendName = APIUtil.getCustomBackendName(api.getUuid(), endpointConfig.get("type").getAsString()); - Map endpointConfigMap = apiProvider.getCustomBackendOfAPIByUUID(backendUUID, currentApiUuid, false); - if(endpointConfigMap != null) { - exportCustomBackend(customBackendName, endpointConfigMap, archivePath); + if(prodElement != null) { + String backendUUID = prodElement.getAsJsonObject().get("sequence_id").getAsString(); + String sequenceName = prodElement.getAsJsonObject().get("sequence_name").getAsString(); + InputStream sequence = apiProvider.getCustomBackendSequenceOfAPIByUUID(backendUUID, api.getUuid(), "PRODUCTION"); + exportCustomBackend(sequenceName, IOUtils.toString(sequence), archivePath); + } + JsonElement sandElement = endpointConfig.get("sandbox"); + if(sandElement != null) { + String backendUUID = sandElement.getAsJsonObject().get("sequence_id").getAsString(); + String sequenceName = sandElement.getAsJsonObject().get("sequence_name").getAsString(); + InputStream sequence = apiProvider.getCustomBackendSequenceOfAPIByUUID(backendUUID, api.getUuid(), "SANDBOX"); + exportCustomBackend(sequenceName, IOUtils.toString(sequence), archivePath); } } catch (IOException | APIImportExportException ex) { - + throw new APIManagementException("Error adding Custom Backends to the API Directory: " + api.getUuid(), ex); } } @@ -761,12 +770,9 @@ public static void exportPolicyData(String policyFileName, OperationPolicyData p } } - public static void exportCustomBackend(String customBackendFileName, Map endpointConfig, String archivePath) throws APIImportExportException, IOException { + public static void exportCustomBackend(String customBackendFileName, String sequence, String archivePath) throws APIImportExportException, IOException { String customBackendName = archivePath + File.separator + ImportExportConstants.CUSTOM_BACKEND_DIRECTORY + File.separator + customBackendFileName; - if(endpointConfig != null && endpointConfig.get(API_ENDPOINT_CONFIG_PROTOCOL_TYPE) != null && APIConstants.ENDPOINT_TYPE_SEQUENCE.equals(endpointConfig.get( - API_ENDPOINT_CONFIG_PROTOCOL_TYPE))) { - CommonUtil.writeFile(customBackendName + APIConstants.SYNAPSE_POLICY_DEFINITION_EXTENSION_XML, endpointConfig.get("sequence")); - } + CommonUtil.writeFile(customBackendName + APIConstants.SYNAPSE_POLICY_DEFINITION_EXTENSION_XML, sequence); } /** diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApi.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApi.java index bdc559b0a5f5..52651f3cdb82 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApi.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApi.java @@ -18,6 +18,7 @@ import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ClientCertificatesDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CommentDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CommentListDTO; +import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CustomBackendDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.DocumentDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.DocumentListDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ErrorDTO; @@ -377,6 +378,27 @@ public Response createNewAPIVersion( @NotNull @Size(max=30) @ApiParam(value = "V return delegate.createNewAPIVersion(newVersion, apiId, defaultVersion, serviceVersion, securityContext); } + @DELETE + @Path("/{apiId}/custom-backend/{customBackendId}") + + @Produces({ "application/json" }) + @ApiOperation(value = "Delete Custom Backend of the API", notes = "This operation can be used to remove the Custom Backend of the API", response = Void.class, authorizations = { + @Authorization(value = "OAuth2Security", scopes = { + @AuthorizationScope(scope = "apim:api_delete", description = "Delete API"), + @AuthorizationScope(scope = "apim:api_manage", description = "Manage all API related operations"), + @AuthorizationScope(scope = "apim:api_import_export", description = "Import and export APIs related operations") + }) + }, tags={ "APIs", }) + @ApiResponses(value = { + @ApiResponse(code = 200, message = "OK. Resource successfully deleted. ", response = Void.class), + @ApiResponse(code = 403, message = "Forbidden. The request must be conditional but no condition has been specified.", response = ErrorDTO.class), + @ApiResponse(code = 404, message = "Not Found. The specified resource does not exist.", response = ErrorDTO.class), + @ApiResponse(code = 409, message = "Conflict. Specified resource already exists.", response = ErrorDTO.class), + @ApiResponse(code = 412, message = "Precondition Failed. The request has not been performed because one of the preconditions is not met.", response = ErrorDTO.class) }) + public Response customBackendDelete(@ApiParam(value = "**API ID** consisting of the **UUID** of the API. ",required=true) @PathParam("apiId") String apiId, @ApiParam(value = "Custom Backend ID ",required=true) @PathParam("customBackendId") String customBackendId, @ApiParam(value = "Validator for conditional requests; based on ETag. " )@HeaderParam("If-Match") String ifMatch) throws APIManagementException{ + return delegate.customBackendDelete(apiId, customBackendId, ifMatch, securityContext); + } + @PUT @Path("/{apiId}/custom-backend") @Consumes({ "multipart/form-data" }) @@ -1310,6 +1332,27 @@ public Response getAuditReportOfAPI(@ApiParam(value = "**API ID** consisting of return delegate.getCommentOfAPI(commentId, apiId, xWSO2Tenant, ifNoneMatch, includeCommenterInfo, replyLimit, replyOffset, securityContext); } + @GET + @Path("/{apiId}/custom-backend/{customBackendId}") + + @Produces({ "application/json" }) + @ApiOperation(value = "Get Custom Backend of the API", notes = "This operation can be used to get Custom Backend data of the API", response = CustomBackendDTO.class, authorizations = { + @Authorization(value = "OAuth2Security", scopes = { + @AuthorizationScope(scope = "apim:api_view", description = "View API"), + @AuthorizationScope(scope = "apim:api_manage", description = "Manage all API related operations"), + @AuthorizationScope(scope = "apim:api_import_export", description = "Import and export APIs related operations"), + @AuthorizationScope(scope = "apim:api_product_import_export", description = "Import and export API Products related operations") + }) + }, tags={ "APIs", }) + @ApiResponses(value = { + @ApiResponse(code = 200, message = "OK. Requested API Custom Backend is returned ", response = CustomBackendDTO.class), + @ApiResponse(code = 304, message = "Not Modified. Empty body because the client has already the latest version of the requested resource (Will be supported in future). ", response = Void.class), + @ApiResponse(code = 404, message = "Not Found. The specified resource does not exist.", response = ErrorDTO.class), + @ApiResponse(code = 406, message = "Not Acceptable. The requested media type is not supported.", response = ErrorDTO.class) }) + public Response getCustomBackendData(@ApiParam(value = "Custom Backend ID ",required=true) @PathParam("customBackendId") String customBackendId, @ApiParam(value = "**API ID** consisting of the **UUID** of the API. ",required=true) @PathParam("apiId") String apiId) throws APIManagementException{ + return delegate.getCustomBackendData(customBackendId, apiId, securityContext); + } + @GET @Path("/{apiId}/generated-mock-scripts") diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApiService.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApiService.java index 17dfc5c9c574..36007861452f 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApiService.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApiService.java @@ -27,6 +27,7 @@ import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ClientCertificatesDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CommentDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CommentListDTO; +import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CustomBackendDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.DocumentDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.DocumentListDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ErrorDTO; @@ -79,6 +80,7 @@ public interface ApisApiService { public Response createAPI(APIDTO APIDTO, String openAPIVersion, MessageContext messageContext) throws APIManagementException; public Response createAPIRevision(String apiId, APIRevisionDTO apIRevisionDTO, MessageContext messageContext) throws APIManagementException; public Response createNewAPIVersion(String newVersion, String apiId, Boolean defaultVersion, String serviceVersion, MessageContext messageContext) throws APIManagementException; + public Response customBackendDelete(String apiId, String customBackendId, String ifMatch, MessageContext messageContext) throws APIManagementException; public Response customBackendUpdate(String apiId, String ifMatch, InputStream sequenceInputStream, Attachment sequenceDetail, String type, MessageContext messageContext) throws APIManagementException; public Response deleteAPI(String apiId, String ifMatch, MessageContext messageContext) throws APIManagementException; public Response deleteAPIClientCertificateByAlias(String alias, String apiId, MessageContext messageContext) throws APIManagementException; @@ -126,6 +128,7 @@ public interface ApisApiService { public Response getAmazonResourceNamesOfAPI(String apiId, MessageContext messageContext) throws APIManagementException; public Response getAuditReportOfAPI(String apiId, String accept, MessageContext messageContext) throws APIManagementException; public Response getCommentOfAPI(String commentId, String apiId, String xWSO2Tenant, String ifNoneMatch, Boolean includeCommenterInfo, Integer replyLimit, Integer replyOffset, MessageContext messageContext) throws APIManagementException; + public Response getCustomBackendData(String customBackendId, String apiId, MessageContext messageContext) throws APIManagementException; public Response getGeneratedMockScriptsOfAPI(String apiId, String ifNoneMatch, MessageContext messageContext) throws APIManagementException; public Response getGraphQLPolicyComplexityOfAPI(String apiId, MessageContext messageContext) throws APIManagementException; public Response getGraphQLPolicyComplexityTypesOfAPI(String apiId, MessageContext messageContext) throws APIManagementException; diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java index bf369c2e08ce..866bb0e1f73c 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java @@ -168,6 +168,34 @@ public Response getAllAPIs(Integer limit, Integer offset, String sortBy, String return null; } + @Override public Response getCustomBackendData(String customBackendId, String apiId, MessageContext messageContext) + throws APIManagementException { + APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider(); + + //validate if api exists + CommonUtils.validateAPIExistence(apiId); + String type = "SANDBOX"; + + Map endpointConfig = apiProvider.getCustomBackendOfAPIByUUID(customBackendId, apiId, type, + true); + CustomBackendDTO backendDTO = new CustomBackendDTO(); + backendDTO.setSequenceName(endpointConfig.get("sequence_name").toString()); + backendDTO.setSequenceId(endpointConfig.get("sequence_id").toString()); + backendDTO.setSequenceType(endpointConfig.get("type").toString()); + return Response.ok().entity(backendDTO).build(); + } + + @Override public Response customBackendDelete(String apiId, String customBackendId, String ifMatch, + MessageContext messageContext) throws APIManagementException { + APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider(); + + //validate if api exists + CommonUtils.validateAPIExistence(apiId); + String type = "SANDBOX"; + apiProvider.deleteCustomBackendByID(apiId, customBackendId, type); + return Response.ok().entity(null).build(); + } + @Override public Response createAPI(APIDTO body, String oasVersion, MessageContext messageContext) throws APIManagementException{ @@ -3812,20 +3840,6 @@ public Response createAPIRevision(String apiId, APIRevisionDTO apIRevisionDTO, M //adding the api revision String revisionId = apiProvider.addAPIRevision(apiRevision, organization); - Object endpointConfig = apiDto.getEndpointConfig(); - - if (endpointConfig != null && endpointConfig instanceof HashMap) { - String endpointType = ((HashMap) endpointConfig).get("endpoint_type").toString(); - if (endpointType != null && APIConstants.ENDPOINT_TYPE_SEQUENCE.equals(endpointType)) { - String sequenceName = ((HashMap) endpointConfig).get("sequence_name").toString(); - String sequenceEndpointType = ((HashMap) endpointConfig).get("sequence_type").toString(); - String backendUUID = ((HashMap) endpointConfig).get("sequence_id").toString(); - // Update Custom Backend table - apiProvider.addNewCustomBackendForRevision(revisionId, backendUUID, apiId, - (HashMap) endpointConfig); - } - } - //Retrieve the newly added APIRevision to send in the response payload APIRevision createdApiRevision = apiProvider.getAPIRevision(revisionId); APIRevisionDTO createdApiRevisionDTO = APIMappingUtil.fromAPIRevisiontoDTO(createdApiRevision); diff --git a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/mysql.sql b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/mysql.sql index 2355c6e3b07b..7785a21f142f 100644 --- a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/mysql.sql +++ b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/mysql.sql @@ -1527,11 +1527,11 @@ CREATE TABLE IF NOT EXISTS AM_API ( CREATE TABLE IF NOT EXISTS AM_API_CUSTOM_BACKEND ( ID VARCHAR(60) NOT NULL, API_UUID VARCHAR(256) NOT NULL, - REVISION_UUID VARCHAR(256), + REVISION_UUID VARCHAR(256) DEFAULT '0', SEQUENCE LONGBLOB, NAME VARCHAR(256) NOT NULL, TYPE VARCHAR(120) NOT NULL, - PRIMARY KEY (ID, API_UUID, REVISION_UUID), + PRIMARY KEY (ID, API_UUID, REVISION_UUID, TYPE), FOREIGN KEY (API_UUID) REFERENCES AM_API(API_UUID) ON DELETE CASCADE )ENGINE INNODB; From 011fdf0dda5329ba53b2691f3ada87d2d6a965a5 Mon Sep 17 00:00:00 2001 From: BLasan Date: Thu, 5 Sep 2024 09:10:56 +0530 Subject: [PATCH 10/56] Remove: Unwanted Codes in SynapseArtifactGenerator --- .../publisher/v1/common/SynapseArtifactGenerator.java | 9 --------- 1 file changed, 9 deletions(-) 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/SynapseArtifactGenerator.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/SynapseArtifactGenerator.java index e6aaea5f17ff..b05f7e4ed571 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/SynapseArtifactGenerator.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/SynapseArtifactGenerator.java @@ -17,7 +17,6 @@ */ package org.wso2.carbon.apimgt.rest.api.publisher.v1.common; -import com.fasterxml.jackson.databind.ObjectMapper; import com.google.gson.Gson; import org.apache.axiom.om.OMElement; import org.apache.commons.io.FileUtils; @@ -39,7 +38,6 @@ import org.wso2.carbon.apimgt.api.model.SwaggerData; import org.wso2.carbon.apimgt.api.model.graphql.queryanalysis.GraphqlComplexityInfo; import org.wso2.carbon.apimgt.impl.APIConstants; -import org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO; import org.wso2.carbon.apimgt.impl.definitions.GraphQLSchemaDefinition; import org.wso2.carbon.apimgt.impl.definitions.OAS3Parser; import org.wso2.carbon.apimgt.impl.dto.APIRuntimeArtifactDto; @@ -48,7 +46,6 @@ import org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.GatewayArtifactGenerator; import org.wso2.carbon.apimgt.impl.importexport.utils.CommonUtil; import org.wso2.carbon.apimgt.impl.utils.APIUtil; -import org.wso2.carbon.apimgt.impl.utils.GatewayUtils; import org.wso2.carbon.apimgt.rest.api.publisher.v1.common.mappings.APIMappingUtil; import org.wso2.carbon.apimgt.rest.api.publisher.v1.common.mappings.ImportUtils; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIDTO; @@ -60,15 +57,10 @@ import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; -import java.util.HashMap; -import java.util.LinkedHashMap; import java.util.List; -import java.util.Map; import javax.xml.namespace.QName; import javax.xml.stream.XMLStreamException; -import static org.wso2.carbon.apimgt.impl.utils.APIUtil.handleException; - /** * This class used to generate Synapse Artifact. */ @@ -81,7 +73,6 @@ public class SynapseArtifactGenerator implements GatewayArtifactGenerator { private static final Log log = LogFactory.getLog(SynapseArtifactGenerator.class); private static final String GATEWAY_EXT_SEQUENCE_PREFIX = "WSO2AMGW--Ext"; - private static final ApiMgtDAO apiMgtDao = ApiMgtDAO.getInstance(); @Override public RuntimeArtifactDto generateGatewayArtifact(List apiRuntimeArtifactDtoList) From d3355fba3facf4e86959511b2fcc79de3d8d460b Mon Sep 17 00:00:00 2001 From: BLasan Date: Thu, 5 Sep 2024 09:15:07 +0530 Subject: [PATCH 11/56] Remove: APIDTO Sequence update --- .../api/publisher/v1/common/mappings/APIMappingUtil.java | 5 ----- 1 file changed, 5 deletions(-) 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/APIMappingUtil.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/APIMappingUtil.java index 1bd8a94d54ed..e7464dfc1fd3 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/APIMappingUtil.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/APIMappingUtil.java @@ -195,11 +195,6 @@ public static API fromDTOtoAPI(APIDTO dto, String provider) throws APIManagement context = updateContextWithVersion(dto.getVersion(), originalContext, context); model.setContext(context); model.setDescription(dto.getDescription()); - - if (dto != null && dto.getSequence() != null) { - model.setSequence(dto.getSequence()); - } - Object endpointConfig = dto.getEndpointConfig(); // update the sequence name generated From 081b0cf13e3132c26377831fc6e271633d3b01a1 Mon Sep 17 00:00:00 2001 From: BLasan Date: Thu, 5 Sep 2024 09:15:59 +0530 Subject: [PATCH 12/56] Remove: APIDTO Sequence update unwanted comments --- .../rest/api/publisher/v1/common/mappings/APIMappingUtil.java | 1 - 1 file changed, 1 deletion(-) 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/APIMappingUtil.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/APIMappingUtil.java index e7464dfc1fd3..88f7399ede8f 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/APIMappingUtil.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/APIMappingUtil.java @@ -197,7 +197,6 @@ public static API fromDTOtoAPI(APIDTO dto, String provider) throws APIManagement model.setDescription(dto.getDescription()); Object endpointConfig = dto.getEndpointConfig(); - // update the sequence name generated if (endpointConfig != null) { ObjectMapper mapper = new ObjectMapper(); try { From 9e18a897cdbc2b6d08cd475d9aae41ccae57bbe1 Mon Sep 17 00:00:00 2001 From: BLasan Date: Thu, 5 Sep 2024 09:17:16 +0530 Subject: [PATCH 13/56] Remove: APIDTO unwanted imports and spaces --- .../rest/api/publisher/v1/common/mappings/APIMappingUtil.java | 2 -- 1 file changed, 2 deletions(-) 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/APIMappingUtil.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/APIMappingUtil.java index 88f7399ede8f..4cbc738ddc77 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/APIMappingUtil.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/APIMappingUtil.java @@ -71,7 +71,6 @@ import org.wso2.carbon.apimgt.rest.api.common.RestApiCommonUtil; import org.wso2.carbon.apimgt.rest.api.common.RestApiConstants; import org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO; -import org.wso2.carbon.apimgt.rest.api.publisher.v1.common.SynapsePolicyAggregator; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIBusinessInformationDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APICorsConfigurationDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIDTO; @@ -196,7 +195,6 @@ public static API fromDTOtoAPI(APIDTO dto, String provider) throws APIManagement model.setContext(context); model.setDescription(dto.getDescription()); Object endpointConfig = dto.getEndpointConfig(); - if (endpointConfig != null) { ObjectMapper mapper = new ObjectMapper(); try { From fb45ec737cc636a4882e725312c7d51174c9c32d Mon Sep 17 00:00:00 2001 From: BLasan Date: Thu, 5 Sep 2024 09:19:11 +0530 Subject: [PATCH 14/56] Remove: Unwnated Imports APIMgtDao --- .../rest/api/publisher/v1/common/mappings/APIMappingUtil.java | 1 - 1 file changed, 1 deletion(-) 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/APIMappingUtil.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/APIMappingUtil.java index 4cbc738ddc77..06e7caf5eb96 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/APIMappingUtil.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/APIMappingUtil.java @@ -60,7 +60,6 @@ import org.wso2.carbon.apimgt.api.model.WebsubSubscriptionConfiguration; import org.wso2.carbon.apimgt.impl.APIConstants; import org.wso2.carbon.apimgt.impl.ServiceCatalogImpl; -import org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO; import org.wso2.carbon.apimgt.impl.definitions.AsyncApiParser; import org.wso2.carbon.apimgt.impl.definitions.OASParserUtil; import org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder; From 8d58008a9446070799c94b2e24503352fc74bad1 Mon Sep 17 00:00:00 2001 From: BLasan Date: Fri, 6 Sep 2024 10:53:00 +0530 Subject: [PATCH 15/56] Fix: Revision create, delete, revision deploy, revision undeploy, revision restore scenarios --- .../wso2/carbon/apimgt/api/APIProvider.java | 1 + .../apimgt/api/model/CustomBackendData.java | 60 ++++++++ .../apimgt/gateway/InMemoryAPIDeployer.java | 2 +- .../wso2/carbon/apimgt/impl/APIConstants.java | 2 +- .../carbon/apimgt/impl/APIProviderImpl.java | 8 +- .../carbon/apimgt/impl/dao/ApiMgtDAO.java | 130 +++++++++++++++--- .../impl/dao/constants/SQLConstants.java | 14 +- .../apimgt/impl/utils/GatewayUtils.java | 8 ++ .../src/test/resources/dbscripts/h2.sql | 11 ++ .../persistence/RegistryPersistenceImpl.java | 2 +- .../src/main/resources/publisher-api.yaml | 28 ---- .../v1/common/TemplateBuilderUtil.java | 20 ++- .../v1/common/mappings/ExportUtils.java | 17 +-- .../src/main/resources/publisher-api.yaml | 3 - .../src/main/resources/sql/h2.sql | 11 ++ 15 files changed, 244 insertions(+), 73 deletions(-) create mode 100644 components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/CustomBackendData.java diff --git a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java index 7ca1d7a2394a..ed0c9f15317c 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java +++ b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java @@ -66,6 +66,7 @@ public interface APIProvider extends APIManager { Comment getComment(ApiTypeWrapper apiTypeWrapper, String commentId, Integer replyLimit, Integer replyOffset) throws APIManagementException; void deleteCustomBackendByID(String backendUUID, String apiUUID, String type) throws APIManagementException; + InputStream getCustomBackendSequenceByAPIAndRevisionUUUID(String apiUUID, String revisionUUID, String type) throws APIManagementException; /** * @param apiTypeWrapper Api type wrapper diff --git a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/CustomBackendData.java b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/CustomBackendData.java new file mode 100644 index 000000000000..c9372a2045bf --- /dev/null +++ b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/CustomBackendData.java @@ -0,0 +1,60 @@ +package org.wso2.carbon.apimgt.api.model; + +import java.io.InputStream; + +public class CustomBackendData { + private String Id; + private InputStream sequence; + private String type; + private String name; + private String apiUUID; + private String revisionUUID; + + public InputStream getSequence() { + return sequence; + } + + public void setSequence(InputStream sequence) { + this.sequence = sequence; + } + + public String getId() { + return Id; + } + + public void setId(String id) { + Id = id; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getApiUUID() { + return apiUUID; + } + + public void setApiUUID(String apiUUID) { + this.apiUUID = apiUUID; + } + + public String getRevisionUUID() { + return revisionUUID; + } + + public void setRevisionUUID(String revisionUUID) { + this.revisionUUID = revisionUUID; + } +} diff --git a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/InMemoryAPIDeployer.java b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/InMemoryAPIDeployer.java index c37d7ef97d57..7cb4200c1feb 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/InMemoryAPIDeployer.java +++ b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/InMemoryAPIDeployer.java @@ -349,7 +349,7 @@ private void unDeployAPI(APIGatewayAdmin apiGatewayAdmin, DeployAPIInGatewayEven } GatewayUtils.setCustomSequencesToBeRemoved(api, gatewayAPIDTO); - // GatewayUtils.setCustomBackendToBeRemoved(api, gatewayAPIDTO); + GatewayUtils.setCustomBackendToBeRemoved(gatewayAPIDTO); } gatewayAPIDTO.setLocalEntriesToBeRemove( diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConstants.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConstants.java index 4ec1b295ab42..46fbfc4ec6a2 100755 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConstants.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConstants.java @@ -1783,7 +1783,7 @@ private ConfigParameters() { public static final String ENDPOINT_TYPE_SERVICE = "service"; public static final String ENDPOINT_TYPE_ADDRESS = "address"; public static final String ENDPOINT_TYPE_AWSLAMBDA = "awslambda"; - public static final String ENDPOINT_TYPE_SEQUENCE = "custom_sequence"; + public static final String ENDPOINT_TYPE_SEQUENCE = "custom_backend"; public static final String SEQUENCE_DATA = "sequence"; public static final String ENDPOINT_PRODUCTION_FAILOVERS = "production_failovers"; public static final String ENDPOINT_SANDBOX_FAILOVERS = "sandbox_failovers"; diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java index 43e40d18a88d..2b2bf44c44c6 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java @@ -34,7 +34,6 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.solr.client.solrj.util.ClientUtils; -import org.apache.solr.common.util.Hash; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; @@ -1193,6 +1192,11 @@ public Map getCustomBackendOfAPIByUUID(String customBackendUUID, return apiMgtDAO.getCustomBackendSequenceOfAPIByUUID(backendUUID, apiUUID, type); } + @Override + public InputStream getCustomBackendSequenceByAPIAndRevisionUUUID(String apiUUID, String revisionUUID, String type) throws APIManagementException { + return apiMgtDAO.getCustomBackendSequenceByAPIandRevisionUUID(apiUUID, revisionUUID, type); + } + @Override public void addNewCustomBackendForRevision(String revisionUUID, String updatedBackendUUID, String apiUUID, Map config) throws APIManagementException { if (config.get("production") != null) { @@ -6173,6 +6177,7 @@ public String addAPIRevision(APIRevision apiRevision, String organization) throw ExceptionCodes.from(ExceptionCodes.API_REVISION_UUID_NOT_FOUND)); } apiRevision.setRevisionUUID(revisionUUID); + try { apiMgtDAO.addAPIRevision(apiRevision); AIConfiguration configuration = apiMgtDAO.getAIConfiguration(apiRevision.getApiUUID(), null, organization); @@ -6762,7 +6767,6 @@ public void deleteAPIRevision(String apiId, String apiRevisionId, String organiz ERROR_DELETING_API_REVISION,apiRevision.getApiUUID())); } apiMgtDAO.deleteAPIRevision(apiRevision); - apiMgtDAO.deleteCustomBackendByRevision(apiId, apiRevisionId); gatewayArtifactsMgtDAO.deleteGatewayArtifact(apiRevision.getApiUUID(), apiRevision.getRevisionUUID()); if (artifactSaver != null) { try { 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 370d039bba74..ca8ec34fd754 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 @@ -61,6 +61,7 @@ import org.wso2.carbon.apimgt.api.model.BlockConditionsDTO; import org.wso2.carbon.apimgt.api.model.Comment; import org.wso2.carbon.apimgt.api.model.CommentList; +import org.wso2.carbon.apimgt.api.model.CustomBackendData; import org.wso2.carbon.apimgt.api.model.DeployedAPIRevision; import org.wso2.carbon.apimgt.api.model.Environment; import org.wso2.carbon.apimgt.api.model.GatewayPolicyData; @@ -11239,14 +11240,37 @@ public Map retrieveCustomBackendOfAPIRevision(String apiUUID, St return map; } + public InputStream getCustomBackendSequenceByAPIandRevisionUUID(String apiUUID, String revisionUUID, String type) throws APIManagementException { + String sqlQuery = SQLConstants.CustomBackendConstants.GET_CUSTOM_BACKEND_FROM_API_AND_REVISION_UUID; + boolean isRev = checkAPIUUIDIsARevisionUUID(apiUUID) != null; + InputStream sequence = null; + try (Connection con = APIMgtDBUtil.getConnection(); + PreparedStatement pstmt = con.prepareStatement(sqlQuery)) { + pstmt.setString(1, apiUUID); + pstmt.setString(2, revisionUUID); + pstmt.setString(3, type); + try (ResultSet rs = pstmt.executeQuery()) { + while(rs.next()) { + sequence = rs.getBinaryStream("SEQUENCE"); + } + } + } catch (SQLException ex) { + handleException("Error when retrieving Custom Backend of API: "+apiUUID, ex); + } + return sequence; + } + public InputStream getCustomBackendSequenceOfAPIByUUID(String backendUUID, String apiUUID, String type) throws APIManagementException { String sqlQuery = SQLConstants.CustomBackendConstants.GET_API_SPECIFIC_CUSTOM_BACKEND_FROM_SEQUENCE_ID; + boolean isRev = checkAPIUUIDIsARevisionUUID(apiUUID) != null; ResultSet rs = null; InputStream sequence = null; try (Connection con = APIMgtDBUtil.getConnection(); PreparedStatement ps = con.prepareStatement(sqlQuery)) { ps.setString(1, backendUUID); ps.setString(2, apiUUID); + ps.setString(3, type); + rs = ps.executeQuery(); while(rs.next()) { try (InputStream in = rs.getBinaryStream("SEQUENCE")) { @@ -17649,8 +17673,6 @@ public void addAPIRevision(APIRevision apiRevision) throws APIManagementExceptio insertScopeResourceMappingStatement.executeBatch(); insertProductResourceMappingStatement.executeBatch(); revisionAPIPolicies(apiRevision, tenantDomain, uriTemplateMap, connection); - // Add Custom Backend - revisionCustomBackend(apiRevision, connection); // Adding to AM_API_CLIENT_CERTIFICATE String getClientCertificatesQuery = SQLConstants.APIRevisionSqlConstants.GET_CLIENT_CERTIFICATES_OF_KEY_TYPE; @@ -17722,6 +17744,8 @@ public void addAPIRevision(APIRevision apiRevision) throws APIManagementExceptio insertGraphQLComplexityStatement.executeBatch(); updateLatestRevisionNumber(connection, apiRevision.getApiUUID(), apiRevision.getId()); addAPIRevisionMetaData(connection, apiRevision.getApiUUID(), apiRevision.getRevisionUUID()); + // Add Custom Backend + revisionCustomBackend(apiRevision, connection); connection.commit(); } catch (SQLException e) { connection.rollback(); @@ -18573,6 +18597,7 @@ public void restoreAPIRevision(APIRevision apiRevision) throws APIManagementExce } restoreAPIPolicies(apiRevision, tenantDomain, uriTemplateMap, connection); + restoreCustomBackend(apiRevision, connection); insertScopeResourceMappingStatement.executeBatch(); insertProductResourceMappingStatement.executeBatch(); @@ -18710,6 +18735,9 @@ public void deleteAPIRevision(APIRevision apiRevision) throws APIManagementExcep // Removing related revision entries from operation policies deleteAllAPISpecificOperationPoliciesByAPIUUID(connection, apiRevision.getApiUUID(), apiRevision.getRevisionUUID()); + // Removing related Custom Backend entries + deleteAllCustomBackendsOfAPIRevision(apiRevision.getApiUUID(), apiRevision.getRevisionUUID(), connection); + connection.commit(); } catch (SQLException e) { connection.rollback(); @@ -18722,6 +18750,18 @@ public void deleteAPIRevision(APIRevision apiRevision) throws APIManagementExcep } } + private void deleteAllCustomBackendsOfAPIRevision(String apiUUID, String revisionUUID, Connection connection) throws APIManagementException { + String deleteSqlQuery = SQLConstants.CustomBackendConstants.DELETE_CUSTOM_BACKEND_BY_REVISION; + try (PreparedStatement pstmt = connection.prepareStatement(deleteSqlQuery)) { + connection.setAutoCommit(false); + pstmt.setString(1, apiUUID); + pstmt.setString(2, revisionUUID); + pstmt.executeUpdate(); + } catch (SQLException ex) { + handleException("Error when deleting Custom Backend of API: " + apiUUID, ex); + } + } + /** * Adds an API Product revision record to the database * @@ -21740,23 +21780,48 @@ private void revisionCustomBackend(APIRevision apiRevision, Connection connectio throws SQLException, APIManagementException { String addCBSqlQuery = SQLConstants.CustomBackendConstants.ADD_CUSTOM_BACKEND; String getCBSQLQuery = SQLConstants.CustomBackendConstants.GET_ALL_API_SPECIFIC_CUSTOM_BACKENDS; - ResultSet rs = null; - try (Connection con = APIMgtDBUtil.getConnection(); - PreparedStatement getPstmt = con.prepareStatement(getCBSQLQuery); - PreparedStatement addPstmt = con.prepareStatement(addCBSqlQuery)) { - con.setAutoCommit(false); + try (PreparedStatement getPstmt = connection.prepareStatement(getCBSQLQuery); + PreparedStatement addPstmt = connection.prepareStatement(addCBSqlQuery)) { + connection.setAutoCommit(false); getPstmt.setString(1, apiRevision.getApiUUID()); - rs = getPstmt.executeQuery(); - while (rs.next()) { - addPstmt.setString(1, rs.getString("ID")); - addPstmt.setString(2, apiRevision.getApiUUID()); - addPstmt.setBinaryStream(3, rs.getBinaryStream("SEQUENCE")); - addPstmt.setString(4, rs.getString("TYPE")); - addPstmt.setString(5, apiRevision.getRevisionUUID()); - addPstmt.setString(6, rs.getString("NAME")); - addPstmt.addBatch(); - } - addPstmt.executeBatch(); + List customBackendDataList = new ArrayList<>(); + int count = 0; + + try (ResultSet rs = getPstmt.executeQuery()) { + while (rs.next()) { + addPstmt.setString(1, rs.getString("ID")); + addPstmt.setString(2, apiRevision.getApiUUID()); + addPstmt.setBinaryStream(3, rs.getBinaryStream("SEQUENCE")); + addPstmt.setString(4, rs.getString("TYPE")); + addPstmt.setString(5, apiRevision.getRevisionUUID()); + addPstmt.setString(6, rs.getString("NAME")); + addPstmt.addBatch(); + count++; + + // CustomBackendData customBackendData = new CustomBackendData(); + // customBackendData.setId(rs.getString("ID")); + // customBackendData.setName(rs.getString("NAME")); + // customBackendData.setApiUUID(apiRevision.getApiUUID()); + // customBackendData.setRevisionUUID(apiRevision.getRevisionUUID()); + // customBackendData.setSequence(rs.getBinaryStream("SEQUENCE")); + // customBackendData.setType(rs.getString("TYPE")); + // customBackendDataList.add(customBackendData); + } + } + + // for(CustomBackendData customBackendData: customBackendDataList) { + // addPstmt.setString(1, customBackendData.getId()); + // addPstmt.setString(2, customBackendData.getApiUUID()); + // addPstmt.setBinaryStream(3, customBackendData.getSequence()); + // addPstmt.setString(4, customBackendData.getType()); + // addPstmt.setString(5, customBackendData.getRevisionUUID()); + // addPstmt.setString(6, customBackendData.getName()); + // addPstmt.addBatch(); + // } + + if (count > 0) { + addPstmt.executeBatch(); + } } catch (SQLException ex) { handleException("Error while adding Custom Backends to the database of API: " + apiRevision.getApiUUID(), ex); @@ -21870,6 +21935,35 @@ private void handlePolicyCloningWhenRevisioning(OperationPolicy policy, String a } } + private void restoreCustomBackend(APIRevision apiRevision, Connection connection) throws SQLException { + String deleteSql = SQLConstants.CustomBackendConstants.DELETE_WORKING_COPY_OF_CUSTOM_BACKEND; + String getSql = SQLConstants.CustomBackendConstants.GET_CUSTOM_BACKEND_OF_API_REVISION; + String addSql = SQLConstants.CustomBackendConstants.ADD_CUSTOM_BACKEND; + try (PreparedStatement pstmt = connection.prepareStatement(deleteSql); + PreparedStatement pstmtGet = connection.prepareStatement(getSql); + PreparedStatement pstmtAdd = connection.prepareStatement(addSql)) { + connection.setAutoCommit(false); + pstmt.setString(1, apiRevision.getApiUUID()); + pstmt.executeUpdate(); + + pstmtGet.setString(1, apiRevision.getApiUUID()); + pstmtGet.setString(2, apiRevision.getRevisionUUID()); + + try(ResultSet rs = pstmtGet.executeQuery()) { + while(rs.next()) { + pstmtAdd.setString(1, rs.getString("ID")); + pstmtAdd.setString(2, apiRevision.getApiUUID()); + pstmtAdd.setBinaryStream(3, rs.getBinaryStream("SEQUENCE")); + pstmtAdd.setString(4, rs.getString("TYPE")); + pstmtAdd.setString(5, "0"); + pstmtAdd.setString(6, rs.getString("NAME")); + pstmtAdd.addBatch(); + } + } + pstmtAdd.executeBatch(); + } + } + /** * Restore a revision of API policies. This will copy the policy and policy mapping to working copy. * 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 28de7e20349b..3eb76a1176ca 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 @@ -4477,14 +4477,16 @@ public static class TransactionCountConstants { 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_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_BY_REVISION = "DELETE FROM AM_API_CUSTOM_BACKEND WHERE REVISION_UUID = ?"; + 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_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 NAME, SEQUENCE, TYPE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ? AND REVISION_UUID = ?"; - public static final String GET_CUSTOM_BACKEND_OF_API_DEFAULT_REVISION = "SELECT ACB.NAME, ACB.TYPE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ? AND REVISION_UUID IS NULL"; + 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 = ?"; + public static final String GET_CUSTOM_BACKEND_OF_API_DEFAULT_REVISION = "SELECT ACB.NAME, ACB.TYPE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ? AND REVISION_UUID = '0'"; public static final String GET_REVISION_SPECIFIC_CUSTOM_BACKEND_FROM_SEQUENCE_ID = "SELECT ACB.ID, ACB.NAME, ACB.SEQUENCE, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.ID = ? AND ACB.REVISION_UUID = ? AND ACB.TYPE = ?"; - public static final String GET_API_SPECIFIC_CUSTOM_BACKEND_FROM_SEQUENCE_ID = "SELECT ACB.ID, ACB.NAME, ACB.SEQUENCE, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.ID = ? AND ACB.API_UUID = ? AND ACB.REVISION_UUID = 0 AND ACB.TYPE = ?"; - public static final String GET_ALL_API_SPECIFIC_CUSTOM_BACKENDS = "SELECT ACB.ID, ACB.NAME, ACB.SEQUENCE, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.API_UUID = ? AND ACB.REVISION_UUID = 0"; + public static final String GET_API_SPECIFIC_CUSTOM_BACKEND_FROM_SEQUENCE_ID = "SELECT ACB.ID, ACB.NAME, ACB.SEQUENCE, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.ID = ? AND ACB.API_UUID = ? AND ACB.REVISION_UUID = '0' AND ACB.TYPE = ?"; + public static final String GET_CUSTOM_BACKEND_FROM_API_AND_REVISION_UUID = "SELECT ACB.ID, ACB.NAME, ACB.SEQUENCE, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.API_UUID = ? AND ACB.REVISION_UUID = ? AND ACB.TYPE = ?"; + public static final String GET_ALL_API_SPECIFIC_CUSTOM_BACKENDS = "SELECT ACB.ID, ACB.NAME, ACB.SEQUENCE, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.API_UUID = ? AND ACB.REVISION_UUID = '0'"; public static final String GET_REVISION_SPECIFIC_CUSTOM_BACKEND_META_DATA_FROM_SEQUENCE_ID = "SELECT ACB.ID, ACB.NAME, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.ID = ? AND ACB.REVISION_UUID = ?"; public static final String GET_API_SPECIFIC_CUSTOM_BACKEND_META_DATA_FROM_SEQUENCE_ID = "SELECT ACB.ID, ACB.NAME, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.ID = ? AND API_UUID = ?"; public static final String GET_API_SPECIFIC_CUSTOM_BACKEND_SEQUENCE_FROM_SEQUENCE_ID = "SELECT ACB.SEQUENCE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.ID = ? AND API_UUID = ?"; diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/GatewayUtils.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/GatewayUtils.java index 633844789fc2..599b2f14e94f 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/GatewayUtils.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/GatewayUtils.java @@ -46,6 +46,14 @@ public static void setCustomSequencesToBeRemoved(API api, GatewayAPIDTO gatewayA gatewayAPIDTO.setSequencesToBeRemove(addStringToList(faultSequence, gatewayAPIDTO.getSequencesToBeRemove())); } + public static void setCustomBackendToBeRemoved(GatewayAPIDTO gatewayAPIDTO) { + String sandBoxBackend = APIUtil.getCustomBackendName(gatewayAPIDTO.getApiId(), "SANDBOX"); + gatewayAPIDTO.setSequencesToBeRemove(addStringToList(sandBoxBackend, gatewayAPIDTO.getSequencesToBeRemove())); + String productionBackend = APIUtil.getCustomBackendName(gatewayAPIDTO.getApiId(), "PRODUCTION"); + gatewayAPIDTO.setSequencesToBeRemove( + addStringToList(productionBackend, gatewayAPIDTO.getSequencesToBeRemove())); + } + public static String[] addStringToList(String key, String[] keys) { if (keys == null) { diff --git a/components/apimgt/org.wso2.carbon.apimgt.keymgt/src/test/resources/dbscripts/h2.sql b/components/apimgt/org.wso2.carbon.apimgt.keymgt/src/test/resources/dbscripts/h2.sql index 834115bd1cb4..654ad3d51f17 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.keymgt/src/test/resources/dbscripts/h2.sql +++ b/components/apimgt/org.wso2.carbon.apimgt.keymgt/src/test/resources/dbscripts/h2.sql @@ -1310,6 +1310,17 @@ CREATE TABLE IF NOT EXISTS AM_API ( UNIQUE (API_PROVIDER,API_NAME,API_VERSION) ); +CREATE TABLE IF NOT EXISTS AM_API_CUSTOM_BACKEND ( + ID VARCHAR(60) NOT NULL, + API_UUID VARCHAR(256) NOT NULL, + REVISION_UUID VARCHAR(256) DEFAULT '0', + SEQUENCE LONGBLOB, + NAME VARCHAR(256) NOT NULL, + TYPE VARCHAR(120) NOT NULL, + PRIMARY KEY(ID, API_UUID, REVISION_UUID, TYPE), + FOREIGN KEY(API_UUID) REFERENCES AM_API(API_UUID) ON DELETE CASCADE +); + CREATE TABLE IF NOT EXISTS AM_API_URL_MAPPING ( URL_MAPPING_ID INTEGER AUTO_INCREMENT, API_ID INTEGER NOT NULL, diff --git a/components/apimgt/org.wso2.carbon.apimgt.persistence/src/main/java/org/wso2/carbon/apimgt/persistence/RegistryPersistenceImpl.java b/components/apimgt/org.wso2.carbon.apimgt.persistence/src/main/java/org/wso2/carbon/apimgt/persistence/RegistryPersistenceImpl.java index dbd0900f19b6..ec04b146d806 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.persistence/src/main/java/org/wso2/carbon/apimgt/persistence/RegistryPersistenceImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.persistence/src/main/java/org/wso2/carbon/apimgt/persistence/RegistryPersistenceImpl.java @@ -392,7 +392,7 @@ public void restoreAPIRevision(Organization org, String apiUUID, String revision registry.put(apiPath, newAPIArtifact); GenericArtifact artifact = getAPIArtifact(apiUUID, registry); artifact.setAttribute(APIConstants.API_OVERVIEW_STATUS, lifecycleStatus); - artifactManager.updateGenericArtifact(apiArtifact); + artifactManager.updateGenericArtifact(artifact); RegistryPersistenceUtil.clearResourcePermissions(apiPath, api.getId(), ((UserRegistry) registry).getTenantId()); RegistryPersistenceUtil.setResourcePermissions(api.getId().getProviderName(), api.getVisibility(), diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml b/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml index 83688d162bc2..667346746d68 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml @@ -577,34 +577,6 @@ paths: source: 'curl -k -X DELETE -H "Authorization: Bearer ae4eae22-3f65-387b-a171-d37eaa366fa8" "https://127.0.0.1:9443/api/am/publisher/v4/apis/7a2298c4-c905-403f-8fac-38c73301631f/custom-backend/7a2298c4-c915-483f-8fac-38c73301631f"' - /apis/{apiId}/custom-backend/{customBackendId}/content: - get: - tags: - - API Custom Backend Sequence - summary: Download an API Specific Custom Backend - description: | - This operation can be used to download a particular API specific Custom Backend. - operationId: getAPISpecificOperationPolicyContentByPolicyId - parameters: - - $ref: '#/components/parameters/apiId' - - $ref: '#/components/parameters/customBackendId' - responses: - 200: - description: | - OK. - Custom Backend returned. - headers: - Content-Type: - description: | - The content type of the body. - schema: - type: string - content: - application/zip: - schema: - type: string - format: binary - /apis/{apiId}/custom-backend: put: tags: 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/TemplateBuilderUtil.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/TemplateBuilderUtil.java index fb54940d6143..e7d68a984e67 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/TemplateBuilderUtil.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/TemplateBuilderUtil.java @@ -973,11 +973,21 @@ private static void setCustomSequencesToBeAdded(API api, GatewayAPIDTO gatewayAP if (APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( endpointConfigMap.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE))) { - GatewayContentDTO gatewayCustomBackendSequenceDTO = retrieveCustomBackendSequence(api, - endpointConfigMap.get("type").toString(), extractedPath); - if (gatewayCustomBackendSequenceDTO != null) { - gatewayAPIDTO.setSequenceToBeAdd( - addGatewayContentToList(gatewayCustomBackendSequenceDTO, gatewayAPIDTO.getSequenceToBeAdd())); + if (endpointConfigMap.get("sandbox") != null) { + GatewayContentDTO gatewayCustomBackendSequenceDTO = retrieveCustomBackendSequence(api, "SANDBOX", + extractedPath); + if (gatewayCustomBackendSequenceDTO != null) { + gatewayAPIDTO.setSequenceToBeAdd(addGatewayContentToList(gatewayCustomBackendSequenceDTO, + gatewayAPIDTO.getSequenceToBeAdd())); + } + } + if (endpointConfigMap.get("production") != null) { + GatewayContentDTO gatewayCustomBackendSequenceDTO = retrieveCustomBackendSequence(api, "PRODUCTION", + extractedPath); + if (gatewayCustomBackendSequenceDTO != null) { + gatewayAPIDTO.setSequenceToBeAdd(addGatewayContentToList(gatewayCustomBackendSequenceDTO, + gatewayAPIDTO.getSequenceToBeAdd())); + } } } } 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/ExportUtils.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/ExportUtils.java index 8a1a416e5847..4bf37e1e4dc7 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/ExportUtils.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/ExportUtils.java @@ -223,7 +223,7 @@ public static File exportApi(APIProvider apiProvider, APIIdentifier apiIdentifie // TODO: Add Custom Backend to the Archive JsonObject endpointConfig = JsonParser.parseString(api.getEndpointConfig()).getAsJsonObject(); if(APIConstants.ENDPOINT_TYPE_SEQUENCE.equals(endpointConfig.get(API_ENDPOINT_CONFIG_PROTOCOL_TYPE).getAsString())) { - addCustomBackendToArchive(archivePath, apiProvider, api, currentApiUuid, endpointConfig); + addCustomBackendToArchive(archivePath, apiProvider, currentApiUuid, endpointConfig); } addGatewayEnvironmentsToArchive(archivePath, apiDtoToReturn.getId(), exportFormat, apiProvider); @@ -638,27 +638,28 @@ public static void addEndpointCertificatesToArchive(String archivePath, APIDTO a } } - public static void addCustomBackendToArchive(String archivePath, - APIProvider apiProvider, API api, String currentApiUuid, JsonObject endpointConfig) throws APIManagementException { + public static void addCustomBackendToArchive(String archivePath, APIProvider apiProvider, String apiUUID, + JsonObject endpointConfig) throws APIManagementException { try { JsonElement prodElement = endpointConfig.get("production"); CommonUtil.createDirectory(archivePath + File.separator + ImportExportConstants.CUSTOM_BACKEND_DIRECTORY); - if(prodElement != null) { + if (prodElement != null) { String backendUUID = prodElement.getAsJsonObject().get("sequence_id").getAsString(); String sequenceName = prodElement.getAsJsonObject().get("sequence_name").getAsString(); - InputStream sequence = apiProvider.getCustomBackendSequenceOfAPIByUUID(backendUUID, api.getUuid(), "PRODUCTION"); + InputStream sequence = apiProvider.getCustomBackendSequenceOfAPIByUUID(apiUUID, backendUUID, + "PRODUCTION"); exportCustomBackend(sequenceName, IOUtils.toString(sequence), archivePath); } JsonElement sandElement = endpointConfig.get("sandbox"); - if(sandElement != null) { + if (sandElement != null) { String backendUUID = sandElement.getAsJsonObject().get("sequence_id").getAsString(); String sequenceName = sandElement.getAsJsonObject().get("sequence_name").getAsString(); - InputStream sequence = apiProvider.getCustomBackendSequenceOfAPIByUUID(backendUUID, api.getUuid(), "SANDBOX"); + InputStream sequence = apiProvider.getCustomBackendSequenceOfAPIByUUID(apiUUID, backendUUID, "SANDBOX"); exportCustomBackend(sequenceName, IOUtils.toString(sequence), archivePath); } } catch (IOException | APIImportExportException ex) { - throw new APIManagementException("Error adding Custom Backends to the API Directory: " + api.getUuid(), ex); + throw new APIManagementException("Error adding Custom Backends to the API Directory: " + apiUUID, ex); } } diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.util/src/main/resources/publisher-api.yaml b/components/apimgt/org.wso2.carbon.apimgt.rest.api.util/src/main/resources/publisher-api.yaml index c202a51c4687..ec8be349b52e 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.util/src/main/resources/publisher-api.yaml +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.util/src/main/resources/publisher-api.yaml @@ -8539,9 +8539,6 @@ components: isDefaultVersion: type: boolean example: false - sequence: - type: string - example: in sequence isRevision: type: boolean example: false diff --git a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/h2.sql b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/h2.sql index 1c512858103d..417343094cbb 100644 --- a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/h2.sql +++ b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/h2.sql @@ -1585,6 +1585,17 @@ CREATE TABLE IF NOT EXISTS AM_API ( UNIQUE (API_PROVIDER,API_NAME,API_VERSION,ORGANIZATION) ); +CREATE TABLE IF NOT EXISTS AM_API_CUSTOM_BACKEND ( + ID VARCHAR(60) NOT NULL, + API_UUID VARCHAR(256) NOT NULL, + REVISION_UUID VARCHAR(256) DEFAULT '0', + SEQUENCE LONGBLOB, + NAME VARCHAR(256) NOT NULL, + TYPE VARCHAR(120) NOT NULL, + PRIMARY KEY(ID, API_UUID, REVISION_UUID, TYPE), + FOREIGN KEY(API_UUID) REFERENCES AM_API(API_UUID) ON DELETE CASCADE +); + CREATE TABLE IF NOT EXISTS AM_GRAPHQL_COMPLEXITY ( UUID VARCHAR(256), API_ID INTEGER NOT NULL, From 6bf0333eb9d4cde44ae5ba8f1915d45c48f59b5f Mon Sep 17 00:00:00 2001 From: BLasan Date: Sat, 7 Sep 2024 19:55:08 +0530 Subject: [PATCH 16/56] Fix: APICTL Import scenario --- .../carbon/apimgt/impl/dao/ApiMgtDAO.java | 11 +-- .../impl/dao/constants/SQLConstants.java | 3 +- .../carbon/apimgt/impl/utils/APIUtil.java | 14 ++++ .../v1/common/mappings/ImportUtils.java | 80 ++++++++++++++++--- 4 files changed, 90 insertions(+), 18 deletions(-) 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 ca8ec34fd754..d2ac89c7a8bb 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 @@ -21489,14 +21489,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 3eb76a1176ca..2b066ebf1dc3 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 @@ -4478,7 +4478,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 c9d50b4b7f5e..1e9bf9da0b7a 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 @@ -10029,6 +10029,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 ca3420a7ed8a..d2d0be3e936c 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; /** @@ -269,18 +271,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); @@ -402,6 +411,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); @@ -690,6 +706,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. * From 76fb15dbbf7f5843ac8334855741f6b596543580 Mon Sep 17 00:00:00 2001 From: BLasan Date: Mon, 9 Sep 2024 06:59:42 +0530 Subject: [PATCH 17/56] Update: Custom Backend REST APIs --- .../carbon/apimgt/api/ExceptionCodes.java | 2 + .../src/main/resources/publisher-api.yaml | 83 +++++++++++++++++++ .../apimgt/rest/api/publisher/v1/ApisApi.java | 29 ++++++- .../rest/api/publisher/v1/ApisApiService.java | 5 +- .../publisher/v1/impl/ApisApiServiceImpl.java | 35 ++++++-- .../v1/utils/RestApiPublisherUtils.java | 19 +++++ .../src/main/resources/publisher-api.yaml | 83 +++++++++++++++++++ 7 files changed, 242 insertions(+), 14 deletions(-) diff --git a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/ExceptionCodes.java b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/ExceptionCodes.java index 739468645392..317def5b31ba 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/ExceptionCodes.java +++ b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/ExceptionCodes.java @@ -534,6 +534,8 @@ public enum ExceptionCodes implements ErrorHandler { "Required attributes(s) %s for api policy specification %s are either missing or empty"), OPERATION_POLICY_NOT_FOUND(902010, "API Policy Not Found", 404, "Requested api policy with id '%s' not found"), + CUSTOM_BACKEND_NOT_FOUND(903250, "Custom Backend not found", + 404, "Requested Custom Backend with id '%s' not found"), OPERATION_POLICY_ALREADY_EXISTS(903001, "The API Policy already exists.", 409, "An Operation Policy with name '%s' and version '%s' already exists"), diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml b/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml index 667346746d68..d0722519fe61 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml @@ -487,6 +487,71 @@ paths: -H "Content-Type: application/json" -d @data.json "https://127.0.0.1:9443/api/am/publisher/v4/apis/7a2298c4-c905-403f-8fac-38c73301631f/topics"' + /apis/{apiId}/custom-backend/{customBackendId}/content: + get: + tags: + - APIs + summary: Get Sequence of Custom Backend + description: This operation can be used to get Sequence of the Custom Backend + operationId: getCustomBackendDataContent + parameters: + - name: type + in: query + description: | + Type of the Endpoint. + SANDBOX or PRODUCTION + required: true + schema: + maxLength: 15 + type: string + - $ref: '#/components/parameters/customBackendId' + - $ref: '#/components/parameters/apiId' + responses: + 200: + description: | + OK. + Requested API Custom Backend is returned + headers: + ETag: + description: | + Entity Tag of the response resource. Used by caches, or in conditional requests (Will be supported in future). + schema: + type: string + Last-Modified: + description: | + Date and time the resource has been modifed the last time. + Used by caches, or in conditional requests (Will be supported in future). + schema: + type: string + Content-Type: + description: | + The content type of the body. + schema: + type: string + content: + application/zip: + schema: + type: string + format: binary + 304: + description: | + Not Modified. + Empty body because the client has already the latest version of the requested resource (Will be supported in future). + content: {} + 404: + $ref: '#/components/responses/NotFound' + 406: + $ref: '#/components/responses/NotAcceptable' + security: + - OAuth2Security: + - apim:api_view + - apim:api_manage + - apim:api_import_export + - apim:api_product_import_export + x-code-samples: + - lang: Curl + source: 'curl -k -H "Authorization: Bearer ae4eae22-3f65-387b-a171-d37eaa366fa8" + "https://127.0.0.1:9443/api/am/publisher/v4/apis/7a2298c4-c905-403f-8fac-38c73301631f/custom-backend/7a2298c4-c915-483f-8fac-38c73301631f/content"' /apis/{apiId}/custom-backend/{customBackendId}: get: @@ -496,6 +561,15 @@ paths: description: This operation can be used to get Custom Backend data of the API operationId: getCustomBackendData parameters: + - name: type + in: query + description: | + Type of the Endpoint. + SANDBOX or PRODUCTION + required: true + schema: + maxLength: 15 + type: string - $ref: '#/components/parameters/customBackendId' - $ref: '#/components/parameters/apiId' responses: @@ -550,6 +624,15 @@ paths: description: This operation can be used to remove the Custom Backend of the API operationId: customBackendDelete parameters: + - name: type + in: query + description: | + Type of the Endpoint. + SANDBOX or PRODUCTION + required: true + schema: + maxLength: 15 + type: string - $ref: '#/components/parameters/apiId' - $ref: '#/components/parameters/If-Match' - $ref: '#/components/parameters/customBackendId' diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApi.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApi.java index 52651f3cdb82..8ab5017e08eb 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApi.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApi.java @@ -395,8 +395,8 @@ public Response createNewAPIVersion( @NotNull @Size(max=30) @ApiParam(value = "V @ApiResponse(code = 404, message = "Not Found. The specified resource does not exist.", response = ErrorDTO.class), @ApiResponse(code = 409, message = "Conflict. Specified resource already exists.", response = ErrorDTO.class), @ApiResponse(code = 412, message = "Precondition Failed. The request has not been performed because one of the preconditions is not met.", response = ErrorDTO.class) }) - public Response customBackendDelete(@ApiParam(value = "**API ID** consisting of the **UUID** of the API. ",required=true) @PathParam("apiId") String apiId, @ApiParam(value = "Custom Backend ID ",required=true) @PathParam("customBackendId") String customBackendId, @ApiParam(value = "Validator for conditional requests; based on ETag. " )@HeaderParam("If-Match") String ifMatch) throws APIManagementException{ - return delegate.customBackendDelete(apiId, customBackendId, ifMatch, securityContext); + public Response customBackendDelete( @NotNull @Size(max=15) @ApiParam(value = "Type of the Endpoint. SANDBOX or PRODUCTION ",required=true) @QueryParam("type") String type, @ApiParam(value = "**API ID** consisting of the **UUID** of the API. ",required=true) @PathParam("apiId") String apiId, @ApiParam(value = "Custom Backend ID ",required=true) @PathParam("customBackendId") String customBackendId, @ApiParam(value = "Validator for conditional requests; based on ETag. " )@HeaderParam("If-Match") String ifMatch) throws APIManagementException{ + return delegate.customBackendDelete(type, apiId, customBackendId, ifMatch, securityContext); } @PUT @@ -1349,8 +1349,29 @@ public Response getAuditReportOfAPI(@ApiParam(value = "**API ID** consisting of @ApiResponse(code = 304, message = "Not Modified. Empty body because the client has already the latest version of the requested resource (Will be supported in future). ", response = Void.class), @ApiResponse(code = 404, message = "Not Found. The specified resource does not exist.", response = ErrorDTO.class), @ApiResponse(code = 406, message = "Not Acceptable. The requested media type is not supported.", response = ErrorDTO.class) }) - public Response getCustomBackendData(@ApiParam(value = "Custom Backend ID ",required=true) @PathParam("customBackendId") String customBackendId, @ApiParam(value = "**API ID** consisting of the **UUID** of the API. ",required=true) @PathParam("apiId") String apiId) throws APIManagementException{ - return delegate.getCustomBackendData(customBackendId, apiId, securityContext); + public Response getCustomBackendData( @NotNull @Size(max=15) @ApiParam(value = "Type of the Endpoint. SANDBOX or PRODUCTION ",required=true) @QueryParam("type") String type, @ApiParam(value = "Custom Backend ID ",required=true) @PathParam("customBackendId") String customBackendId, @ApiParam(value = "**API ID** consisting of the **UUID** of the API. ",required=true) @PathParam("apiId") String apiId) throws APIManagementException{ + return delegate.getCustomBackendData(type, customBackendId, apiId, securityContext); + } + + @GET + @Path("/{apiId}/custom-backend/{customBackendId}/content") + + @Produces({ "application/zip", "application/json" }) + @ApiOperation(value = "Get Sequence of Custom Backend", notes = "This operation can be used to get Sequence of the Custom Backend", response = File.class, authorizations = { + @Authorization(value = "OAuth2Security", scopes = { + @AuthorizationScope(scope = "apim:api_view", description = "View API"), + @AuthorizationScope(scope = "apim:api_manage", description = "Manage all API related operations"), + @AuthorizationScope(scope = "apim:api_import_export", description = "Import and export APIs related operations"), + @AuthorizationScope(scope = "apim:api_product_import_export", description = "Import and export API Products related operations") + }) + }, tags={ "APIs", }) + @ApiResponses(value = { + @ApiResponse(code = 200, message = "OK. Requested API Custom Backend is returned ", response = File.class), + @ApiResponse(code = 304, message = "Not Modified. Empty body because the client has already the latest version of the requested resource (Will be supported in future). ", response = Void.class), + @ApiResponse(code = 404, message = "Not Found. The specified resource does not exist.", response = ErrorDTO.class), + @ApiResponse(code = 406, message = "Not Acceptable. The requested media type is not supported.", response = ErrorDTO.class) }) + public Response getCustomBackendDataContent( @NotNull @Size(max=15) @ApiParam(value = "Type of the Endpoint. SANDBOX or PRODUCTION ",required=true) @QueryParam("type") String type, @ApiParam(value = "Custom Backend ID ",required=true) @PathParam("customBackendId") String customBackendId, @ApiParam(value = "**API ID** consisting of the **UUID** of the API. ",required=true) @PathParam("apiId") String apiId) throws APIManagementException{ + return delegate.getCustomBackendDataContent(type, customBackendId, apiId, securityContext); } @GET diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApiService.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApiService.java index 36007861452f..d247e8d3af14 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApiService.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApiService.java @@ -80,7 +80,7 @@ public interface ApisApiService { public Response createAPI(APIDTO APIDTO, String openAPIVersion, MessageContext messageContext) throws APIManagementException; public Response createAPIRevision(String apiId, APIRevisionDTO apIRevisionDTO, MessageContext messageContext) throws APIManagementException; public Response createNewAPIVersion(String newVersion, String apiId, Boolean defaultVersion, String serviceVersion, MessageContext messageContext) throws APIManagementException; - public Response customBackendDelete(String apiId, String customBackendId, String ifMatch, MessageContext messageContext) throws APIManagementException; + public Response customBackendDelete(String type, String apiId, String customBackendId, String ifMatch, MessageContext messageContext) throws APIManagementException; public Response customBackendUpdate(String apiId, String ifMatch, InputStream sequenceInputStream, Attachment sequenceDetail, String type, MessageContext messageContext) throws APIManagementException; public Response deleteAPI(String apiId, String ifMatch, MessageContext messageContext) throws APIManagementException; public Response deleteAPIClientCertificateByAlias(String alias, String apiId, MessageContext messageContext) throws APIManagementException; @@ -128,7 +128,8 @@ public interface ApisApiService { public Response getAmazonResourceNamesOfAPI(String apiId, MessageContext messageContext) throws APIManagementException; public Response getAuditReportOfAPI(String apiId, String accept, MessageContext messageContext) throws APIManagementException; public Response getCommentOfAPI(String commentId, String apiId, String xWSO2Tenant, String ifNoneMatch, Boolean includeCommenterInfo, Integer replyLimit, Integer replyOffset, MessageContext messageContext) throws APIManagementException; - public Response getCustomBackendData(String customBackendId, String apiId, MessageContext messageContext) throws APIManagementException; + public Response getCustomBackendData(String type, String customBackendId, String apiId, MessageContext messageContext) throws APIManagementException; + public Response getCustomBackendDataContent(String type, String customBackendId, String apiId, MessageContext messageContext) throws APIManagementException; public Response getGeneratedMockScriptsOfAPI(String apiId, String ifNoneMatch, MessageContext messageContext) throws APIManagementException; public Response getGraphQLPolicyComplexityOfAPI(String apiId, MessageContext messageContext) throws APIManagementException; public Response getGraphQLPolicyComplexityTypesOfAPI(String apiId, MessageContext messageContext) throws APIManagementException; diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java index 866bb0e1f73c..8c6cdffd2a46 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java @@ -168,32 +168,51 @@ public Response getAllAPIs(Integer limit, Integer offset, String sortBy, String return null; } - @Override public Response getCustomBackendData(String customBackendId, String apiId, MessageContext messageContext) - throws APIManagementException { + @Override public Response getCustomBackendData(String type, String customBackendId, String apiId, + MessageContext messageContext) throws APIManagementException { APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider(); //validate if api exists CommonUtils.validateAPIExistence(apiId); - String type = "SANDBOX"; Map endpointConfig = apiProvider.getCustomBackendOfAPIByUUID(customBackendId, apiId, type, - true); + false); + + if(endpointConfig != null) { + throw new APIMgtResourceNotFoundException("Couldn't retrieve an existing Custom Backend with ID: " + + customBackendId + " for API " + apiId, + ExceptionCodes.from(ExceptionCodes.CUSTOM_BACKEND_NOT_FOUND, customBackendId)); + } CustomBackendDTO backendDTO = new CustomBackendDTO(); backendDTO.setSequenceName(endpointConfig.get("sequence_name").toString()); backendDTO.setSequenceId(endpointConfig.get("sequence_id").toString()); backendDTO.setSequenceType(endpointConfig.get("type").toString()); return Response.ok().entity(backendDTO).build(); } + @Override + public Response getCustomBackendDataContent(String type, String customBackendId, String apiId, MessageContext messageContext) throws APIManagementException { + APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider(); + CommonUtils.validateAPIExistence(apiId); + + InputStream seq = apiProvider.getCustomBackendSequenceOfAPIByUUID(apiId, customBackendId, type); + if(seq != null) { + throw new APIMgtResourceNotFoundException("Couldn't retrieve an existing Custom Backend with ID: " + + customBackendId + " for API " + apiId, + ExceptionCodes.from(ExceptionCodes.CUSTOM_BACKEND_NOT_FOUND, customBackendId)); + } + String seqName = APIUtil.getCustomBackendName(apiId, type); + File file = RestApiPublisherUtils.exportCustomBackendData(seq, seqName); + return Response.ok(file).header(RestApiConstants.HEADER_CONTENT_DISPOSITION, + "attachment; filename=\"" + file.getName() + "\"").build(); + } - @Override public Response customBackendDelete(String apiId, String customBackendId, String ifMatch, + @Override public Response customBackendDelete(String type, String apiId, String customBackendId, String ifMatch, MessageContext messageContext) throws APIManagementException { APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider(); - //validate if api exists CommonUtils.validateAPIExistence(apiId); - String type = "SANDBOX"; apiProvider.deleteCustomBackendByID(apiId, customBackendId, type); - return Response.ok().entity(null).build(); + return Response.ok().build(); } @Override diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/utils/RestApiPublisherUtils.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/utils/RestApiPublisherUtils.java index ad580f43d337..506e9b88ba47 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/utils/RestApiPublisherUtils.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/utils/RestApiPublisherUtils.java @@ -46,6 +46,7 @@ import org.wso2.carbon.apimgt.rest.api.common.RestApiConstants; import org.wso2.carbon.apimgt.rest.api.publisher.v1.common.mappings.PublisherCommonUtils; import org.wso2.carbon.apimgt.rest.api.util.utils.RestApiUtil; +import org.wso2.carbon.utils.FileUtil; import org.xml.sax.InputSource; import org.xml.sax.SAXException; @@ -277,6 +278,24 @@ public static String getContentType(Attachment fileDetail) { return fileContentType; } + public static File exportCustomBackendData(InputStream seq, String seqName) throws APIManagementException { + File exportFolder = null; + try { + exportFolder = CommonUtil.createTempDirectoryFromName(seqName + "_" + "Custom-Backend"); + String exportCustomBackendBasePath = exportFolder.toString(); + String archivePath = exportCustomBackendBasePath.concat(File.separator + seqName); + CommonUtil.createDirectory(archivePath); + String customBackendName = + archivePath + File.separator + seqName + APIConstants.SYNAPSE_POLICY_DEFINITION_EXTENSION_XML; + CommonUtil.writeFile(customBackendName, IOUtils.toString(seq)); + CommonUtil.archiveDirectory(exportCustomBackendBasePath); + FileUtils.deleteQuietly(new File(exportCustomBackendBasePath)); + return new File(exportCustomBackendBasePath + APIConstants.ZIP_FILE_EXTENSION); + } catch (APIImportExportException | IOException ex) { + throw new APIManagementException("Error when exporting Custom Backend: " + seqName, ex); + } + } + public static File exportOperationPolicyData(OperationPolicyData policyData, String format) throws APIManagementException { diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/resources/publisher-api.yaml b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/resources/publisher-api.yaml index 667346746d68..d0722519fe61 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/resources/publisher-api.yaml +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/resources/publisher-api.yaml @@ -487,6 +487,71 @@ paths: -H "Content-Type: application/json" -d @data.json "https://127.0.0.1:9443/api/am/publisher/v4/apis/7a2298c4-c905-403f-8fac-38c73301631f/topics"' + /apis/{apiId}/custom-backend/{customBackendId}/content: + get: + tags: + - APIs + summary: Get Sequence of Custom Backend + description: This operation can be used to get Sequence of the Custom Backend + operationId: getCustomBackendDataContent + parameters: + - name: type + in: query + description: | + Type of the Endpoint. + SANDBOX or PRODUCTION + required: true + schema: + maxLength: 15 + type: string + - $ref: '#/components/parameters/customBackendId' + - $ref: '#/components/parameters/apiId' + responses: + 200: + description: | + OK. + Requested API Custom Backend is returned + headers: + ETag: + description: | + Entity Tag of the response resource. Used by caches, or in conditional requests (Will be supported in future). + schema: + type: string + Last-Modified: + description: | + Date and time the resource has been modifed the last time. + Used by caches, or in conditional requests (Will be supported in future). + schema: + type: string + Content-Type: + description: | + The content type of the body. + schema: + type: string + content: + application/zip: + schema: + type: string + format: binary + 304: + description: | + Not Modified. + Empty body because the client has already the latest version of the requested resource (Will be supported in future). + content: {} + 404: + $ref: '#/components/responses/NotFound' + 406: + $ref: '#/components/responses/NotAcceptable' + security: + - OAuth2Security: + - apim:api_view + - apim:api_manage + - apim:api_import_export + - apim:api_product_import_export + x-code-samples: + - lang: Curl + source: 'curl -k -H "Authorization: Bearer ae4eae22-3f65-387b-a171-d37eaa366fa8" + "https://127.0.0.1:9443/api/am/publisher/v4/apis/7a2298c4-c905-403f-8fac-38c73301631f/custom-backend/7a2298c4-c915-483f-8fac-38c73301631f/content"' /apis/{apiId}/custom-backend/{customBackendId}: get: @@ -496,6 +561,15 @@ paths: description: This operation can be used to get Custom Backend data of the API operationId: getCustomBackendData parameters: + - name: type + in: query + description: | + Type of the Endpoint. + SANDBOX or PRODUCTION + required: true + schema: + maxLength: 15 + type: string - $ref: '#/components/parameters/customBackendId' - $ref: '#/components/parameters/apiId' responses: @@ -550,6 +624,15 @@ paths: description: This operation can be used to remove the Custom Backend of the API operationId: customBackendDelete parameters: + - name: type + in: query + description: | + Type of the Endpoint. + SANDBOX or PRODUCTION + required: true + schema: + maxLength: 15 + type: string - $ref: '#/components/parameters/apiId' - $ref: '#/components/parameters/If-Match' - $ref: '#/components/parameters/customBackendId' From e99d50acb1aa8432900c14ae1d2c93272f446908 Mon Sep 17 00:00:00 2001 From: BLasan Date: Mon, 9 Sep 2024 11:26:04 +0530 Subject: [PATCH 18/56] Fix: Check Style Issues --- .../wso2/carbon/apimgt/api/APIProvider.java | 20 ++++++--- .../carbon/apimgt/api/ExceptionCodes.java | 4 +- .../carbon/apimgt/impl/APIProviderImpl.java | 43 ++++++------------- .../impl/dao/constants/SQLConstants.java | 3 +- .../carbon/apimgt/impl/utils/APIUtil.java | 24 +++++++++++ .../persistence/RegistryPersistenceImpl.java | 1 + .../v1/common/SynapsePolicyAggregator.java | 15 ++++--- .../v1/common/TemplateBuilderUtil.java | 4 +- .../v1/common/mappings/ExportUtils.java | 16 ++++++- .../v1/common/mappings/ImportUtils.java | 39 ++++------------- .../rest/api/publisher/v1/ApisApiService.java | 12 ++++-- .../publisher/v1/impl/ApisApiServiceImpl.java | 30 +++++++------ 12 files changed, 117 insertions(+), 94 deletions(-) diff --git a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java index ed0c9f15317c..40a628030efe 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java +++ b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java @@ -65,8 +65,11 @@ public interface APIProvider extends APIManager { */ Comment getComment(ApiTypeWrapper apiTypeWrapper, String commentId, Integer replyLimit, Integer replyOffset) throws APIManagementException; + void deleteCustomBackendByID(String backendUUID, String apiUUID, String type) throws APIManagementException; - InputStream getCustomBackendSequenceByAPIAndRevisionUUUID(String apiUUID, String revisionUUID, String type) throws APIManagementException; + + InputStream getCustomBackendSequenceByAPIAndRevisionUUUID(String apiUUID, String revisionUUID, String type) + throws APIManagementException; /** * @param apiTypeWrapper Api type wrapper @@ -318,12 +321,17 @@ List getSubscriptionsOfAPI(String apiName, String apiVersion, Str */ API updateAPI(API api, API existingAPI) throws APIManagementException, FaultGatewaysException; - void updateCustomBackend(API api,String type, InputStream sequence, String fileName, String customBackendUUID) throws APIManagementException; - Map getCustomBackendOfAPIByUUID(String customBackendUUID, String apiUUID, String type, boolean isInfoOnly) throws APIManagementException; - InputStream getCustomBackendSequenceOfAPIByUUID(String apiUUID, String backendUUID, String type) throws APIManagementException; + void updateCustomBackend(API api, String type, InputStream sequence, String fileName, String customBackendUUID) + throws APIManagementException; + + Map getCustomBackendOfAPIByUUID(String customBackendUUID, String apiUUID, String type, + boolean isInfoOnly) throws APIManagementException; + + InputStream getCustomBackendSequenceOfAPIByUUID(String apiUUID, String backendUUID, String type) + throws APIManagementException; - void updateCustomBackendByRevisionID(String apiUUID, String type, String revision, - String seqName, String backendUUID) throws APIManagementException; + void updateCustomBackendByRevisionID(String apiUUID, String type, String revision, String seqName, + String backendUUID) throws APIManagementException; void addNewCustomBackendForRevision(String revisionUUID, String updatedBackendUUID, String apiUUID, Map config) throws APIManagementException; diff --git a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/ExceptionCodes.java b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/ExceptionCodes.java index 317def5b31ba..aa9f9525a7c2 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/ExceptionCodes.java +++ b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/ExceptionCodes.java @@ -483,8 +483,8 @@ public enum ExceptionCodes implements ErrorHandler { ERROR_READING_META_DATA(900907, "Error while reading meta information from the definition", 400, "Error while reading meta information from the definition"), - ERROR_READING_CUSTOM_SEQUENCE(900908, "Error while reading Custom Sequence from the API Endpoint Configuration", 400, - "Error while reading Custom Sequence from the API Endpoint Configuration"), + ERROR_READING_CUSTOM_SEQUENCE(900908, "Error while reading Custom Sequence from the API Endpoint Configuration", + 400, "Error while reading Custom Sequence from the API Endpoint Configuration"), ERROR_READING_PARAMS_FILE(900908, "Error while reading meta information from the params file", 400, "Error while reading meta information from the params file"), ERROR_FETCHING_DEFINITION_FILE(900909, "Cannot find the definition file of the project", 400, diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java index 2b2bf44c44c6..88dec2b3dd99 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java @@ -21,8 +21,6 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.gson.Gson; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; import org.apache.axiom.om.OMElement; import org.apache.axiom.om.util.AXIOMUtil; import org.apache.axis2.Constants; @@ -1170,34 +1168,39 @@ private void updateAPIPolicies(API api, String tenantDomain) throws APIManagemen } @Override - public void updateCustomBackend(API api, String type, InputStream sequence, String seqName, String customBackendUUID) - throws APIManagementException { + public void updateCustomBackend(API api, String type, InputStream sequence, String seqName, + String customBackendUUID) throws APIManagementException { apiMgtDAO.updateCustomBackend(api.getUuid(), seqName, sequence, type, customBackendUUID); } @Override - public Map getCustomBackendOfAPIByUUID(String customBackendUUID, String apiUUID, String type, boolean isInfoOnly) throws APIManagementException { + public Map getCustomBackendOfAPIByUUID(String customBackendUUID, String apiUUID, + String type, boolean isInfoOnly) throws APIManagementException { return apiMgtDAO.getCustomBackendOfAPIByUUID(customBackendUUID, apiUUID, type, isInfoOnly); } - @Override public void updateCustomBackendByRevisionID(String apiUUID, String type, String revision, String seqName, + @Override + public void updateCustomBackendByRevisionID(String apiUUID, String type, String revision, String seqName, String backendUUID) throws APIManagementException { String customBackendUUID = UUID.randomUUID().toString(); InputStream sequence = apiMgtDAO.getCustomBackendSequenceOfAPIByUUID(backendUUID, apiUUID, type); apiMgtDAO.updateCustomBackendByRevision(apiUUID, seqName, sequence, type, revision, customBackendUUID); } - @Override public InputStream getCustomBackendSequenceOfAPIByUUID(String apiUUID, String backendUUID, String type) + @Override + public InputStream getCustomBackendSequenceOfAPIByUUID(String apiUUID, String backendUUID, String type) throws APIManagementException { return apiMgtDAO.getCustomBackendSequenceOfAPIByUUID(backendUUID, apiUUID, type); } @Override - public InputStream getCustomBackendSequenceByAPIAndRevisionUUUID(String apiUUID, String revisionUUID, String type) throws APIManagementException { + public InputStream getCustomBackendSequenceByAPIAndRevisionUUUID(String apiUUID, String revisionUUID, + String type) throws APIManagementException { return apiMgtDAO.getCustomBackendSequenceByAPIandRevisionUUID(apiUUID, revisionUUID, type); } - @Override public void addNewCustomBackendForRevision(String revisionUUID, String updatedBackendUUID, String apiUUID, + @Override + public void addNewCustomBackendForRevision(String revisionUUID, String updatedBackendUUID, String apiUUID, Map config) throws APIManagementException { if (config.get("production") != null) { InputStream sequence = apiMgtDAO.getCustomBackendSequenceOfAPIByUUID(updatedBackendUUID, apiUUID, @@ -1222,7 +1225,8 @@ public InputStream getCustomBackendSequenceByAPIAndRevisionUUUID(String apiUUID, } } - @Override public void deleteCustomBackendByID(String backendUUID, String apiUUID, String type) + @Override + public void deleteCustomBackendByID(String backendUUID, String apiUUID, String type) throws APIManagementException { apiMgtDAO.deleteCustomBackend(apiUUID, backendUUID, type); } @@ -5517,23 +5521,6 @@ private void populateAPILevelPolicies(API api) throws APIManagementException { } } - private void populateCustomBackend(API api) throws APIManagementException { - JsonObject endpointConfig = JsonParser.parseString(api.getEndpointConfig()).getAsJsonObject(); - if(endpointConfig != null) { - if(endpointConfig.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE) != null && - APIConstants.ENDPOINT_TYPE_SEQUENCE.equals(endpointConfig.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE).getAsString())) { - ObjectMapper mapper = new ObjectMapper(); - // TODO: Check DB Queries. Following fetches data by API UUID and Revision ID = null - Map conf = apiMgtDAO.retrieveCustomBackendOfAPI(api.getUuid()); - try { - api.setEndpointConfig(mapper.writeValueAsString(conf)); - } catch (IOException ex) { - handleException("Error while converting endpointConfig to json", ex); - } - } - } - } - @Override public APISearchResult searchPaginatedAPIsByFQDN(String endpoint, String tenantDomain, int offset, int limit) throws APIManagementException { @@ -6382,8 +6369,6 @@ public void deployAPIRevision(String apiId, String apiRevisionUUID, } apiMgtDAO.addAPIRevisionDeployment(apiRevisionUUID, apiRevisionDeployments); - // update AM_API_CUSTOM_BACKEND table - WorkflowExecutor revisionDeploymentWFExecutor = getWorkflowExecutor( WorkflowConstants.WF_TYPE_AM_REVISION_DEPLOYMENT); 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 2b066ebf1dc3..ab20fa52f57a 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 @@ -4476,7 +4476,8 @@ public static class TransactionCountConstants { } 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 (?,?,?,?,?,?)"; + "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 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'"; 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 1e9bf9da0b7a..b241d59a0c8b 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 @@ -2921,6 +2921,14 @@ public static void validateAPIContext(String context, String apiName) throws API } } + /** + * This method is used to validate the endpoint configuration for API + * + * @param endpointConfigObject Endpoint Configuratioj of the API + * @param apiType API Type + * @param apiName Name of the API + * @throws APIManagementException Throws an error if endpoint configuration is not valid + */ public static void validateAPIEndpointConfig(Object endpointConfigObject, String apiType, String apiName) throws APIManagementException { Map endpointConfigMap = (Map) endpointConfigObject; @@ -4302,6 +4310,13 @@ public static String getSequenceExtensionName(API api) { return api.getId().getApiName() + ":v" + api.getId().getVersion(); } + /** + * Return the Custom Backend name + * + * @param apiUUID API Id + * @param type Type of the custom backend used for (SANDBOX, PRODUCTION) + * @return The name of the Custom Backend + */ public static String getCustomBackendName(String apiUUID, String type) { return apiUUID + "-" + type; } @@ -10029,6 +10044,15 @@ public static void loadCommonOperationPolicies(String organization) { } } + /** + * Method is used to retrieve the Custom Backend sequence + * + * @param extractedFolderPath Extracted folder path of the APICTL project + * @param customBackendFileName Custom Backend name + * @param fileExtension .xml + * @return The Sequence of the Custom Backend as an Input Stream + * @throws APIManagementException If an error occurs while reading, throws an error + */ public static InputStream getCustomBackendSequence(String extractedFolderPath, String customBackendFileName, String fileExtension) throws APIManagementException { String fileName = extractedFolderPath + File.separator + customBackendFileName + fileExtension; diff --git a/components/apimgt/org.wso2.carbon.apimgt.persistence/src/main/java/org/wso2/carbon/apimgt/persistence/RegistryPersistenceImpl.java b/components/apimgt/org.wso2.carbon.apimgt.persistence/src/main/java/org/wso2/carbon/apimgt/persistence/RegistryPersistenceImpl.java index ec04b146d806..e70fa9d7138c 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.persistence/src/main/java/org/wso2/carbon/apimgt/persistence/RegistryPersistenceImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.persistence/src/main/java/org/wso2/carbon/apimgt/persistence/RegistryPersistenceImpl.java @@ -392,6 +392,7 @@ public void restoreAPIRevision(Organization org, String apiUUID, String revision registry.put(apiPath, newAPIArtifact); GenericArtifact artifact = getAPIArtifact(apiUUID, registry); artifact.setAttribute(APIConstants.API_OVERVIEW_STATUS, lifecycleStatus); + // Update with the modified artifact artifactManager.updateGenericArtifact(artifact); RegistryPersistenceUtil.clearResourcePermissions(apiPath, api.getId(), ((UserRegistry) registry).getTenantId()); 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/SynapsePolicyAggregator.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/SynapsePolicyAggregator.java index ae350198d250..50f15b802da8 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/SynapsePolicyAggregator.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/SynapsePolicyAggregator.java @@ -57,9 +57,9 @@ public class SynapsePolicyAggregator { private static final String POLICY_SEQUENCE_TEMPLATE_LOCATION = CarbonUtils.getCarbonHome() + File.separator + "repository" + File.separator + "resources" + File.separator + "api_templates" + File.separator + "operation_policy_template.j2"; - private static final String CUSTOM_BACKEND_SEQUENCE_TEMPLATE_LOCATION = CarbonUtils.getCarbonHome() + File.separator - + "repository" + File.separator + "resources" + File.separator + "api_templates" + File.separator - + "custom_backend_sequence_template.j2"; + private static final String CUSTOM_BACKEND_SEQUENCE_TEMPLATE_LOCATION = + CarbonUtils.getCarbonHome() + File.separator + "repository" + File.separator + "resources" + File.separator + + "api_templates" + File.separator + "custom_backend_sequence_template.j2"; private static final String GATEWAY_POLICY_SEQUENCE_TEMPLATE_LOCATION = CarbonUtils.getCarbonHome() + File.separator + "repository" + File.separator + "resources" + File.separator + "templates" + File.separator + "gateway_policy_template.j2"; @@ -107,7 +107,8 @@ public static String generatePolicySequenceForUriTemplateSet(Set ur } } - public static String generateBackendSequenceForCustomSequence(String sequenceName, String pathToArchive, String endpointType) throws APIManagementException, IOException { + public static String generateBackendSequenceForCustomSequence(String sequenceName, String pathToArchive, + String endpointType) throws APIManagementException, IOException { Map configMap = new HashMap<>(); String customBackendTemplate = FileUtil.readFileToString(CUSTOM_BACKEND_SEQUENCE_TEMPLATE_LOCATION) .replace("\\", ""); @@ -219,9 +220,11 @@ private static List renderPolicyMapping(List policyList return renderedPolicyMappingList; } - private static String renderCustomBackendSequence(String sequenceName, String pathToArchive) throws APIManagementException { + private static String renderCustomBackendSequence(String sequenceName, String pathToArchive) + throws APIManagementException { String policyDirectory = pathToArchive + File.separator + ImportExportConstants.CUSTOM_BACKEND_DIRECTORY; - String sequence = APIUtil.getOperationCustomBackendSequenceFromFile(policyDirectory, sequenceName, ".xml"); + String sequence = APIUtil.getOperationCustomBackendSequenceFromFile(policyDirectory, sequenceName, + APIConstants.SYNAPSE_POLICY_DEFINITION_EXTENSION_XML); return renderPolicyTemplate(sequence, new HashMap<>()); } 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/TemplateBuilderUtil.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/TemplateBuilderUtil.java index e7d68a984e67..c5533facbe62 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/TemplateBuilderUtil.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/TemplateBuilderUtil.java @@ -1444,8 +1444,8 @@ private static GatewayContentDTO retrieveCustomBackendSequence(API api, String e String customSequence = null; String seqExt = APIUtil.getCustomBackendName(api.getUuid(), endpointType); try { - customSequence = SynapsePolicyAggregator.generateBackendSequenceForCustomSequence(seqExt, - pathToAchieve, endpointType); + customSequence = SynapsePolicyAggregator.generateBackendSequenceForCustomSequence(seqExt, pathToAchieve, + endpointType); } catch (IOException e) { throw new APIManagementException(e); } 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/ExportUtils.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/ExportUtils.java index 4bf37e1e4dc7..e2b7f5b187b4 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/ExportUtils.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/ExportUtils.java @@ -771,8 +771,20 @@ public static void exportPolicyData(String policyFileName, OperationPolicyData p } } - public static void exportCustomBackend(String customBackendFileName, String sequence, String archivePath) throws APIImportExportException, IOException { - String customBackendName = archivePath + File.separator + ImportExportConstants.CUSTOM_BACKEND_DIRECTORY + File.separator + customBackendFileName; + /** + * Method is used to write Custom Backend file to the Directory + * + * @param customBackendFileName Custom Backend file name + * @param sequence Content of the Custom Backend + * @param archivePath Archived path + * @throws APIImportExportException Import/Export error if exists + * @throws IOException IO Error when reading/writing to the file + */ + public static void exportCustomBackend(String customBackendFileName, String sequence, String archivePath) + throws APIImportExportException, IOException { + String customBackendName = + archivePath + File.separator + ImportExportConstants.CUSTOM_BACKEND_DIRECTORY + File.separator + + customBackendFileName; CommonUtil.writeFile(customBackendName + APIConstants.SYNAPSE_POLICY_DEFINITION_EXTENSION_XML, sequence); } 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 d2d0be3e936c..ad54ac60321c 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 @@ -271,26 +271,6 @@ 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("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); @@ -706,6 +686,14 @@ public static void validateAppliedPolicy(OperationPolicy appliedPolicy, } } + /** + * Method is used to Update the Custom Backend of the API + * + * @param api API Object + * @param extractedFolderPath Extracted folder path + * @param apiProvider API Provider + * @throws APIManagementException Throws an error occurs while reading the sequence as an Input Stream + */ public static void updateAPIWithCustomBackend(API api, String extractedFolderPath, APIProvider apiProvider) throws APIManagementException { String customBackendDir = extractedFolderPath + File.separator + ImportExportConstants.CUSTOM_BACKEND_DIRECTORY; @@ -1382,17 +1370,6 @@ public static APIProductDTO retrieveAPIProductDto(String pathToArchive) throws I return new Gson().fromJson(jsonObject, APIProductDTO.class); } - public static String retrieveXMLContent(InputStream sequenceInputStream) throws IOException { - StringBuilder result = new StringBuilder(); - try (BufferedReader reader = new BufferedReader(new InputStreamReader(sequenceInputStream))) { - String line; - while ((line = reader.readLine()) != null) { - result.append(line).append("\n"); - } - } - return result.toString(); - } - /** * This function will preprocess endpoint config security. * diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApiService.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApiService.java index d247e8d3af14..a8b36c1fea01 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApiService.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApiService.java @@ -80,7 +80,9 @@ public interface ApisApiService { public Response createAPI(APIDTO APIDTO, String openAPIVersion, MessageContext messageContext) throws APIManagementException; public Response createAPIRevision(String apiId, APIRevisionDTO apIRevisionDTO, MessageContext messageContext) throws APIManagementException; public Response createNewAPIVersion(String newVersion, String apiId, Boolean defaultVersion, String serviceVersion, MessageContext messageContext) throws APIManagementException; - public Response customBackendDelete(String type, String apiId, String customBackendId, String ifMatch, MessageContext messageContext) throws APIManagementException; + + Response customBackendDelete(String type, String apiId, String customBackendId, String ifMatch, + MessageContext messageContext) throws APIManagementException; public Response customBackendUpdate(String apiId, String ifMatch, InputStream sequenceInputStream, Attachment sequenceDetail, String type, MessageContext messageContext) throws APIManagementException; public Response deleteAPI(String apiId, String ifMatch, MessageContext messageContext) throws APIManagementException; public Response deleteAPIClientCertificateByAlias(String alias, String apiId, MessageContext messageContext) throws APIManagementException; @@ -128,8 +130,12 @@ public interface ApisApiService { public Response getAmazonResourceNamesOfAPI(String apiId, MessageContext messageContext) throws APIManagementException; public Response getAuditReportOfAPI(String apiId, String accept, MessageContext messageContext) throws APIManagementException; public Response getCommentOfAPI(String commentId, String apiId, String xWSO2Tenant, String ifNoneMatch, Boolean includeCommenterInfo, Integer replyLimit, Integer replyOffset, MessageContext messageContext) throws APIManagementException; - public Response getCustomBackendData(String type, String customBackendId, String apiId, MessageContext messageContext) throws APIManagementException; - public Response getCustomBackendDataContent(String type, String customBackendId, String apiId, MessageContext messageContext) throws APIManagementException; + + Response getCustomBackendData(String type, String customBackendId, String apiId, + MessageContext messageContext) throws APIManagementException; + + Response getCustomBackendDataContent(String type, String customBackendId, String apiId, + MessageContext messageContext) throws APIManagementException; public Response getGeneratedMockScriptsOfAPI(String apiId, String ifNoneMatch, MessageContext messageContext) throws APIManagementException; public Response getGraphQLPolicyComplexityOfAPI(String apiId, MessageContext messageContext) throws APIManagementException; public Response getGraphQLPolicyComplexityTypesOfAPI(String apiId, MessageContext messageContext) throws APIManagementException; diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java index 8c6cdffd2a46..06e722f97d35 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java @@ -168,7 +168,8 @@ public Response getAllAPIs(Integer limit, Integer offset, String sortBy, String return null; } - @Override public Response getCustomBackendData(String type, String customBackendId, String apiId, + @Override + public Response getCustomBackendData(String type, String customBackendId, String apiId, MessageContext messageContext) throws APIManagementException { APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider(); @@ -178,9 +179,9 @@ public Response getAllAPIs(Integer limit, Integer offset, String sortBy, String Map endpointConfig = apiProvider.getCustomBackendOfAPIByUUID(customBackendId, apiId, type, false); - if(endpointConfig != null) { - throw new APIMgtResourceNotFoundException("Couldn't retrieve an existing Custom Backend with ID: " - + customBackendId + " for API " + apiId, + if (endpointConfig != null) { + throw new APIMgtResourceNotFoundException( + "Couldn't retrieve an existing Custom Backend with ID: " + customBackendId + " for API " + apiId, ExceptionCodes.from(ExceptionCodes.CUSTOM_BACKEND_NOT_FOUND, customBackendId)); } CustomBackendDTO backendDTO = new CustomBackendDTO(); @@ -189,24 +190,28 @@ public Response getAllAPIs(Integer limit, Integer offset, String sortBy, String backendDTO.setSequenceType(endpointConfig.get("type").toString()); return Response.ok().entity(backendDTO).build(); } + @Override - public Response getCustomBackendDataContent(String type, String customBackendId, String apiId, MessageContext messageContext) throws APIManagementException { + public Response getCustomBackendDataContent(String type, String customBackendId, String apiId, + MessageContext messageContext) throws APIManagementException { APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider(); CommonUtils.validateAPIExistence(apiId); InputStream seq = apiProvider.getCustomBackendSequenceOfAPIByUUID(apiId, customBackendId, type); - if(seq != null) { - throw new APIMgtResourceNotFoundException("Couldn't retrieve an existing Custom Backend with ID: " - + customBackendId + " for API " + apiId, + if (seq == null) { + throw new APIMgtResourceNotFoundException( + "Couldn't retrieve an existing Custom Backend with ID: " + customBackendId + " for API " + apiId, ExceptionCodes.from(ExceptionCodes.CUSTOM_BACKEND_NOT_FOUND, customBackendId)); } String seqName = APIUtil.getCustomBackendName(apiId, type); File file = RestApiPublisherUtils.exportCustomBackendData(seq, seqName); - return Response.ok(file).header(RestApiConstants.HEADER_CONTENT_DISPOSITION, - "attachment; filename=\"" + file.getName() + "\"").build(); + return Response.ok(file) + .header(RestApiConstants.HEADER_CONTENT_DISPOSITION, "attachment; filename=\"" + file.getName() + "\"") + .build(); } - @Override public Response customBackendDelete(String type, String apiId, String customBackendId, String ifMatch, + @Override + public Response customBackendDelete(String type, String apiId, String customBackendId, String ifMatch, MessageContext messageContext) throws APIManagementException { APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider(); //validate if api exists @@ -250,7 +255,8 @@ public Response getAPI(String apiId, String xWSO2Tenant, String ifNoneMatch, return Response.ok().entity(apiToReturn).build(); } - @Override public Response customBackendUpdate(String apiId, String ifMatch, InputStream sequenceInputStream, + @Override + public Response customBackendUpdate(String apiId, String ifMatch, InputStream sequenceInputStream, Attachment sequenceDetail, String type, MessageContext messageContext) throws APIManagementException { String username = RestApiCommonUtil.getLoggedInUsername(); APIProvider apiProvider = RestApiCommonUtil.getProvider(username); From 485ba0872c0314eace17a70a69289f3af8adfb6f Mon Sep 17 00:00:00 2001 From: BLasan Date: Mon, 9 Sep 2024 11:26:51 +0530 Subject: [PATCH 19/56] Remove: Unused Imports --- .../main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java | 1 + 1 file changed, 1 insertion(+) diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java index 88dec2b3dd99..08d366a0e053 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java @@ -21,6 +21,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.gson.Gson; + import org.apache.axiom.om.OMElement; import org.apache.axiom.om.util.AXIOMUtil; import org.apache.axis2.Constants; From 5f3f42e1fccf0ec5b36c2ec5a59d7c99fc4bbd50 Mon Sep 17 00:00:00 2001 From: BLasan Date: Mon, 9 Sep 2024 13:25:06 +0530 Subject: [PATCH 20/56] Fix: Ununsed imports and checkstyle issues --- .../carbon/apimgt/impl/utils/APIUtil.java | 25 ++++++++++++------- .../src/main/resources/publisher-api.yaml | 3 --- .../rest/api/publisher/v1/dto/APIDTO.java | 22 +--------------- .../v1/common/SynapsePolicyAggregator.java | 2 +- .../v1/common/mappings/APIMappingUtil.java | 1 + .../v1/common/mappings/ExportUtils.java | 6 ++--- .../v1/common/mappings/ImportUtils.java | 2 -- .../v1/utils/RestApiPublisherUtils.java | 1 - .../src/main/resources/publisher-api.yaml | 3 --- 9 files changed, 21 insertions(+), 44 deletions(-) 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 b241d59a0c8b..597e8b5db64f 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 @@ -4314,7 +4314,7 @@ public static String getSequenceExtensionName(API api) { * Return the Custom Backend name * * @param apiUUID API Id - * @param type Type of the custom backend used for (SANDBOX, PRODUCTION) + * @param type Type of the custom backend used for (SANDBOX, PRODUCTION) * @return The name of the Custom Backend */ public static String getCustomBackendName(String apiUUID, String type) { @@ -10047,9 +10047,9 @@ public static void loadCommonOperationPolicies(String organization) { /** * Method is used to retrieve the Custom Backend sequence * - * @param extractedFolderPath Extracted folder path of the APICTL project + * @param extractedFolderPath Extracted folder path of the APICTL project * @param customBackendFileName Custom Backend name - * @param fileExtension .xml + * @param fileExtension .xml * @return The Sequence of the Custom Backend as an Input Stream * @throws APIManagementException If an error occurs while reading, throws an error */ @@ -10103,10 +10103,17 @@ public static OperationPolicyDefinition getOperationPolicyDefinitionFromFile(Str return policyDefinition; } - public static String getOperationCustomBackendSequenceFromFile(String extractedFolderPath, - String sequenceName, - String fileExtension) - throws APIManagementException { + /** + * Method is used to get Custom Backend Sequence from the APICTL project + * + * @param extractedFolderPath Extracted Folder path + * @param sequenceName Sequence File name + * @param fileExtension File extension of the Custom Backend + * @return Custom Backend sequence as a String + * @throws APIManagementException Throws if an error occurs when reading the file + */ + public static String getCustomBackendSequenceFromFile(String extractedFolderPath, String sequenceName, + String fileExtension) throws APIManagementException { String customBackendContent = null; try { @@ -10118,8 +10125,8 @@ public static String getOperationCustomBackendSequenceFromFile(String extractedF customBackendContent = FileUtils.readFileToString(new File(fileName)); } } catch (IOException e) { - throw new APIManagementException("Error while reading Custom Backend from path: " - + extractedFolderPath, e, ExceptionCodes.ERROR_READING_META_DATA); + throw new APIManagementException("Error while reading Custom Backend from path: " + extractedFolderPath, e, + ExceptionCodes.ERROR_READING_META_DATA); } return customBackendContent; } diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml b/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml index d0722519fe61..a414167ea3b4 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml @@ -10098,9 +10098,6 @@ components: enableSubscriberVerification: type: boolean example: false - sequence: - type: string - example: in sequence type: type: string description: The api creation type to be used. Accepted values are HTTP, diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/dto/APIDTO.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/dto/APIDTO.java index f67451bca1da..81d415d20ffa 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/dto/APIDTO.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/dto/APIDTO.java @@ -63,7 +63,6 @@ public class APIDTO { private Integer revisionId = null; private Boolean enableSchemaValidation = null; private Boolean enableSubscriberVerification = null; - private String sequence = null; @XmlType(name="TypeEnum") @XmlEnum(String.class) @@ -640,23 +639,6 @@ public void setEnableSubscriberVerification(Boolean enableSubscriberVerification this.enableSubscriberVerification = enableSubscriberVerification; } - /** - **/ - public APIDTO sequence(String sequence) { - this.sequence = sequence; - return this; - } - - - @ApiModelProperty(example = "in sequence", value = "") - @JsonProperty("sequence") - public String getSequence() { - return sequence; - } - public void setSequence(String sequence) { - this.sequence = sequence; - } - /** * The api creation type to be used. Accepted values are HTTP, WS, SOAPTOREST, GRAPHQL, WEBSUB, SSE, WEBHOOK, ASYNC **/ @@ -1488,7 +1470,6 @@ public boolean equals(java.lang.Object o) { Objects.equals(revisionId, API.revisionId) && Objects.equals(enableSchemaValidation, API.enableSchemaValidation) && Objects.equals(enableSubscriberVerification, API.enableSubscriberVerification) && - Objects.equals(sequence, API.sequence) && Objects.equals(type, API.type) && Objects.equals(subtype, API.subtype) && Objects.equals(audience, API.audience) && @@ -1538,7 +1519,7 @@ public boolean equals(java.lang.Object o) { @Override public int hashCode() { - return Objects.hash(id, name, description, context, version, sequence, provider, lifeCycleStatus, wsdlInfo, wsdlUrl, responseCachingEnabled, cacheTimeout, hasThumbnail, isDefaultVersion, isRevision, revisionedApiId, revisionId, enableSchemaValidation, enableSubscriberVerification, type, subtype, audience, audiences, transport, tags, policies, apiThrottlingPolicy, authorizationHeader, apiKeyHeader, securityScheme, maxTps, visibility, visibleRoles, visibleTenants, mediationPolicies, apiPolicies, subscriptionAvailability, subscriptionAvailableTenants, additionalProperties, additionalPropertiesMap, monetization, accessControl, accessControlRoles, businessInformation, corsConfiguration, websubSubscriptionConfiguration, workflowStatus, createdTime, lastUpdatedTimestamp, lastUpdatedTime, endpointConfig, endpointImplementationType, aiConfiguration, scopes, operations, threatProtectionPolicies, categories, keyManagers, serviceInfo, advertiseInfo, gatewayVendor, gatewayType, asyncTransportProtocols, egress); + return Objects.hash(id, name, description, context, version, provider, lifeCycleStatus, wsdlInfo, wsdlUrl, responseCachingEnabled, cacheTimeout, hasThumbnail, isDefaultVersion, isRevision, revisionedApiId, revisionId, enableSchemaValidation, enableSubscriberVerification, type, subtype, audience, audiences, transport, tags, policies, apiThrottlingPolicy, authorizationHeader, apiKeyHeader, securityScheme, maxTps, visibility, visibleRoles, visibleTenants, mediationPolicies, apiPolicies, subscriptionAvailability, subscriptionAvailableTenants, additionalProperties, additionalPropertiesMap, monetization, accessControl, accessControlRoles, businessInformation, corsConfiguration, websubSubscriptionConfiguration, workflowStatus, createdTime, lastUpdatedTimestamp, lastUpdatedTime, endpointConfig, endpointImplementationType, aiConfiguration, scopes, operations, threatProtectionPolicies, categories, keyManagers, serviceInfo, advertiseInfo, gatewayVendor, gatewayType, asyncTransportProtocols, egress); } @Override @@ -1564,7 +1545,6 @@ public String toString() { sb.append(" revisionId: ").append(toIndentedString(revisionId)).append("\n"); sb.append(" enableSchemaValidation: ").append(toIndentedString(enableSchemaValidation)).append("\n"); sb.append(" enableSubscriberVerification: ").append(toIndentedString(enableSubscriberVerification)).append("\n"); - sb.append(" sequence: ").append(toIndentedString(sequence)).append("\n"); sb.append(" type: ").append(toIndentedString(type)).append("\n"); sb.append(" subtype: ").append(toIndentedString(subtype)).append("\n"); sb.append(" audience: ").append(toIndentedString(audience)).append("\n"); 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/SynapsePolicyAggregator.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/SynapsePolicyAggregator.java index 50f15b802da8..262ffa47b558 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/SynapsePolicyAggregator.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/SynapsePolicyAggregator.java @@ -223,7 +223,7 @@ private static List renderPolicyMapping(List policyList private static String renderCustomBackendSequence(String sequenceName, String pathToArchive) throws APIManagementException { String policyDirectory = pathToArchive + File.separator + ImportExportConstants.CUSTOM_BACKEND_DIRECTORY; - String sequence = APIUtil.getOperationCustomBackendSequenceFromFile(policyDirectory, sequenceName, + String sequence = APIUtil.getCustomBackendSequenceFromFile(policyDirectory, sequenceName, APIConstants.SYNAPSE_POLICY_DEFINITION_EXTENSION_XML); return renderPolicyTemplate(sequence, new HashMap<>()); } 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/APIMappingUtil.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/APIMappingUtil.java index 06e7caf5eb96..5804fb64cb86 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/APIMappingUtil.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/APIMappingUtil.java @@ -193,6 +193,7 @@ public static API fromDTOtoAPI(APIDTO dto, String provider) throws APIManagement context = updateContextWithVersion(dto.getVersion(), originalContext, context); model.setContext(context); model.setDescription(dto.getDescription()); + Object endpointConfig = dto.getEndpointConfig(); if (endpointConfig != null) { ObjectMapper mapper = new ObjectMapper(); 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/ExportUtils.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/ExportUtils.java index e2b7f5b187b4..609985d491c7 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/ExportUtils.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/ExportUtils.java @@ -30,7 +30,6 @@ import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.eclipse.jetty.util.IO; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -70,7 +69,6 @@ import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIProductDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.AdvertiseInfoDTO; -import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CustomBackendDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.GraphQLQueryComplexityInfoDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ProductAPIDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ResourcePolicyInfoDTO; @@ -88,7 +86,6 @@ import java.util.HashMap; import java.util.HashSet; import java.util.List; -import java.util.Map; import java.util.Set; import static org.wso2.carbon.apimgt.impl.APIConstants.API_DATA_PRODUCTION_ENDPOINTS; @@ -222,7 +219,8 @@ public static File exportApi(APIProvider apiProvider, APIIdentifier apiIdentifie // TODO: Add Custom Backend to the Archive JsonObject endpointConfig = JsonParser.parseString(api.getEndpointConfig()).getAsJsonObject(); - if(APIConstants.ENDPOINT_TYPE_SEQUENCE.equals(endpointConfig.get(API_ENDPOINT_CONFIG_PROTOCOL_TYPE).getAsString())) { + if (APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( + endpointConfig.get(API_ENDPOINT_CONFIG_PROTOCOL_TYPE).getAsString())) { addCustomBackendToArchive(archivePath, apiProvider, currentApiUuid, endpointConfig); } 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 ad54ac60321c..e8c7b5ba659c 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 @@ -104,7 +104,6 @@ import org.wso2.carbon.utils.multitenancy.MultitenantConstants; import org.wso2.carbon.utils.multitenancy.MultitenantUtils; -import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; @@ -112,7 +111,6 @@ import java.io.FileReader; import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; import java.net.URLConnection; import java.nio.file.DirectoryIteratorException; import java.nio.file.DirectoryStream; diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/utils/RestApiPublisherUtils.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/utils/RestApiPublisherUtils.java index 506e9b88ba47..d311f4981909 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/utils/RestApiPublisherUtils.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/utils/RestApiPublisherUtils.java @@ -46,7 +46,6 @@ import org.wso2.carbon.apimgt.rest.api.common.RestApiConstants; import org.wso2.carbon.apimgt.rest.api.publisher.v1.common.mappings.PublisherCommonUtils; import org.wso2.carbon.apimgt.rest.api.util.utils.RestApiUtil; -import org.wso2.carbon.utils.FileUtil; import org.xml.sax.InputSource; import org.xml.sax.SAXException; diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/resources/publisher-api.yaml b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/resources/publisher-api.yaml index d0722519fe61..a414167ea3b4 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/resources/publisher-api.yaml +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/resources/publisher-api.yaml @@ -10098,9 +10098,6 @@ components: enableSubscriberVerification: type: boolean example: false - sequence: - type: string - example: in sequence type: type: string description: The api creation type to be used. Accepted values are HTTP, From a98b09d7545e6a140a4c7229d53df1dceb33fa7e Mon Sep 17 00:00:00 2001 From: BLasan Date: Mon, 9 Sep 2024 13:47:53 +0530 Subject: [PATCH 21/56] Fix: CheckStyle issue in If clause --- .../api/publisher/v1/common/mappings/PublisherCommonUtils.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 fb938092a9d8..6288b24dcfa8 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 @@ -210,9 +210,10 @@ public static API updateApi(API originalAPI, APIDTO apiDtoToUpdate, APIProvider public static JSONObject updateCustomBackend(API api, APIProvider apiProvider, String endpointType, InputStream customBackend, String contentDecomp) throws APIManagementException { String fileName = getFileNameFromContentDisposition(contentDecomp); - if (fileName == null) + if (fileName == null) { throw new APIManagementException( "Error when retrieving Custom Backend file name of API: " + api.getId().getApiName()); + } String seqName = APIUtil.getCustomBackendName(api.getUuid(), endpointType); String customBackendUUID = UUID.randomUUID().toString(); apiProvider.updateCustomBackend(api, endpointType, customBackend, seqName, customBackendUUID); From 10cebc6cd39aa85ef16c698304d0466d76f8923b Mon Sep 17 00:00:00 2001 From: BLasan Date: Tue, 10 Sep 2024 00:42:03 +0530 Subject: [PATCH 22/56] Fix: APIProduct Issue --- .../v1/common/TemplateBuilderUtil.java | 30 ++++++++++++++++++- .../rest/api/publisher/v1/ApisApiService.java | 12 ++------ 2 files changed, 32 insertions(+), 10 deletions(-) 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/TemplateBuilderUtil.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/TemplateBuilderUtil.java index c5533facbe62..bdfcb5485fed 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/TemplateBuilderUtil.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/TemplateBuilderUtil.java @@ -18,6 +18,8 @@ package org.wso2.carbon.apimgt.rest.api.publisher.v1.common; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; import org.apache.axiom.om.OMAttribute; import org.apache.axiom.om.OMElement; import org.apache.axiom.om.util.AXIOMUtil; @@ -691,7 +693,12 @@ private static GatewayAPIDTO createAPIGatewayDTOtoPublishAPI(Environment environ GatewayUtils.setCustomSequencesToBeRemoved(apiProduct.getId(), api.getUuid(), productAPIDto); APITemplateBuilder apiTemplateBuilder = new APITemplateBuilderImpl(api, apiProduct); // check the endpoint type - addEndpoints(api, apiTemplateBuilder, productAPIDto); + if (api.getEndpointConfig() != null) { + JsonObject endpointConfObj = JsonParser.parseString(api.getEndpointConfig()).getAsJsonObject(); + if(APIConstants.ENDPOINT_TYPE_SEQUENCE.equals(endpointConfObj.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE).getAsString())) { + addEndpoints(api, apiTemplateBuilder, productAPIDto); + } + } setCustomSequencesToBeAdded(apiProduct, api, productAPIDto, apiExtractedPath, apidto); setAPIFaultSequencesToBeAdded(api, productAPIDto, apiExtractedPath, apidto); String prefix = id.getName() + "--v" + id.getVersion(); @@ -736,6 +743,27 @@ private static void setCustomSequencesToBeAdded(APIProduct apiProduct, API api, gatewayAPIDTO.setSequenceToBeAdd( addGatewayContentToList(gatewayFaultContentDTO, gatewayAPIDTO.getSequenceToBeAdd())); } + + JsonObject endpointConfigMap = JsonParser.parseString(api.getEndpointConfig()).getAsJsonObject(); + if (APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( + endpointConfigMap.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE))) { + if (endpointConfigMap.get("sandbox") != null) { + GatewayContentDTO gatewayCustomBackendSequenceDTO = retrieveCustomBackendSequence(api, "SANDBOX", + extractedPath); + if (gatewayCustomBackendSequenceDTO != null) { + gatewayAPIDTO.setSequenceToBeAdd(addGatewayContentToList(gatewayCustomBackendSequenceDTO, + gatewayAPIDTO.getSequenceToBeAdd())); + } + } + if (endpointConfigMap.get("production") != null) { + GatewayContentDTO gatewayCustomBackendSequenceDTO = retrieveCustomBackendSequence(api, "PRODUCTION", + extractedPath); + if (gatewayCustomBackendSequenceDTO != null) { + gatewayAPIDTO.setSequenceToBeAdd(addGatewayContentToList(gatewayCustomBackendSequenceDTO, + gatewayAPIDTO.getSequenceToBeAdd())); + } + } + } } } diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApiService.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApiService.java index a8b36c1fea01..d247e8d3af14 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApiService.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApiService.java @@ -80,9 +80,7 @@ public interface ApisApiService { public Response createAPI(APIDTO APIDTO, String openAPIVersion, MessageContext messageContext) throws APIManagementException; public Response createAPIRevision(String apiId, APIRevisionDTO apIRevisionDTO, MessageContext messageContext) throws APIManagementException; public Response createNewAPIVersion(String newVersion, String apiId, Boolean defaultVersion, String serviceVersion, MessageContext messageContext) throws APIManagementException; - - Response customBackendDelete(String type, String apiId, String customBackendId, String ifMatch, - MessageContext messageContext) throws APIManagementException; + public Response customBackendDelete(String type, String apiId, String customBackendId, String ifMatch, MessageContext messageContext) throws APIManagementException; public Response customBackendUpdate(String apiId, String ifMatch, InputStream sequenceInputStream, Attachment sequenceDetail, String type, MessageContext messageContext) throws APIManagementException; public Response deleteAPI(String apiId, String ifMatch, MessageContext messageContext) throws APIManagementException; public Response deleteAPIClientCertificateByAlias(String alias, String apiId, MessageContext messageContext) throws APIManagementException; @@ -130,12 +128,8 @@ Response customBackendDelete(String type, String apiId, String customBackendId, public Response getAmazonResourceNamesOfAPI(String apiId, MessageContext messageContext) throws APIManagementException; public Response getAuditReportOfAPI(String apiId, String accept, MessageContext messageContext) throws APIManagementException; public Response getCommentOfAPI(String commentId, String apiId, String xWSO2Tenant, String ifNoneMatch, Boolean includeCommenterInfo, Integer replyLimit, Integer replyOffset, MessageContext messageContext) throws APIManagementException; - - Response getCustomBackendData(String type, String customBackendId, String apiId, - MessageContext messageContext) throws APIManagementException; - - Response getCustomBackendDataContent(String type, String customBackendId, String apiId, - MessageContext messageContext) throws APIManagementException; + public Response getCustomBackendData(String type, String customBackendId, String apiId, MessageContext messageContext) throws APIManagementException; + public Response getCustomBackendDataContent(String type, String customBackendId, String apiId, MessageContext messageContext) throws APIManagementException; public Response getGeneratedMockScriptsOfAPI(String apiId, String ifNoneMatch, MessageContext messageContext) throws APIManagementException; public Response getGraphQLPolicyComplexityOfAPI(String apiId, MessageContext messageContext) throws APIManagementException; public Response getGraphQLPolicyComplexityTypesOfAPI(String apiId, MessageContext messageContext) throws APIManagementException; From 471a6f4cda10f883e4e643d209e959452d427208 Mon Sep 17 00:00:00 2001 From: BLasan Date: Tue, 10 Sep 2024 01:04:16 +0530 Subject: [PATCH 23/56] Fix: APIProduct deployment error --- .../rest/api/publisher/v1/common/TemplateBuilderUtil.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) 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/TemplateBuilderUtil.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/TemplateBuilderUtil.java index bdfcb5485fed..f021eb7b7817 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/TemplateBuilderUtil.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/TemplateBuilderUtil.java @@ -695,7 +695,8 @@ private static GatewayAPIDTO createAPIGatewayDTOtoPublishAPI(Environment environ // check the endpoint type if (api.getEndpointConfig() != null) { JsonObject endpointConfObj = JsonParser.parseString(api.getEndpointConfig()).getAsJsonObject(); - if(APIConstants.ENDPOINT_TYPE_SEQUENCE.equals(endpointConfObj.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE).getAsString())) { + if (APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( + endpointConfObj.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE).getAsString())) { addEndpoints(api, apiTemplateBuilder, productAPIDto); } } @@ -745,7 +746,7 @@ private static void setCustomSequencesToBeAdded(APIProduct apiProduct, API api, } JsonObject endpointConfigMap = JsonParser.parseString(api.getEndpointConfig()).getAsJsonObject(); - if (APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( + if (endpointConfigMap != null && APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( endpointConfigMap.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE))) { if (endpointConfigMap.get("sandbox") != null) { GatewayContentDTO gatewayCustomBackendSequenceDTO = retrieveCustomBackendSequence(api, "SANDBOX", From 769bcab5b982df919c658f84460325653b0b20df Mon Sep 17 00:00:00 2001 From: BLasan Date: Tue, 10 Sep 2024 01:38:39 +0530 Subject: [PATCH 24/56] Fix: Custom Backend Get Issue --- .../apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java index 06e722f97d35..07271d67f9f4 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java @@ -179,7 +179,7 @@ public Response getCustomBackendData(String type, String customBackendId, String Map endpointConfig = apiProvider.getCustomBackendOfAPIByUUID(customBackendId, apiId, type, false); - if (endpointConfig != null) { + if (endpointConfig == null) { throw new APIMgtResourceNotFoundException( "Couldn't retrieve an existing Custom Backend with ID: " + customBackendId + " for API " + apiId, ExceptionCodes.from(ExceptionCodes.CUSTOM_BACKEND_NOT_FOUND, customBackendId)); From e4f8d8a60889ea200dc5c71b86f03692260ff0ff Mon Sep 17 00:00:00 2001 From: BLasan Date: Wed, 11 Sep 2024 08:42:25 +0530 Subject: [PATCH 25/56] Add: Custom Backend Delete --- .../java/org/wso2/carbon/apimgt/api/APIProvider.java | 1 + .../org/wso2/carbon/apimgt/impl/APIProviderImpl.java | 6 ++++++ .../org/wso2/carbon/apimgt/impl/dao/ApiMgtDAO.java | 12 ++++++++++++ 3 files changed, 19 insertions(+) diff --git a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java index 40a628030efe..61044ed5e333 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java +++ b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java @@ -67,6 +67,7 @@ Comment getComment(ApiTypeWrapper apiTypeWrapper, String commentId, Integer repl APIManagementException; void deleteCustomBackendByID(String backendUUID, String apiUUID, String type) throws APIManagementException; + void deleteCustomBackendByAPIID(String apiUUID) throws APIManagementException; InputStream getCustomBackendSequenceByAPIAndRevisionUUUID(String apiUUID, String revisionUUID, String type) throws APIManagementException; diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java index 08d366a0e053..3759af3eca71 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java @@ -1226,6 +1226,10 @@ public void addNewCustomBackendForRevision(String revisionUUID, String updatedBa } } + @Override + public void deleteCustomBackendByAPIID(String apiUUID) throws APIManagementException { + apiMgtDAO.deleteCustomBackendByAPIID(apiUUID); + } @Override public void deleteCustomBackendByID(String backendUUID, String apiUUID, String type) throws APIManagementException { @@ -2626,6 +2630,8 @@ public void deleteAPI(String apiUuid, String organization) throws APIManagementE // DB delete operations if (!isError && api != null) { try { + // Remove Custom Backend entries of the API + deleteCustomBackendByAPIID(apiUuid); deleteAPIRevisions(apiUuid, organization); deleteAPIFromDB(api); if (log.isDebugEnabled()) { 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 d2ac89c7a8bb..74efc11ee33c 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 @@ -21521,6 +21521,18 @@ public void deleteCustomBackend(String apiUUID, String type, String backendUUID) } } + public void deleteCustomBackendByAPIID(String apiUUID) throws APIManagementException { + String deleteCustomBackendSql = SQLConstants.CustomBackendConstants.DELETE_CUSTOM_BACKEND_BY_API; + try (Connection connection = APIMgtDBUtil.getConnection(); + PreparedStatement prepStmt = connection.prepareStatement(deleteCustomBackendSql)) { + connection.setAutoCommit(false); + prepStmt.setString(1, apiUUID); + connection.commit(); + } catch (SQLException ex) { + handleException("Error while deleting Custom Backend for API: " + apiUUID, ex); + } + } + public void deleteCustomBackendByRevision(String apiUUID, String revisionUUID) throws APIManagementException { boolean isRevisioned = checkAPIUUIDIsARevisionUUID(apiUUID) != null; String deleteSqlQuery = SQLConstants.CustomBackendConstants.DELETE_CUSTOM_BACKEND_BY_REVISION; From a310af7301a2874ed68a828b480efa290a06fd90 Mon Sep 17 00:00:00 2001 From: BLasan Date: Thu, 12 Sep 2024 07:25:32 +0530 Subject: [PATCH 26/56] Fix: API Product Endpoint adding issue --- .../rest/api/publisher/v1/common/TemplateBuilderUtil.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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/TemplateBuilderUtil.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/TemplateBuilderUtil.java index f021eb7b7817..435ac7a3e2e2 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/TemplateBuilderUtil.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/TemplateBuilderUtil.java @@ -695,7 +695,7 @@ private static GatewayAPIDTO createAPIGatewayDTOtoPublishAPI(Environment environ // check the endpoint type if (api.getEndpointConfig() != null) { JsonObject endpointConfObj = JsonParser.parseString(api.getEndpointConfig()).getAsJsonObject(); - if (APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( + if (!APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( endpointConfObj.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE).getAsString())) { addEndpoints(api, apiTemplateBuilder, productAPIDto); } @@ -747,7 +747,7 @@ 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))) { + endpointConfigMap.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE).getAsString())) { if (endpointConfigMap.get("sandbox") != null) { GatewayContentDTO gatewayCustomBackendSequenceDTO = retrieveCustomBackendSequence(api, "SANDBOX", extractedPath); From 6b8e22d5a6db45c157e86c0bbd4bce93a375b0b7 Mon Sep 17 00:00:00 2001 From: BLasan Date: Thu, 12 Sep 2024 16:25:55 +0530 Subject: [PATCH 27/56] Update: REST APIs of Custom Backend --- .../src/main/resources/publisher-api.yaml | 35 ------------------- .../apimgt/rest/api/publisher/v1/ApisApi.java | 8 ++--- .../rest/api/publisher/v1/ApisApiService.java | 4 +-- .../publisher/v1/impl/ApisApiServiceImpl.java | 4 +-- .../src/main/resources/publisher-api.yaml | 35 ------------------- 5 files changed, 8 insertions(+), 78 deletions(-) diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml b/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml index a414167ea3b4..c44472ee5f4b 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml @@ -512,17 +512,6 @@ paths: OK. Requested API Custom Backend is returned headers: - ETag: - description: | - Entity Tag of the response resource. Used by caches, or in conditional requests (Will be supported in future). - schema: - type: string - Last-Modified: - description: | - Date and time the resource has been modifed the last time. - Used by caches, or in conditional requests (Will be supported in future). - schema: - type: string Content-Type: description: | The content type of the body. @@ -578,17 +567,6 @@ paths: OK. Requested API Custom Backend is returned headers: - ETag: - description: | - Entity Tag of the response resource. Used by caches, or in conditional requests (Will be supported in future). - schema: - type: string - Last-Modified: - description: | - Date and time the resource has been modifed the last time. - Used by caches, or in conditional requests (Will be supported in future). - schema: - type: string Content-Type: description: | The content type of the body. @@ -634,7 +612,6 @@ paths: maxLength: 15 type: string - $ref: '#/components/parameters/apiId' - - $ref: '#/components/parameters/If-Match' - $ref: '#/components/parameters/customBackendId' responses: 200: @@ -669,7 +646,6 @@ paths: operationId: customBackendUpdate parameters: - $ref: '#/components/parameters/apiId' - - $ref: '#/components/parameters/If-Match' requestBody: content: multipart/form-data: @@ -688,17 +664,6 @@ paths: OK. Successful response with updated API object headers: - ETag: - description: | - Entity Tag of the response resource. Used by caches, or in conditional requests (Will be supported in future). - schema: - type: string - Last-Modified: - description: | - Date and time the resource has been modifed the last time. - Used by caches, or in conditional requests (Will be supported in future). - schema: - type: string Location: description: | The URL of the newly created resource. diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApi.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApi.java index 8ab5017e08eb..9f3bd4e5a335 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApi.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApi.java @@ -395,8 +395,8 @@ public Response createNewAPIVersion( @NotNull @Size(max=30) @ApiParam(value = "V @ApiResponse(code = 404, message = "Not Found. The specified resource does not exist.", response = ErrorDTO.class), @ApiResponse(code = 409, message = "Conflict. Specified resource already exists.", response = ErrorDTO.class), @ApiResponse(code = 412, message = "Precondition Failed. The request has not been performed because one of the preconditions is not met.", response = ErrorDTO.class) }) - public Response customBackendDelete( @NotNull @Size(max=15) @ApiParam(value = "Type of the Endpoint. SANDBOX or PRODUCTION ",required=true) @QueryParam("type") String type, @ApiParam(value = "**API ID** consisting of the **UUID** of the API. ",required=true) @PathParam("apiId") String apiId, @ApiParam(value = "Custom Backend ID ",required=true) @PathParam("customBackendId") String customBackendId, @ApiParam(value = "Validator for conditional requests; based on ETag. " )@HeaderParam("If-Match") String ifMatch) throws APIManagementException{ - return delegate.customBackendDelete(type, apiId, customBackendId, ifMatch, securityContext); + public Response customBackendDelete( @NotNull @Size(max=15) @ApiParam(value = "Type of the Endpoint. SANDBOX or PRODUCTION ",required=true) @QueryParam("type") String type, @ApiParam(value = "**API ID** consisting of the **UUID** of the API. ",required=true) @PathParam("apiId") String apiId, @ApiParam(value = "Custom Backend ID ",required=true) @PathParam("customBackendId") String customBackendId) throws APIManagementException{ + return delegate.customBackendDelete(type, apiId, customBackendId, securityContext); } @PUT @@ -417,8 +417,8 @@ public Response createNewAPIVersion( @NotNull @Size(max=30) @ApiParam(value = "V @ApiResponse(code = 404, message = "Not Found. The specified resource does not exist.", response = ErrorDTO.class), @ApiResponse(code = 409, message = "Conflict. Specified resource already exists.", response = ErrorDTO.class), @ApiResponse(code = 412, message = "Precondition Failed. The request has not been performed because one of the preconditions is not met.", response = ErrorDTO.class) }) - public Response customBackendUpdate(@ApiParam(value = "**API ID** consisting of the **UUID** of the API. ",required=true) @PathParam("apiId") String apiId, @ApiParam(value = "Validator for conditional requests; based on ETag. " )@HeaderParam("If-Match") String ifMatch, @Multipart(value = "sequence", required = false) InputStream sequenceInputStream, @Multipart(value = "sequence" , required = false) Attachment sequenceDetail, @Multipart(value = "type", required = false) String type) throws APIManagementException{ - return delegate.customBackendUpdate(apiId, ifMatch, sequenceInputStream, sequenceDetail, type, securityContext); + public Response customBackendUpdate(@ApiParam(value = "**API ID** consisting of the **UUID** of the API. ",required=true) @PathParam("apiId") String apiId, @Multipart(value = "sequence", required = false) InputStream sequenceInputStream, @Multipart(value = "sequence" , required = false) Attachment sequenceDetail, @Multipart(value = "type", required = false) String type) throws APIManagementException{ + return delegate.customBackendUpdate(apiId, sequenceInputStream, sequenceDetail, type, securityContext); } @DELETE diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApiService.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApiService.java index d247e8d3af14..d5ff4cb844b8 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApiService.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApiService.java @@ -80,8 +80,8 @@ public interface ApisApiService { public Response createAPI(APIDTO APIDTO, String openAPIVersion, MessageContext messageContext) throws APIManagementException; public Response createAPIRevision(String apiId, APIRevisionDTO apIRevisionDTO, MessageContext messageContext) throws APIManagementException; public Response createNewAPIVersion(String newVersion, String apiId, Boolean defaultVersion, String serviceVersion, MessageContext messageContext) throws APIManagementException; - public Response customBackendDelete(String type, String apiId, String customBackendId, String ifMatch, MessageContext messageContext) throws APIManagementException; - public Response customBackendUpdate(String apiId, String ifMatch, InputStream sequenceInputStream, Attachment sequenceDetail, String type, MessageContext messageContext) throws APIManagementException; + public Response customBackendDelete(String type, String apiId, String customBackendId, MessageContext messageContext) throws APIManagementException; + public Response customBackendUpdate(String apiId, InputStream sequenceInputStream, Attachment sequenceDetail, String type, MessageContext messageContext) throws APIManagementException; public Response deleteAPI(String apiId, String ifMatch, MessageContext messageContext) throws APIManagementException; public Response deleteAPIClientCertificateByAlias(String alias, String apiId, MessageContext messageContext) throws APIManagementException; public Response deleteAPIClientCertificateByKeyTypeAndAlias(String keyType, String alias, String apiId, MessageContext messageContext) throws APIManagementException; diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java index 07271d67f9f4..ed48b86a5e3a 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java @@ -211,7 +211,7 @@ public Response getCustomBackendDataContent(String type, String customBackendId, } @Override - public Response customBackendDelete(String type, String apiId, String customBackendId, String ifMatch, + public Response customBackendDelete(String type, String apiId, String customBackendId, MessageContext messageContext) throws APIManagementException { APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider(); //validate if api exists @@ -256,7 +256,7 @@ public Response getAPI(String apiId, String xWSO2Tenant, String ifNoneMatch, } @Override - public Response customBackendUpdate(String apiId, String ifMatch, InputStream sequenceInputStream, + public Response customBackendUpdate(String apiId, InputStream sequenceInputStream, Attachment sequenceDetail, String type, MessageContext messageContext) throws APIManagementException { String username = RestApiCommonUtil.getLoggedInUsername(); APIProvider apiProvider = RestApiCommonUtil.getProvider(username); diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/resources/publisher-api.yaml b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/resources/publisher-api.yaml index a414167ea3b4..c44472ee5f4b 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/resources/publisher-api.yaml +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/resources/publisher-api.yaml @@ -512,17 +512,6 @@ paths: OK. Requested API Custom Backend is returned headers: - ETag: - description: | - Entity Tag of the response resource. Used by caches, or in conditional requests (Will be supported in future). - schema: - type: string - Last-Modified: - description: | - Date and time the resource has been modifed the last time. - Used by caches, or in conditional requests (Will be supported in future). - schema: - type: string Content-Type: description: | The content type of the body. @@ -578,17 +567,6 @@ paths: OK. Requested API Custom Backend is returned headers: - ETag: - description: | - Entity Tag of the response resource. Used by caches, or in conditional requests (Will be supported in future). - schema: - type: string - Last-Modified: - description: | - Date and time the resource has been modifed the last time. - Used by caches, or in conditional requests (Will be supported in future). - schema: - type: string Content-Type: description: | The content type of the body. @@ -634,7 +612,6 @@ paths: maxLength: 15 type: string - $ref: '#/components/parameters/apiId' - - $ref: '#/components/parameters/If-Match' - $ref: '#/components/parameters/customBackendId' responses: 200: @@ -669,7 +646,6 @@ paths: operationId: customBackendUpdate parameters: - $ref: '#/components/parameters/apiId' - - $ref: '#/components/parameters/If-Match' requestBody: content: multipart/form-data: @@ -688,17 +664,6 @@ paths: OK. Successful response with updated API object headers: - ETag: - description: | - Entity Tag of the response resource. Used by caches, or in conditional requests (Will be supported in future). - schema: - type: string - Last-Modified: - description: | - Date and time the resource has been modifed the last time. - Used by caches, or in conditional requests (Will be supported in future). - schema: - type: string Location: description: | The URL of the newly created resource. From 4af356e620242f15b5925f99cfe0fb7b70f76359 Mon Sep 17 00:00:00 2001 From: BLasan Date: Sat, 14 Sep 2024 16:35:54 +0530 Subject: [PATCH 28/56] Fix: REST API Issues --- .../wso2/carbon/apimgt/api/APIProvider.java | 14 +- .../apimgt/api/model/CustomBackendData.java | 8 +- .../carbon/apimgt/impl/APIProviderImpl.java | 48 +------ .../carbon/apimgt/impl/dao/ApiMgtDAO.java | 64 ++++----- .../impl/dao/constants/SQLConstants.java | 2 +- .../src/main/resources/publisher-api.yaml | 135 ++++++++---------- ...ackendDTO.java => SequenceBackendDTO.java} | 22 +-- .../v1/dto/SequenceBackendListDTO.java | 106 ++++++++++++++ .../v1/common/TemplateBuilderUtil.java | 24 ++-- .../v1/common/mappings/ExportUtils.java | 24 ++-- .../v1/common/mappings/ImportUtils.java | 49 +++---- .../common/mappings/PublisherCommonUtils.java | 14 +- .../apimgt/rest/api/publisher/v1/ApisApi.java | 11 +- .../publisher/v1/impl/ApisApiServiceImpl.java | 25 ++-- .../v1/utils/RestApiPublisherUtils.java | 20 +-- .../src/main/resources/publisher-api.yaml | 135 ++++++++---------- 16 files changed, 341 insertions(+), 360 deletions(-) rename components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/dto/{CustomBackendDTO.java => SequenceBackendDTO.java} (82%) create mode 100644 components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/dto/SequenceBackendListDTO.java diff --git a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java index 61044ed5e333..672a3b14ca0d 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java +++ b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java @@ -69,9 +69,6 @@ Comment getComment(ApiTypeWrapper apiTypeWrapper, String commentId, Integer repl void deleteCustomBackendByID(String backendUUID, String apiUUID, String type) throws APIManagementException; void deleteCustomBackendByAPIID(String apiUUID) throws APIManagementException; - InputStream getCustomBackendSequenceByAPIAndRevisionUUUID(String apiUUID, String revisionUUID, String type) - throws APIManagementException; - /** * @param apiTypeWrapper Api type wrapper * @param parentCommentID @@ -322,20 +319,15 @@ List getSubscriptionsOfAPI(String apiName, String apiVersion, Str */ API updateAPI(API api, API existingAPI) throws APIManagementException, FaultGatewaysException; - void updateCustomBackend(API api, String type, InputStream sequence, String fileName, String customBackendUUID) + void updateCustomBackend(String api, String type, InputStream sequence, String fileName, String customBackendUUID) throws APIManagementException; Map getCustomBackendOfAPIByUUID(String customBackendUUID, String apiUUID, String type, boolean isInfoOnly) throws APIManagementException; - InputStream getCustomBackendSequenceOfAPIByUUID(String apiUUID, String backendUUID, String type) - throws APIManagementException; - - void updateCustomBackendByRevisionID(String apiUUID, String type, String revision, String seqName, - String backendUUID) throws APIManagementException; + String getCustomBackendSequenceOfAPIByUUID(String apiUUID, String type) throws APIManagementException; - void addNewCustomBackendForRevision(String revisionUUID, String updatedBackendUUID, String apiUUID, - Map config) throws APIManagementException; + CustomBackendData getCustomBackendByAPIUUID(String apiUUID, String type) throws APIManagementException; /** * Create a new version of the api, with version newVersion diff --git a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/CustomBackendData.java b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/CustomBackendData.java index c9372a2045bf..ab9576002c28 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/CustomBackendData.java +++ b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/CustomBackendData.java @@ -1,20 +1,18 @@ package org.wso2.carbon.apimgt.api.model; -import java.io.InputStream; - public class CustomBackendData { private String Id; - private InputStream sequence; + private String sequence; private String type; private String name; private String apiUUID; private String revisionUUID; - public InputStream getSequence() { + public String getSequence() { return sequence; } - public void setSequence(InputStream sequence) { + public void setSequence(String sequence) { this.sequence = sequence; } diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java index 3759af3eca71..3757c210e28b 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java @@ -73,6 +73,7 @@ import org.wso2.carbon.apimgt.api.model.BlockConditionsDTO; import org.wso2.carbon.apimgt.api.model.Comment; import org.wso2.carbon.apimgt.api.model.CommentList; +import org.wso2.carbon.apimgt.api.model.CustomBackendData; import org.wso2.carbon.apimgt.api.model.DeployedAPIRevision; import org.wso2.carbon.apimgt.api.model.Documentation; import org.wso2.carbon.apimgt.api.model.Documentation.DocumentSourceType; @@ -1169,9 +1170,9 @@ private void updateAPIPolicies(API api, String tenantDomain) throws APIManagemen } @Override - public void updateCustomBackend(API api, String type, InputStream sequence, String seqName, + public void updateCustomBackend(String apiUUID, String type, InputStream sequence, String seqName, String customBackendUUID) throws APIManagementException { - apiMgtDAO.updateCustomBackend(api.getUuid(), seqName, sequence, type, customBackendUUID); + apiMgtDAO.updateCustomBackend(apiUUID, seqName, sequence, type, customBackendUUID); } @Override @@ -1181,49 +1182,14 @@ public Map getCustomBackendOfAPIByUUID(String customBackendUUID, } @Override - public void updateCustomBackendByRevisionID(String apiUUID, String type, String revision, String seqName, - String backendUUID) throws APIManagementException { - String customBackendUUID = UUID.randomUUID().toString(); - InputStream sequence = apiMgtDAO.getCustomBackendSequenceOfAPIByUUID(backendUUID, apiUUID, type); - apiMgtDAO.updateCustomBackendByRevision(apiUUID, seqName, sequence, type, revision, customBackendUUID); - } - - @Override - public InputStream getCustomBackendSequenceOfAPIByUUID(String apiUUID, String backendUUID, String type) + public String getCustomBackendSequenceOfAPIByUUID(String apiUUID, String type) throws APIManagementException { - return apiMgtDAO.getCustomBackendSequenceOfAPIByUUID(backendUUID, apiUUID, type); - } - - @Override - public InputStream getCustomBackendSequenceByAPIAndRevisionUUUID(String apiUUID, String revisionUUID, - String type) throws APIManagementException { - return apiMgtDAO.getCustomBackendSequenceByAPIandRevisionUUID(apiUUID, revisionUUID, type); + return apiMgtDAO.getCustomBackendSequenceOfAPIByUUID(apiUUID, type); } @Override - public void addNewCustomBackendForRevision(String revisionUUID, String updatedBackendUUID, String apiUUID, - Map config) throws APIManagementException { - if (config.get("production") != null) { - InputStream sequence = apiMgtDAO.getCustomBackendSequenceOfAPIByUUID(updatedBackendUUID, apiUUID, - "PRODUCTION"); - if (config.get("production") instanceof HashMap) { - String seqName = ((HashMap) config.get("production")).get("sequence_name").toString(); - String backendUUID = ((HashMap) config.get("production")).get("sequence_id").toString(); - apiMgtDAO.addNewCustomBackendForAPIRevision(apiUUID, revisionUUID, "PRODUCTION", seqName, sequence, - backendUUID); - } - } - - if (config.get("SANDBOX") != null) { - InputStream sequence = apiMgtDAO.getCustomBackendSequenceOfAPIByUUID(updatedBackendUUID, apiUUID, - "SANDBOX"); - if (config.get("sandbox") instanceof HashMap) { - String seqName = ((HashMap) config.get("sandbox")).get("sequence_name").toString(); - String backendUUID = ((HashMap) config.get("sandbox")).get("sequence_id").toString(); - apiMgtDAO.addNewCustomBackendForAPIRevision(apiUUID, revisionUUID, "SANDBOX", seqName, sequence, - backendUUID); - } - } + public CustomBackendData getCustomBackendByAPIUUID(String apiUUID, String type) throws APIManagementException { + return apiMgtDAO.getCustomBackendByAPIUUID(apiUUID, type); } @Override 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 74efc11ee33c..87fbec3d3b72 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 @@ -11240,50 +11240,50 @@ public Map retrieveCustomBackendOfAPIRevision(String apiUUID, St return map; } - public InputStream getCustomBackendSequenceByAPIandRevisionUUID(String apiUUID, String revisionUUID, String type) throws APIManagementException { - String sqlQuery = SQLConstants.CustomBackendConstants.GET_CUSTOM_BACKEND_FROM_API_AND_REVISION_UUID; - boolean isRev = checkAPIUUIDIsARevisionUUID(apiUUID) != null; - InputStream sequence = null; - try (Connection con = APIMgtDBUtil.getConnection(); - PreparedStatement pstmt = con.prepareStatement(sqlQuery)) { - pstmt.setString(1, apiUUID); - pstmt.setString(2, revisionUUID); - pstmt.setString(3, type); - try (ResultSet rs = pstmt.executeQuery()) { - while(rs.next()) { - sequence = rs.getBinaryStream("SEQUENCE"); + public CustomBackendData getCustomBackendByAPIUUID(String apiUUID, String type) throws APIManagementException { + String sqlQuery = SQLConstants.CustomBackendConstants.GET_API_SPECIFIC_CUSTOM_BACKEND_FROM_SEQUENCE_ID; + CustomBackendData customBackendData = null; + try (Connection con = APIMgtDBUtil.getConnection(); PreparedStatement ps = con.prepareStatement(sqlQuery)) { + ps.setString(1, apiUUID); + ps.setString(2, type); + try (ResultSet rs = ps.executeQuery()) { + while (rs.next()) { + customBackendData = new CustomBackendData(); + customBackendData.setApiUUID(apiUUID); + customBackendData.setRevisionUUID("0"); + customBackendData.setSequence(IOUtils.toString(rs.getBinaryStream("SEQUENCE"))); + customBackendData.setId(rs.getString("ID")); + customBackendData.setName(rs.getString("NAME")); + customBackendData.setType(type); } } - } catch (SQLException ex) { - handleException("Error when retrieving Custom Backend of API: "+apiUUID, ex); + } catch (SQLException | IOException ex) { + handleException("Error when fetching Custom Backend data for API: " + apiUUID, ex); } - return sequence; + return customBackendData; } - public InputStream getCustomBackendSequenceOfAPIByUUID(String backendUUID, String apiUUID, String type) throws APIManagementException { + public String getCustomBackendSequenceOfAPIByUUID(String apiUUID, String type) throws APIManagementException { String sqlQuery = SQLConstants.CustomBackendConstants.GET_API_SPECIFIC_CUSTOM_BACKEND_FROM_SEQUENCE_ID; - boolean isRev = checkAPIUUIDIsARevisionUUID(apiUUID) != null; - ResultSet rs = null; - InputStream sequence = null; - try (Connection con = APIMgtDBUtil.getConnection(); - PreparedStatement ps = con.prepareStatement(sqlQuery)) { - ps.setString(1, backendUUID); - ps.setString(2, apiUUID); - ps.setString(3, type); + String sequence = null; + try (Connection con = APIMgtDBUtil.getConnection(); PreparedStatement ps = con.prepareStatement(sqlQuery)) { + ps.setString(1, apiUUID); + ps.setString(2, type); - rs = ps.executeQuery(); - while(rs.next()) { - try (InputStream in = rs.getBinaryStream("SEQUENCE")) { - sequence = in; - } catch (IOException ex) { - handleException("Error reading the sequence of Custom Backend: "+ backendUUID + ", API: " + apiUUID, ex); + try (ResultSet rs = ps.executeQuery()) { + while (rs.next()) { + try (InputStream in = rs.getBinaryStream("SEQUENCE")) { + sequence = IOUtils.toString(in); + } catch (IOException ex) { + handleException("Error reading the sequence of Custom Backend API: " + apiUUID, ex); + } } } } catch (SQLException ex) { - handleException("Error when fetching Custom Backend data: "+ backendUUID + " of API: " + apiUUID, ex); + handleException("Error when fetching Custom Backend data of API: " + apiUUID, ex); } - if(sequence == null) { + if (sequence == null) { throw new APIManagementException("Custom Backend Content cannot be empty"); } return sequence; 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 ab20fa52f57a..0e8d7c406bb7 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 @@ -4486,7 +4486,7 @@ public static class CustomBackendConstants { 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 = ?"; public static final String GET_CUSTOM_BACKEND_OF_API_DEFAULT_REVISION = "SELECT ACB.NAME, ACB.TYPE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ? AND REVISION_UUID = '0'"; public static final String GET_REVISION_SPECIFIC_CUSTOM_BACKEND_FROM_SEQUENCE_ID = "SELECT ACB.ID, ACB.NAME, ACB.SEQUENCE, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.ID = ? AND ACB.REVISION_UUID = ? AND ACB.TYPE = ?"; - public static final String GET_API_SPECIFIC_CUSTOM_BACKEND_FROM_SEQUENCE_ID = "SELECT ACB.ID, ACB.NAME, ACB.SEQUENCE, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.ID = ? AND ACB.API_UUID = ? AND ACB.REVISION_UUID = '0' AND ACB.TYPE = ?"; + public static final String GET_API_SPECIFIC_CUSTOM_BACKEND_FROM_SEQUENCE_ID = "SELECT ACB.ID, ACB.NAME, ACB.SEQUENCE, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.API_UUID = ? AND ACB.REVISION_UUID = '0' AND ACB.TYPE = ?"; public static final String GET_CUSTOM_BACKEND_FROM_API_AND_REVISION_UUID = "SELECT ACB.ID, ACB.NAME, ACB.SEQUENCE, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.API_UUID = ? AND ACB.REVISION_UUID = ? AND ACB.TYPE = ?"; public static final String GET_ALL_API_SPECIFIC_CUSTOM_BACKENDS = "SELECT ACB.ID, ACB.NAME, ACB.SEQUENCE, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.API_UUID = ? AND ACB.REVISION_UUID = '0'"; public static final String GET_REVISION_SPECIFIC_CUSTOM_BACKEND_META_DATA_FROM_SEQUENCE_ID = "SELECT ACB.ID, ACB.NAME, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.ID = ? AND ACB.REVISION_UUID = ?"; diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml b/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml index c44472ee5f4b..b70f2c4ea170 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml @@ -487,13 +487,13 @@ paths: -H "Content-Type: application/json" -d @data.json "https://127.0.0.1:9443/api/am/publisher/v4/apis/7a2298c4-c905-403f-8fac-38c73301631f/topics"' - /apis/{apiId}/custom-backend/{customBackendId}/content: + /apis/{apiId}/sequence-backend/{type}/content: get: tags: - APIs summary: Get Sequence of Custom Backend description: This operation can be used to get Sequence of the Custom Backend - operationId: getCustomBackendDataContent + operationId: getSequenceBackendContent parameters: - name: type in: query @@ -504,7 +504,6 @@ paths: schema: maxLength: 15 type: string - - $ref: '#/components/parameters/customBackendId' - $ref: '#/components/parameters/apiId' responses: 200: @@ -518,15 +517,10 @@ paths: schema: type: string content: - application/zip: + application/xml: schema: type: string format: binary - 304: - description: | - Not Modified. - Empty body because the client has already the latest version of the requested resource (Will be supported in future). - content: {} 404: $ref: '#/components/responses/NotFound' 406: @@ -542,16 +536,16 @@ paths: source: 'curl -k -H "Authorization: Bearer ae4eae22-3f65-387b-a171-d37eaa366fa8" "https://127.0.0.1:9443/api/am/publisher/v4/apis/7a2298c4-c905-403f-8fac-38c73301631f/custom-backend/7a2298c4-c915-483f-8fac-38c73301631f/content"' - /apis/{apiId}/custom-backend/{customBackendId}: - get: + /apis/{apiId}/sequence-backend/{type}: + delete: tags: - APIs - summary: Get Custom Backend of the API - description: This operation can be used to get Custom Backend data of the API - operationId: getCustomBackendData + summary: Delete Sequence Backend of the API + description: This operation can be used to remove the Sequence Backend of the API + operationId: sequenceBackendDelete parameters: - name: type - in: query + in: path description: | Type of the Endpoint. SANDBOX or PRODUCTION @@ -559,91 +553,75 @@ paths: schema: maxLength: 15 type: string - - $ref: '#/components/parameters/customBackendId' - $ref: '#/components/parameters/apiId' responses: 200: description: | OK. - Requested API Custom Backend is returned - headers: - Content-Type: - description: | - The content type of the body. - schema: - type: string - content: - application/json: - schema: - $ref: '#/components/schemas/CustomBackend' - 304: - description: | - Not Modified. - Empty body because the client has already the latest version of the requested resource (Will be supported in future). - content: {} + Resource successfully deleted. + content: { } + 403: + $ref: '#/components/responses/Forbidden' 404: $ref: '#/components/responses/NotFound' - 406: - $ref: '#/components/responses/NotAcceptable' + 409: + $ref: '#/components/responses/Conflict' + 412: + $ref: '#/components/responses/PreconditionFailed' security: - OAuth2Security: - - apim:api_view + - apim:api_delete - apim:api_manage - apim:api_import_export - - apim:api_product_import_export x-code-samples: - lang: Curl - source: 'curl -k -H "Authorization: Bearer ae4eae22-3f65-387b-a171-d37eaa366fa8" + source: 'curl -k -X DELETE -H "Authorization: Bearer ae4eae22-3f65-387b-a171-d37eaa366fa8" "https://127.0.0.1:9443/api/am/publisher/v4/apis/7a2298c4-c905-403f-8fac-38c73301631f/custom-backend/7a2298c4-c915-483f-8fac-38c73301631f"' - delete: + + /apis/{apiId}/sequence-backend: + get: tags: - APIs - summary: Delete Custom Backend of the API - description: This operation can be used to remove the Custom Backend of the API - operationId: customBackendDelete + summary: Get Sequence Backends of the API + description: This operation can be used to get Sequence Backend data of the API + operationId: getSequenceBackendData parameters: - - name: type - in: query - description: | - Type of the Endpoint. - SANDBOX or PRODUCTION - required: true - schema: - maxLength: 15 - type: string - $ref: '#/components/parameters/apiId' - - $ref: '#/components/parameters/customBackendId' responses: 200: description: | OK. - Resource successfully deleted. - content: { } - 403: - $ref: '#/components/responses/Forbidden' + Requested API Sequence Backend is returned + headers: + Content-Type: + description: | + The content type of the body. + schema: + type: string + content: + application/json: + schema: + $ref: '#/components/schemas/SequenceBackendList' 404: $ref: '#/components/responses/NotFound' - 409: - $ref: '#/components/responses/Conflict' - 412: - $ref: '#/components/responses/PreconditionFailed' + 406: + $ref: '#/components/responses/NotAcceptable' security: - OAuth2Security: - - apim:api_delete + - apim:api_view - apim:api_manage - apim:api_import_export + - apim:api_product_import_export x-code-samples: - lang: Curl - source: 'curl -k -X DELETE -H "Authorization: Bearer ae4eae22-3f65-387b-a171-d37eaa366fa8" + source: 'curl -k -H "Authorization: Bearer ae4eae22-3f65-387b-a171-d37eaa366fa8" "https://127.0.0.1:9443/api/am/publisher/v4/apis/7a2298c4-c905-403f-8fac-38c73301631f/custom-backend/7a2298c4-c915-483f-8fac-38c73301631f"' - - /apis/{apiId}/custom-backend: put: tags: - APIs - summary: Upload Custom Sequence as the Endpoint of the API - description: This operation can be used to change the endpoint of the API to Custom Sequence - operationId: customBackendUpdate + summary: Upload Sequence Sequence as the Endpoint of the API + description: This operation can be used to change the endpoint of the API to Sequence Backend + operationId: sequenceBackendUpdate parameters: - $ref: '#/components/parameters/apiId' requestBody: @@ -9570,8 +9548,21 @@ paths: -H "Content-Type: application/json" -d @data.json "https://127.0.0.1:9443/api/am/admin/v4/workflows/update-workflow-status?workflowReferenceId=56e3a170-a7a7-45f8-b051-7e43a58a67e1"' components: schemas: - CustomBackend: - title: Custom Backend + SequenceBackendList: + title: Sequence Backend List + type: object + properties: + count: + type: integer + description: | + Number of Sequence Backends returned. + example: 1 + list: + type: array + items: + $ref: '#/components/schemas/SequenceBackend' + SequenceBackend: + title: Sequence Backend type: object properties: sequenceId: @@ -13918,14 +13909,6 @@ components: schema: type: integer default: 0 - customBackendId: - name: customBackendId - in: path - description: | - Custom Backend ID - required: true - schema: - type: string commentId: name: commentId in: path diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/dto/CustomBackendDTO.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/dto/SequenceBackendDTO.java similarity index 82% rename from components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/dto/CustomBackendDTO.java rename to components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/dto/SequenceBackendDTO.java index 395cb3e890e2..39b78888d961 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/dto/CustomBackendDTO.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/dto/SequenceBackendDTO.java @@ -19,7 +19,7 @@ -public class CustomBackendDTO { +public class SequenceBackendDTO { private String sequenceId = null; private String sequenceName = null; @@ -28,7 +28,7 @@ public class CustomBackendDTO { /** **/ - public CustomBackendDTO sequenceId(String sequenceId) { + public SequenceBackendDTO sequenceId(String sequenceId) { this.sequenceId = sequenceId; return this; } @@ -45,7 +45,7 @@ public void setSequenceId(String sequenceId) { /** **/ - public CustomBackendDTO sequenceName(String sequenceName) { + public SequenceBackendDTO sequenceName(String sequenceName) { this.sequenceName = sequenceName; return this; } @@ -62,7 +62,7 @@ public void setSequenceName(String sequenceName) { /** **/ - public CustomBackendDTO sequenceType(String sequenceType) { + public SequenceBackendDTO sequenceType(String sequenceType) { this.sequenceType = sequenceType; return this; } @@ -79,7 +79,7 @@ public void setSequenceType(String sequenceType) { /** **/ - public CustomBackendDTO sequence(File sequence) { + public SequenceBackendDTO sequence(File sequence) { this.sequence = sequence; return this; } @@ -103,11 +103,11 @@ public boolean equals(java.lang.Object o) { if (o == null || getClass() != o.getClass()) { return false; } - CustomBackendDTO customBackend = (CustomBackendDTO) o; - return Objects.equals(sequenceId, customBackend.sequenceId) && - Objects.equals(sequenceName, customBackend.sequenceName) && - Objects.equals(sequenceType, customBackend.sequenceType) && - Objects.equals(sequence, customBackend.sequence); + SequenceBackendDTO sequenceBackend = (SequenceBackendDTO) o; + return Objects.equals(sequenceId, sequenceBackend.sequenceId) && + Objects.equals(sequenceName, sequenceBackend.sequenceName) && + Objects.equals(sequenceType, sequenceBackend.sequenceType) && + Objects.equals(sequence, sequenceBackend.sequence); } @Override @@ -118,7 +118,7 @@ public int hashCode() { @Override public String toString() { StringBuilder sb = new StringBuilder(); - sb.append("class CustomBackendDTO {\n"); + sb.append("class SequenceBackendDTO {\n"); sb.append(" sequenceId: ").append(toIndentedString(sequenceId)).append("\n"); sb.append(" sequenceName: ").append(toIndentedString(sequenceName)).append("\n"); diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/dto/SequenceBackendListDTO.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/dto/SequenceBackendListDTO.java new file mode 100644 index 000000000000..ef33c302c048 --- /dev/null +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/dto/SequenceBackendListDTO.java @@ -0,0 +1,106 @@ +package org.wso2.carbon.apimgt.rest.api.publisher.v1.dto; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonCreator; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import java.util.ArrayList; +import java.util.List; +import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.SequenceBackendDTO; +import javax.validation.constraints.*; + + +import io.swagger.annotations.*; +import java.util.Objects; + +import javax.xml.bind.annotation.*; +import org.wso2.carbon.apimgt.rest.api.common.annotations.Scope; +import com.fasterxml.jackson.annotation.JsonCreator; + +import javax.validation.Valid; + + + +public class SequenceBackendListDTO { + + private Integer count = null; + private List list = new ArrayList(); + + /** + * Number of Sequence Backends returned. + **/ + public SequenceBackendListDTO count(Integer count) { + this.count = count; + return this; + } + + + @ApiModelProperty(example = "1", value = "Number of Sequence Backends returned. ") + @JsonProperty("count") + public Integer getCount() { + return count; + } + public void setCount(Integer count) { + this.count = count; + } + + /** + **/ + public SequenceBackendListDTO list(List list) { + this.list = list; + return this; + } + + + @ApiModelProperty(value = "") + @Valid + @JsonProperty("list") + public List getList() { + return list; + } + public void setList(List list) { + this.list = list; + } + + + @Override + public boolean equals(java.lang.Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + SequenceBackendListDTO sequenceBackendList = (SequenceBackendListDTO) o; + return Objects.equals(count, sequenceBackendList.count) && + Objects.equals(list, sequenceBackendList.list); + } + + @Override + public int hashCode() { + return Objects.hash(count, list); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("class SequenceBackendListDTO {\n"); + + sb.append(" count: ").append(toIndentedString(count)).append("\n"); + sb.append(" list: ").append(toIndentedString(list)).append("\n"); + sb.append("}"); + return sb.toString(); + } + + /** + * Convert the given object to string with each line indented by 4 spaces + * (except the first line). + */ + private String toIndentedString(java.lang.Object o) { + if (o == null) { + return "null"; + } + return o.toString().replace("\n", "\n "); + } +} + 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/TemplateBuilderUtil.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/TemplateBuilderUtil.java index 435ac7a3e2e2..ade6b6e57438 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/TemplateBuilderUtil.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/TemplateBuilderUtil.java @@ -748,21 +748,17 @@ 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())) { - if (endpointConfigMap.get("sandbox") != null) { - GatewayContentDTO gatewayCustomBackendSequenceDTO = retrieveCustomBackendSequence(api, "SANDBOX", - extractedPath); - if (gatewayCustomBackendSequenceDTO != null) { - gatewayAPIDTO.setSequenceToBeAdd(addGatewayContentToList(gatewayCustomBackendSequenceDTO, - gatewayAPIDTO.getSequenceToBeAdd())); - } + GatewayContentDTO gatewayCustomBackendSequenceDTO = retrieveCustomBackendSequence(api, + APIConstants.API_KEY_TYPE_SANDBOX, extractedPath); + if (gatewayCustomBackendSequenceDTO != null) { + gatewayAPIDTO.setSequenceToBeAdd(addGatewayContentToList(gatewayCustomBackendSequenceDTO, + gatewayAPIDTO.getSequenceToBeAdd())); } - if (endpointConfigMap.get("production") != null) { - GatewayContentDTO gatewayCustomBackendSequenceDTO = retrieveCustomBackendSequence(api, "PRODUCTION", - extractedPath); - if (gatewayCustomBackendSequenceDTO != null) { - gatewayAPIDTO.setSequenceToBeAdd(addGatewayContentToList(gatewayCustomBackendSequenceDTO, - gatewayAPIDTO.getSequenceToBeAdd())); - } + gatewayCustomBackendSequenceDTO = retrieveCustomBackendSequence(api, + APIConstants.API_KEY_TYPE_PRODUCTION, extractedPath); + if (gatewayCustomBackendSequenceDTO != null) { + gatewayAPIDTO.setSequenceToBeAdd(addGatewayContentToList(gatewayCustomBackendSequenceDTO, + gatewayAPIDTO.getSequenceToBeAdd())); } } } 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/ExportUtils.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/ExportUtils.java index 609985d491c7..fd9668dd3459 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/ExportUtils.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/ExportUtils.java @@ -46,6 +46,7 @@ import org.wso2.carbon.apimgt.api.model.APIProductIdentifier; import org.wso2.carbon.apimgt.api.model.APIRevision; import org.wso2.carbon.apimgt.api.model.APIRevisionDeployment; +import org.wso2.carbon.apimgt.api.model.CustomBackendData; import org.wso2.carbon.apimgt.api.model.Documentation; import org.wso2.carbon.apimgt.api.model.DocumentationContent; import org.wso2.carbon.apimgt.api.model.Identifier; @@ -639,21 +640,18 @@ public static void addEndpointCertificatesToArchive(String archivePath, APIDTO a public static void addCustomBackendToArchive(String archivePath, APIProvider apiProvider, String apiUUID, JsonObject endpointConfig) throws APIManagementException { try { - JsonElement prodElement = endpointConfig.get("production"); CommonUtil.createDirectory(archivePath + File.separator + ImportExportConstants.CUSTOM_BACKEND_DIRECTORY); - if (prodElement != null) { - String backendUUID = prodElement.getAsJsonObject().get("sequence_id").getAsString(); - String sequenceName = prodElement.getAsJsonObject().get("sequence_name").getAsString(); - InputStream sequence = apiProvider.getCustomBackendSequenceOfAPIByUUID(apiUUID, backendUUID, - "PRODUCTION"); - exportCustomBackend(sequenceName, IOUtils.toString(sequence), archivePath); + + // Add production Backend Sequences + CustomBackendData data = apiProvider.getCustomBackendByAPIUUID(apiUUID, APIConstants.API_KEY_TYPE_PRODUCTION); + if(data != null) { + exportCustomBackend(data.getName(), data.getSequence(), archivePath); } - JsonElement sandElement = endpointConfig.get("sandbox"); - if (sandElement != null) { - String backendUUID = sandElement.getAsJsonObject().get("sequence_id").getAsString(); - String sequenceName = sandElement.getAsJsonObject().get("sequence_name").getAsString(); - InputStream sequence = apiProvider.getCustomBackendSequenceOfAPIByUUID(apiUUID, backendUUID, "SANDBOX"); - exportCustomBackend(sequenceName, IOUtils.toString(sequence), archivePath); + + // Add sandbox Backend Sequences + data = apiProvider.getCustomBackendByAPIUUID(apiUUID, APIConstants.API_KEY_TYPE_SANDBOX); + if(data != null) { + exportCustomBackend(data.getName(), data.getSequence(), archivePath); } } catch (IOException | APIImportExportException ex) { 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 e8c7b5ba659c..5a49cf281e44 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 @@ -696,39 +696,24 @@ public static void updateAPIWithCustomBackend(API api, String extractedFolderPat 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)); + + if (endpointConfig != null) { + if (endpointConfig.get("sandbox") != null) { + JsonObject sandboxConf = endpointConfig.get("sandbox").getAsJsonObject(); + String seqFile = sandboxConf.get("sequence_file").getAsString(); + String seqName = APIUtil.getCustomBackendName(api.getUuid(), APIConstants.API_KEY_TYPE_SANDBOX); + String seqId = UUID.randomUUID().toString(); + InputStream seq = APIUtil.getCustomBackendSequence(customBackendDir, seqFile, ".xml"); + apiProvider.updateCustomBackend(api.getUuid(), APIConstants.API_KEY_TYPE_SANDBOX, seq, seqName, seqId); + } + if (endpointConfig.get("production") != null) { + JsonObject prodConf = endpointConfig.get("production").getAsJsonObject(); + String seqFile = prodConf.get("sequence_file").getAsString(); + String seqName = APIUtil.getCustomBackendName(api.getUuid(), APIConstants.API_KEY_TYPE_PRODUCTION); + String seqId = UUID.randomUUID().toString(); + InputStream seq = APIUtil.getCustomBackendSequence(customBackendDir, seqFile, ".xml"); + apiProvider.updateCustomBackend(api.getUuid(), APIConstants.API_KEY_TYPE_PRODUCTION, seq, seqName, seqId); } - } catch (IOException ex) { - throw new APIManagementException("Error when updating Endpoint Configuration of API: " + api.getUuid()); } } 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 6288b24dcfa8..2f6027782960 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 @@ -29,7 +29,6 @@ import graphql.schema.validation.SchemaValidationError; import graphql.schema.validation.SchemaValidator; import io.swagger.v3.parser.ObjectMapperFactory; -import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; @@ -99,11 +98,9 @@ import org.wso2.carbon.core.util.CryptoUtil; import org.wso2.carbon.utils.multitenancy.MultitenantConstants; -import java.io.File; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Field; -import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -207,7 +204,7 @@ public static API updateApi(API originalAPI, APIDTO apiDtoToUpdate, APIProvider * @return Custom Backend File Name * @throws APIManagementException If an error occurs while updating the API and API definition */ - public static JSONObject updateCustomBackend(API api, APIProvider apiProvider, String endpointType, + public static void updateCustomBackend(API api, APIProvider apiProvider, String endpointType, InputStream customBackend, String contentDecomp) throws APIManagementException { String fileName = getFileNameFromContentDisposition(contentDecomp); if (fileName == null) { @@ -216,14 +213,7 @@ public static JSONObject updateCustomBackend(API api, APIProvider apiProvider, S } String seqName = APIUtil.getCustomBackendName(api.getUuid(), endpointType); String customBackendUUID = UUID.randomUUID().toString(); - apiProvider.updateCustomBackend(api, endpointType, customBackend, seqName, customBackendUUID); - JSONObject obj = new JSONObject(); - obj.put("sequence_name", seqName); - obj.put("sequence_file", fileName); - obj.put("sequence_id", customBackendUUID); - obj.put("type", endpointType); - obj.put("endpoint_type", "custom_backend"); - return obj; + apiProvider.updateCustomBackend(api.getUuid(), endpointType, customBackend, seqName, customBackendUUID); } private static String getFileNameFromContentDisposition(String contentDisposition) { diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApi.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApi.java index 9f3bd4e5a335..5a7f1424c980 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApi.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApi.java @@ -18,7 +18,6 @@ import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ClientCertificatesDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CommentDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CommentListDTO; -import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CustomBackendDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.DocumentDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.DocumentListDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ErrorDTO; @@ -41,20 +40,18 @@ import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ResourcePathListDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ResourcePolicyInfoDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ResourcePolicyListDTO; +import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.SequenceBackendDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ThrottlingPolicyDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.TopicListDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.WSDLInfoDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.WSDLValidationResponseDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.WorkflowResponseDTO; -import org.wso2.carbon.apimgt.rest.api.publisher.v1.ApisApiService; import org.wso2.carbon.apimgt.rest.api.publisher.v1.impl.ApisApiServiceImpl; import org.wso2.carbon.apimgt.api.APIManagementException; import javax.ws.rs.*; import javax.ws.rs.core.Context; import javax.ws.rs.core.Response; -import javax.ws.rs.core.SecurityContext; -import javax.inject.Inject; import io.swagger.annotations.*; import java.io.InputStream; @@ -63,8 +60,6 @@ import org.apache.cxf.jaxrs.ext.multipart.Attachment; import org.apache.cxf.jaxrs.ext.multipart.Multipart; -import java.util.Map; -import java.util.List; import javax.validation.constraints.*; @Path("/apis") @@ -1336,7 +1331,7 @@ public Response getAuditReportOfAPI(@ApiParam(value = "**API ID** consisting of @Path("/{apiId}/custom-backend/{customBackendId}") @Produces({ "application/json" }) - @ApiOperation(value = "Get Custom Backend of the API", notes = "This operation can be used to get Custom Backend data of the API", response = CustomBackendDTO.class, authorizations = { + @ApiOperation(value = "Get Custom Backend of the API", notes = "This operation can be used to get Custom Backend data of the API", response = SequenceBackendDTO.class, authorizations = { @Authorization(value = "OAuth2Security", scopes = { @AuthorizationScope(scope = "apim:api_view", description = "View API"), @AuthorizationScope(scope = "apim:api_manage", description = "Manage all API related operations"), @@ -1345,7 +1340,7 @@ public Response getAuditReportOfAPI(@ApiParam(value = "**API ID** consisting of }) }, tags={ "APIs", }) @ApiResponses(value = { - @ApiResponse(code = 200, message = "OK. Requested API Custom Backend is returned ", response = CustomBackendDTO.class), + @ApiResponse(code = 200, message = "OK. Requested API Custom Backend is returned ", response = SequenceBackendDTO.class), @ApiResponse(code = 304, message = "Not Modified. Empty body because the client has already the latest version of the requested resource (Will be supported in future). ", response = Void.class), @ApiResponse(code = 404, message = "Not Found. The specified resource does not exist.", response = ErrorDTO.class), @ApiResponse(code = 406, message = "Not Acceptable. The requested media type is not supported.", response = ErrorDTO.class) }) diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java index ed48b86a5e3a..e920cd328210 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java @@ -175,19 +175,15 @@ public Response getCustomBackendData(String type, String customBackendId, String //validate if api exists CommonUtils.validateAPIExistence(apiId); - - Map endpointConfig = apiProvider.getCustomBackendOfAPIByUUID(customBackendId, apiId, type, - false); - - if (endpointConfig == null) { + CustomBackendData data = apiProvider.getCustomBackendByAPIUUID(apiId, type); + if (data == null) { throw new APIMgtResourceNotFoundException( "Couldn't retrieve an existing Custom Backend with ID: " + customBackendId + " for API " + apiId, ExceptionCodes.from(ExceptionCodes.CUSTOM_BACKEND_NOT_FOUND, customBackendId)); } - CustomBackendDTO backendDTO = new CustomBackendDTO(); - backendDTO.setSequenceName(endpointConfig.get("sequence_name").toString()); - backendDTO.setSequenceId(endpointConfig.get("sequence_id").toString()); - backendDTO.setSequenceType(endpointConfig.get("type").toString()); + SequenceBackendDTO backendDTO = new SequenceBackendDTO(); + backendDTO.setSequenceName(data.getName()); + backendDTO.setSequenceId(data.getId()); return Response.ok().entity(backendDTO).build(); } @@ -197,14 +193,13 @@ public Response getCustomBackendDataContent(String type, String customBackendId, APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider(); CommonUtils.validateAPIExistence(apiId); - InputStream seq = apiProvider.getCustomBackendSequenceOfAPIByUUID(apiId, customBackendId, type); - if (seq == null) { + CustomBackendData data = apiProvider.getCustomBackendByAPIUUID(apiId, type); + if (data == null) { throw new APIMgtResourceNotFoundException( "Couldn't retrieve an existing Custom Backend with ID: " + customBackendId + " for API " + apiId, ExceptionCodes.from(ExceptionCodes.CUSTOM_BACKEND_NOT_FOUND, customBackendId)); } - String seqName = APIUtil.getCustomBackendName(apiId, type); - File file = RestApiPublisherUtils.exportCustomBackendData(seq, seqName); + File file = RestApiPublisherUtils.exportCustomBackendData(data.getSequence(), data.getName()); return Response.ok(file) .header(RestApiConstants.HEADER_CONTENT_DISPOSITION, "attachment; filename=\"" + file.getName() + "\"") .build(); @@ -266,9 +261,9 @@ public Response customBackendUpdate(String apiId, InputStream sequenceInputStrea MultivaluedMap headers = sequenceDetail.getHeaders(); String contentDecomp = headers.getFirst("Content-Disposition"); - JSONObject resp = PublisherCommonUtils.updateCustomBackend(api, apiProvider, type, sequenceInputStream, + PublisherCommonUtils.updateCustomBackend(api, apiProvider, type, sequenceInputStream, contentDecomp); - return Response.ok().entity(resp).build(); + return Response.ok().build(); } @Override diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/utils/RestApiPublisherUtils.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/utils/RestApiPublisherUtils.java index d311f4981909..d03f0c548703 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/utils/RestApiPublisherUtils.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/utils/RestApiPublisherUtils.java @@ -277,20 +277,14 @@ public static String getContentType(Attachment fileDetail) { return fileContentType; } - public static File exportCustomBackendData(InputStream seq, String seqName) throws APIManagementException { - File exportFolder = null; + public static File exportCustomBackendData(String seq, String seqName) throws APIManagementException { try { - exportFolder = CommonUtil.createTempDirectoryFromName(seqName + "_" + "Custom-Backend"); - String exportCustomBackendBasePath = exportFolder.toString(); - String archivePath = exportCustomBackendBasePath.concat(File.separator + seqName); - CommonUtil.createDirectory(archivePath); - String customBackendName = - archivePath + File.separator + seqName + APIConstants.SYNAPSE_POLICY_DEFINITION_EXTENSION_XML; - CommonUtil.writeFile(customBackendName, IOUtils.toString(seq)); - CommonUtil.archiveDirectory(exportCustomBackendBasePath); - FileUtils.deleteQuietly(new File(exportCustomBackendBasePath)); - return new File(exportCustomBackendBasePath + APIConstants.ZIP_FILE_EXTENSION); - } catch (APIImportExportException | IOException ex) { + // Provided Sequence Name by the user + String customBackendName = seqName + APIConstants.SYNAPSE_POLICY_DEFINITION_EXTENSION_XML; + CommonUtil.writeFile(customBackendName, seq); + FileUtils.deleteQuietly(new File(customBackendName)); + return new File(customBackendName); + } catch (APIImportExportException ex) { throw new APIManagementException("Error when exporting Custom Backend: " + seqName, ex); } } diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/resources/publisher-api.yaml b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/resources/publisher-api.yaml index c44472ee5f4b..b70f2c4ea170 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/resources/publisher-api.yaml +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/resources/publisher-api.yaml @@ -487,13 +487,13 @@ paths: -H "Content-Type: application/json" -d @data.json "https://127.0.0.1:9443/api/am/publisher/v4/apis/7a2298c4-c905-403f-8fac-38c73301631f/topics"' - /apis/{apiId}/custom-backend/{customBackendId}/content: + /apis/{apiId}/sequence-backend/{type}/content: get: tags: - APIs summary: Get Sequence of Custom Backend description: This operation can be used to get Sequence of the Custom Backend - operationId: getCustomBackendDataContent + operationId: getSequenceBackendContent parameters: - name: type in: query @@ -504,7 +504,6 @@ paths: schema: maxLength: 15 type: string - - $ref: '#/components/parameters/customBackendId' - $ref: '#/components/parameters/apiId' responses: 200: @@ -518,15 +517,10 @@ paths: schema: type: string content: - application/zip: + application/xml: schema: type: string format: binary - 304: - description: | - Not Modified. - Empty body because the client has already the latest version of the requested resource (Will be supported in future). - content: {} 404: $ref: '#/components/responses/NotFound' 406: @@ -542,16 +536,16 @@ paths: source: 'curl -k -H "Authorization: Bearer ae4eae22-3f65-387b-a171-d37eaa366fa8" "https://127.0.0.1:9443/api/am/publisher/v4/apis/7a2298c4-c905-403f-8fac-38c73301631f/custom-backend/7a2298c4-c915-483f-8fac-38c73301631f/content"' - /apis/{apiId}/custom-backend/{customBackendId}: - get: + /apis/{apiId}/sequence-backend/{type}: + delete: tags: - APIs - summary: Get Custom Backend of the API - description: This operation can be used to get Custom Backend data of the API - operationId: getCustomBackendData + summary: Delete Sequence Backend of the API + description: This operation can be used to remove the Sequence Backend of the API + operationId: sequenceBackendDelete parameters: - name: type - in: query + in: path description: | Type of the Endpoint. SANDBOX or PRODUCTION @@ -559,91 +553,75 @@ paths: schema: maxLength: 15 type: string - - $ref: '#/components/parameters/customBackendId' - $ref: '#/components/parameters/apiId' responses: 200: description: | OK. - Requested API Custom Backend is returned - headers: - Content-Type: - description: | - The content type of the body. - schema: - type: string - content: - application/json: - schema: - $ref: '#/components/schemas/CustomBackend' - 304: - description: | - Not Modified. - Empty body because the client has already the latest version of the requested resource (Will be supported in future). - content: {} + Resource successfully deleted. + content: { } + 403: + $ref: '#/components/responses/Forbidden' 404: $ref: '#/components/responses/NotFound' - 406: - $ref: '#/components/responses/NotAcceptable' + 409: + $ref: '#/components/responses/Conflict' + 412: + $ref: '#/components/responses/PreconditionFailed' security: - OAuth2Security: - - apim:api_view + - apim:api_delete - apim:api_manage - apim:api_import_export - - apim:api_product_import_export x-code-samples: - lang: Curl - source: 'curl -k -H "Authorization: Bearer ae4eae22-3f65-387b-a171-d37eaa366fa8" + source: 'curl -k -X DELETE -H "Authorization: Bearer ae4eae22-3f65-387b-a171-d37eaa366fa8" "https://127.0.0.1:9443/api/am/publisher/v4/apis/7a2298c4-c905-403f-8fac-38c73301631f/custom-backend/7a2298c4-c915-483f-8fac-38c73301631f"' - delete: + + /apis/{apiId}/sequence-backend: + get: tags: - APIs - summary: Delete Custom Backend of the API - description: This operation can be used to remove the Custom Backend of the API - operationId: customBackendDelete + summary: Get Sequence Backends of the API + description: This operation can be used to get Sequence Backend data of the API + operationId: getSequenceBackendData parameters: - - name: type - in: query - description: | - Type of the Endpoint. - SANDBOX or PRODUCTION - required: true - schema: - maxLength: 15 - type: string - $ref: '#/components/parameters/apiId' - - $ref: '#/components/parameters/customBackendId' responses: 200: description: | OK. - Resource successfully deleted. - content: { } - 403: - $ref: '#/components/responses/Forbidden' + Requested API Sequence Backend is returned + headers: + Content-Type: + description: | + The content type of the body. + schema: + type: string + content: + application/json: + schema: + $ref: '#/components/schemas/SequenceBackendList' 404: $ref: '#/components/responses/NotFound' - 409: - $ref: '#/components/responses/Conflict' - 412: - $ref: '#/components/responses/PreconditionFailed' + 406: + $ref: '#/components/responses/NotAcceptable' security: - OAuth2Security: - - apim:api_delete + - apim:api_view - apim:api_manage - apim:api_import_export + - apim:api_product_import_export x-code-samples: - lang: Curl - source: 'curl -k -X DELETE -H "Authorization: Bearer ae4eae22-3f65-387b-a171-d37eaa366fa8" + source: 'curl -k -H "Authorization: Bearer ae4eae22-3f65-387b-a171-d37eaa366fa8" "https://127.0.0.1:9443/api/am/publisher/v4/apis/7a2298c4-c905-403f-8fac-38c73301631f/custom-backend/7a2298c4-c915-483f-8fac-38c73301631f"' - - /apis/{apiId}/custom-backend: put: tags: - APIs - summary: Upload Custom Sequence as the Endpoint of the API - description: This operation can be used to change the endpoint of the API to Custom Sequence - operationId: customBackendUpdate + summary: Upload Sequence Sequence as the Endpoint of the API + description: This operation can be used to change the endpoint of the API to Sequence Backend + operationId: sequenceBackendUpdate parameters: - $ref: '#/components/parameters/apiId' requestBody: @@ -9570,8 +9548,21 @@ paths: -H "Content-Type: application/json" -d @data.json "https://127.0.0.1:9443/api/am/admin/v4/workflows/update-workflow-status?workflowReferenceId=56e3a170-a7a7-45f8-b051-7e43a58a67e1"' components: schemas: - CustomBackend: - title: Custom Backend + SequenceBackendList: + title: Sequence Backend List + type: object + properties: + count: + type: integer + description: | + Number of Sequence Backends returned. + example: 1 + list: + type: array + items: + $ref: '#/components/schemas/SequenceBackend' + SequenceBackend: + title: Sequence Backend type: object properties: sequenceId: @@ -13918,14 +13909,6 @@ components: schema: type: integer default: 0 - customBackendId: - name: customBackendId - in: path - description: | - Custom Backend ID - required: true - schema: - type: string commentId: name: commentId in: path From 5b573487d6f29bda536ae47841f755ed74c416fc Mon Sep 17 00:00:00 2001 From: BLasan Date: Sat, 14 Sep 2024 22:59:48 +0530 Subject: [PATCH 29/56] Fix: Checkstyle Issues --- .../wso2/carbon/apimgt/api/APIProvider.java | 6 +- .../carbon/apimgt/api/ExceptionCodes.java | 4 +- ...kendData.java => SequenceBackendData.java} | 2 +- .../carbon/apimgt/impl/APIProviderImpl.java | 13 +- .../carbon/apimgt/impl/dao/ApiMgtDAO.java | 51 +++-- .../impl/dao/constants/SQLConstants.java | 7 +- .../v1/common/mappings/APIMappingUtil.java | 19 ++ .../v1/common/mappings/ExportUtils.java | 16 +- .../v1/common/mappings/ImportUtils.java | 4 +- .../common/mappings/PublisherCommonUtils.java | 1 - .../apimgt/rest/api/publisher/v1/ApisApi.java | 175 +++++++++--------- .../rest/api/publisher/v1/ApisApiService.java | 10 +- .../publisher/v1/impl/ApisApiServiceImpl.java | 31 ++-- 13 files changed, 188 insertions(+), 151 deletions(-) rename components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/{CustomBackendData.java => SequenceBackendData.java} (96%) diff --git a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java index 672a3b14ca0d..4d84d5aa6cb6 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java +++ b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java @@ -66,7 +66,7 @@ public interface APIProvider extends APIManager { Comment getComment(ApiTypeWrapper apiTypeWrapper, String commentId, Integer replyLimit, Integer replyOffset) throws APIManagementException; - void deleteCustomBackendByID(String backendUUID, String apiUUID, String type) throws APIManagementException; + void deleteCustomBackendByID(String apiUUID, String type) throws APIManagementException; void deleteCustomBackendByAPIID(String apiUUID) throws APIManagementException; /** @@ -327,7 +327,9 @@ Map getCustomBackendOfAPIByUUID(String customBackendUUID, String String getCustomBackendSequenceOfAPIByUUID(String apiUUID, String type) throws APIManagementException; - CustomBackendData getCustomBackendByAPIUUID(String apiUUID, String type) throws APIManagementException; + SequenceBackendData getCustomBackendByAPIUUID(String apiUUID, String type) throws APIManagementException; + + List getAllSequenceBackendsByAPIUUID(String apiUUID) throws APIManagementException; /** * Create a new version of the api, with version newVersion diff --git a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/ExceptionCodes.java b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/ExceptionCodes.java index aa9f9525a7c2..26d6bf917fa9 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/ExceptionCodes.java +++ b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/ExceptionCodes.java @@ -534,8 +534,8 @@ public enum ExceptionCodes implements ErrorHandler { "Required attributes(s) %s for api policy specification %s are either missing or empty"), OPERATION_POLICY_NOT_FOUND(902010, "API Policy Not Found", 404, "Requested api policy with id '%s' not found"), - CUSTOM_BACKEND_NOT_FOUND(903250, "Custom Backend not found", - 404, "Requested Custom Backend with id '%s' not found"), + CUSTOM_BACKEND_NOT_FOUND(903250, "Sequence Backend not found", + 404, "Requested Sequence Backend of API '%s' not found"), OPERATION_POLICY_ALREADY_EXISTS(903001, "The API Policy already exists.", 409, "An Operation Policy with name '%s' and version '%s' already exists"), diff --git a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/CustomBackendData.java b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/SequenceBackendData.java similarity index 96% rename from components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/CustomBackendData.java rename to components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/SequenceBackendData.java index ab9576002c28..5cccbbbbc433 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/CustomBackendData.java +++ b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/SequenceBackendData.java @@ -1,6 +1,6 @@ package org.wso2.carbon.apimgt.api.model; -public class CustomBackendData { +public class SequenceBackendData { private String Id; private String sequence; private String type; diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java index 3757c210e28b..d79a62d13fa4 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java @@ -73,7 +73,7 @@ import org.wso2.carbon.apimgt.api.model.BlockConditionsDTO; import org.wso2.carbon.apimgt.api.model.Comment; import org.wso2.carbon.apimgt.api.model.CommentList; -import org.wso2.carbon.apimgt.api.model.CustomBackendData; +import org.wso2.carbon.apimgt.api.model.SequenceBackendData; import org.wso2.carbon.apimgt.api.model.DeployedAPIRevision; import org.wso2.carbon.apimgt.api.model.Documentation; import org.wso2.carbon.apimgt.api.model.Documentation.DocumentSourceType; @@ -1188,18 +1188,21 @@ public String getCustomBackendSequenceOfAPIByUUID(String apiUUID, String type) } @Override - public CustomBackendData getCustomBackendByAPIUUID(String apiUUID, String type) throws APIManagementException { + public SequenceBackendData getCustomBackendByAPIUUID(String apiUUID, String type) throws APIManagementException { return apiMgtDAO.getCustomBackendByAPIUUID(apiUUID, type); } + public List getAllSequenceBackendsByAPIUUID(String apiUUID) throws APIManagementException { + return apiMgtDAO.getSequenceBackendsByAPIUUID(apiUUID); + } + @Override public void deleteCustomBackendByAPIID(String apiUUID) throws APIManagementException { apiMgtDAO.deleteCustomBackendByAPIID(apiUUID); } @Override - public void deleteCustomBackendByID(String backendUUID, String apiUUID, String type) - throws APIManagementException { - apiMgtDAO.deleteCustomBackend(apiUUID, backendUUID, type); + public void deleteCustomBackendByID(String apiUUID, String type) throws APIManagementException { + apiMgtDAO.deleteCustomBackend(apiUUID, type); } private void validateKeyManagers(API api) throws APIManagementException { 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 87fbec3d3b72..0a98f04ee4f4 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 @@ -61,7 +61,7 @@ import org.wso2.carbon.apimgt.api.model.BlockConditionsDTO; import org.wso2.carbon.apimgt.api.model.Comment; import org.wso2.carbon.apimgt.api.model.CommentList; -import org.wso2.carbon.apimgt.api.model.CustomBackendData; +import org.wso2.carbon.apimgt.api.model.SequenceBackendData; import org.wso2.carbon.apimgt.api.model.DeployedAPIRevision; import org.wso2.carbon.apimgt.api.model.Environment; import org.wso2.carbon.apimgt.api.model.GatewayPolicyData; @@ -11240,27 +11240,49 @@ public Map retrieveCustomBackendOfAPIRevision(String apiUUID, St return map; } - public CustomBackendData getCustomBackendByAPIUUID(String apiUUID, String type) throws APIManagementException { + public SequenceBackendData getCustomBackendByAPIUUID(String apiUUID, String type) throws APIManagementException { String sqlQuery = SQLConstants.CustomBackendConstants.GET_API_SPECIFIC_CUSTOM_BACKEND_FROM_SEQUENCE_ID; - CustomBackendData customBackendData = null; + SequenceBackendData sequenceBackendData = null; try (Connection con = APIMgtDBUtil.getConnection(); PreparedStatement ps = con.prepareStatement(sqlQuery)) { ps.setString(1, apiUUID); ps.setString(2, type); try (ResultSet rs = ps.executeQuery()) { while (rs.next()) { - customBackendData = new CustomBackendData(); - customBackendData.setApiUUID(apiUUID); - customBackendData.setRevisionUUID("0"); - customBackendData.setSequence(IOUtils.toString(rs.getBinaryStream("SEQUENCE"))); - customBackendData.setId(rs.getString("ID")); - customBackendData.setName(rs.getString("NAME")); - customBackendData.setType(type); + sequenceBackendData = new SequenceBackendData(); + sequenceBackendData.setApiUUID(apiUUID); + sequenceBackendData.setRevisionUUID("0"); + sequenceBackendData.setSequence(IOUtils.toString(rs.getBinaryStream("SEQUENCE"))); + sequenceBackendData.setId(rs.getString("ID")); + sequenceBackendData.setName(rs.getString("NAME")); + sequenceBackendData.setType(type); } } } catch (SQLException | IOException ex) { handleException("Error when fetching Custom Backend data for API: " + apiUUID, ex); } - return customBackendData; + return sequenceBackendData; + } + + public List getSequenceBackendsByAPIUUID(String apiUUID) throws APIManagementException { + String sqlQuery = SQLConstants.CustomBackendConstants.GET_ALL_API_SPECIFIC_CUSTOM_BACKENDS; + List backendDataList = new ArrayList<>(); + try (Connection con = APIMgtDBUtil.getConnection(); PreparedStatement ps = con.prepareStatement(sqlQuery)) { + ps.setString(1, apiUUID); + try (ResultSet rs = ps.executeQuery()) { + while (rs.next()) { + SequenceBackendData sqBackend = new SequenceBackendData(); + sqBackend.setApiUUID(apiUUID); + sqBackend.setId(rs.getString("ID")); + sqBackend.setName(rs.getString("NAME")); + sqBackend.setType(rs.getString("TYPE")); + backendDataList.add(sqBackend); + } + } + return backendDataList; + } catch (SQLException ex) { + handleException("Error when retrieving Sequence Backends of API: " + apiUUID, ex); + } + return null; } public String getCustomBackendSequenceOfAPIByUUID(String apiUUID, String type) throws APIManagementException { @@ -21506,14 +21528,13 @@ public void updateCustomBackend(String apiUUID, String sequenceName, InputStream } } - public void deleteCustomBackend(String apiUUID, String type, String backendUUID) throws APIManagementException { + public void deleteCustomBackend(String apiUUID, String type) throws APIManagementException { String deleteCustomBackedQuery = SQLConstants.CustomBackendConstants.DELETE_CUSTOM_BACKEND; try (Connection connection = APIMgtDBUtil.getConnection(); 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(); connection.commit(); } catch (SQLException e) { @@ -21797,7 +21818,7 @@ private void revisionCustomBackend(APIRevision apiRevision, Connection connectio PreparedStatement addPstmt = connection.prepareStatement(addCBSqlQuery)) { connection.setAutoCommit(false); getPstmt.setString(1, apiRevision.getApiUUID()); - List customBackendDataList = new ArrayList<>(); + List sequenceBackendDataList = new ArrayList<>(); int count = 0; try (ResultSet rs = getPstmt.executeQuery()) { 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 0e8d7c406bb7..334c0b0b6b2e 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 @@ -4479,7 +4479,7 @@ public static class CustomBackendConstants { "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 TYPE = ? AND ID = ? AND REVISION_UUID = '0'"; + public static final String DELETE_CUSTOM_BACKEND = "DELETE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ? AND TYPE = ? 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 = ?"; @@ -4487,12 +4487,7 @@ public static class CustomBackendConstants { public static final String GET_CUSTOM_BACKEND_OF_API_DEFAULT_REVISION = "SELECT ACB.NAME, ACB.TYPE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ? AND REVISION_UUID = '0'"; public static final String GET_REVISION_SPECIFIC_CUSTOM_BACKEND_FROM_SEQUENCE_ID = "SELECT ACB.ID, ACB.NAME, ACB.SEQUENCE, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.ID = ? AND ACB.REVISION_UUID = ? AND ACB.TYPE = ?"; public static final String GET_API_SPECIFIC_CUSTOM_BACKEND_FROM_SEQUENCE_ID = "SELECT ACB.ID, ACB.NAME, ACB.SEQUENCE, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.API_UUID = ? AND ACB.REVISION_UUID = '0' AND ACB.TYPE = ?"; - public static final String GET_CUSTOM_BACKEND_FROM_API_AND_REVISION_UUID = "SELECT ACB.ID, ACB.NAME, ACB.SEQUENCE, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.API_UUID = ? AND ACB.REVISION_UUID = ? AND ACB.TYPE = ?"; public static final String GET_ALL_API_SPECIFIC_CUSTOM_BACKENDS = "SELECT ACB.ID, ACB.NAME, ACB.SEQUENCE, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.API_UUID = ? AND ACB.REVISION_UUID = '0'"; - public static final String GET_REVISION_SPECIFIC_CUSTOM_BACKEND_META_DATA_FROM_SEQUENCE_ID = "SELECT ACB.ID, ACB.NAME, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.ID = ? AND ACB.REVISION_UUID = ?"; - public static final String GET_API_SPECIFIC_CUSTOM_BACKEND_META_DATA_FROM_SEQUENCE_ID = "SELECT ACB.ID, ACB.NAME, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.ID = ? AND API_UUID = ?"; - public static final String GET_API_SPECIFIC_CUSTOM_BACKEND_SEQUENCE_FROM_SEQUENCE_ID = "SELECT ACB.SEQUENCE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.ID = ? AND API_UUID = ?"; - public static final String GET_REVISION_SPECIFIC_CUSTOM_BACKEND_SEQUENCE_FROM_SEQUENCE_ID = "SELECT ACB.SEQUENCE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.ID = ? AND ACB.REVISION_UUID = ?"; } } 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/APIMappingUtil.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/APIMappingUtil.java index 5804fb64cb86..21b6056bb04c 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/APIMappingUtil.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/APIMappingUtil.java @@ -54,6 +54,7 @@ import org.wso2.carbon.apimgt.api.model.OperationPolicy; import org.wso2.carbon.apimgt.api.model.ResourcePath; import org.wso2.carbon.apimgt.api.model.Scope; +import org.wso2.carbon.apimgt.api.model.SequenceBackendData; import org.wso2.carbon.apimgt.api.model.ServiceEntry; import org.wso2.carbon.apimgt.api.model.Tier; import org.wso2.carbon.apimgt.api.model.URITemplate; @@ -116,6 +117,8 @@ import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ResourcePolicyInfoDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ResourcePolicyListDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ScopeDTO; +import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.SequenceBackendDTO; +import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.SequenceBackendListDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.WSDLInfoDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.WSDLValidationResponseDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.WSDLValidationResponseWsdlInfoDTO; @@ -509,6 +512,22 @@ public static MockResponsePayloadInfoDTO fromMockPayloadToDTO(APIResourceMediati return mockResponsePayloadInfoDTO; } + public static SequenceBackendListDTO fromSequenceDataToDTO(List list) { + SequenceBackendListDTO res = new SequenceBackendListDTO(); + res.setCount(list.size()); + List backends = new ArrayList<>(); + + for (SequenceBackendData backend : list) { + SequenceBackendDTO dto = new SequenceBackendDTO(); + dto.sequenceId(backend.getId()); + dto.setSequenceName(backend.getName()); + dto.setSequenceType(backend.getType()); + backends.add(dto); + } + res.setList(backends); + return res; + } + /** * This method creates the API monetization information DTO. * 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/ExportUtils.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/ExportUtils.java index fd9668dd3459..3614c677ba80 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/ExportUtils.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/ExportUtils.java @@ -46,7 +46,7 @@ import org.wso2.carbon.apimgt.api.model.APIProductIdentifier; import org.wso2.carbon.apimgt.api.model.APIRevision; import org.wso2.carbon.apimgt.api.model.APIRevisionDeployment; -import org.wso2.carbon.apimgt.api.model.CustomBackendData; +import org.wso2.carbon.apimgt.api.model.SequenceBackendData; import org.wso2.carbon.apimgt.api.model.Documentation; import org.wso2.carbon.apimgt.api.model.DocumentationContent; import org.wso2.carbon.apimgt.api.model.Identifier; @@ -218,11 +218,10 @@ public static File exportApi(APIProvider apiProvider, APIIdentifier apiIdentifie addOperationPoliciesToArchive(archivePath, tenantDomain, exportFormat, apiProvider, api, currentApiUuid); - // TODO: Add Custom Backend to the Archive JsonObject endpointConfig = JsonParser.parseString(api.getEndpointConfig()).getAsJsonObject(); if (APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( endpointConfig.get(API_ENDPOINT_CONFIG_PROTOCOL_TYPE).getAsString())) { - addCustomBackendToArchive(archivePath, apiProvider, currentApiUuid, endpointConfig); + addCustomBackendToArchive(archivePath, apiProvider, currentApiUuid); } addGatewayEnvironmentsToArchive(archivePath, apiDtoToReturn.getId(), exportFormat, apiProvider); @@ -637,20 +636,21 @@ public static void addEndpointCertificatesToArchive(String archivePath, APIDTO a } } - public static void addCustomBackendToArchive(String archivePath, APIProvider apiProvider, String apiUUID, - JsonObject endpointConfig) throws APIManagementException { + public static void addCustomBackendToArchive(String archivePath, APIProvider apiProvider, String apiUUID) + throws APIManagementException { try { CommonUtil.createDirectory(archivePath + File.separator + ImportExportConstants.CUSTOM_BACKEND_DIRECTORY); // Add production Backend Sequences - CustomBackendData data = apiProvider.getCustomBackendByAPIUUID(apiUUID, APIConstants.API_KEY_TYPE_PRODUCTION); - if(data != null) { + SequenceBackendData data = apiProvider.getCustomBackendByAPIUUID(apiUUID, + APIConstants.API_KEY_TYPE_PRODUCTION); + if (data != null) { exportCustomBackend(data.getName(), data.getSequence(), archivePath); } // Add sandbox Backend Sequences data = apiProvider.getCustomBackendByAPIUUID(apiUUID, APIConstants.API_KEY_TYPE_SANDBOX); - if(data != null) { + if (data != null) { exportCustomBackend(data.getName(), data.getSequence(), archivePath); } 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 5a49cf281e44..0dca6636bc74 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,7 +18,6 @@ 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; @@ -712,7 +711,8 @@ public static void updateAPIWithCustomBackend(API api, String extractedFolderPat String seqName = APIUtil.getCustomBackendName(api.getUuid(), APIConstants.API_KEY_TYPE_PRODUCTION); String seqId = UUID.randomUUID().toString(); InputStream seq = APIUtil.getCustomBackendSequence(customBackendDir, seqFile, ".xml"); - apiProvider.updateCustomBackend(api.getUuid(), APIConstants.API_KEY_TYPE_PRODUCTION, seq, seqName, seqId); + apiProvider.updateCustomBackend(api.getUuid(), APIConstants.API_KEY_TYPE_PRODUCTION, seq, seqName, + seqId); } } } 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 2f6027782960..d32845665b30 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 @@ -201,7 +201,6 @@ public static API updateApi(API originalAPI, APIDTO apiDtoToUpdate, APIProvider * @param endpointType Endpoint Type of the Custom Backend (SANDBOX, PRODUCTION) * @param customBackend Custom Backend * @param contentDecomp Header Content of the Request - * @return Custom Backend File Name * @throws APIManagementException If an error occurs while updating the API and API definition */ public static void updateCustomBackend(API api, APIProvider apiProvider, String endpointType, diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApi.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApi.java index 5a7f1424c980..96016012b1b1 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApi.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApi.java @@ -40,18 +40,21 @@ import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ResourcePathListDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ResourcePolicyInfoDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ResourcePolicyListDTO; -import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.SequenceBackendDTO; +import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.SequenceBackendListDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ThrottlingPolicyDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.TopicListDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.WSDLInfoDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.WSDLValidationResponseDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.WorkflowResponseDTO; +import org.wso2.carbon.apimgt.rest.api.publisher.v1.ApisApiService; import org.wso2.carbon.apimgt.rest.api.publisher.v1.impl.ApisApiServiceImpl; import org.wso2.carbon.apimgt.api.APIManagementException; import javax.ws.rs.*; import javax.ws.rs.core.Context; import javax.ws.rs.core.Response; +import javax.ws.rs.core.SecurityContext; +import javax.inject.Inject; import io.swagger.annotations.*; import java.io.InputStream; @@ -60,6 +63,8 @@ import org.apache.cxf.jaxrs.ext.multipart.Attachment; import org.apache.cxf.jaxrs.ext.multipart.Multipart; +import java.util.Map; +import java.util.List; import javax.validation.constraints.*; @Path("/apis") @@ -373,49 +378,6 @@ public Response createNewAPIVersion( @NotNull @Size(max=30) @ApiParam(value = "V return delegate.createNewAPIVersion(newVersion, apiId, defaultVersion, serviceVersion, securityContext); } - @DELETE - @Path("/{apiId}/custom-backend/{customBackendId}") - - @Produces({ "application/json" }) - @ApiOperation(value = "Delete Custom Backend of the API", notes = "This operation can be used to remove the Custom Backend of the API", response = Void.class, authorizations = { - @Authorization(value = "OAuth2Security", scopes = { - @AuthorizationScope(scope = "apim:api_delete", description = "Delete API"), - @AuthorizationScope(scope = "apim:api_manage", description = "Manage all API related operations"), - @AuthorizationScope(scope = "apim:api_import_export", description = "Import and export APIs related operations") - }) - }, tags={ "APIs", }) - @ApiResponses(value = { - @ApiResponse(code = 200, message = "OK. Resource successfully deleted. ", response = Void.class), - @ApiResponse(code = 403, message = "Forbidden. The request must be conditional but no condition has been specified.", response = ErrorDTO.class), - @ApiResponse(code = 404, message = "Not Found. The specified resource does not exist.", response = ErrorDTO.class), - @ApiResponse(code = 409, message = "Conflict. Specified resource already exists.", response = ErrorDTO.class), - @ApiResponse(code = 412, message = "Precondition Failed. The request has not been performed because one of the preconditions is not met.", response = ErrorDTO.class) }) - public Response customBackendDelete( @NotNull @Size(max=15) @ApiParam(value = "Type of the Endpoint. SANDBOX or PRODUCTION ",required=true) @QueryParam("type") String type, @ApiParam(value = "**API ID** consisting of the **UUID** of the API. ",required=true) @PathParam("apiId") String apiId, @ApiParam(value = "Custom Backend ID ",required=true) @PathParam("customBackendId") String customBackendId) throws APIManagementException{ - return delegate.customBackendDelete(type, apiId, customBackendId, securityContext); - } - - @PUT - @Path("/{apiId}/custom-backend") - @Consumes({ "multipart/form-data" }) - @Produces({ "application/json" }) - @ApiOperation(value = "Upload Custom Sequence as the Endpoint of the API", notes = "This operation can be used to change the endpoint of the API to Custom Sequence", response = APIDTO.class, authorizations = { - @Authorization(value = "OAuth2Security", scopes = { - @AuthorizationScope(scope = "apim:api_create", description = "Create API"), - @AuthorizationScope(scope = "apim:api_manage", description = "Manage all API related operations"), - @AuthorizationScope(scope = "apim:api_publish", description = "Publish API") - }) - }, tags={ "APIs", }) - @ApiResponses(value = { - @ApiResponse(code = 200, message = "OK. Successful response with updated API object ", response = APIDTO.class), - @ApiResponse(code = 400, message = "Bad Request. Invalid request or validation error.", response = ErrorDTO.class), - @ApiResponse(code = 403, message = "Forbidden. The request must be conditional but no condition has been specified.", response = ErrorDTO.class), - @ApiResponse(code = 404, message = "Not Found. The specified resource does not exist.", response = ErrorDTO.class), - @ApiResponse(code = 409, message = "Conflict. Specified resource already exists.", response = ErrorDTO.class), - @ApiResponse(code = 412, message = "Precondition Failed. The request has not been performed because one of the preconditions is not met.", response = ErrorDTO.class) }) - public Response customBackendUpdate(@ApiParam(value = "**API ID** consisting of the **UUID** of the API. ",required=true) @PathParam("apiId") String apiId, @Multipart(value = "sequence", required = false) InputStream sequenceInputStream, @Multipart(value = "sequence" , required = false) Attachment sequenceDetail, @Multipart(value = "type", required = false) String type) throws APIManagementException{ - return delegate.customBackendUpdate(apiId, sequenceInputStream, sequenceDetail, type, securityContext); - } - @DELETE @Path("/{apiId}") @@ -1327,48 +1289,6 @@ public Response getAuditReportOfAPI(@ApiParam(value = "**API ID** consisting of return delegate.getCommentOfAPI(commentId, apiId, xWSO2Tenant, ifNoneMatch, includeCommenterInfo, replyLimit, replyOffset, securityContext); } - @GET - @Path("/{apiId}/custom-backend/{customBackendId}") - - @Produces({ "application/json" }) - @ApiOperation(value = "Get Custom Backend of the API", notes = "This operation can be used to get Custom Backend data of the API", response = SequenceBackendDTO.class, authorizations = { - @Authorization(value = "OAuth2Security", scopes = { - @AuthorizationScope(scope = "apim:api_view", description = "View API"), - @AuthorizationScope(scope = "apim:api_manage", description = "Manage all API related operations"), - @AuthorizationScope(scope = "apim:api_import_export", description = "Import and export APIs related operations"), - @AuthorizationScope(scope = "apim:api_product_import_export", description = "Import and export API Products related operations") - }) - }, tags={ "APIs", }) - @ApiResponses(value = { - @ApiResponse(code = 200, message = "OK. Requested API Custom Backend is returned ", response = SequenceBackendDTO.class), - @ApiResponse(code = 304, message = "Not Modified. Empty body because the client has already the latest version of the requested resource (Will be supported in future). ", response = Void.class), - @ApiResponse(code = 404, message = "Not Found. The specified resource does not exist.", response = ErrorDTO.class), - @ApiResponse(code = 406, message = "Not Acceptable. The requested media type is not supported.", response = ErrorDTO.class) }) - public Response getCustomBackendData( @NotNull @Size(max=15) @ApiParam(value = "Type of the Endpoint. SANDBOX or PRODUCTION ",required=true) @QueryParam("type") String type, @ApiParam(value = "Custom Backend ID ",required=true) @PathParam("customBackendId") String customBackendId, @ApiParam(value = "**API ID** consisting of the **UUID** of the API. ",required=true) @PathParam("apiId") String apiId) throws APIManagementException{ - return delegate.getCustomBackendData(type, customBackendId, apiId, securityContext); - } - - @GET - @Path("/{apiId}/custom-backend/{customBackendId}/content") - - @Produces({ "application/zip", "application/json" }) - @ApiOperation(value = "Get Sequence of Custom Backend", notes = "This operation can be used to get Sequence of the Custom Backend", response = File.class, authorizations = { - @Authorization(value = "OAuth2Security", scopes = { - @AuthorizationScope(scope = "apim:api_view", description = "View API"), - @AuthorizationScope(scope = "apim:api_manage", description = "Manage all API related operations"), - @AuthorizationScope(scope = "apim:api_import_export", description = "Import and export APIs related operations"), - @AuthorizationScope(scope = "apim:api_product_import_export", description = "Import and export API Products related operations") - }) - }, tags={ "APIs", }) - @ApiResponses(value = { - @ApiResponse(code = 200, message = "OK. Requested API Custom Backend is returned ", response = File.class), - @ApiResponse(code = 304, message = "Not Modified. Empty body because the client has already the latest version of the requested resource (Will be supported in future). ", response = Void.class), - @ApiResponse(code = 404, message = "Not Found. The specified resource does not exist.", response = ErrorDTO.class), - @ApiResponse(code = 406, message = "Not Acceptable. The requested media type is not supported.", response = ErrorDTO.class) }) - public Response getCustomBackendDataContent( @NotNull @Size(max=15) @ApiParam(value = "Type of the Endpoint. SANDBOX or PRODUCTION ",required=true) @QueryParam("type") String type, @ApiParam(value = "Custom Backend ID ",required=true) @PathParam("customBackendId") String customBackendId, @ApiParam(value = "**API ID** consisting of the **UUID** of the API. ",required=true) @PathParam("apiId") String apiId) throws APIManagementException{ - return delegate.getCustomBackendDataContent(type, customBackendId, apiId, securityContext); - } - @GET @Path("/{apiId}/generated-mock-scripts") @@ -1469,6 +1389,46 @@ public Response getOperationPolicyForAPIByPolicyId(@ApiParam(value = "**API ID** return delegate.getRepliesOfComment(commentId, apiId, xWSO2Tenant, limit, offset, ifNoneMatch, includeCommenterInfo, securityContext); } + @GET + @Path("/{apiId}/sequence-backend/{type}/content") + + @Produces({ "application/xml", "application/json" }) + @ApiOperation(value = "Get Sequence of Custom Backend", notes = "This operation can be used to get Sequence of the Custom Backend", response = File.class, authorizations = { + @Authorization(value = "OAuth2Security", scopes = { + @AuthorizationScope(scope = "apim:api_view", description = "View API"), + @AuthorizationScope(scope = "apim:api_manage", description = "Manage all API related operations"), + @AuthorizationScope(scope = "apim:api_import_export", description = "Import and export APIs related operations"), + @AuthorizationScope(scope = "apim:api_product_import_export", description = "Import and export API Products related operations") + }) + }, tags={ "APIs", }) + @ApiResponses(value = { + @ApiResponse(code = 200, message = "OK. Requested API Custom Backend is returned ", response = File.class), + @ApiResponse(code = 404, message = "Not Found. The specified resource does not exist.", response = ErrorDTO.class), + @ApiResponse(code = 406, message = "Not Acceptable. The requested media type is not supported.", response = ErrorDTO.class) }) + public Response getSequenceBackendContent( @NotNull @Size(max=15) @ApiParam(value = "Type of the Endpoint. SANDBOX or PRODUCTION ",required=true) @QueryParam("type") String type, @ApiParam(value = "**API ID** consisting of the **UUID** of the API. ",required=true) @PathParam("apiId") String apiId) throws APIManagementException{ + return delegate.getSequenceBackendContent(type, apiId, securityContext); + } + + @GET + @Path("/{apiId}/sequence-backend") + + @Produces({ "application/json" }) + @ApiOperation(value = "Get Sequence Backends of the API", notes = "This operation can be used to get Sequence Backend data of the API", response = SequenceBackendListDTO.class, authorizations = { + @Authorization(value = "OAuth2Security", scopes = { + @AuthorizationScope(scope = "apim:api_view", description = "View API"), + @AuthorizationScope(scope = "apim:api_manage", description = "Manage all API related operations"), + @AuthorizationScope(scope = "apim:api_import_export", description = "Import and export APIs related operations"), + @AuthorizationScope(scope = "apim:api_product_import_export", description = "Import and export API Products related operations") + }) + }, tags={ "APIs", }) + @ApiResponses(value = { + @ApiResponse(code = 200, message = "OK. Requested API Sequence Backend is returned ", response = SequenceBackendListDTO.class), + @ApiResponse(code = 404, message = "Not Found. The specified resource does not exist.", response = ErrorDTO.class), + @ApiResponse(code = 406, message = "Not Acceptable. The requested media type is not supported.", response = ErrorDTO.class) }) + public Response getSequenceBackendData(@ApiParam(value = "**API ID** consisting of the **UUID** of the API. ",required=true) @PathParam("apiId") String apiId) throws APIManagementException{ + return delegate.getSequenceBackendData(apiId, securityContext); + } + @GET @Path("/{apiId}/wsdl-info") @@ -1670,6 +1630,49 @@ public Response restoreAPIRevision(@ApiParam(value = "**API ID** consisting of t return delegate.restoreAPIRevision(apiId, revisionId, securityContext); } + @DELETE + @Path("/{apiId}/sequence-backend/{type}") + + @Produces({ "application/json" }) + @ApiOperation(value = "Delete Sequence Backend of the API", notes = "This operation can be used to remove the Sequence Backend of the API", response = Void.class, authorizations = { + @Authorization(value = "OAuth2Security", scopes = { + @AuthorizationScope(scope = "apim:api_delete", description = "Delete API"), + @AuthorizationScope(scope = "apim:api_manage", description = "Manage all API related operations"), + @AuthorizationScope(scope = "apim:api_import_export", description = "Import and export APIs related operations") + }) + }, tags={ "APIs", }) + @ApiResponses(value = { + @ApiResponse(code = 200, message = "OK. Resource successfully deleted. ", response = Void.class), + @ApiResponse(code = 403, message = "Forbidden. The request must be conditional but no condition has been specified.", response = ErrorDTO.class), + @ApiResponse(code = 404, message = "Not Found. The specified resource does not exist.", response = ErrorDTO.class), + @ApiResponse(code = 409, message = "Conflict. Specified resource already exists.", response = ErrorDTO.class), + @ApiResponse(code = 412, message = "Precondition Failed. The request has not been performed because one of the preconditions is not met.", response = ErrorDTO.class) }) + public Response sequenceBackendDelete( @Size(max=15)@ApiParam(value = "Type of the Endpoint. SANDBOX or PRODUCTION ",required=true) @PathParam("type") String type, @ApiParam(value = "**API ID** consisting of the **UUID** of the API. ",required=true) @PathParam("apiId") String apiId) throws APIManagementException{ + return delegate.sequenceBackendDelete(type, apiId, securityContext); + } + + @PUT + @Path("/{apiId}/sequence-backend") + @Consumes({ "multipart/form-data" }) + @Produces({ "application/json" }) + @ApiOperation(value = "Upload Sequence Sequence as the Endpoint of the API", notes = "This operation can be used to change the endpoint of the API to Sequence Backend", response = APIDTO.class, authorizations = { + @Authorization(value = "OAuth2Security", scopes = { + @AuthorizationScope(scope = "apim:api_create", description = "Create API"), + @AuthorizationScope(scope = "apim:api_manage", description = "Manage all API related operations"), + @AuthorizationScope(scope = "apim:api_publish", description = "Publish API") + }) + }, tags={ "APIs", }) + @ApiResponses(value = { + @ApiResponse(code = 200, message = "OK. Successful response with updated API object ", response = APIDTO.class), + @ApiResponse(code = 400, message = "Bad Request. Invalid request or validation error.", response = ErrorDTO.class), + @ApiResponse(code = 403, message = "Forbidden. The request must be conditional but no condition has been specified.", response = ErrorDTO.class), + @ApiResponse(code = 404, message = "Not Found. The specified resource does not exist.", response = ErrorDTO.class), + @ApiResponse(code = 409, message = "Conflict. Specified resource already exists.", response = ErrorDTO.class), + @ApiResponse(code = 412, message = "Precondition Failed. The request has not been performed because one of the preconditions is not met.", response = ErrorDTO.class) }) + public Response sequenceBackendUpdate(@ApiParam(value = "**API ID** consisting of the **UUID** of the API. ",required=true) @PathParam("apiId") String apiId, @Multipart(value = "sequence", required = false) InputStream sequenceInputStream, @Multipart(value = "sequence" , required = false) Attachment sequenceDetail, @Multipart(value = "type", required = false) String type) throws APIManagementException{ + return delegate.sequenceBackendUpdate(apiId, sequenceInputStream, sequenceDetail, type, securityContext); + } + @POST @Path("/{apiId}/undeploy-revision") @Consumes({ "application/json" }) diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApiService.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApiService.java index d5ff4cb844b8..7caffe1a712f 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApiService.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApiService.java @@ -27,7 +27,6 @@ import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ClientCertificatesDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CommentDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CommentListDTO; -import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CustomBackendDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.DocumentDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.DocumentListDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ErrorDTO; @@ -50,6 +49,7 @@ import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ResourcePathListDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ResourcePolicyInfoDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ResourcePolicyListDTO; +import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.SequenceBackendListDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ThrottlingPolicyDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.TopicListDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.WSDLInfoDTO; @@ -80,8 +80,6 @@ public interface ApisApiService { public Response createAPI(APIDTO APIDTO, String openAPIVersion, MessageContext messageContext) throws APIManagementException; public Response createAPIRevision(String apiId, APIRevisionDTO apIRevisionDTO, MessageContext messageContext) throws APIManagementException; public Response createNewAPIVersion(String newVersion, String apiId, Boolean defaultVersion, String serviceVersion, MessageContext messageContext) throws APIManagementException; - public Response customBackendDelete(String type, String apiId, String customBackendId, MessageContext messageContext) throws APIManagementException; - public Response customBackendUpdate(String apiId, InputStream sequenceInputStream, Attachment sequenceDetail, String type, MessageContext messageContext) throws APIManagementException; public Response deleteAPI(String apiId, String ifMatch, MessageContext messageContext) throws APIManagementException; public Response deleteAPIClientCertificateByAlias(String alias, String apiId, MessageContext messageContext) throws APIManagementException; public Response deleteAPIClientCertificateByKeyTypeAndAlias(String keyType, String alias, String apiId, MessageContext messageContext) throws APIManagementException; @@ -128,13 +126,13 @@ public interface ApisApiService { public Response getAmazonResourceNamesOfAPI(String apiId, MessageContext messageContext) throws APIManagementException; public Response getAuditReportOfAPI(String apiId, String accept, MessageContext messageContext) throws APIManagementException; public Response getCommentOfAPI(String commentId, String apiId, String xWSO2Tenant, String ifNoneMatch, Boolean includeCommenterInfo, Integer replyLimit, Integer replyOffset, MessageContext messageContext) throws APIManagementException; - public Response getCustomBackendData(String type, String customBackendId, String apiId, MessageContext messageContext) throws APIManagementException; - public Response getCustomBackendDataContent(String type, String customBackendId, String apiId, MessageContext messageContext) throws APIManagementException; public Response getGeneratedMockScriptsOfAPI(String apiId, String ifNoneMatch, MessageContext messageContext) throws APIManagementException; public Response getGraphQLPolicyComplexityOfAPI(String apiId, MessageContext messageContext) throws APIManagementException; public Response getGraphQLPolicyComplexityTypesOfAPI(String apiId, MessageContext messageContext) throws APIManagementException; public Response getOperationPolicyForAPIByPolicyId(String apiId, String operationPolicyId, MessageContext messageContext) throws APIManagementException; public Response getRepliesOfComment(String commentId, String apiId, String xWSO2Tenant, Integer limit, Integer offset, String ifNoneMatch, Boolean includeCommenterInfo, MessageContext messageContext) throws APIManagementException; + public Response getSequenceBackendContent(String type, String apiId, MessageContext messageContext) throws APIManagementException; + public Response getSequenceBackendData(String apiId, MessageContext messageContext) throws APIManagementException; public Response getWSDLInfoOfAPI(String apiId, MessageContext messageContext) throws APIManagementException; public Response getWSDLOfAPI(String apiId, String ifNoneMatch, MessageContext messageContext) throws APIManagementException; public Response importAPI(InputStream fileInputStream, Attachment fileDetail, Boolean preserveProvider, Boolean rotateRevision, Boolean overwrite, Boolean preservePortalConfigurations, String accept, MessageContext messageContext) throws APIManagementException; @@ -146,6 +144,8 @@ public interface ApisApiService { public Response publishAPIToExternalStores(String apiId, String externalStoreIds, String ifMatch, MessageContext messageContext) throws APIManagementException; public Response reimportServiceFromCatalog(String apiId, MessageContext messageContext) throws APIManagementException; public Response restoreAPIRevision(String apiId, String revisionId, MessageContext messageContext) throws APIManagementException; + public Response sequenceBackendDelete(String type, String apiId, MessageContext messageContext) throws APIManagementException; + public Response sequenceBackendUpdate(String apiId, InputStream sequenceInputStream, Attachment sequenceDetail, String type, MessageContext messageContext) throws APIManagementException; public Response undeployAPIRevision(String apiId, String revisionId, String revisionNumber, Boolean allEnvironments, List apIRevisionDeploymentDTO, MessageContext messageContext) throws APIManagementException; public Response updateAPI(String apiId, APIDTO APIDTO, String ifMatch, MessageContext messageContext) throws APIManagementException; public Response updateAPIClientCertificateByAlias(String alias, String apiId, InputStream certificateInputStream, Attachment certificateDetail, String tier, MessageContext messageContext) throws APIManagementException; diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java index e920cd328210..77af8d3d461f 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java @@ -169,35 +169,31 @@ public Response getAllAPIs(Integer limit, Integer offset, String sortBy, String } @Override - public Response getCustomBackendData(String type, String customBackendId, String apiId, - MessageContext messageContext) throws APIManagementException { + public Response getSequenceBackendData(String apiId, MessageContext messageContext) throws APIManagementException { APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider(); //validate if api exists CommonUtils.validateAPIExistence(apiId); - CustomBackendData data = apiProvider.getCustomBackendByAPIUUID(apiId, type); + List data = apiProvider.getAllSequenceBackendsByAPIUUID(apiId); if (data == null) { throw new APIMgtResourceNotFoundException( - "Couldn't retrieve an existing Custom Backend with ID: " + customBackendId + " for API " + apiId, - ExceptionCodes.from(ExceptionCodes.CUSTOM_BACKEND_NOT_FOUND, customBackendId)); + "Couldn't retrieve an existing Sequence Backend for API " + apiId, + ExceptionCodes.from(ExceptionCodes.CUSTOM_BACKEND_NOT_FOUND, apiId)); } - SequenceBackendDTO backendDTO = new SequenceBackendDTO(); - backendDTO.setSequenceName(data.getName()); - backendDTO.setSequenceId(data.getId()); - return Response.ok().entity(backendDTO).build(); + SequenceBackendListDTO respObj = APIMappingUtil.fromSequenceDataToDTO(data); + return Response.ok().entity(respObj).build(); } @Override - public Response getCustomBackendDataContent(String type, String customBackendId, String apiId, - MessageContext messageContext) throws APIManagementException { + public Response getSequenceBackendContent(String type, String apiId, MessageContext messageContext) throws APIManagementException { APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider(); CommonUtils.validateAPIExistence(apiId); - CustomBackendData data = apiProvider.getCustomBackendByAPIUUID(apiId, type); + SequenceBackendData data = apiProvider.getCustomBackendByAPIUUID(apiId, type); if (data == null) { throw new APIMgtResourceNotFoundException( - "Couldn't retrieve an existing Custom Backend with ID: " + customBackendId + " for API " + apiId, - ExceptionCodes.from(ExceptionCodes.CUSTOM_BACKEND_NOT_FOUND, customBackendId)); + "Couldn't retrieve an existing Sequence Backend for API: " + apiId, + ExceptionCodes.from(ExceptionCodes.CUSTOM_BACKEND_NOT_FOUND, apiId)); } File file = RestApiPublisherUtils.exportCustomBackendData(data.getSequence(), data.getName()); return Response.ok(file) @@ -206,12 +202,11 @@ public Response getCustomBackendDataContent(String type, String customBackendId, } @Override - public Response customBackendDelete(String type, String apiId, String customBackendId, - MessageContext messageContext) throws APIManagementException { + public Response sequenceBackendDelete(String type, String apiId, MessageContext messageContext) throws APIManagementException { APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider(); //validate if api exists CommonUtils.validateAPIExistence(apiId); - apiProvider.deleteCustomBackendByID(apiId, customBackendId, type); + apiProvider.deleteCustomBackendByID(apiId, type); return Response.ok().build(); } @@ -251,7 +246,7 @@ public Response getAPI(String apiId, String xWSO2Tenant, String ifNoneMatch, } @Override - public Response customBackendUpdate(String apiId, InputStream sequenceInputStream, + public Response sequenceBackendUpdate(String apiId, InputStream sequenceInputStream, Attachment sequenceDetail, String type, MessageContext messageContext) throws APIManagementException { String username = RestApiCommonUtil.getLoggedInUsername(); APIProvider apiProvider = RestApiCommonUtil.getProvider(username); From e1317ec2322445b78636ca23e09306e7f0f8c1f0 Mon Sep 17 00:00:00 2001 From: BLasan Date: Sat, 14 Sep 2024 23:18:09 +0530 Subject: [PATCH 30/56] Fix: Import Order Issue --- .../rest/api/publisher/v1/common/mappings/ExportUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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/ExportUtils.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/ExportUtils.java index 3614c677ba80..e19110ce479f 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/ExportUtils.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/ExportUtils.java @@ -46,7 +46,6 @@ import org.wso2.carbon.apimgt.api.model.APIProductIdentifier; import org.wso2.carbon.apimgt.api.model.APIRevision; import org.wso2.carbon.apimgt.api.model.APIRevisionDeployment; -import org.wso2.carbon.apimgt.api.model.SequenceBackendData; import org.wso2.carbon.apimgt.api.model.Documentation; import org.wso2.carbon.apimgt.api.model.DocumentationContent; import org.wso2.carbon.apimgt.api.model.Identifier; @@ -54,6 +53,7 @@ import org.wso2.carbon.apimgt.api.model.OperationPolicy; import org.wso2.carbon.apimgt.api.model.OperationPolicyData; import org.wso2.carbon.apimgt.api.model.ResourceFile; +import org.wso2.carbon.apimgt.api.model.SequenceBackendData; import org.wso2.carbon.apimgt.api.model.URITemplate; import org.wso2.carbon.apimgt.api.model.graphql.queryanalysis.GraphqlComplexityInfo; import org.wso2.carbon.apimgt.impl.APIConstants; From 4209a86af9caace98f9a43e722c633e42fe91955 Mon Sep 17 00:00:00 2001 From: BLasan Date: Sun, 15 Sep 2024 03:06:06 +0530 Subject: [PATCH 31/56] Add: Endpoint Configurations data --- .../wso2/carbon/apimgt/impl/APIConstants.java | 2 +- .../v1/common/TemplateBuilderUtil.java | 53 ++++++++++++++----- 2 files changed, 40 insertions(+), 15 deletions(-) diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConstants.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConstants.java index 46fbfc4ec6a2..ce4318f69714 100755 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConstants.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConstants.java @@ -1783,7 +1783,7 @@ private ConfigParameters() { public static final String ENDPOINT_TYPE_SERVICE = "service"; public static final String ENDPOINT_TYPE_ADDRESS = "address"; public static final String ENDPOINT_TYPE_AWSLAMBDA = "awslambda"; - public static final String ENDPOINT_TYPE_SEQUENCE = "custom_backend"; + public static final String ENDPOINT_TYPE_SEQUENCE = "sequence_backend"; public static final String SEQUENCE_DATA = "sequence"; public static final String ENDPOINT_PRODUCTION_FAILOVERS = "production_failovers"; public static final String ENDPOINT_SANDBOX_FAILOVERS = "sandbox_failovers"; 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/TemplateBuilderUtil.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/TemplateBuilderUtil.java index ade6b6e57438..6d4da46f3abd 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/TemplateBuilderUtil.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/TemplateBuilderUtil.java @@ -18,6 +18,7 @@ package org.wso2.carbon.apimgt.rest.api.publisher.v1.common; +import com.fasterxml.jackson.databind.ObjectMapper; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import org.apache.axiom.om.OMAttribute; @@ -558,6 +559,34 @@ public static GatewayAPIDTO retrieveGatewayAPIDto(API api, Environment environme // add new property for entires that has a __display suffix JSONObject modifiedProperties = getModifiedProperties(originalProperties); api.setAdditionalProperties(modifiedProperties); + + String endpointConfigString = api.getEndpointConfig(); + if (StringUtils.isNotBlank(endpointConfigString)) { + try { + JSONParser parser = new JSONParser(); + ObjectMapper objectMapper = new ObjectMapper(); + JSONObject endpointConfig = (JSONObject) parser.parse(endpointConfigString); + if (APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( + endpointConfig.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE))) { + 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); + } + 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); + } + api.setEndpointConfig(objectMapper.writeValueAsString(endpointConfig)); + } + } catch (IOException | ParseException ex) { + throw new APIManagementException("Error when updating Endpoint Configuration", ex); + } + } + APITemplateBuilder apiTemplateBuilder = TemplateBuilderUtil .getAPITemplateBuilder(api, tenantDomain, clientCertificatesDTOListProduction, clientCertificatesDTOListSandbox, soapToRestInMediationDtoList, soapToRestOutMediationDtoList); @@ -998,21 +1027,17 @@ private static void setCustomSequencesToBeAdded(API api, GatewayAPIDTO gatewayAP if (APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( endpointConfigMap.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE))) { - if (endpointConfigMap.get("sandbox") != null) { - GatewayContentDTO gatewayCustomBackendSequenceDTO = retrieveCustomBackendSequence(api, "SANDBOX", - extractedPath); - if (gatewayCustomBackendSequenceDTO != null) { - gatewayAPIDTO.setSequenceToBeAdd(addGatewayContentToList(gatewayCustomBackendSequenceDTO, - gatewayAPIDTO.getSequenceToBeAdd())); - } + GatewayContentDTO gatewayCustomBackendSequenceDTO = retrieveCustomBackendSequence(api, + APIConstants.API_KEY_TYPE_SANDBOX, extractedPath); + if (gatewayCustomBackendSequenceDTO != null) { + gatewayAPIDTO.setSequenceToBeAdd( + addGatewayContentToList(gatewayCustomBackendSequenceDTO, gatewayAPIDTO.getSequenceToBeAdd())); } - if (endpointConfigMap.get("production") != null) { - GatewayContentDTO gatewayCustomBackendSequenceDTO = retrieveCustomBackendSequence(api, "PRODUCTION", - extractedPath); - if (gatewayCustomBackendSequenceDTO != null) { - gatewayAPIDTO.setSequenceToBeAdd(addGatewayContentToList(gatewayCustomBackendSequenceDTO, - gatewayAPIDTO.getSequenceToBeAdd())); - } + gatewayCustomBackendSequenceDTO = retrieveCustomBackendSequence(api, APIConstants.API_KEY_TYPE_PRODUCTION, + extractedPath); + if (gatewayCustomBackendSequenceDTO != null) { + gatewayAPIDTO.setSequenceToBeAdd( + addGatewayContentToList(gatewayCustomBackendSequenceDTO, gatewayAPIDTO.getSequenceToBeAdd())); } } } From aa58c51cdc9eb83544850bf326af5bab8c3f5934 Mon Sep 17 00:00:00 2001 From: BLasan Date: Tue, 17 Sep 2024 02:33:24 +0530 Subject: [PATCH 32/56] Fix: Bugs in Revision scenarios --- .../wso2/carbon/apimgt/api/APIProvider.java | 3 +- .../carbon/apimgt/impl/APIProviderImpl.java | 6 +++ .../carbon/apimgt/impl/dao/ApiMgtDAO.java | 38 +++++-------------- .../src/main/resources/publisher-api.yaml | 2 +- .../v1/common/SynapsePolicyAggregator.java | 5 ++- .../v1/common/mappings/ExportUtils.java | 6 ++- .../v1/common/mappings/ImportUtils.java | 15 +++----- .../common/mappings/PublisherCommonUtils.java | 13 +++++-- .../apimgt/rest/api/publisher/v1/ApisApi.java | 2 +- .../publisher/v1/impl/ApisApiServiceImpl.java | 3 +- .../src/main/resources/publisher-api.yaml | 2 +- 11 files changed, 45 insertions(+), 50 deletions(-) diff --git a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java index 4d84d5aa6cb6..482c88946d9e 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java +++ b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java @@ -68,6 +68,7 @@ Comment getComment(ApiTypeWrapper apiTypeWrapper, String commentId, Integer repl void deleteCustomBackendByID(String apiUUID, String type) throws APIManagementException; void deleteCustomBackendByAPIID(String apiUUID) throws APIManagementException; + void deleteSequenceBackendByRevision(String apiUUID, String revisionId) throws APIManagementException; /** * @param apiTypeWrapper Api type wrapper @@ -319,7 +320,7 @@ List getSubscriptionsOfAPI(String apiName, String apiVersion, Str */ API updateAPI(API api, API existingAPI) throws APIManagementException, FaultGatewaysException; - void updateCustomBackend(String api, String type, InputStream sequence, String fileName, String customBackendUUID) + void updateCustomBackend(String api, String type, InputStream sequence, String seqName, String customBackendUUID) throws APIManagementException; Map getCustomBackendOfAPIByUUID(String customBackendUUID, String apiUUID, String type, diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java index d79a62d13fa4..29de8d1fe7ba 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java @@ -5401,6 +5401,12 @@ public boolean isSubscriptionValidationDisabled(String uuid) throws APIManagemen return !"ENABLED".equalsIgnoreCase(status); } + @Override + public void deleteSequenceBackendByRevision(String apiUUID, String revisionId) + throws APIManagementException { + apiMgtDAO.deleteCustomBackendByRevision(apiUUID, "0"); + } + @Override public API getAPIbyUUID(String uuid, String organization) throws APIManagementException { Organization org = new Organization(organization); 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 0a98f04ee4f4..0ec0b9327274 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 @@ -21555,36 +21555,16 @@ public void deleteCustomBackendByAPIID(String apiUUID) throws APIManagementExcep } public void deleteCustomBackendByRevision(String apiUUID, String revisionUUID) throws APIManagementException { - boolean isRevisioned = checkAPIUUIDIsARevisionUUID(apiUUID) != null; String deleteSqlQuery = SQLConstants.CustomBackendConstants.DELETE_CUSTOM_BACKEND_BY_REVISION; - if(isRevisioned) { - try (Connection con = APIMgtDBUtil.getConnection(); - PreparedStatement ps = con.prepareStatement(deleteSqlQuery)) { - con.setAutoCommit(false); - ps.setString(1, apiUUID); - ps.executeUpdate(); - con.commit(); - } catch (SQLException ex) { - handleException("Error deleting Custom Backend for Revision: "+ apiUUID, ex); - } - } else { - throw new APIManagementException("Invalid Revision Id: "+ apiUUID); - } - } - - public void updateCustomBackendByRevision(String apiUUID, String sequenceName, InputStream sequence, String type, - String revision, String backendUUID) throws APIManagementException { - String deleteCustomBackedQuery = SQLConstants.CustomBackendConstants.DELETE_CUSTOM_BACKEND_BY_REVISION; - try (Connection connection = APIMgtDBUtil.getConnection(); - PreparedStatement prepStmt = connection.prepareStatement(deleteCustomBackedQuery)) { - connection.setAutoCommit(false); - prepStmt.setString(1, apiUUID); - prepStmt.setString(2, revision); - prepStmt.executeUpdate(); - addCustomBackend(apiUUID, sequenceName, revision, sequence, type, connection, backendUUID); - connection.commit(); - } catch (SQLException e) { - handleException("Error while adding Custom Backend for API : " + apiUUID, e); + try (Connection con = APIMgtDBUtil.getConnection(); + PreparedStatement ps = con.prepareStatement(deleteSqlQuery)) { + con.setAutoCommit(false); + ps.setString(1, apiUUID); + ps.setString(2, revisionUUID); + ps.executeUpdate(); + con.commit(); + } catch (SQLException ex) { + handleException("Error deleting Custom Backend for Revision: " + apiUUID, ex); } } diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml b/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml index b70f2c4ea170..b5e358788c95 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml @@ -496,7 +496,7 @@ paths: operationId: getSequenceBackendContent parameters: - name: type - in: query + in: path description: | Type of the Endpoint. SANDBOX or PRODUCTION 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/SynapsePolicyAggregator.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/SynapsePolicyAggregator.java index 262ffa47b558..c235b7d9d64d 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/SynapsePolicyAggregator.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/SynapsePolicyAggregator.java @@ -115,7 +115,7 @@ public static String generateBackendSequenceForCustomSequence(String sequenceNam configMap.put("sequence_name", sequenceName); String sanitizedSequence = renderCustomBackendSequence(sequenceName, pathToArchive); if (sanitizedSequence == null) { - throw new APIManagementException("Error when preparing the sequence: " + sequenceName); + return null; } configMap.put("custom_sequence", sanitizedSequence); configMap.put("endpoint_type", endpointType); @@ -225,6 +225,9 @@ private static String renderCustomBackendSequence(String sequenceName, String pa String policyDirectory = pathToArchive + File.separator + ImportExportConstants.CUSTOM_BACKEND_DIRECTORY; String sequence = APIUtil.getCustomBackendSequenceFromFile(policyDirectory, sequenceName, APIConstants.SYNAPSE_POLICY_DEFINITION_EXTENSION_XML); + if (sequence == null) { + return null; + } return renderPolicyTemplate(sequence, new HashMap<>()); } 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/ExportUtils.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/ExportUtils.java index e19110ce479f..22f409b3c195 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/ExportUtils.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/ExportUtils.java @@ -645,13 +645,15 @@ public static void addCustomBackendToArchive(String archivePath, APIProvider api SequenceBackendData data = apiProvider.getCustomBackendByAPIUUID(apiUUID, APIConstants.API_KEY_TYPE_PRODUCTION); if (data != null) { - exportCustomBackend(data.getName(), data.getSequence(), archivePath); + String seqName = APIUtil.getCustomBackendName(apiUUID, APIConstants.API_KEY_TYPE_PRODUCTION); + exportCustomBackend(seqName, data.getSequence(), archivePath); } // Add sandbox Backend Sequences data = apiProvider.getCustomBackendByAPIUUID(apiUUID, APIConstants.API_KEY_TYPE_SANDBOX); if (data != null) { - exportCustomBackend(data.getName(), data.getSequence(), archivePath); + String seqName = APIUtil.getCustomBackendName(apiUUID, APIConstants.API_KEY_TYPE_SANDBOX); + exportCustomBackend(seqName, data.getSequence(), archivePath); } } catch (IOException | APIImportExportException ex) { 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 0dca6636bc74..9e573af54b52 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 @@ -698,21 +698,18 @@ public static void updateAPIWithCustomBackend(API api, String extractedFolderPat if (endpointConfig != null) { if (endpointConfig.get("sandbox") != null) { - JsonObject sandboxConf = endpointConfig.get("sandbox").getAsJsonObject(); - String seqFile = sandboxConf.get("sequence_file").getAsString(); - String seqName = APIUtil.getCustomBackendName(api.getUuid(), APIConstants.API_KEY_TYPE_SANDBOX); + String seqFile = endpointConfig.get("sandbox").getAsString(); String seqId = UUID.randomUUID().toString(); InputStream seq = APIUtil.getCustomBackendSequence(customBackendDir, seqFile, ".xml"); - apiProvider.updateCustomBackend(api.getUuid(), APIConstants.API_KEY_TYPE_SANDBOX, seq, seqName, seqId); + apiProvider.updateCustomBackend(api.getUuid(), APIConstants.API_KEY_TYPE_SANDBOX, seq, seqFile + ".xml", + seqId); } if (endpointConfig.get("production") != null) { - JsonObject prodConf = endpointConfig.get("production").getAsJsonObject(); - String seqFile = prodConf.get("sequence_file").getAsString(); - String seqName = APIUtil.getCustomBackendName(api.getUuid(), APIConstants.API_KEY_TYPE_PRODUCTION); + String seqFile = endpointConfig.get("production").getAsString(); String seqId = UUID.randomUUID().toString(); InputStream seq = APIUtil.getCustomBackendSequence(customBackendDir, seqFile, ".xml"); - apiProvider.updateCustomBackend(api.getUuid(), APIConstants.API_KEY_TYPE_PRODUCTION, seq, seqName, - seqId); + apiProvider.updateCustomBackend(api.getUuid(), APIConstants.API_KEY_TYPE_PRODUCTION, seq, + seqFile + ".xml", seqId); } } } 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 d32845665b30..6a59ce728bb4 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 @@ -21,6 +21,8 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; import graphql.schema.GraphQLSchema; import graphql.schema.idl.SchemaParser; import graphql.schema.idl.TypeDefinitionRegistry; @@ -190,8 +192,13 @@ public static API updateApi(API originalAPI, APIDTO apiDtoToUpdate, APIProvider API apiToUpdate = prepareForUpdateApi(originalAPI, apiDtoToUpdate, apiProvider, tokenScopes); apiProvider.updateAPI(apiToUpdate, originalAPI); - - return apiProvider.getAPIbyUUID(originalAPI.getUuid(), originalAPI.getOrganization()); + API apiUpdated = apiProvider.getAPIbyUUID(originalAPI.getUuid(), originalAPI.getOrganization()); + JsonObject endpointConfig = JsonParser.parseString(apiUpdated.getEndpointConfig()).getAsJsonObject(); + if (!APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( + endpointConfig.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE).getAsString())) { + apiProvider.deleteSequenceBackendByRevision(apiUpdated.getUuid(), "0"); + } + return apiUpdated; // TODO use returend api } @@ -212,7 +219,7 @@ public static void updateCustomBackend(API api, APIProvider apiProvider, String } String seqName = APIUtil.getCustomBackendName(api.getUuid(), endpointType); String customBackendUUID = UUID.randomUUID().toString(); - apiProvider.updateCustomBackend(api.getUuid(), endpointType, customBackend, seqName, customBackendUUID); + apiProvider.updateCustomBackend(api.getUuid(), endpointType, customBackend, fileName, customBackendUUID); } private static String getFileNameFromContentDisposition(String contentDisposition) { diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApi.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApi.java index 96016012b1b1..7b687a1250a5 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApi.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/ApisApi.java @@ -1405,7 +1405,7 @@ public Response getOperationPolicyForAPIByPolicyId(@ApiParam(value = "**API ID** @ApiResponse(code = 200, message = "OK. Requested API Custom Backend is returned ", response = File.class), @ApiResponse(code = 404, message = "Not Found. The specified resource does not exist.", response = ErrorDTO.class), @ApiResponse(code = 406, message = "Not Acceptable. The requested media type is not supported.", response = ErrorDTO.class) }) - public Response getSequenceBackendContent( @NotNull @Size(max=15) @ApiParam(value = "Type of the Endpoint. SANDBOX or PRODUCTION ",required=true) @QueryParam("type") String type, @ApiParam(value = "**API ID** consisting of the **UUID** of the API. ",required=true) @PathParam("apiId") String apiId) throws APIManagementException{ + public Response getSequenceBackendContent( @Size(max=15)@ApiParam(value = "Type of the Endpoint. SANDBOX or PRODUCTION ",required=true) @PathParam("type") String type, @ApiParam(value = "**API ID** consisting of the **UUID** of the API. ",required=true) @PathParam("apiId") String apiId) throws APIManagementException{ return delegate.getSequenceBackendContent(type, apiId, securityContext); } diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java index 77af8d3d461f..fba1e681dceb 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java @@ -256,8 +256,7 @@ public Response sequenceBackendUpdate(String apiId, InputStream sequenceInputStr MultivaluedMap headers = sequenceDetail.getHeaders(); String contentDecomp = headers.getFirst("Content-Disposition"); - PublisherCommonUtils.updateCustomBackend(api, apiProvider, type, sequenceInputStream, - contentDecomp); + PublisherCommonUtils.updateCustomBackend(api, apiProvider, type, sequenceInputStream, contentDecomp); return Response.ok().build(); } diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/resources/publisher-api.yaml b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/resources/publisher-api.yaml index b70f2c4ea170..b5e358788c95 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/resources/publisher-api.yaml +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/resources/publisher-api.yaml @@ -496,7 +496,7 @@ paths: operationId: getSequenceBackendContent parameters: - name: type - in: query + in: path description: | Type of the Endpoint. SANDBOX or PRODUCTION From 1798fa5f0523d3c390ba0f8403e8e97d57e4f241 Mon Sep 17 00:00:00 2001 From: BLasan Date: Tue, 17 Sep 2024 11:38:07 +0530 Subject: [PATCH 33/56] Add: API Validation for Sequence Backend --- .../main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java | 4 +--- .../carbon/apimgt/persistence/RegistryPersistenceImpl.java | 2 +- .../rest/api/publisher/v1/common/TemplateBuilderUtil.java | 4 +++- .../rest/api/publisher/v1/common/mappings/ExportUtils.java | 5 ++++- 4 files changed, 9 insertions(+), 6 deletions(-) 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 597e8b5db64f..f8bb191eb6b5 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 @@ -2933,10 +2933,8 @@ public static void validateAPIEndpointConfig(Object endpointConfigObject, String throws APIManagementException { Map endpointConfigMap = (Map) endpointConfigObject; - if (endpointConfigMap.containsKey("endpoint_type") && endpointConfigMap.containsKey("sequence_name") - && APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( + if (endpointConfigMap.containsKey("endpoint_type") && APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( endpointConfigMap.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE)) - && !APIConstants.APITransportType.GRAPHQL.toString().equalsIgnoreCase(apiType) && !APIConstants.API_TYPE_HTTP.equalsIgnoreCase(apiType) && !APIConstants.API_TYPE_SOAPTOREST.equalsIgnoreCase(apiType)) { throw new APIManagementException("Invalid endpoint configuration provided for the API " + apiName); diff --git a/components/apimgt/org.wso2.carbon.apimgt.persistence/src/main/java/org/wso2/carbon/apimgt/persistence/RegistryPersistenceImpl.java b/components/apimgt/org.wso2.carbon.apimgt.persistence/src/main/java/org/wso2/carbon/apimgt/persistence/RegistryPersistenceImpl.java index e70fa9d7138c..4bbea252cbf8 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.persistence/src/main/java/org/wso2/carbon/apimgt/persistence/RegistryPersistenceImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.persistence/src/main/java/org/wso2/carbon/apimgt/persistence/RegistryPersistenceImpl.java @@ -393,7 +393,7 @@ public void restoreAPIRevision(Organization org, String apiUUID, String revision GenericArtifact artifact = getAPIArtifact(apiUUID, registry); artifact.setAttribute(APIConstants.API_OVERVIEW_STATUS, lifecycleStatus); // Update with the modified artifact - artifactManager.updateGenericArtifact(artifact); + artifactManager.updateGenericArtifact(apiArtifact); RegistryPersistenceUtil.clearResourcePermissions(apiPath, api.getId(), ((UserRegistry) registry).getTenantId()); RegistryPersistenceUtil.setResourcePermissions(api.getId().getProviderName(), api.getVisibility(), 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/TemplateBuilderUtil.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/TemplateBuilderUtil.java index 6d4da46f3abd..cce9c326c27e 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/TemplateBuilderUtil.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/TemplateBuilderUtil.java @@ -776,7 +776,9 @@ 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())) { + 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); if (gatewayCustomBackendSequenceDTO != null) { 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/ExportUtils.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/ExportUtils.java index 22f409b3c195..746dff5c4378 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/ExportUtils.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/ExportUtils.java @@ -220,7 +220,10 @@ public static File exportApi(APIProvider apiProvider, APIIdentifier apiIdentifie JsonObject endpointConfig = JsonParser.parseString(api.getEndpointConfig()).getAsJsonObject(); if (APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( - endpointConfig.get(API_ENDPOINT_CONFIG_PROTOCOL_TYPE).getAsString())) { + endpointConfig.get(API_ENDPOINT_CONFIG_PROTOCOL_TYPE).getAsString()) && StringUtils.equals( + apiDtoToReturn.getType().toString().toLowerCase(), APIConstants.API_TYPE_HTTP.toLowerCase()) + || StringUtils.equals(apiDtoToReturn.getType().toString().toLowerCase(), + APIConstants.API_TYPE_SOAPTOREST.toLowerCase())) { addCustomBackendToArchive(archivePath, apiProvider, currentApiUuid); } From 63f10f61a6c522b72a59607c333c4223a770878c Mon Sep 17 00:00:00 2001 From: BLasan Date: Tue, 17 Sep 2024 17:50:11 +0530 Subject: [PATCH 34/56] Add: NPE handling --- .../carbon/apimgt/impl/utils/APIUtil.java | 16 +++++---- .../apimgt/impl/utils/GatewayUtils.java | 6 ++-- .../v1/common/mappings/ExportUtils.java | 16 +++++---- .../v1/common/mappings/ImportUtils.java | 34 ++++++++++--------- .../common/mappings/PublisherCommonUtils.java | 14 +++++--- .../publisher/v1/impl/ApisApiServiceImpl.java | 2 +- 6 files changed, 51 insertions(+), 37 deletions(-) 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 f8bb191eb6b5..d3a7ce102ee0 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 @@ -2931,13 +2931,15 @@ public static void validateAPIContext(String context, String apiName) throws API */ public static void validateAPIEndpointConfig(Object endpointConfigObject, String apiType, String apiName) throws APIManagementException { - Map endpointConfigMap = (Map) endpointConfigObject; - - if (endpointConfigMap.containsKey("endpoint_type") && APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( - endpointConfigMap.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE)) - && !APIConstants.API_TYPE_HTTP.equalsIgnoreCase(apiType) - && !APIConstants.API_TYPE_SOAPTOREST.equalsIgnoreCase(apiType)) { - throw new APIManagementException("Invalid endpoint configuration provided for the API " + apiName); + if (endpointConfigObject != null) { + Map endpointConfigMap = (Map) endpointConfigObject; + if (endpointConfigMap != null && endpointConfigMap.containsKey("endpoint_type") + && APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( + endpointConfigMap.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE)) + && !APIConstants.API_TYPE_HTTP.equalsIgnoreCase(apiType) + && !APIConstants.API_TYPE_SOAPTOREST.equalsIgnoreCase(apiType)) { + throw new APIManagementException("Invalid endpoint configuration provided for the API " + apiName); + } } } diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/GatewayUtils.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/GatewayUtils.java index 599b2f14e94f..62dc26ca8544 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/GatewayUtils.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/GatewayUtils.java @@ -47,9 +47,11 @@ public static void setCustomSequencesToBeRemoved(API api, GatewayAPIDTO gatewayA } public static void setCustomBackendToBeRemoved(GatewayAPIDTO gatewayAPIDTO) { - String sandBoxBackend = APIUtil.getCustomBackendName(gatewayAPIDTO.getApiId(), "SANDBOX"); + String sandBoxBackend = APIUtil.getCustomBackendName(gatewayAPIDTO.getApiId(), + APIConstants.API_KEY_TYPE_SANDBOX); gatewayAPIDTO.setSequencesToBeRemove(addStringToList(sandBoxBackend, gatewayAPIDTO.getSequencesToBeRemove())); - String productionBackend = APIUtil.getCustomBackendName(gatewayAPIDTO.getApiId(), "PRODUCTION"); + String productionBackend = APIUtil.getCustomBackendName(gatewayAPIDTO.getApiId(), + APIConstants.API_KEY_TYPE_PRODUCTION); gatewayAPIDTO.setSequencesToBeRemove( addStringToList(productionBackend, gatewayAPIDTO.getSequencesToBeRemove())); } 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/ExportUtils.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/ExportUtils.java index 746dff5c4378..47eae3fe4aff 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/ExportUtils.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/ExportUtils.java @@ -218,13 +218,15 @@ public static File exportApi(APIProvider apiProvider, APIIdentifier apiIdentifie addOperationPoliciesToArchive(archivePath, tenantDomain, exportFormat, apiProvider, api, currentApiUuid); - JsonObject endpointConfig = JsonParser.parseString(api.getEndpointConfig()).getAsJsonObject(); - if (APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( - endpointConfig.get(API_ENDPOINT_CONFIG_PROTOCOL_TYPE).getAsString()) && StringUtils.equals( - apiDtoToReturn.getType().toString().toLowerCase(), APIConstants.API_TYPE_HTTP.toLowerCase()) - || StringUtils.equals(apiDtoToReturn.getType().toString().toLowerCase(), - APIConstants.API_TYPE_SOAPTOREST.toLowerCase())) { - addCustomBackendToArchive(archivePath, apiProvider, currentApiUuid); + if (api != null && !StringUtils.isEmpty(api.getEndpointConfig())) { + JsonObject endpointConfig = JsonParser.parseString(api.getEndpointConfig()).getAsJsonObject(); + if (APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( + endpointConfig.get(API_ENDPOINT_CONFIG_PROTOCOL_TYPE).getAsString()) && StringUtils.equals( + apiDtoToReturn.getType().toString().toLowerCase(), APIConstants.API_TYPE_HTTP.toLowerCase()) + || StringUtils.equals(apiDtoToReturn.getType().toString().toLowerCase(), + APIConstants.API_TYPE_SOAPTOREST.toLowerCase())) { + addCustomBackendToArchive(archivePath, apiProvider, currentApiUuid); + } } addGatewayEnvironmentsToArchive(archivePath, apiDtoToReturn.getId(), exportFormat, apiProvider); 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 9e573af54b52..10a769baf850 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 @@ -694,22 +694,24 @@ 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(); - - if (endpointConfig != null) { - if (endpointConfig.get("sandbox") != null) { - String seqFile = endpointConfig.get("sandbox").getAsString(); - String seqId = UUID.randomUUID().toString(); - InputStream seq = APIUtil.getCustomBackendSequence(customBackendDir, seqFile, ".xml"); - apiProvider.updateCustomBackend(api.getUuid(), APIConstants.API_KEY_TYPE_SANDBOX, seq, seqFile + ".xml", - seqId); - } - if (endpointConfig.get("production") != null) { - String seqFile = endpointConfig.get("production").getAsString(); - String seqId = UUID.randomUUID().toString(); - InputStream seq = APIUtil.getCustomBackendSequence(customBackendDir, seqFile, ".xml"); - apiProvider.updateCustomBackend(api.getUuid(), APIConstants.API_KEY_TYPE_PRODUCTION, seq, - seqFile + ".xml", seqId); + if (api != null && !StringUtils.isEmpty(api.getEndpointConfig())) { + JsonObject endpointConfig = JsonParser.parseString(api.getEndpointConfig()).getAsJsonObject(); + + if (endpointConfig != null) { + if (endpointConfig.get("sandbox") != null) { + String seqFile = endpointConfig.get("sandbox").getAsString(); + String seqId = UUID.randomUUID().toString(); + InputStream seq = APIUtil.getCustomBackendSequence(customBackendDir, seqFile, ".xml"); + apiProvider.updateCustomBackend(api.getUuid(), APIConstants.API_KEY_TYPE_SANDBOX, seq, + seqFile + ".xml", seqId); + } + if (endpointConfig.get("production") != null) { + String seqFile = endpointConfig.get("production").getAsString(); + String seqId = UUID.randomUUID().toString(); + InputStream seq = APIUtil.getCustomBackendSequence(customBackendDir, seqFile, ".xml"); + apiProvider.updateCustomBackend(api.getUuid(), APIConstants.API_KEY_TYPE_PRODUCTION, seq, + seqFile + ".xml", seqId); + } } } } 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 6a59ce728bb4..722f4be49416 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 @@ -193,10 +193,14 @@ public static API updateApi(API originalAPI, APIDTO apiDtoToUpdate, APIProvider API apiToUpdate = prepareForUpdateApi(originalAPI, apiDtoToUpdate, apiProvider, tokenScopes); apiProvider.updateAPI(apiToUpdate, originalAPI); API apiUpdated = apiProvider.getAPIbyUUID(originalAPI.getUuid(), originalAPI.getOrganization()); - JsonObject endpointConfig = JsonParser.parseString(apiUpdated.getEndpointConfig()).getAsJsonObject(); - if (!APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( - endpointConfig.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE).getAsString())) { - apiProvider.deleteSequenceBackendByRevision(apiUpdated.getUuid(), "0"); + if (apiUpdated != null && !StringUtils.isEmpty(apiUpdated.getEndpointConfig())) { + JsonObject endpointConfig = JsonParser.parseString(apiUpdated.getEndpointConfig()).getAsJsonObject(); + if (!APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( + endpointConfig.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE).getAsString()) && ( + APIConstants.API_TYPE_HTTP.equals(apiUpdated.getType()) || APIConstants.API_TYPE_SOAPTOREST.equals( + apiUpdated.getType()))) { + apiProvider.deleteSequenceBackendByRevision(apiUpdated.getUuid(), "0"); + } } return apiUpdated; // TODO use returend api @@ -258,6 +262,8 @@ private static API prepareForUpdateApi(API originalAPI, APIDTO apiDtoToUpdate, A + " as the token information hasn't been correctly set internally", ExceptionCodes.TOKEN_SCOPES_NOT_SET); } + APIUtil.validateAPIEndpointConfig(apiDtoToUpdate.getEndpointConfig(), apiDtoToUpdate.getType().toString(), + apiDtoToUpdate.getName()); if (apiDtoToUpdate.getVisibility() == APIDTO.VisibilityEnum.RESTRICTED && apiDtoToUpdate.getVisibleRoles() .isEmpty()) { throw new APIManagementException("Access control roles cannot be empty when visibility is restricted", diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java index fba1e681dceb..ce93509652e3 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java @@ -3953,7 +3953,7 @@ public Response deployAPIRevision(String apiId, String revisionId, // Cannot deploy an API with custom sequence to the APK gateway Map endpointConfigMap = (Map) apiDto.getEndpointConfig(); - if (APIConstants.WSO2_APK_GATEWAY.equals(apiDto.getGatewayType()) && APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( + if (!APIConstants.WSO2_SYNAPSE_GATEWAY.equals(apiDto.getGatewayType()) && APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( endpointConfigMap.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE))) { return Response.status(Response.Status.BAD_REQUEST) .entity("Cannot Deploy an API with a Custom Sequence to APK Gateway: " + apiId).build(); From df82c206ce0c232e27bce6aad9a45eb850b6ab85 Mon Sep 17 00:00:00 2001 From: BLasan Date: Tue, 17 Sep 2024 20:38:59 +0530 Subject: [PATCH 35/56] Fix: Sequence Backend Download Issue --- .../rest/api/publisher/v1/utils/RestApiPublisherUtils.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/utils/RestApiPublisherUtils.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/utils/RestApiPublisherUtils.java index d03f0c548703..cd7022132f65 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/utils/RestApiPublisherUtils.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/utils/RestApiPublisherUtils.java @@ -280,9 +280,11 @@ public static String getContentType(Attachment fileDetail) { public static File exportCustomBackendData(String seq, String seqName) throws APIManagementException { try { // Provided Sequence Name by the user - String customBackendName = seqName + APIConstants.SYNAPSE_POLICY_DEFINITION_EXTENSION_XML; + String customBackendName = seqName; + if (!customBackendName.contains(".xml")) { + customBackendName = seqName + APIConstants.SYNAPSE_POLICY_DEFINITION_EXTENSION_XML; + } CommonUtil.writeFile(customBackendName, seq); - FileUtils.deleteQuietly(new File(customBackendName)); return new File(customBackendName); } catch (APIImportExportException ex) { throw new APIManagementException("Error when exporting Custom Backend: " + seqName, ex); From 63ae2cf0a46d5ac57c4ff7fb4b61ce06ace57cad Mon Sep 17 00:00:00 2001 From: BLasan Date: Tue, 17 Sep 2024 21:35:45 +0530 Subject: [PATCH 36/56] Fix: Advertised only API Issue --- .../rest/api/publisher/v1/common/TemplateBuilderUtil.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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/TemplateBuilderUtil.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/TemplateBuilderUtil.java index cce9c326c27e..a62f1b9b23d7 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/TemplateBuilderUtil.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/TemplateBuilderUtil.java @@ -722,12 +722,14 @@ private static GatewayAPIDTO createAPIGatewayDTOtoPublishAPI(Environment environ GatewayUtils.setCustomSequencesToBeRemoved(apiProduct.getId(), api.getUuid(), productAPIDto); APITemplateBuilder apiTemplateBuilder = new APITemplateBuilderImpl(api, apiProduct); // check the endpoint type - if (api.getEndpointConfig() != null) { + if (!StringUtils.isEmpty(api.getEndpointConfig())) { JsonObject endpointConfObj = JsonParser.parseString(api.getEndpointConfig()).getAsJsonObject(); if (!APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( endpointConfObj.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE).getAsString())) { addEndpoints(api, apiTemplateBuilder, productAPIDto); } + } else { + addEndpoints(api, apiTemplateBuilder, productAPIDto); } setCustomSequencesToBeAdded(apiProduct, api, productAPIDto, apiExtractedPath, apidto); setAPIFaultSequencesToBeAdded(api, productAPIDto, apiExtractedPath, apidto); From 2edd31ef24d3b2edca84a962c30cab600d03b5a0 Mon Sep 17 00:00:00 2001 From: BLasan Date: Wed, 18 Sep 2024 19:25:09 +0530 Subject: [PATCH 37/56] Add: Validations for APICTL Export scenario --- .../carbon/apimgt/impl/utils/APIUtil.java | 3 +- .../v1/common/mappings/ExportUtils.java | 28 +++++++++++++------ 2 files changed, 21 insertions(+), 10 deletions(-) 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 d3a7ce102ee0..9fc9175ae030 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 @@ -2936,8 +2936,7 @@ public static void validateAPIEndpointConfig(Object endpointConfigObject, String if (endpointConfigMap != null && endpointConfigMap.containsKey("endpoint_type") && APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( endpointConfigMap.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE)) - && !APIConstants.API_TYPE_HTTP.equalsIgnoreCase(apiType) - && !APIConstants.API_TYPE_SOAPTOREST.equalsIgnoreCase(apiType)) { + && !APIConstants.API_TYPE_HTTP.equalsIgnoreCase(apiType)) { throw new APIManagementException("Invalid endpoint configuration provided for the API " + apiName); } } 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/ExportUtils.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/ExportUtils.java index 47eae3fe4aff..2907ff35edf3 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/ExportUtils.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/ExportUtils.java @@ -222,10 +222,16 @@ public static File exportApi(APIProvider apiProvider, APIIdentifier apiIdentifie JsonObject endpointConfig = JsonParser.parseString(api.getEndpointConfig()).getAsJsonObject(); if (APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( endpointConfig.get(API_ENDPOINT_CONFIG_PROTOCOL_TYPE).getAsString()) && StringUtils.equals( - apiDtoToReturn.getType().toString().toLowerCase(), APIConstants.API_TYPE_HTTP.toLowerCase()) - || StringUtils.equals(apiDtoToReturn.getType().toString().toLowerCase(), - APIConstants.API_TYPE_SOAPTOREST.toLowerCase())) { - addCustomBackendToArchive(archivePath, apiProvider, currentApiUuid); + apiDtoToReturn.getType().toString().toLowerCase(), APIConstants.API_TYPE_HTTP.toLowerCase())) { + String sandSequenceName = null; + String prodSequenceName = null; + if (endpointConfig.get("sandbox") != null) { + sandSequenceName = endpointConfig.get("sandbox").getAsString(); + } + if (endpointConfig.get("production") != null) { + prodSequenceName = endpointConfig.get("production").getAsString(); + } + addCustomBackendToArchive(archivePath, apiProvider, currentApiUuid, sandSequenceName, prodSequenceName); } } @@ -641,8 +647,8 @@ public static void addEndpointCertificatesToArchive(String archivePath, APIDTO a } } - public static void addCustomBackendToArchive(String archivePath, APIProvider apiProvider, String apiUUID) - throws APIManagementException { + public static void addCustomBackendToArchive(String archivePath, APIProvider apiProvider, String apiUUID, + String prodBackendName, String sandBackendName) throws APIManagementException { try { CommonUtil.createDirectory(archivePath + File.separator + ImportExportConstants.CUSTOM_BACKEND_DIRECTORY); @@ -650,14 +656,20 @@ public static void addCustomBackendToArchive(String archivePath, APIProvider api SequenceBackendData data = apiProvider.getCustomBackendByAPIUUID(apiUUID, APIConstants.API_KEY_TYPE_PRODUCTION); if (data != null) { - String seqName = APIUtil.getCustomBackendName(apiUUID, APIConstants.API_KEY_TYPE_PRODUCTION); + String seqName = prodBackendName; + if (StringUtils.isEmpty(seqName)) { + seqName = APIUtil.getCustomBackendName(apiUUID, APIConstants.API_KEY_TYPE_PRODUCTION); + } exportCustomBackend(seqName, data.getSequence(), archivePath); } // Add sandbox Backend Sequences data = apiProvider.getCustomBackendByAPIUUID(apiUUID, APIConstants.API_KEY_TYPE_SANDBOX); if (data != null) { - String seqName = APIUtil.getCustomBackendName(apiUUID, APIConstants.API_KEY_TYPE_SANDBOX); + String seqName = sandBackendName; + if (StringUtils.isEmpty(seqName)) { + seqName = APIUtil.getCustomBackendName(apiUUID, APIConstants.API_KEY_TYPE_SANDBOX); + } exportCustomBackend(seqName, data.getSequence(), archivePath); } From 720909f7964cc90c135d2808754dfc15a4cb5be5 Mon Sep 17 00:00:00 2001 From: BLasan Date: Thu, 19 Sep 2024 06:38:25 +0530 Subject: [PATCH 38/56] Rename: Custom Backend dir to Sequence Backend --- .../carbon/apimgt/impl/importexport/ImportExportConstants.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/importexport/ImportExportConstants.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/importexport/ImportExportConstants.java index 885fc5c595e7..6aca3a4a3696 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/importexport/ImportExportConstants.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/importexport/ImportExportConstants.java @@ -309,7 +309,7 @@ public final class ImportExportConstants { public static final String DISPLAY_ON_DEVPORTAL_OPTION = "displayOnDevportal"; public static final String POLICIES_DIRECTORY = "Policies"; - public static final String CUSTOM_BACKEND_DIRECTORY = "Custom-Backend"; + public static final String CUSTOM_BACKEND_DIRECTORY = "Sequence-Backend"; public static final String SWAGGER_X_WSO2_APICTL_INIT = "x-wso2-apictl-init"; public static final String EXPORT_POLICY_TYPE_YAML = "YAML"; From ffac3a3b4d3a1705eea31b31de1785b33df6e734 Mon Sep 17 00:00:00 2001 From: BLasan Date: Thu, 19 Sep 2024 06:55:09 +0530 Subject: [PATCH 39/56] Rename: Custom Backend dir to Sequence Backend --- .../impl/dao/constants/SQLConstants.java | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) 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 334c0b0b6b2e..e3c7708c7076 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 @@ -4476,18 +4476,18 @@ public static class TransactionCountConstants { } 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) " + "INSERT INTO AM_API_SEQUENCE_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 TYPE = ? 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 = ?"; - public static final String GET_CUSTOM_BACKEND_OF_API_DEFAULT_REVISION = "SELECT ACB.NAME, ACB.TYPE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ? AND REVISION_UUID = '0'"; - public static final String GET_REVISION_SPECIFIC_CUSTOM_BACKEND_FROM_SEQUENCE_ID = "SELECT ACB.ID, ACB.NAME, ACB.SEQUENCE, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.ID = ? AND ACB.REVISION_UUID = ? AND ACB.TYPE = ?"; - public static final String GET_API_SPECIFIC_CUSTOM_BACKEND_FROM_SEQUENCE_ID = "SELECT ACB.ID, ACB.NAME, ACB.SEQUENCE, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.API_UUID = ? AND ACB.REVISION_UUID = '0' AND ACB.TYPE = ?"; - public static final String GET_ALL_API_SPECIFIC_CUSTOM_BACKENDS = "SELECT ACB.ID, ACB.NAME, ACB.SEQUENCE, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.API_UUID = ? AND ACB.REVISION_UUID = '0'"; + public static final String DELETE_WORKING_COPY_OF_CUSTOM_BACKEND = "DELETE FROM AM_API_SEQUENCE_BACKEND WHERE API_UUID = ? AND REVISION_UUID = '0'"; + public static final String DELETE_CUSTOM_BACKEND = "DELETE FROM AM_API_SEQUENCE_BACKEND WHERE API_UUID = ? AND TYPE = ? AND REVISION_UUID = '0'"; + public static final String DELETE_CUSTOM_BACKEND_BY_API_AND_TYPE = "DELETE FROM AM_API_SEQUENCE_BACKEND WHERE API_UUID = ? AND TYPE = ? AND REVISION_UUID = '0'"; + public static final String DELETE_CUSTOM_BACKEND_BY_REVISION = "DELETE FROM AM_API_SEQUENCE_BACKEND WHERE API_UUID = ? AND REVISION_UUID = ?"; + public static final String DELETE_CUSTOM_BACKEND_BY_API = "DELETE FROM AM_API_SEQUENCE_BACKEND WHERE API_UUID = ?"; + public static final String GET_CUSTOM_BACKEND_OF_API_REVISION = "SELECT ID, NAME, SEQUENCE, TYPE FROM AM_API_SEQUENCE_BACKEND WHERE API_UUID = ? AND REVISION_UUID = ?"; + public static final String GET_CUSTOM_BACKEND_OF_API_DEFAULT_REVISION = "SELECT ACB.NAME, ACB.TYPE FROM AM_API_SEQUENCE_BACKEND WHERE API_UUID = ? AND REVISION_UUID = '0'"; + public static final String GET_REVISION_SPECIFIC_CUSTOM_BACKEND_FROM_SEQUENCE_ID = "SELECT ACB.ID, ACB.NAME, ACB.SEQUENCE, ACB.TYPE FROM AM_API_SEQUENCE_BACKEND ACB WHERE ACB.ID = ? AND ACB.REVISION_UUID = ? AND ACB.TYPE = ?"; + public static final String GET_API_SPECIFIC_CUSTOM_BACKEND_FROM_SEQUENCE_ID = "SELECT ACB.ID, ACB.NAME, ACB.SEQUENCE, ACB.TYPE FROM AM_API_SEQUENCE_BACKEND ACB WHERE ACB.API_UUID = ? AND ACB.REVISION_UUID = '0' AND ACB.TYPE = ?"; + public static final String GET_ALL_API_SPECIFIC_CUSTOM_BACKENDS = "SELECT ACB.ID, ACB.NAME, ACB.SEQUENCE, ACB.TYPE FROM AM_API_SEQUENCE_BACKEND ACB WHERE ACB.API_UUID = ? AND ACB.REVISION_UUID = '0'"; } } From a5c791bf3bd0c82fdeb06924458a61dc06fba5b1 Mon Sep 17 00:00:00 2001 From: BLasan Date: Thu, 19 Sep 2024 06:55:31 +0530 Subject: [PATCH 40/56] Add: Schema for all database types --- .../src/test/resources/dbscripts/h2.sql | 2 +- .../src/main/resources/sql/h2.sql | 6 +++--- .../src/main/resources/sql/mysql.sql | 6 +++--- .../src/main/resources/sql/oracle.sql | 12 ++++++++++++ .../src/main/resources/sql/oracle_23c.sql | 12 ++++++++++++ .../src/main/resources/sql/oracle_rac.sql | 12 ++++++++++++ .../src/main/resources/sql/postgresql.sql | 11 +++++++++++ 7 files changed, 54 insertions(+), 7 deletions(-) diff --git a/components/apimgt/org.wso2.carbon.apimgt.keymgt/src/test/resources/dbscripts/h2.sql b/components/apimgt/org.wso2.carbon.apimgt.keymgt/src/test/resources/dbscripts/h2.sql index 654ad3d51f17..ba3921bd77e6 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.keymgt/src/test/resources/dbscripts/h2.sql +++ b/components/apimgt/org.wso2.carbon.apimgt.keymgt/src/test/resources/dbscripts/h2.sql @@ -1310,7 +1310,7 @@ CREATE TABLE IF NOT EXISTS AM_API ( UNIQUE (API_PROVIDER,API_NAME,API_VERSION) ); -CREATE TABLE IF NOT EXISTS AM_API_CUSTOM_BACKEND ( +CREATE TABLE IF NOT EXISTS AM_API_SEQUENCE_BACKEND ( ID VARCHAR(60) NOT NULL, API_UUID VARCHAR(256) NOT NULL, REVISION_UUID VARCHAR(256) DEFAULT '0', diff --git a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/h2.sql b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/h2.sql index 417343094cbb..638b489ed955 100644 --- a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/h2.sql +++ b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/h2.sql @@ -1585,14 +1585,14 @@ CREATE TABLE IF NOT EXISTS AM_API ( UNIQUE (API_PROVIDER,API_NAME,API_VERSION,ORGANIZATION) ); -CREATE TABLE IF NOT EXISTS AM_API_CUSTOM_BACKEND ( +CREATE TABLE IF NOT EXISTS AM_API_SEQUENCE_BACKEND ( ID VARCHAR(60) NOT NULL, API_UUID VARCHAR(256) NOT NULL, REVISION_UUID VARCHAR(256) DEFAULT '0', - SEQUENCE LONGBLOB, + SEQUENCE LONGBLOB NOT NULL, NAME VARCHAR(256) NOT NULL, TYPE VARCHAR(120) NOT NULL, - PRIMARY KEY(ID, API_UUID, REVISION_UUID, TYPE), + PRIMARY KEY(ID), FOREIGN KEY(API_UUID) REFERENCES AM_API(API_UUID) ON DELETE CASCADE ); diff --git a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/mysql.sql b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/mysql.sql index 7785a21f142f..c9ecc963651e 100644 --- a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/mysql.sql +++ b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/mysql.sql @@ -1524,14 +1524,14 @@ CREATE TABLE IF NOT EXISTS AM_API ( UNIQUE (API_UUID) )ENGINE INNODB; -CREATE TABLE IF NOT EXISTS AM_API_CUSTOM_BACKEND ( +CREATE TABLE IF NOT EXISTS AM_API_SEQUENCE_BACKEND ( ID VARCHAR(60) NOT NULL, API_UUID VARCHAR(256) NOT NULL, REVISION_UUID VARCHAR(256) DEFAULT '0', - SEQUENCE LONGBLOB, + SEQUENCE LONGBLOB NOT NULL, NAME VARCHAR(256) NOT NULL, TYPE VARCHAR(120) NOT NULL, - PRIMARY KEY (ID, API_UUID, REVISION_UUID, TYPE), + PRIMARY KEY (ID), FOREIGN KEY (API_UUID) REFERENCES AM_API(API_UUID) ON DELETE CASCADE )ENGINE INNODB; diff --git a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/oracle.sql b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/oracle.sql index 1b5e1591a845..a9f914c4badc 100644 --- a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/oracle.sql +++ b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/oracle.sql @@ -3458,6 +3458,18 @@ CREATE TABLE AM_API_REVISION_METADATA ( ) / +CREATE TABLE AM_API_SEQUENCE_BACKEND ( + ID VARCHAR2(60) NOT NULL, + API_UUID VARCHAR2(256) NOT NULL, + REVISION_UUID VARCHAR2(256) DEFAULT '0', + SEQUENCE BLOB NOT NULL, + NAME VARCHAR2(256) NOT NULL, + TYPE VARCHAR2(120) NOT NULL, + PRIMARY KEY (ID), + FOREIGN KEY (API_UUID) REFERENCES AM_API(API_UUID) ON DELETE CASCADE +) +/ + CREATE TABLE AM_DEPLOYMENT_REVISION_MAPPING ( NAME VARCHAR(255) NOT NULL, VHOST VARCHAR(255) NULL, diff --git a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/oracle_23c.sql b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/oracle_23c.sql index 9988a92fe6eb..c9ca66b51e69 100644 --- a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/oracle_23c.sql +++ b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/oracle_23c.sql @@ -2471,6 +2471,18 @@ CREATE TABLE AM_API ( ) / +CREATE TABLE AM_API_SEQUENCE_BACKEND ( + ID VARCHAR2(60) NOT NULL, + API_UUID VARCHAR2(256) NOT NULL, + REVISION_UUID VARCHAR2(256) DEFAULT '0', + SEQUENCE BLOB NOT NULL, + NAME VARCHAR2(256) NOT NULL, + TYPE VARCHAR2(120) NOT NULL, + PRIMARY KEY (ID), + FOREIGN KEY (API_UUID) REFERENCES AM_API(API_UUID) ON DELETE CASCADE +) +/ + CREATE TABLE AM_GRAPHQL_COMPLEXITY ( UUID VARCHAR(256), API_ID INTEGER NOT NULL, diff --git a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/oracle_rac.sql b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/oracle_rac.sql index 582f2c25f263..53d99a30e37f 100644 --- a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/oracle_rac.sql +++ b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/oracle_rac.sql @@ -2461,6 +2461,18 @@ CREATE TABLE AM_API ( ) / +CREATE TABLE AM_API_SEQUENCE_BACKEND ( + ID VARCHAR2(60) NOT NULL, + API_UUID VARCHAR2(256) NOT NULL, + REVISION_UUID VARCHAR2(256) DEFAULT '0', + SEQUENCE BLOB NOT NULL, + NAME VARCHAR2(256) NOT NULL, + TYPE VARCHAR2(120) NOT NULL, + PRIMARY KEY (ID), + FOREIGN KEY (API_UUID) REFERENCES AM_API(API_UUID) ON DELETE CASCADE +) +/ + CREATE TABLE AM_GRAPHQL_COMPLEXITY ( UUID VARCHAR(256), API_ID INTEGER NOT NULL, diff --git a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/postgresql.sql b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/postgresql.sql index e07a424a2904..ae4f6a14f49f 100644 --- a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/postgresql.sql +++ b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/postgresql.sql @@ -1860,6 +1860,17 @@ CREATE TABLE IF NOT EXISTS AM_API ( UNIQUE (API_UUID) ); +CREATE TABLE IF NOT EXISTS AM_API_SEQUENCE_BACKEND ( + ID VARCHAR(60) NOT NULL, + API_UUID VARCHAR(256) NOT NULL, + REVISION_UUID VARCHAR(256) DEFAULT '0', + SEQUENCE BYTEA NOT NULL, + NAME VARCHAR(256) NOT NULL, + TYPE VARCHAR(120) NOT NULL, + PRIMARY KEY (ID), + FOREIGN KEY (API_UUID) REFERENCES AM_API(API_UUID) ON DELETE CASCADE +); + CREATE TABLE IF NOT EXISTS AM_GRAPHQL_COMPLEXITY ( UUID VARCHAR(256), API_ID INTEGER NOT NULL, From 3784395f4c763541ae33c2756b43f15388f3b7a2 Mon Sep 17 00:00:00 2001 From: BLasan Date: Thu, 19 Sep 2024 11:09:35 +0530 Subject: [PATCH 41/56] Add: APICTL Project export Endpoint Configuration --- .../v1/common/mappings/ExportUtils.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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/ExportUtils.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/ExportUtils.java index 2907ff35edf3..c4c0849a926d 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/ExportUtils.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/ExportUtils.java @@ -87,6 +87,7 @@ import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Set; import static org.wso2.carbon.apimgt.impl.APIConstants.API_DATA_PRODUCTION_ENDPOINTS; @@ -227,9 +228,24 @@ public static File exportApi(APIProvider apiProvider, APIIdentifier apiIdentifie String prodSequenceName = null; if (endpointConfig.get("sandbox") != null) { sandSequenceName = endpointConfig.get("sandbox").getAsString(); + } else { + sandSequenceName = apiProvider.getCustomBackendSequenceOfAPIByUUID(currentApiUuid, + APIConstants.API_KEY_TYPE_SANDBOX); } if (endpointConfig.get("production") != null) { prodSequenceName = endpointConfig.get("production").getAsString(); + } else { + prodSequenceName = apiProvider.getCustomBackendSequenceOfAPIByUUID(currentApiUuid, + APIConstants.API_KEY_TYPE_PRODUCTION); + } + if (apiDtoToReturn.getEndpointConfig() != null) { + Map endpointConf = (Map) apiDtoToReturn.getEndpointConfig(); + if (endpointConf != null && APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( + endpointConf.get(API_ENDPOINT_CONFIG_PROTOCOL_TYPE))) { + endpointConf.put("sandbox", sandSequenceName); + endpointConf.put("production", prodSequenceName); + apiDtoToReturn.setEndpointConfig(endpointConf); + } } addCustomBackendToArchive(archivePath, apiProvider, currentApiUuid, sandSequenceName, prodSequenceName); } From 87f2e12a4c1d463a7fcb4e261402cc6d3e3726e3 Mon Sep 17 00:00:00 2001 From: BLasan Date: Fri, 20 Sep 2024 08:21:48 +0530 Subject: [PATCH 42/56] Fix: Sequence Backend storing issue in APICTL project --- .../apimgt/gateway/InMemoryAPIDeployer.java | 2 + .../carbon/apimgt/impl/utils/APIUtil.java | 10 +- .../apimgt/impl/utils/GatewayUtils.java | 11 ++ .../v1/common/SynapsePolicyAggregator.java | 23 +++- .../v1/common/TemplateBuilderUtil.java | 120 +++++++++++++----- .../v1/common/mappings/ExportUtils.java | 48 +++---- .../v1/common/mappings/ImportUtils.java | 16 ++- 7 files changed, 163 insertions(+), 67 deletions(-) diff --git a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/InMemoryAPIDeployer.java b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/InMemoryAPIDeployer.java index 7cb4200c1feb..768ee1bee2db 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/InMemoryAPIDeployer.java +++ b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/InMemoryAPIDeployer.java @@ -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(), 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 9fc9175ae030..a81bf74ad87f 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 @@ -10054,7 +10054,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 { @@ -10116,7 +10119,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); diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/GatewayUtils.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/GatewayUtils.java index 62dc26ca8544..bb1f8537a997 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/GatewayUtils.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/GatewayUtils.java @@ -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) { 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/SynapsePolicyAggregator.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/SynapsePolicyAggregator.java index c235b7d9d64d..c204989287fb 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/SynapsePolicyAggregator.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/SynapsePolicyAggregator.java @@ -107,13 +107,30 @@ public static String generatePolicySequenceForUriTemplateSet(Set 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 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 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; } 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/TemplateBuilderUtil.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/TemplateBuilderUtil.java index a62f1b9b23d7..8c59178d3b18 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/TemplateBuilderUtil.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/TemplateBuilderUtil.java @@ -46,11 +46,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; @@ -92,6 +94,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 clientCertificateDTOSProduction, @@ -571,14 +574,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)); } @@ -778,16 +797,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, @@ -1491,32 +1509,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; 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/ExportUtils.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/ExportUtils.java index c4c0849a926d..b35d2a235eea 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/ExportUtils.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/ExportUtils.java @@ -224,30 +224,24 @@ public static File exportApi(APIProvider apiProvider, APIIdentifier apiIdentifie if (APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( endpointConfig.get(API_ENDPOINT_CONFIG_PROTOCOL_TYPE).getAsString()) && StringUtils.equals( apiDtoToReturn.getType().toString().toLowerCase(), APIConstants.API_TYPE_HTTP.toLowerCase())) { - String sandSequenceName = null; - String prodSequenceName = null; - if (endpointConfig.get("sandbox") != null) { - sandSequenceName = endpointConfig.get("sandbox").getAsString(); - } else { - sandSequenceName = apiProvider.getCustomBackendSequenceOfAPIByUUID(currentApiUuid, - APIConstants.API_KEY_TYPE_SANDBOX); - } - if (endpointConfig.get("production") != null) { - prodSequenceName = endpointConfig.get("production").getAsString(); - } else { - prodSequenceName = apiProvider.getCustomBackendSequenceOfAPIByUUID(currentApiUuid, - APIConstants.API_KEY_TYPE_PRODUCTION); - } if (apiDtoToReturn.getEndpointConfig() != null) { Map endpointConf = (Map) apiDtoToReturn.getEndpointConfig(); if (endpointConf != null && APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( endpointConf.get(API_ENDPOINT_CONFIG_PROTOCOL_TYPE))) { - endpointConf.put("sandbox", sandSequenceName); - endpointConf.put("production", prodSequenceName); + SequenceBackendData sqData = apiProvider.getCustomBackendByAPIUUID(currentApiUuid, + APIConstants.API_KEY_TYPE_SANDBOX); + if (sqData != null) { + endpointConf.put("sandbox", sqData.getName()); + } + sqData = apiProvider.getCustomBackendByAPIUUID(currentApiUuid, + APIConstants.API_KEY_TYPE_PRODUCTION); + if (sqData != null) { + endpointConf.put("production", sqData.getName()); + } apiDtoToReturn.setEndpointConfig(endpointConf); } } - addCustomBackendToArchive(archivePath, apiProvider, currentApiUuid, sandSequenceName, prodSequenceName); + addCustomBackendToArchive(archivePath, apiProvider, currentApiUuid); } } @@ -663,8 +657,8 @@ public static void addEndpointCertificatesToArchive(String archivePath, APIDTO a } } - public static void addCustomBackendToArchive(String archivePath, APIProvider apiProvider, String apiUUID, - String prodBackendName, String sandBackendName) throws APIManagementException { + public static void addCustomBackendToArchive(String archivePath, APIProvider apiProvider, String apiUUID) + throws APIManagementException { try { CommonUtil.createDirectory(archivePath + File.separator + ImportExportConstants.CUSTOM_BACKEND_DIRECTORY); @@ -672,20 +666,14 @@ public static void addCustomBackendToArchive(String archivePath, APIProvider api SequenceBackendData data = apiProvider.getCustomBackendByAPIUUID(apiUUID, APIConstants.API_KEY_TYPE_PRODUCTION); if (data != null) { - String seqName = prodBackendName; - if (StringUtils.isEmpty(seqName)) { - seqName = APIUtil.getCustomBackendName(apiUUID, APIConstants.API_KEY_TYPE_PRODUCTION); - } + String seqName = data.getName(); exportCustomBackend(seqName, data.getSequence(), archivePath); } // Add sandbox Backend Sequences data = apiProvider.getCustomBackendByAPIUUID(apiUUID, APIConstants.API_KEY_TYPE_SANDBOX); if (data != null) { - String seqName = sandBackendName; - if (StringUtils.isEmpty(seqName)) { - seqName = APIUtil.getCustomBackendName(apiUUID, APIConstants.API_KEY_TYPE_SANDBOX); - } + String seqName = data.getName(); exportCustomBackend(seqName, data.getSequence(), archivePath); } @@ -813,10 +801,14 @@ public static void exportPolicyData(String policyFileName, OperationPolicyData p */ public static void exportCustomBackend(String customBackendFileName, String sequence, String archivePath) throws APIImportExportException, IOException { + if (!StringUtils.isEmpty(customBackendFileName) && !customBackendFileName.contains( + APIConstants.SYNAPSE_POLICY_DEFINITION_EXTENSION_XML)) { + customBackendFileName = customBackendFileName + APIConstants.SYNAPSE_POLICY_DEFINITION_EXTENSION_XML; + } String customBackendName = archivePath + File.separator + ImportExportConstants.CUSTOM_BACKEND_DIRECTORY + File.separator + customBackendFileName; - CommonUtil.writeFile(customBackendName + APIConstants.SYNAPSE_POLICY_DEFINITION_EXTENSION_XML, sequence); + CommonUtil.writeFile(customBackendName, sequence); } /** 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 10a769baf850..0b6860a86bc8 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 @@ -702,15 +702,23 @@ public static void updateAPIWithCustomBackend(API api, String extractedFolderPat String seqFile = endpointConfig.get("sandbox").getAsString(); String seqId = UUID.randomUUID().toString(); InputStream seq = APIUtil.getCustomBackendSequence(customBackendDir, seqFile, ".xml"); - apiProvider.updateCustomBackend(api.getUuid(), APIConstants.API_KEY_TYPE_SANDBOX, seq, - seqFile + ".xml", seqId); + if (!StringUtils.isEmpty(seqFile) && !seqFile.contains( + APIConstants.SYNAPSE_POLICY_DEFINITION_EXTENSION_XML)) { + seqFile = seqFile + APIConstants.SYNAPSE_POLICY_DEFINITION_EXTENSION_XML; + } + apiProvider.updateCustomBackend(api.getUuid(), APIConstants.API_KEY_TYPE_SANDBOX, seq, seqFile, + seqId); } if (endpointConfig.get("production") != null) { String seqFile = endpointConfig.get("production").getAsString(); String seqId = UUID.randomUUID().toString(); InputStream seq = APIUtil.getCustomBackendSequence(customBackendDir, seqFile, ".xml"); - apiProvider.updateCustomBackend(api.getUuid(), APIConstants.API_KEY_TYPE_PRODUCTION, seq, - seqFile + ".xml", seqId); + if (!StringUtils.isEmpty(seqFile) && !seqFile.contains( + APIConstants.SYNAPSE_POLICY_DEFINITION_EXTENSION_XML)) { + seqFile = seqFile + APIConstants.SYNAPSE_POLICY_DEFINITION_EXTENSION_XML; + } + apiProvider.updateCustomBackend(api.getUuid(), APIConstants.API_KEY_TYPE_PRODUCTION, seq, seqFile, + seqId); } } } From 1620f6c299da0f5e06e5f3f6de231a0657d314fc Mon Sep 17 00:00:00 2001 From: BLasan Date: Fri, 20 Sep 2024 08:25:20 +0530 Subject: [PATCH 43/56] Add: Composite Primary Key --- .../src/main/resources/sql/mysql.sql | 2 +- .../src/main/resources/sql/oracle.sql | 2 +- .../src/main/resources/sql/oracle_23c.sql | 2 +- .../src/main/resources/sql/oracle_rac.sql | 2 +- .../src/main/resources/sql/postgresql.sql | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/mysql.sql b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/mysql.sql index c9ecc963651e..6667963e6002 100644 --- a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/mysql.sql +++ b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/mysql.sql @@ -1531,7 +1531,7 @@ CREATE TABLE IF NOT EXISTS AM_API_SEQUENCE_BACKEND ( SEQUENCE LONGBLOB NOT NULL, NAME VARCHAR(256) NOT NULL, TYPE VARCHAR(120) NOT NULL, - PRIMARY KEY (ID), + PRIMARY KEY (ID,API_UUID,REVISION_UUID,TYPE), FOREIGN KEY (API_UUID) REFERENCES AM_API(API_UUID) ON DELETE CASCADE )ENGINE INNODB; diff --git a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/oracle.sql b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/oracle.sql index a9f914c4badc..c71ec2599a5e 100644 --- a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/oracle.sql +++ b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/oracle.sql @@ -3465,7 +3465,7 @@ CREATE TABLE AM_API_SEQUENCE_BACKEND ( SEQUENCE BLOB NOT NULL, NAME VARCHAR2(256) NOT NULL, TYPE VARCHAR2(120) NOT NULL, - PRIMARY KEY (ID), + PRIMARY KEY (ID,API_UUID,REVISION_UUID,TYPE), FOREIGN KEY (API_UUID) REFERENCES AM_API(API_UUID) ON DELETE CASCADE ) / diff --git a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/oracle_23c.sql b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/oracle_23c.sql index c9ca66b51e69..85c475646ac3 100644 --- a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/oracle_23c.sql +++ b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/oracle_23c.sql @@ -2478,7 +2478,7 @@ CREATE TABLE AM_API_SEQUENCE_BACKEND ( SEQUENCE BLOB NOT NULL, NAME VARCHAR2(256) NOT NULL, TYPE VARCHAR2(120) NOT NULL, - PRIMARY KEY (ID), + PRIMARY KEY (ID,API_UUID,REVISION_UUID,TYPE), FOREIGN KEY (API_UUID) REFERENCES AM_API(API_UUID) ON DELETE CASCADE ) / diff --git a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/oracle_rac.sql b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/oracle_rac.sql index 53d99a30e37f..61d58f5232ee 100644 --- a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/oracle_rac.sql +++ b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/oracle_rac.sql @@ -2468,7 +2468,7 @@ CREATE TABLE AM_API_SEQUENCE_BACKEND ( SEQUENCE BLOB NOT NULL, NAME VARCHAR2(256) NOT NULL, TYPE VARCHAR2(120) NOT NULL, - PRIMARY KEY (ID), + PRIMARY KEY (ID,API_UUID,REVISION_UUID,TYPE), FOREIGN KEY (API_UUID) REFERENCES AM_API(API_UUID) ON DELETE CASCADE ) / diff --git a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/postgresql.sql b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/postgresql.sql index ae4f6a14f49f..bfe82f068044 100644 --- a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/postgresql.sql +++ b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/postgresql.sql @@ -1867,7 +1867,7 @@ CREATE TABLE IF NOT EXISTS AM_API_SEQUENCE_BACKEND ( SEQUENCE BYTEA NOT NULL, NAME VARCHAR(256) NOT NULL, TYPE VARCHAR(120) NOT NULL, - PRIMARY KEY (ID), + PRIMARY KEY (ID,API_UUID,REVISION_UUID,TYPE), FOREIGN KEY (API_UUID) REFERENCES AM_API(API_UUID) ON DELETE CASCADE ); From b758283949916c52e00a6d9af015bd4f4d68e7f6 Mon Sep 17 00:00:00 2001 From: BLasan Date: Fri, 20 Sep 2024 23:37:19 +0530 Subject: [PATCH 44/56] Fix: JSON Parsing error with Number format exception --- .../v1/common/mappings/PublisherCommonUtilsTest.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/test/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/PublisherCommonUtilsTest.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/test/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/PublisherCommonUtilsTest.java index f09f0ddc77b1..6a0de16218c8 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/test/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/PublisherCommonUtilsTest.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/test/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/PublisherCommonUtilsTest.java @@ -46,6 +46,7 @@ import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.DocumentDTO; import static org.junit.Assert.fail; +import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; @@ -443,7 +444,8 @@ public void testValidateEndpointConfigs() { endpointConfigs.put(PublisherCommonUtils.SESSION_TIMEOUT_CONFIG_KEY, "sdwed"); flag = PublisherCommonUtils.validateEndpointConfigs(apiDTO); Assert.assertFalse(flag); - endpointConfigs.put(PublisherCommonUtils.SESSION_TIMEOUT_CONFIG_KEY, "1000000000000000000000000000000000"); + endpointConfigs.put(PublisherCommonUtils.SESSION_TIMEOUT_CONFIG_KEY, + new BigInteger("1000000000000000000000000000000000")); flag = PublisherCommonUtils.validateEndpointConfigs(apiDTO); Assert.assertFalse(flag); From c47b485b77edeaa5c3596f579a175c130e3ee8d9 Mon Sep 17 00:00:00 2001 From: BLasan Date: Fri, 20 Sep 2024 23:38:56 +0530 Subject: [PATCH 45/56] Fix: Null Pointer Exception for non endpoint configs --- .../rest/api/publisher/v1/common/TemplateBuilderUtil.java | 2 +- .../apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) 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/TemplateBuilderUtil.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/TemplateBuilderUtil.java index 8c59178d3b18..0d07f358483e 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/TemplateBuilderUtil.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/TemplateBuilderUtil.java @@ -1047,7 +1047,7 @@ private static void setCustomSequencesToBeAdded(API api, GatewayAPIDTO gatewayAP } Map endpointConfigMap = (Map) apidto.getEndpointConfig(); - if (APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( + if (endpointConfigMap != null && APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( endpointConfigMap.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE))) { GatewayContentDTO gatewayCustomBackendSequenceDTO = retrieveCustomBackendSequence(api, APIConstants.API_KEY_TYPE_SANDBOX, extractedPath); diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java index ce93509652e3..b203f4b37286 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java @@ -3953,7 +3953,8 @@ public Response deployAPIRevision(String apiId, String revisionId, // Cannot deploy an API with custom sequence to the APK gateway Map endpointConfigMap = (Map) apiDto.getEndpointConfig(); - if (!APIConstants.WSO2_SYNAPSE_GATEWAY.equals(apiDto.getGatewayType()) && APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( + if (endpointConfigMap != null && !APIConstants.WSO2_SYNAPSE_GATEWAY.equals(apiDto.getGatewayType()) + && APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( endpointConfigMap.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE))) { return Response.status(Response.Status.BAD_REQUEST) .entity("Cannot Deploy an API with a Custom Sequence to APK Gateway: " + apiId).build(); From 052e53ac29f068f1a7f7b6466830364239221704 Mon Sep 17 00:00:00 2001 From: BLasan Date: Sat, 21 Sep 2024 07:18:30 +0530 Subject: [PATCH 46/56] Hadneld: Number Format Exceptions when parsing --- .../v1/common/TemplateBuilderUtil.java | 91 ++++++++++--------- .../v1/common/mappings/ExportUtils.java | 2 +- 2 files changed, 51 insertions(+), 42 deletions(-) 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/TemplateBuilderUtil.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/TemplateBuilderUtil.java index 0d07f358483e..6885cd38cbd8 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/TemplateBuilderUtil.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/TemplateBuilderUtil.java @@ -85,6 +85,8 @@ import javax.xml.namespace.QName; import javax.xml.stream.XMLStreamException; +import static org.wso2.carbon.apimgt.impl.APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE; + /** * This class used to utility for Template. */ @@ -566,40 +568,47 @@ public static GatewayAPIDTO retrieveGatewayAPIDto(API api, Environment environme String endpointConfigString = api.getEndpointConfig(); if (StringUtils.isNotBlank(endpointConfigString)) { try { - JSONParser parser = new JSONParser(); - ObjectMapper objectMapper = new ObjectMapper(); - JSONObject endpointConfig = (JSONObject) parser.parse(endpointConfigString); - if (APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( - endpointConfig.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE))) { - String policyDirectory = - extractedFolderPath + File.separator + ImportExportConstants.CUSTOM_BACKEND_DIRECTORY; - String seqName = APIUtil.getCustomBackendName(api.getUuid(), APIConstants.API_KEY_TYPE_SANDBOX); - 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); + JsonObject endpointConf = JsonParser.parseString(api.getEndpointConfig()).getAsJsonObject(); + if (endpointConf != null && APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( + endpointConf.get(API_ENDPOINT_CONFIG_PROTOCOL_TYPE).getAsString()) && StringUtils.equals( + api.getType().toLowerCase(), APIConstants.API_TYPE_HTTP.toLowerCase())) { + // To modify the endpoint config string + JSONParser parser = new JSONParser(); + ObjectMapper objectMapper = new ObjectMapper(); + JSONObject endpointConfig = (JSONObject) parser.parse(endpointConfigString); + if (APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( + endpointConfig.get(API_ENDPOINT_CONFIG_PROTOCOL_TYPE))) { + String policyDirectory = + extractedFolderPath + File.separator + ImportExportConstants.CUSTOM_BACKEND_DIRECTORY; + String seqName = APIUtil.getCustomBackendName(api.getUuid(), APIConstants.API_KEY_TYPE_SANDBOX); + 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); - 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); + seqName = APIUtil.getCustomBackendName(api.getUuid(), APIConstants.API_KEY_TYPE_PRODUCTION); + 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)); } - api.setEndpointConfig(objectMapper.writeValueAsString(endpointConfig)); } } catch (IOException | ParseException ex) { throw new APIManagementException("Error when updating Endpoint Configuration", ex); @@ -744,7 +753,7 @@ private static GatewayAPIDTO createAPIGatewayDTOtoPublishAPI(Environment environ if (!StringUtils.isEmpty(api.getEndpointConfig())) { JsonObject endpointConfObj = JsonParser.parseString(api.getEndpointConfig()).getAsJsonObject(); if (!APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( - endpointConfObj.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE).getAsString())) { + endpointConfObj.get(API_ENDPOINT_CONFIG_PROTOCOL_TYPE).getAsString())) { addEndpoints(api, apiTemplateBuilder, productAPIDto); } } else { @@ -797,7 +806,7 @@ 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()) + endpointConfigMap.get(API_ENDPOINT_CONFIG_PROTOCOL_TYPE).getAsString()) && APIConstants.API_TYPE_HTTP.equals(api.getType())) { GatewayContentDTO gatewayCustomBackendSequenceDTO = retrieveSequenceBackendForAPIProduct(api, apiProduct, APIConstants.API_KEY_TYPE_SANDBOX, extractedPath); @@ -906,7 +915,7 @@ private static GatewayAPIDTO createAPIGatewayDTOtoPublishAPI(Environment environ // specified Or if the Gateway type is 'sandbox' and a sandbox url has not been specified if (endpointConfig != null && !APIConstants.ENDPOINT_TYPE_AWSLAMBDA.equals( - endpointConfig.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE)) && ( + endpointConfig.get(API_ENDPOINT_CONFIG_PROTOCOL_TYPE)) && ( (APIConstants.GATEWAY_ENV_TYPE_PRODUCTION.equals(environment.getType()) && !APIUtil.isProductionEndpointsExists(api.getEndpointConfig())) || ( APIConstants.GATEWAY_ENV_TYPE_SANDBOX.equals(environment.getType()) @@ -935,9 +944,9 @@ private static GatewayAPIDTO createAPIGatewayDTOtoPublishAPI(Environment environ } else if (APIConstants.IMPLEMENTATION_TYPE_ENDPOINT.equalsIgnoreCase(api.getImplementation())) { String apiConfig = builder.getConfigStringForTemplate(environment); gatewayAPIDTO.setApiDefinition(apiConfig); - if (endpointConfig != null && !endpointConfig.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE) + if (endpointConfig != null && !endpointConfig.get(API_ENDPOINT_CONFIG_PROTOCOL_TYPE) .equals(APIConstants.ENDPOINT_TYPE_AWSLAMBDA) && !endpointConfig.get( - APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE).equals(APIConstants.ENDPOINT_TYPE_SEQUENCE)) { + API_ENDPOINT_CONFIG_PROTOCOL_TYPE).equals(APIConstants.ENDPOINT_TYPE_SEQUENCE)) { if (!isWsApi) { addEndpoints(api, builder, gatewayAPIDTO); } @@ -1048,7 +1057,7 @@ private static void setCustomSequencesToBeAdded(API api, GatewayAPIDTO gatewayAP Map endpointConfigMap = (Map) apidto.getEndpointConfig(); if (endpointConfigMap != null && APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( - endpointConfigMap.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE))) { + endpointConfigMap.get(API_ENDPOINT_CONFIG_PROTOCOL_TYPE))) { GatewayContentDTO gatewayCustomBackendSequenceDTO = retrieveCustomBackendSequence(api, APIConstants.API_KEY_TYPE_SANDBOX, extractedPath); if (gatewayCustomBackendSequenceDTO != null) { @@ -1289,7 +1298,7 @@ private static void setSecureVaultPropertyToBeAdded(String prefix, API api, Gate } } else if (APIConstants.ENDPOINT_TYPE_AWSLAMBDA - .equals(endpointConfig.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE))) { + .equals(endpointConfig.get(API_ENDPOINT_CONFIG_PROTOCOL_TYPE))) { addAWSCredentialsToList(prefix, api, gatewayAPIDTO, endpointConfig); } } @@ -1806,19 +1815,19 @@ public static String populateSubscriptionEndpointConfig(String endpointConfig) t try { JSONObject newEndpointConfigJson = new JSONObject(); - newEndpointConfigJson.put(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE, + newEndpointConfigJson.put(API_ENDPOINT_CONFIG_PROTOCOL_TYPE, APIConstants.ENDPOINT_TYPE_GRAPHQL); JSONObject oldEndpointConfigJson = (JSONObject) new JSONParser().parse(endpointConfig); newEndpointConfigJson.put(APIConstants.ENDPOINT_TYPE_HTTP, oldEndpointConfigJson); JSONObject wsEndpointConfig = new JSONObject(); - wsEndpointConfig.put(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE, APIConstants.WS_PROTOCOL); + wsEndpointConfig.put(API_ENDPOINT_CONFIG_PROTOCOL_TYPE, APIConstants.WS_PROTOCOL); // If production_endpoints exists if (oldEndpointConfigJson.get(APIConstants.ENDPOINT_PRODUCTION_ENDPOINTS) != null) { JSONObject prodWSEndpointConfig; String prodWsEndpoint = ""; // If load_balanced endpoints get the first prod endpoint url from the list if (APIConstants.ENDPOINT_TYPE_LOADBALANCE.equals( - oldEndpointConfigJson.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE))) { + oldEndpointConfigJson.get(API_ENDPOINT_CONFIG_PROTOCOL_TYPE))) { // get first load balanced endpoint prodWsEndpoint = (String) ((JSONObject) ((org.json.simple.JSONArray) oldEndpointConfigJson .get(APIConstants.ENDPOINT_PRODUCTION_ENDPOINTS)).get(0)).get(APIConstants.ENDPOINT_URL); @@ -1851,7 +1860,7 @@ public static String populateSubscriptionEndpointConfig(String endpointConfig) t String sandboxWsEndpoint = ""; // If load_balanced endpoints get the first sandbox endpoint url from the list if (APIConstants.ENDPOINT_TYPE_LOADBALANCE.equals( - oldEndpointConfigJson.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE))) { + oldEndpointConfigJson.get(API_ENDPOINT_CONFIG_PROTOCOL_TYPE))) { // get first load balanced endpoint sandboxWsEndpoint = (String) ((JSONObject) ((org.json.simple.JSONArray) oldEndpointConfigJson .get(APIConstants.ENDPOINT_SANDBOX_ENDPOINTS)).get(0)).get(APIConstants.ENDPOINT_URL); 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/ExportUtils.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/ExportUtils.java index b35d2a235eea..6abb8e24a416 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/ExportUtils.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/ExportUtils.java @@ -221,7 +221,7 @@ public static File exportApi(APIProvider apiProvider, APIIdentifier apiIdentifie if (api != null && !StringUtils.isEmpty(api.getEndpointConfig())) { JsonObject endpointConfig = JsonParser.parseString(api.getEndpointConfig()).getAsJsonObject(); - if (APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( + if (endpointConfig != null && APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( endpointConfig.get(API_ENDPOINT_CONFIG_PROTOCOL_TYPE).getAsString()) && StringUtils.equals( apiDtoToReturn.getType().toString().toLowerCase(), APIConstants.API_TYPE_HTTP.toLowerCase())) { if (apiDtoToReturn.getEndpointConfig() != null) { From 8bd45c3f6bad39ea3a4497ffb185efd44103bf0c Mon Sep 17 00:00:00 2001 From: BLasan Date: Sat, 21 Sep 2024 07:39:06 +0530 Subject: [PATCH 47/56] Revert: SESSION TIMEOUT CONFIG in Publisher CommonUtil --- .../publisher/v1/common/mappings/PublisherCommonUtilsTest.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/test/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/PublisherCommonUtilsTest.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/test/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/PublisherCommonUtilsTest.java index 6a0de16218c8..d7b20435c302 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/test/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/PublisherCommonUtilsTest.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/test/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/PublisherCommonUtilsTest.java @@ -444,8 +444,7 @@ public void testValidateEndpointConfigs() { endpointConfigs.put(PublisherCommonUtils.SESSION_TIMEOUT_CONFIG_KEY, "sdwed"); flag = PublisherCommonUtils.validateEndpointConfigs(apiDTO); Assert.assertFalse(flag); - endpointConfigs.put(PublisherCommonUtils.SESSION_TIMEOUT_CONFIG_KEY, - new BigInteger("1000000000000000000000000000000000")); + endpointConfigs.put(PublisherCommonUtils.SESSION_TIMEOUT_CONFIG_KEY, "1000000000000000000000000000000000"); flag = PublisherCommonUtils.validateEndpointConfigs(apiDTO); Assert.assertFalse(flag); From 58e7b2069039e03474064ce5dcfaacfe6c8ac94c Mon Sep 17 00:00:00 2001 From: BLasan Date: Sat, 21 Sep 2024 07:45:04 +0530 Subject: [PATCH 48/56] Remove: Unused Import --- .../publisher/v1/common/mappings/PublisherCommonUtilsTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/test/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/PublisherCommonUtilsTest.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/test/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/PublisherCommonUtilsTest.java index d7b20435c302..f09f0ddc77b1 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/test/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/PublisherCommonUtilsTest.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/test/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/PublisherCommonUtilsTest.java @@ -46,7 +46,6 @@ import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.DocumentDTO; import static org.junit.Assert.fail; -import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; From be1cb812c8cbf81ef94de3072acff3c0e63e6690 Mon Sep 17 00:00:00 2001 From: BLasan Date: Mon, 23 Sep 2024 14:38:14 +0530 Subject: [PATCH 49/56] Fix: Commented Issues --- .../wso2/carbon/apimgt/api/APIProvider.java | 51 +++++++++++++++++-- .../apimgt/api/model/SequenceBackendData.java | 18 +++++++ .../carbon/apimgt/impl/APIProviderImpl.java | 13 ----- .../carbon/apimgt/impl/dao/ApiMgtDAO.java | 23 +-------- .../carbon/apimgt/impl/utils/APIUtil.java | 7 +-- .../v1/common/TemplateBuilderUtil.java | 4 +- 6 files changed, 73 insertions(+), 43 deletions(-) diff --git a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java index 482c88946d9e..ef8e618518ae 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java +++ b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java @@ -66,8 +66,28 @@ public interface APIProvider extends APIManager { Comment getComment(ApiTypeWrapper apiTypeWrapper, String commentId, Integer replyLimit, Integer replyOffset) throws APIManagementException; + /** + * This method is to delete Sequence Backend by type + * + * @param apiUUID API Id + * @param type Key type + * @throws APIManagementException If failed to delete Sequence Backend + */ void deleteCustomBackendByID(String apiUUID, String type) throws APIManagementException; + + /** + * This method is to delete all Sequence Backends by APIID + * @param apiUUID API ID + * @throws APIManagementException If failed to delete Sequence Backend + */ void deleteCustomBackendByAPIID(String apiUUID) throws APIManagementException; + + /** + * This method is to delete Sequence Backends of a specific revision + * @param apiUUID API Id + * @param revisionId Revision Id + * @throws APIManagementException If failed to delete Sequence Backend + */ void deleteSequenceBackendByRevision(String apiUUID, String revisionId) throws APIManagementException; /** @@ -320,16 +340,37 @@ List getSubscriptionsOfAPI(String apiName, String apiVersion, Str */ API updateAPI(API api, API existingAPI) throws APIManagementException, FaultGatewaysException; + /** + * This method is to update Sequence Backend + * + * @param api API + * @param type Key Type + * @param sequence Sequence Content + * @param seqName Sequence Name + * @param customBackendUUID Sequence Id + * @throws APIManagementException If not updated + */ void updateCustomBackend(String api, String type, InputStream sequence, String seqName, String customBackendUUID) throws APIManagementException; - Map getCustomBackendOfAPIByUUID(String customBackendUUID, String apiUUID, String type, - boolean isInfoOnly) throws APIManagementException; - - String getCustomBackendSequenceOfAPIByUUID(String apiUUID, String type) throws APIManagementException; - + /** + * THis method is to retrieve Sequence Backend data + * + * @param apiUUID API Id + * @param type Key Type + * @return SequenceBackendData object + * @throws APIManagementException If data is not properly retrieved + */ SequenceBackendData getCustomBackendByAPIUUID(String apiUUID, String type) throws APIManagementException; + /** + * This method is to retrieve all Sequence Backends of an API + * + * @param apiUUID API Id + * @return List of Sequence Backends + * @throws APIManagementException If not found + */ + List getAllSequenceBackendsByAPIUUID(String apiUUID) throws APIManagementException; /** diff --git a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/SequenceBackendData.java b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/SequenceBackendData.java index 5cccbbbbc433..a2774dd021e1 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/SequenceBackendData.java +++ b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/SequenceBackendData.java @@ -1,3 +1,21 @@ +/* + * Copyright (c) 2024, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + package org.wso2.carbon.apimgt.api.model; public class SequenceBackendData { diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java index 29de8d1fe7ba..c2dc55e43532 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java @@ -21,7 +21,6 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.gson.Gson; - import org.apache.axiom.om.OMElement; import org.apache.axiom.om.util.AXIOMUtil; import org.apache.axis2.Constants; @@ -1175,18 +1174,6 @@ public void updateCustomBackend(String apiUUID, String type, InputStream sequenc apiMgtDAO.updateCustomBackend(apiUUID, seqName, sequence, type, customBackendUUID); } - @Override - public Map getCustomBackendOfAPIByUUID(String customBackendUUID, String apiUUID, - String type, boolean isInfoOnly) throws APIManagementException { - return apiMgtDAO.getCustomBackendOfAPIByUUID(customBackendUUID, apiUUID, type, isInfoOnly); - } - - @Override - public String getCustomBackendSequenceOfAPIByUUID(String apiUUID, String type) - throws APIManagementException { - return apiMgtDAO.getCustomBackendSequenceOfAPIByUUID(apiUUID, type); - } - @Override public SequenceBackendData getCustomBackendByAPIUUID(String apiUUID, String type) throws APIManagementException { return apiMgtDAO.getCustomBackendByAPIUUID(apiUUID, type); 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 0ec0b9327274..ec2a846a54ed 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 @@ -21811,27 +21811,8 @@ private void revisionCustomBackend(APIRevision apiRevision, Connection connectio addPstmt.setString(6, rs.getString("NAME")); addPstmt.addBatch(); count++; - - // CustomBackendData customBackendData = new CustomBackendData(); - // customBackendData.setId(rs.getString("ID")); - // customBackendData.setName(rs.getString("NAME")); - // customBackendData.setApiUUID(apiRevision.getApiUUID()); - // customBackendData.setRevisionUUID(apiRevision.getRevisionUUID()); - // customBackendData.setSequence(rs.getBinaryStream("SEQUENCE")); - // customBackendData.setType(rs.getString("TYPE")); - // customBackendDataList.add(customBackendData); - } - } - - // for(CustomBackendData customBackendData: customBackendDataList) { - // addPstmt.setString(1, customBackendData.getId()); - // addPstmt.setString(2, customBackendData.getApiUUID()); - // addPstmt.setBinaryStream(3, customBackendData.getSequence()); - // addPstmt.setString(4, customBackendData.getType()); - // addPstmt.setString(5, customBackendData.getRevisionUUID()); - // addPstmt.setString(6, customBackendData.getName()); - // addPstmt.addBatch(); - // } + } + } if (count > 0) { addPstmt.executeBatch(); 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 a81bf74ad87f..4596a5483e7b 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 @@ -10058,15 +10058,16 @@ public static InputStream getCustomBackendSequence(String extractedFolderPath, S customBackendFileName = customBackendFileName + fileExtension; } String fileName = extractedFolderPath + File.separator + customBackendFileName; - InputStream inputStream = null; if (checkFileExistence(fileName)) { try { - inputStream = new FileInputStream(fileName); + try (InputStream inputStream = new FileInputStream(fileName)) { + return inputStream; + } } catch (IOException ex) { handleException("Error reading Custom Backend " + customBackendFileName); } } - return inputStream; + return null; } /** 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/TemplateBuilderUtil.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/TemplateBuilderUtil.java index 6885cd38cbd8..786df11672c1 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/TemplateBuilderUtil.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/TemplateBuilderUtil.java @@ -568,6 +568,7 @@ public static GatewayAPIDTO retrieveGatewayAPIDto(API api, Environment environme String endpointConfigString = api.getEndpointConfig(); if (StringUtils.isNotBlank(endpointConfigString)) { try { + // Avoid number format issues in Endpoint Configuration JsonObject endpointConf = JsonParser.parseString(api.getEndpointConfig()).getAsJsonObject(); if (endpointConf != null && APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( endpointConf.get(API_ENDPOINT_CONFIG_PROTOCOL_TYPE).getAsString()) && StringUtils.equals( @@ -611,7 +612,8 @@ public static GatewayAPIDTO retrieveGatewayAPIDto(API api, Environment environme } } } catch (IOException | ParseException ex) { - throw new APIManagementException("Error when updating Endpoint Configuration", ex); + throw new APIManagementException("Error when updating Endpoint Configuration of API: " + api.getUuid(), + ex); } } From d1bb0888d15b1798abb4dcb2298150cc79793a0f Mon Sep 17 00:00:00 2001 From: BLasan Date: Mon, 23 Sep 2024 19:03:21 +0530 Subject: [PATCH 50/56] Fix: Input Stream Closing Issue --- .../wso2/carbon/apimgt/api/APIProvider.java | 15 +++++++------- .../carbon/apimgt/impl/APIProviderImpl.java | 2 +- .../carbon/apimgt/impl/dao/ApiMgtDAO.java | 20 +++++++++++++++---- .../carbon/apimgt/impl/utils/APIUtil.java | 4 ++-- .../v1/common/mappings/ImportUtils.java | 16 +++++++++------ .../common/mappings/PublisherCommonUtils.java | 9 +++++++-- 6 files changed, 44 insertions(+), 22 deletions(-) diff --git a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java index ef8e618518ae..fcd13d13c6c4 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java +++ b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java @@ -70,13 +70,14 @@ Comment getComment(ApiTypeWrapper apiTypeWrapper, String commentId, Integer repl * This method is to delete Sequence Backend by type * * @param apiUUID API Id - * @param type Key type + * @param type Key type * @throws APIManagementException If failed to delete Sequence Backend */ void deleteCustomBackendByID(String apiUUID, String type) throws APIManagementException; /** * This method is to delete all Sequence Backends by APIID + * * @param apiUUID API ID * @throws APIManagementException If failed to delete Sequence Backend */ @@ -343,21 +344,21 @@ List getSubscriptionsOfAPI(String apiName, String apiVersion, Str /** * This method is to update Sequence Backend * - * @param api API - * @param type Key Type - * @param sequence Sequence Content - * @param seqName Sequence Name + * @param api API + * @param type Key Type + * @param sequence Sequence Content + * @param seqName Sequence Name * @param customBackendUUID Sequence Id * @throws APIManagementException If not updated */ - void updateCustomBackend(String api, String type, InputStream sequence, String seqName, String customBackendUUID) + void updateCustomBackend(String api, String type, String sequence, String seqName, String customBackendUUID) throws APIManagementException; /** * THis method is to retrieve Sequence Backend data * * @param apiUUID API Id - * @param type Key Type + * @param type Key Type * @return SequenceBackendData object * @throws APIManagementException If data is not properly retrieved */ diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java index c2dc55e43532..2b5b30d75d69 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java @@ -1169,7 +1169,7 @@ private void updateAPIPolicies(API api, String tenantDomain) throws APIManagemen } @Override - public void updateCustomBackend(String apiUUID, String type, InputStream sequence, String seqName, + public void updateCustomBackend(String apiUUID, String type, String sequence, String seqName, String customBackendUUID) throws APIManagementException { apiMgtDAO.updateCustomBackend(apiUUID, seqName, sequence, type, customBackendUUID); } 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 ec2a846a54ed..2d55bdbe409a 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 @@ -21511,7 +21511,17 @@ public void updateAPIPoliciesMapping(String apiUUID, Set uriTemplat } } - public void updateCustomBackend(String apiUUID, String sequenceName, InputStream sequence, String type, + /** + * This method is to update Sequence Backend data + * + * @param apiUUID API Id + * @param sequenceName Sequence Name + * @param sequence Sequence Content + * @param type Key type + * @param backendUUID Sequence Id + * @throws APIManagementException If not properly updated + */ + public void updateCustomBackend(String apiUUID, String sequenceName, String sequence, String type, String backendUUID) throws APIManagementException { // delete current working copy String deleteCustomBackedQuery = SQLConstants.CustomBackendConstants.DELETE_CUSTOM_BACKEND_BY_API_AND_TYPE; @@ -21586,14 +21596,16 @@ public void addNewCustomBackendForAPIRevision(String apiUUID, String revisionID, } } - public void addCustomBackend(String apiUUID, String sequenceName, String revision, InputStream sequence, + public void addCustomBackend(String apiUUID, String sequenceName, String revision, String sequence, String type, Connection connection, String backendUUID) throws APIManagementException { String insertCustomBackendQuery = SQLConstants.CustomBackendConstants.ADD_CUSTOM_BACKEND; try (PreparedStatement prepStmt = connection.prepareStatement(insertCustomBackendQuery)) { connection.setAutoCommit(false); prepStmt.setString(1, backendUUID); prepStmt.setString(2, apiUUID); - prepStmt.setBinaryStream(3, sequence); + try (InputStream seqStream = new ByteArrayInputStream(sequence.getBytes())) { + prepStmt.setBinaryStream(3, seqStream); + } prepStmt.setString(4, type); if (revision == null) { revision = "0"; @@ -21601,7 +21613,7 @@ public void addCustomBackend(String apiUUID, String sequenceName, String revisio prepStmt.setString(5, revision); prepStmt.setString(6, sequenceName); prepStmt.executeUpdate(); - } catch (SQLException e) { + } catch (SQLException | IOException e) { handleException("Error while adding Custom Backend for API : " + apiUUID, e); } } 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 4596a5483e7b..a462add89c71 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 @@ -10052,7 +10052,7 @@ public static void loadCommonOperationPolicies(String organization) { * @return The Sequence of the Custom Backend as an Input Stream * @throws APIManagementException If an error occurs while reading, throws an error */ - public static InputStream getCustomBackendSequence(String extractedFolderPath, String customBackendFileName, + public static String getCustomBackendSequence(String extractedFolderPath, String customBackendFileName, String fileExtension) throws APIManagementException { if (!StringUtils.isEmpty(customBackendFileName) && !customBackendFileName.contains(fileExtension)) { customBackendFileName = customBackendFileName + fileExtension; @@ -10061,7 +10061,7 @@ public static InputStream getCustomBackendSequence(String extractedFolderPath, S if (checkFileExistence(fileName)) { try { try (InputStream inputStream = new FileInputStream(fileName)) { - return inputStream; + return IOUtils.toString(inputStream); } } catch (IOException ex) { handleException("Error reading Custom Backend " + customBackendFileName); 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 0b6860a86bc8..d4f5b0891ed8 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 @@ -701,24 +701,28 @@ public static void updateAPIWithCustomBackend(API api, String extractedFolderPat if (endpointConfig.get("sandbox") != null) { String seqFile = endpointConfig.get("sandbox").getAsString(); String seqId = UUID.randomUUID().toString(); - InputStream seq = APIUtil.getCustomBackendSequence(customBackendDir, seqFile, ".xml"); + String seq = APIUtil.getCustomBackendSequence(customBackendDir, seqFile, ".xml"); if (!StringUtils.isEmpty(seqFile) && !seqFile.contains( APIConstants.SYNAPSE_POLICY_DEFINITION_EXTENSION_XML)) { seqFile = seqFile + APIConstants.SYNAPSE_POLICY_DEFINITION_EXTENSION_XML; } - apiProvider.updateCustomBackend(api.getUuid(), APIConstants.API_KEY_TYPE_SANDBOX, seq, seqFile, - seqId); + if (seq != null) { + apiProvider.updateCustomBackend(api.getUuid(), APIConstants.API_KEY_TYPE_SANDBOX, seq, seqFile, + seqId); + } } if (endpointConfig.get("production") != null) { String seqFile = endpointConfig.get("production").getAsString(); String seqId = UUID.randomUUID().toString(); - InputStream seq = APIUtil.getCustomBackendSequence(customBackendDir, seqFile, ".xml"); + String seq = APIUtil.getCustomBackendSequence(customBackendDir, seqFile, ".xml"); if (!StringUtils.isEmpty(seqFile) && !seqFile.contains( APIConstants.SYNAPSE_POLICY_DEFINITION_EXTENSION_XML)) { seqFile = seqFile + APIConstants.SYNAPSE_POLICY_DEFINITION_EXTENSION_XML; } - apiProvider.updateCustomBackend(api.getUuid(), APIConstants.API_KEY_TYPE_PRODUCTION, seq, seqFile, - seqId); + if (seq != null) { + apiProvider.updateCustomBackend(api.getUuid(), APIConstants.API_KEY_TYPE_PRODUCTION, seq, + seqFile, seqId); + } } } } 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 722f4be49416..239cac422190 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 @@ -31,6 +31,7 @@ import graphql.schema.validation.SchemaValidationError; import graphql.schema.validation.SchemaValidator; import io.swagger.v3.parser.ObjectMapperFactory; +import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; @@ -221,9 +222,13 @@ public static void updateCustomBackend(API api, APIProvider apiProvider, String throw new APIManagementException( "Error when retrieving Custom Backend file name of API: " + api.getId().getApiName()); } - String seqName = APIUtil.getCustomBackendName(api.getUuid(), endpointType); String customBackendUUID = UUID.randomUUID().toString(); - apiProvider.updateCustomBackend(api.getUuid(), endpointType, customBackend, fileName, customBackendUUID); + try { + String customBackendStr = IOUtils.toString(customBackend); + apiProvider.updateCustomBackend(api.getUuid(), endpointType, customBackendStr, fileName, customBackendUUID); + } catch (IOException ex) { + throw new APIManagementException("Error retrieving sequence backend of API: " + api.getUuid(), ex); + } } private static String getFileNameFromContentDisposition(String contentDisposition) { From 15c6b2fe57e2117c4e226cd14345df2391679629 Mon Sep 17 00:00:00 2001 From: BLasan Date: Mon, 23 Sep 2024 23:56:02 +0530 Subject: [PATCH 51/56] Fix: TemplateUtil Test error --- .../rest/api/publisher/v1/common/TemplateBuilderUtil.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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/TemplateBuilderUtil.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/TemplateBuilderUtil.java index 786df11672c1..05b31f15b96e 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/TemplateBuilderUtil.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/TemplateBuilderUtil.java @@ -96,7 +96,6 @@ 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 clientCertificateDTOSProduction, @@ -573,6 +572,7 @@ public static GatewayAPIDTO retrieveGatewayAPIDto(API api, Environment environme if (endpointConf != null && APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( endpointConf.get(API_ENDPOINT_CONFIG_PROTOCOL_TYPE).getAsString()) && StringUtils.equals( api.getType().toLowerCase(), APIConstants.API_TYPE_HTTP.toLowerCase())) { + ApiMgtDAO apiMgtDAO = ApiMgtDAO.getInstance(); // To modify the endpoint config string JSONParser parser = new JSONParser(); ObjectMapper objectMapper = new ObjectMapper(); @@ -1524,6 +1524,7 @@ private static GatewayContentDTO retrieveSequenceBackendForAPIProduct(API api, A String endpointType, String pathToAchieve) throws APIManagementException { GatewayContentDTO customBackendSequenceContentDto = new GatewayContentDTO(); String customSequence = null; + ApiMgtDAO apiMgtDAO = ApiMgtDAO.getInstance(); SequenceBackendData data = apiMgtDAO.getCustomBackendByAPIUUID(api.getUuid(), endpointType); if (data != null) { String seqExt = data.getName(); @@ -1561,6 +1562,7 @@ private static GatewayContentDTO retrieveSequenceBackendForAPIProduct(API api, A private static GatewayContentDTO retrieveCustomBackendSequence(API api, String endpointType, String pathToAchieve) throws APIManagementException { + ApiMgtDAO apiMgtDAO = ApiMgtDAO.getInstance(); GatewayContentDTO customBackendSequenceContentDto = new GatewayContentDTO(); String customSequence = null; SequenceBackendData data = apiMgtDAO.getCustomBackendByAPIUUID(api.getUuid(), endpointType); From d7415a84d1ef48bd6e893e478bb91cc6be95f972 Mon Sep 17 00:00:00 2001 From: BLasan Date: Thu, 26 Sep 2024 12:05:21 +0530 Subject: [PATCH 52/56] Handle: Transaction Rollbacks on failure --- .../carbon/apimgt/impl/dao/ApiMgtDAO.java | 76 ++++++++++--------- 1 file changed, 39 insertions(+), 37 deletions(-) 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 2d55bdbe409a..2775c930cb3a 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 @@ -21527,12 +21527,17 @@ public void updateCustomBackend(String apiUUID, String sequenceName, String sequ String deleteCustomBackedQuery = SQLConstants.CustomBackendConstants.DELETE_CUSTOM_BACKEND_BY_API_AND_TYPE; try (Connection connection = APIMgtDBUtil.getConnection(); PreparedStatement prepStmt = connection.prepareStatement(deleteCustomBackedQuery)) { - connection.setAutoCommit(false); - prepStmt.setString(1, apiUUID); - prepStmt.setString(2, type); - prepStmt.executeUpdate(); - addCustomBackend(apiUUID, sequenceName, null, sequence, type, connection, backendUUID); - connection.commit(); + try { + connection.setAutoCommit(false); + prepStmt.setString(1, apiUUID); + prepStmt.setString(2, type); + prepStmt.executeUpdate(); + addCustomBackend(apiUUID, sequenceName, null, sequence, type, connection, backendUUID); + connection.commit(); + } catch (SQLException ex) { + connection.rollback(); + handleException("Error while adding Custom Backend for API : " + apiUUID, ex); + } } catch (SQLException e) { handleException("Error while adding Custom Backend for API : " + apiUUID, e); } @@ -21542,11 +21547,16 @@ public void deleteCustomBackend(String apiUUID, String type) throws APIManagemen String deleteCustomBackedQuery = SQLConstants.CustomBackendConstants.DELETE_CUSTOM_BACKEND; try (Connection connection = APIMgtDBUtil.getConnection(); PreparedStatement prepStmt = connection.prepareStatement(deleteCustomBackedQuery)) { - connection.setAutoCommit(false); - prepStmt.setString(1, apiUUID); - prepStmt.setString(2, type); - prepStmt.executeUpdate(); - connection.commit(); + try { + connection.setAutoCommit(false); + prepStmt.setString(1, apiUUID); + prepStmt.setString(2, type); + prepStmt.executeUpdate(); + connection.commit(); + } catch (SQLException ex) { + connection.rollback(); + handleException("Error while deleting Custom Backend for API : " + apiUUID, ex); + } } catch (SQLException e) { handleException("Error while deleting Custom Backend for API : " + apiUUID, e); } @@ -21556,9 +21566,14 @@ public void deleteCustomBackendByAPIID(String apiUUID) throws APIManagementExcep String deleteCustomBackendSql = SQLConstants.CustomBackendConstants.DELETE_CUSTOM_BACKEND_BY_API; try (Connection connection = APIMgtDBUtil.getConnection(); PreparedStatement prepStmt = connection.prepareStatement(deleteCustomBackendSql)) { - connection.setAutoCommit(false); - prepStmt.setString(1, apiUUID); - connection.commit(); + try { + connection.setAutoCommit(false); + prepStmt.setString(1, apiUUID); + connection.commit(); + } catch (SQLException ex) { + connection.rollback(); + handleException("Error while deleting Custom Backend for API: " + apiUUID, ex); + } } catch (SQLException ex) { handleException("Error while deleting Custom Backend for API: " + apiUUID, ex); } @@ -21568,34 +21583,21 @@ public void deleteCustomBackendByRevision(String apiUUID, String revisionUUID) t String deleteSqlQuery = SQLConstants.CustomBackendConstants.DELETE_CUSTOM_BACKEND_BY_REVISION; try (Connection con = APIMgtDBUtil.getConnection(); PreparedStatement ps = con.prepareStatement(deleteSqlQuery)) { - con.setAutoCommit(false); - ps.setString(1, apiUUID); - ps.setString(2, revisionUUID); - ps.executeUpdate(); - con.commit(); + try { + con.setAutoCommit(false); + ps.setString(1, apiUUID); + ps.setString(2, revisionUUID); + ps.executeUpdate(); + con.commit(); + } catch (SQLException ex) { + con.rollback(); + handleException("Error deleting Custom Backend for Revision: " + apiUUID, ex); + } } catch (SQLException ex) { handleException("Error deleting Custom Backend for Revision: " + apiUUID, ex); } } - public void addNewCustomBackendForAPIRevision(String apiUUID, String revisionID, String type, String seqName, InputStream sequence, String backendUUID) throws APIManagementException { - String addSqlQuery = SQLConstants.CustomBackendConstants.ADD_CUSTOM_BACKEND; - try (Connection con = APIMgtDBUtil.getConnection(); - PreparedStatement ps = con.prepareStatement(addSqlQuery)) { - con.setAutoCommit(false); - ps.setString(1, backendUUID); - ps.setString(2, apiUUID); - ps.setBinaryStream(3, sequence); - ps.setString(4, type); - ps.setString(5, revisionID); - ps.setString(6, seqName); - ps.executeUpdate(); - con.commit(); - } catch (SQLException ex) { - handleException("Error when inserting Custom Backend data for API: " + apiUUID, ex); - } - } - public void addCustomBackend(String apiUUID, String sequenceName, String revision, String sequence, String type, Connection connection, String backendUUID) throws APIManagementException { String insertCustomBackendQuery = SQLConstants.CustomBackendConstants.ADD_CUSTOM_BACKEND; From 67d5b98f9d9d8b15edd6793aff1bfd707e59e925 Mon Sep 17 00:00:00 2001 From: BLasan Date: Thu, 26 Sep 2024 12:06:09 +0530 Subject: [PATCH 53/56] Fix: Type in debug log --- .../main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 a462add89c71..04ceb097da10 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 @@ -10126,7 +10126,7 @@ public static String getCustomBackendSequenceFromFile(String extractedFolderPath String fileName = extractedFolderPath + File.separator + sequenceName; if (checkFileExistence(fileName)) { if (log.isDebugEnabled()) { - log.debug("Found policy definition file " + fileName); + log.debug("Found Sequence Backend file " + fileName); } customBackendContent = FileUtils.readFileToString(new File(fileName)); } From 54ae42a616323a1d01d41d2af78e1a824371b67f Mon Sep 17 00:00:00 2001 From: BLasan Date: Thu, 26 Sep 2024 12:16:04 +0530 Subject: [PATCH 54/56] Fix: Lint Issues --- .../carbon/apimgt/impl/dao/constants/SQLConstants.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) 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 e3c7708c7076..daf7622bed74 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 @@ -4471,9 +4471,13 @@ public static class SystemConfigsConstants { * Static class to hold database queries related to AM_TRANSACTION_RECORDS table */ public static class TransactionCountConstants { - public static final String INSERT_TRANSACTION_COUNT = "INSERT INTO AM_TRANSACTION_RECORDS " + "(ID, HOST, SERVER_ID, SERVER_TYPE, COUNT, RECORDED_TIME) " + "VALUES (?,?,?,?,?,?)"; - public static final String GET_TRANSACTION_COUNT = "SELECT SUM(COUNT) FROM AM_TRANSACTION_RECORDS " + "WHERE RECORDED_TIME >= ? AND RECORDED_TIME <= ?"; + public static final String INSERT_TRANSACTION_COUNT = + "INSERT INTO AM_TRANSACTION_RECORDS " + "(ID, HOST, SERVER_ID, SERVER_TYPE, COUNT, RECORDED_TIME) " + + "VALUES (?,?,?,?,?,?)"; + public static final String GET_TRANSACTION_COUNT = + "SELECT SUM(COUNT) FROM AM_TRANSACTION_RECORDS " + "WHERE RECORDED_TIME >= ? AND RECORDED_TIME <= ?"; } + public static class CustomBackendConstants { public static final String ADD_CUSTOM_BACKEND = "INSERT INTO AM_API_SEQUENCE_BACKEND (ID,API_UUID,SEQUENCE,TYPE,REVISION_UUID,NAME) " From 43047d786b7e33776483f7a0d69ef481f2b29394 Mon Sep 17 00:00:00 2001 From: BLasan Date: Thu, 26 Sep 2024 14:02:20 +0530 Subject: [PATCH 55/56] Add: Sequence Backend schema for MultiDC --- .../resources/multi-dc/OGG/oracle/apimgt/tables.sql | 11 +++++++++++ .../multi-dc/OGG/oracle/apimgt/tables_23c.sql | 11 +++++++++++ .../resources/multi-dc/Postgresql/apimgt/tables.sql | 11 +++++++++++ .../multi-dc/SQLServer/mssql/apimgt/tables.sql | 11 +++++++++++ .../src/main/resources/sql/mssql.sql | 2 ++ 5 files changed, 46 insertions(+) diff --git a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/multi-dc/OGG/oracle/apimgt/tables.sql b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/multi-dc/OGG/oracle/apimgt/tables.sql index 16b7d7ea56b3..87bde5d3d63a 100644 --- a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/multi-dc/OGG/oracle/apimgt/tables.sql +++ b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/multi-dc/OGG/oracle/apimgt/tables.sql @@ -1496,6 +1496,17 @@ CREATE TABLE AM_API ( UNIQUE (API_UUID) ) / +CREATE TABLE AM_API_SEQUENCE_BACKEND ( + ID VARCHAR2(60) NOT NULL, + API_UUID VARCHAR2(256) NOT NULL, + REVISION_UUID VARCHAR2(256) DEFAULT '0', + SEQUENCE BLOB NOT NULL, + NAME VARCHAR2(256) NOT NULL, + TYPE VARCHAR2(120) NOT NULL, + PRIMARY KEY (ID,API_UUID,REVISION_UUID,TYPE), + FOREIGN KEY (API_UUID) REFERENCES AM_API(API_UUID) ON DELETE CASCADE +) +/ CREATE TABLE AM_GRAPHQL_COMPLEXITY ( UUID VARCHAR(256), API_ID INTEGER NOT NULL, diff --git a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/multi-dc/OGG/oracle/apimgt/tables_23c.sql b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/multi-dc/OGG/oracle/apimgt/tables_23c.sql index 25ecda61f38d..8f404e39d9d2 100644 --- a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/multi-dc/OGG/oracle/apimgt/tables_23c.sql +++ b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/multi-dc/OGG/oracle/apimgt/tables_23c.sql @@ -1496,6 +1496,17 @@ CREATE TABLE AM_API ( UNIQUE (API_UUID) ) / +CREATE TABLE AM_API_SEQUENCE_BACKEND ( + ID VARCHAR2(60) NOT NULL, + API_UUID VARCHAR2(256) NOT NULL, + REVISION_UUID VARCHAR2(256) DEFAULT '0', + SEQUENCE BLOB NOT NULL, + NAME VARCHAR2(256) NOT NULL, + TYPE VARCHAR2(120) NOT NULL, + PRIMARY KEY (ID,API_UUID,REVISION_UUID,TYPE), + FOREIGN KEY (API_UUID) REFERENCES AM_API(API_UUID) ON DELETE CASCADE +) +/ CREATE TABLE AM_GRAPHQL_COMPLEXITY ( UUID VARCHAR(256), API_ID INTEGER NOT NULL, diff --git a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/multi-dc/Postgresql/apimgt/tables.sql b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/multi-dc/Postgresql/apimgt/tables.sql index 6a9900d85673..0432714113c9 100644 --- a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/multi-dc/Postgresql/apimgt/tables.sql +++ b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/multi-dc/Postgresql/apimgt/tables.sql @@ -1860,6 +1860,17 @@ CREATE TABLE IF NOT EXISTS AM_API ( UNIQUE (API_UUID) ); +CREATE TABLE IF NOT EXISTS AM_API_SEQUENCE_BACKEND ( + ID VARCHAR(60) NOT NULL, + API_UUID VARCHAR(256) NOT NULL, + REVISION_UUID VARCHAR(256) DEFAULT '0', + SEQUENCE BYTEA NOT NULL, + NAME VARCHAR(256) NOT NULL, + TYPE VARCHAR(120) NOT NULL, + PRIMARY KEY (ID,API_UUID,REVISION_UUID,TYPE), + FOREIGN KEY (API_UUID) REFERENCES AM_API(API_UUID) ON DELETE CASCADE +); + CREATE TABLE IF NOT EXISTS AM_GRAPHQL_COMPLEXITY ( UUID VARCHAR(256), API_ID INTEGER NOT NULL, diff --git a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/multi-dc/SQLServer/mssql/apimgt/tables.sql b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/multi-dc/SQLServer/mssql/apimgt/tables.sql index b383d7f619dc..eb2091cb8964 100644 --- a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/multi-dc/SQLServer/mssql/apimgt/tables.sql +++ b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/multi-dc/SQLServer/mssql/apimgt/tables.sql @@ -1729,6 +1729,17 @@ CREATE TABLE AM_API ( UNIQUE (API_UUID) ); +CREATE TABLE AM_API_SEQUENCE_BACKEND ( + ID VARCHAR(60) NOT NULL, + API_UUID VARCHAR(256) NOT NULL, + REVISION_UUID VARCHAR(256) DEFAULT '0', + SEQUENCE VARBINARY(MAX) NOT NULL, + NAME VARCHAR(256) NOT NULL, + TYPE VARCHAR(120) NOT NULL, + PRIMARY KEY (ID, API_UUID, REVISION_UUID, TYPE), + FOREIGN KEY (API_UUID) REFERENCES AM_API(API_UUID) ON DELETE CASCADE +); + IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[AM_GRAPHQL_COMPLEXITY]') AND TYPE IN (N'U')) CREATE TABLE AM_GRAPHQL_COMPLEXITY ( diff --git a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/mssql.sql b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/mssql.sql index 62f623739490..8aa2f087bc83 100644 --- a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/mssql.sql +++ b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/mssql.sql @@ -1740,6 +1740,8 @@ CREATE TABLE AM_API ( UNIQUE (API_UUID) ); + + IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[AM_GRAPHQL_COMPLEXITY]') AND TYPE IN (N'U')) CREATE TABLE AM_GRAPHQL_COMPLEXITY ( From d12fca01507cba1d8b72fed61943eeb6bbc48101 Mon Sep 17 00:00:00 2001 From: BLasan Date: Thu, 26 Sep 2024 15:38:17 +0530 Subject: [PATCH 56/56] Fix: Merge Conflicts --- .../common/mappings/PublisherCommonUtils.java | 22 +++---------------- 1 file changed, 3 insertions(+), 19 deletions(-) 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 239cac422190..d8635ad5bbba 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 @@ -31,6 +31,7 @@ import graphql.schema.validation.SchemaValidationError; import graphql.schema.validation.SchemaValidator; import io.swagger.v3.parser.ObjectMapperFactory; +import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringUtils; @@ -101,9 +102,11 @@ import org.wso2.carbon.core.util.CryptoUtil; import org.wso2.carbon.utils.multitenancy.MultitenantConstants; +import java.io.File; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Field; +import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -204,7 +207,6 @@ public static API updateApi(API originalAPI, APIDTO apiDtoToUpdate, APIProvider } } return apiUpdated; - // TODO use returend api } /** @@ -1144,23 +1146,6 @@ public static API addAPIWithGeneratedSwaggerDefinition(APIDTO apiDto, String oas encryptEndpointSecurityApiKeyCredentials(endpointConfig, cryptoUtil, StringUtils.EMPTY, StringUtils.EMPTY, apiDto); - // update endpointConfig with the provided custom sequence - if (endpointConfig != null) { - 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); - apiDto.setEndpointConfig(endpointConfig); - } - } catch (IOException ex) { - throw new APIManagementException("Error while reading Custom Sequence of API: " + apiDto.getId(), - ex, ExceptionCodes.ERROR_READING_CUSTOM_SEQUENCE); - } - } - } // AWS Lambda: secret key encryption while creating the API if (endpointConfig.containsKey(APIConstants.AMZN_SECRET_KEY)) { @@ -1171,7 +1156,6 @@ public static API addAPIWithGeneratedSwaggerDefinition(APIDTO apiDto, String oas apiDto.setEndpointConfig(endpointConfig); } } - } /* if (isWSAPI) { ArrayList websocketTransports = new ArrayList<>();