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 f09d8d5f500e..2d8773807b9f 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 @@ -328,6 +328,17 @@ List getSubscriptionsOfAPI(String apiName, String apiVersion, Str API createNewAPIVersion(String apiId, String newVersion, Boolean defaultVersion, String organization) throws APIManagementException; + /** + * Create a new version of the apiProduct, with version newVersion + * + * @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; /** 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 1cc201027efb..e4c5f05aa81e 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 @@ -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"; 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 fe7f1f0b2489..badd8fa55727 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 @@ -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); @@ -1948,11 +1947,13 @@ public APIProduct createNewAPIProductVersion(String existingApiProductId, String //Add new version of the API Product Map> 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 existingDocs = getAllDocumentation(existingApiProductId, organization); if (existingDocs != null) { @@ -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); } @@ -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> extractedOperationPoliciesMap, diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/LifeCycleUtils.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/LifeCycleUtils.java index 65abd1339ec1..45afc2f8a779 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/LifeCycleUtils.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/LifeCycleUtils.java @@ -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); } } } 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/ApiProductsApiServiceImpl.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApiProductsApiServiceImpl.java index 30c798be889a..b363e72458e0 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApiProductsApiServiceImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApiProductsApiServiceImpl.java @@ -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);