Skip to content

Commit

Permalink
Refactoring codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
HiranyaKavishani committed Oct 10, 2023
1 parent db93064 commit 1f28aa1
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,17 @@ List<SubscribedAPI> getSubscriptionsOfAPI(String apiName, String apiVersion, Str
API createNewAPIVersion(String apiId, String newVersion, Boolean defaultVersion, String organization)
throws APIManagementException;

/**
* Create a new version of the <code>apiProduct</code>, with version <code>newVersion</code>
*
* @param apiProductId The id of the API Product to be copied
* @param newVersion The version of the new API Product
* @param defaultVersion whether this version is default or not
* @param organization Identifier of an organization
* @return apiProduct created apiProduct
* @throws APIManagementException If an error occurs while trying to create
* * the new version of the API Product
*/
APIProduct createNewAPIProductVersion(String apiProductId, String newVersion, Boolean defaultVersion,
String organization) throws APIManagementException;
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1732,7 +1732,9 @@ public enum RegistryResourceTypesForUI {

public static final String API_LC_ACTION_DEPRECATE = "Deprecate";
public static final String DEPRECATE_CHECK_LIST_ITEM = "Deprecate old versions after publishing the API";
public static final String DEPRECATE_CHECK_LIST_ITEM_API_PRODUCT = "Deprecate old versions after publishing the API Product";
public static final String RESUBSCRIBE_CHECK_LIST_ITEM = "Requires re-subscription when publishing the API";
public static final String RESUBSCRIBE_CHECK_LIST_ITEM_API_PRODUCT = "Requires re-subscription when publishing the API Product";
public static final String PUBLISH_IN_PRIVATE_JET_MODE = "Publish In Private-Jet Mode";

public static final String METRICS_PREFIX = "org.wso2.am";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1923,18 +1923,17 @@ public APIProduct createNewAPIProductVersion(String existingApiProductId, String
Boolean isDefaultVersion, String organization) throws APIManagementException {

APIProduct existingAPIProduct = getAPIProductbyUUID(existingApiProductId, organization);
APIProduct clonedAPIProduct = cloneExistingAPIProduct(existingAPIProduct);

if (existingApiProductId == null || existingAPIProduct == null) {
if (existingAPIProduct == null) {
throw new APIMgtResourceNotFoundException("API Product not found for id " + existingApiProductId,
ExceptionCodes.from(ExceptionCodes.API_PRODUCT_NOT_FOUND, existingApiProductId));
}

if (newVersion.equals(clonedAPIProduct.getId().getVersion())) {
if (newVersion.equals(existingAPIProduct.getId().getVersion())) {
throw new APIMgtResourceAlreadyExistsException(
"Version " + newVersion + " exists for apiProduct " + clonedAPIProduct.getId().getName());
"Version " + newVersion + " exists for apiProduct " + existingAPIProduct.getId().getName());
}

APIProduct clonedAPIProduct = cloneExistingAPIProduct(existingAPIProduct);
clonedAPIProduct.setOrganization(organization);
APIProductIdentifier newApiProductId = new APIProductIdentifier(
clonedAPIProduct.getId().getProviderName(), clonedAPIProduct.getId().getName(), newVersion);
Expand All @@ -1948,11 +1947,13 @@ public APIProduct createNewAPIProductVersion(String existingApiProductId, String
//Add new version of the API Product
Map<API, List<APIProductResource>> apiToProductResourceMapping = addAPIProductWithoutPublishingToGateway(
clonedAPIProduct);

APIProduct createdApiProduct = getAPIProduct(newApiProductId);
addAPIProductSwagger(createdApiProduct.getUuid(), apiToProductResourceMapping, createdApiProduct,
organization);
String newAPIProductUUId = createdApiProduct.getUuid();

// add swagger
addAPIProductSwagger(newAPIProductUUId, apiToProductResourceMapping, createdApiProduct, organization);

// copy docs
List<Documentation> existingDocs = getAllDocumentation(existingApiProductId, organization);
if (existingDocs != null) {
Expand All @@ -1972,10 +1973,6 @@ public APIProduct createNewAPIProductVersion(String existingApiProductId, String
setThumbnailToAPI(newAPIProductUUId, icon, organization);
}

if (Boolean.TRUE.equals(isDefaultVersion)) {
updateIsDefaultVersionFalseForPreviousAPIProductVersion(existingAPIProduct);
}

return getAPIProductbyUUID(newAPIProductUUId, organization);
}

Expand All @@ -1984,17 +1981,6 @@ private APIProduct cloneExistingAPIProduct(APIProduct apiProduct) {
Gson gson = new Gson();
return gson.fromJson(gson.toJson(apiProduct), APIProduct.class);
}
private void updateIsDefaultVersionFalseForPreviousAPIProductVersion(APIProduct existingApiProduct)
throws APIManagementException {

existingApiProduct.setDefaultVersion(false);
PublisherAPIProduct publisherAPIProduct = APIProductMapper.INSTANCE.toPublisherApiProduct(existingApiProduct);
try {
apiPersistenceInstance.updateAPIProduct(new Organization(organization), publisherAPIProduct);
} catch (APIPersistenceException e) {
handleException("Error while persisting the updated API Product", e);
}
}

private void cloneAPIPoliciesForNewAPIVersion(String oldAPIUuid, API newAPI,
Map<String, List<OperationPolicy>> extractedOperationPoliciesMap,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,14 @@ private static void executeLifeCycleChecklist(APIProvider apiProvider, ApiTypeWr
if (checklist != null) {
if (checklist.containsKey(APIConstants.DEPRECATE_CHECK_LIST_ITEM)) {
deprecateOldVersions = checklist.get(APIConstants.DEPRECATE_CHECK_LIST_ITEM);
} else if (checklist.containsKey(APIConstants.DEPRECATE_CHECK_LIST_ITEM_API_PRODUCT)) {
deprecateOldVersions = checklist.get(APIConstants.DEPRECATE_CHECK_LIST_ITEM_API_PRODUCT);
}

if (checklist.containsKey(APIConstants.RESUBSCRIBE_CHECK_LIST_ITEM)) {
makeKeysForwardCompatible = !checklist.get(APIConstants.RESUBSCRIBE_CHECK_LIST_ITEM);
} else if (checklist.containsKey(APIConstants.RESUBSCRIBE_CHECK_LIST_ITEM_API_PRODUCT)) {
makeKeysForwardCompatible = !checklist.get(APIConstants.RESUBSCRIBE_CHECK_LIST_ITEM_API_PRODUCT);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1149,35 +1149,38 @@ public Response deleteAPIProductLifecycleStatePendingTasks(String apiProductId,
public Response createNewAPIProductVersion(String newVersion, String apiProductId, Boolean defaultVersion,
MessageContext messageContext) {

URI newVersionedApiUri;
APIProductDTO newVersionedApi;
URI newVersionedApiProductUri;
APIProductDTO newVersionedApiProduct;

try {
APIProductIdentifier productIdentifier = APIUtil.getAPIProductIdentifierFromUUID(apiProductId);
if (productIdentifier == null) {
throw new APIMgtResourceNotFoundException(
"Couldn't retrieve existing API with API UUID: " + apiProductId,
"Couldn't retrieve existing API Product with API Product Id: " + apiProductId,
ExceptionCodes.from(ExceptionCodes.API_NOT_FOUND, apiProductId));
}
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
String tenantDomain = RestApiCommonUtil.getLoggedInUserTenantDomain();

APIProduct versionedAPI = apiProvider.createNewAPIProductVersion(apiProductId, newVersion, defaultVersion,
tenantDomain);
newVersionedApi = APIMappingUtil.fromAPIProducttoDTO(versionedAPI);
newVersionedApiUri = new URI(RestApiConstants.RESOURCE_PATH_APIS + "/" + versionedAPI.getUuid());
return Response.created(newVersionedApiUri).entity(newVersionedApi).build();
APIProduct versionedAPIProduct = apiProvider.createNewAPIProductVersion(apiProductId, newVersion,
defaultVersion, tenantDomain);
newVersionedApiProduct = APIMappingUtil.fromAPIProducttoDTO(versionedAPIProduct);
newVersionedApiProductUri = new URI(
RestApiConstants.RESOURCE_PATH_API_PRODUCTS + "/" + versionedAPIProduct.getUuid());
return Response.created(newVersionedApiProductUri).entity(newVersionedApiProduct).build();

} catch (APIManagementException e) {
if (RestApiUtil.isDueToResourceAlreadyExists(e)) {
String errorMessage = "Requested new version " + newVersion + " of API Product " + apiProductId + " already exists";
String errorMessage = "Requested new version " + newVersion + " of API Product " + apiProductId
+ " already exists";
RestApiUtil.handleResourceAlreadyExistsError(errorMessage, e, log);
} else if (RestApiUtil.isDueToResourceNotFound(e) || RestApiUtil.isDueToAuthorizationFailure(e)) {
//Auth failure occurs when cross tenant accessing APIs. Sends 404, since we don't need to expose the existence of the resource
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, apiProductId, e, log);
//Auth failure occurs when cross tenant accessing APIs. Sends 404, since we don't need to expose
// the existence of the resource
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API_PRODUCT, apiProductId, e, log);
} else if (isAuthorizationFailure(e)) {
RestApiUtil.handleAuthorizationFailure("Authorization failure while copying API : " + apiProductId, e,
log);
RestApiUtil.handleAuthorizationFailure("Authorization failure while copying API : "
+ apiProductId, e, log);
} else {
String errorMessage = "Error while copying API Product : " + apiProductId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
Expand Down

0 comments on commit 1f28aa1

Please sign in to comment.