Skip to content

Commit

Permalink
Adding context Template for API Products
Browse files Browse the repository at this point in the history
  • Loading branch information
HiranyaKavishani committed Dec 6, 2023
1 parent 5ede3d2 commit aa0668d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4173,6 +4173,25 @@ public Map<API, List<APIProductResource>> 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 {

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, " +
Expand Down

0 comments on commit aa0668d

Please sign in to comment.