From aa0668de833deaa929d51820dbc0ef668f7ece2d Mon Sep 17 00:00:00 2001 From: hiranyakavishani Date: Wed, 6 Dec 2023 10:13:27 +0530 Subject: [PATCH] Adding context Template for API Products --- .../carbon/apimgt/impl/APIProviderImpl.java | 21 +++++++++++++++++++ .../carbon/apimgt/impl/dao/ApiMgtDAO.java | 19 +++++++++-------- .../impl/dao/constants/SQLConstants.java | 8 +++---- 3 files changed, 34 insertions(+), 14 deletions(-) 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 73dd14880ed5..5ba6e411210e 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 @@ -4173,6 +4173,25 @@ public Map> addAPIProductWithoutPublishingToGatewa return apiToProductResourceMapping; } + private static void ValidateAPIProductContextTemplate(APIProduct product) throws APIManagementException { + String contextTemplate = product.getContextTemplate(); + + //Validate if the API Product has an unsupported context before executing the query + String invalidContext = "/" + APIConstants.VERSION_PLACEHOLDER; + if (invalidContext.equals(contextTemplate)) { + throw new APIManagementException( + "Cannot add API Product : " + product.getId() + " with unsupported context : " + + contextTemplate); + } + + //If the context template ends with {version} this means that the version will be at the end of the context. + if (contextTemplate.endsWith("/" + APIConstants.VERSION_PLACEHOLDER)) { + //Remove the {version} part from the context template. + contextTemplate = contextTemplate.split(Pattern.quote("/" + APIConstants.VERSION_PLACEHOLDER))[0]; + } + product.setContextTemplate(contextTemplate); + } + private String calculateVersionTimestamp(String provider, String name, String version, String org) throws APIManagementException { @@ -4462,6 +4481,8 @@ private void validateApiProductInfo(APIProduct product) throws APIManagementExce throw new APIManagementException("Character length exceeds the allowable limit", ExceptionCodes.LENGTH_EXCEEDS); } + + ValidateAPIProductContextTemplate(product); } /** * Create an Api Product 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 49dcff884515..a0a22543bb86 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 @@ -14525,15 +14525,16 @@ public void addAPIProduct(APIProduct apiProduct, String organization) throws API prepStmtAddAPIProduct.setString(2, identifier.getName()); prepStmtAddAPIProduct.setString(3, identifier.getVersion()); prepStmtAddAPIProduct.setString(4, apiProduct.getContext()); - prepStmtAddAPIProduct.setString(5, apiProduct.getProductLevelPolicy()); - prepStmtAddAPIProduct.setString(6, APIUtil.replaceEmailDomainBack(identifier.getProviderName())); - prepStmtAddAPIProduct.setTimestamp(7, new Timestamp(System.currentTimeMillis())); - prepStmtAddAPIProduct.setString(8, APIConstants.API_PRODUCT); - prepStmtAddAPIProduct.setString(9, apiProduct.getUuid()); - prepStmtAddAPIProduct.setString(10, apiProduct.getState()); - prepStmtAddAPIProduct.setString(11, organization); - prepStmtAddAPIProduct.setString(12, apiProduct.getGatewayVendor()); - prepStmtAddAPIProduct.setString(13, apiProduct.getVersionTimestamp()); + prepStmtAddAPIProduct.setString(5, apiProduct.getContextTemplate()); + prepStmtAddAPIProduct.setString(6, apiProduct.getProductLevelPolicy()); + prepStmtAddAPIProduct.setString(7, APIUtil.replaceEmailDomainBack(identifier.getProviderName())); + prepStmtAddAPIProduct.setTimestamp(8, new Timestamp(System.currentTimeMillis())); + prepStmtAddAPIProduct.setString(9, APIConstants.API_PRODUCT); + prepStmtAddAPIProduct.setString(10, apiProduct.getUuid()); + prepStmtAddAPIProduct.setString(11, apiProduct.getState()); + prepStmtAddAPIProduct.setString(12, organization); + prepStmtAddAPIProduct.setString(13, apiProduct.getGatewayVendor()); + prepStmtAddAPIProduct.setString(14, apiProduct.getVersionTimestamp()); prepStmtAddAPIProduct.execute(); rs = prepStmtAddAPIProduct.getGeneratedKeys(); 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 38ce9feaab97..69f5e4070230 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 @@ -2716,11 +2716,9 @@ public class SQLConstants { "SELECT URL_PATTERN , URL_MAPPING_ID, HTTP_METHOD FROM AM_API API , AM_API_URL_MAPPING URL " + "WHERE API.API_ID = URL.API_ID AND API.API_UUID =? AND URL.REVISION_UUID IS NULL"; - public static final String ADD_API_PRODUCT = - "INSERT INTO " - + "AM_API(API_PROVIDER, API_NAME, API_VERSION, CONTEXT," - + "API_TIER, CREATED_BY, CREATED_TIME, API_TYPE, API_UUID, STATUS, ORGANIZATION, GATEWAY_VENDOR, VERSION_COMPARABLE) VALUES (?,?,?,?,?,?,?,?,?" - + ",?,?,?,?)"; + public static final String ADD_API_PRODUCT = "INSERT INTO " + "AM_API(API_PROVIDER, API_NAME, API_VERSION, " + + "CONTEXT, CONTEXT_TEMPLATE, API_TIER, CREATED_BY, CREATED_TIME, API_TYPE, API_UUID, STATUS, " + + "ORGANIZATION, GATEWAY_VENDOR, VERSION_COMPARABLE) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; public static final String GET_RESOURCES_OF_PRODUCT = "SELECT API_UM.URL_MAPPING_ID, API_UM.URL_PATTERN, API_UM.HTTP_METHOD, API_UM.AUTH_SCHEME, " +