use of org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException in project carbon-apimgt by wso2.
the class APIProviderImpl method getAPIbyUUID.
@Override
public API getAPIbyUUID(String uuid, String organization) throws APIManagementException {
Organization org = new Organization(organization);
try {
PublisherAPI publisherAPI = apiPersistenceInstance.getPublisherAPI(org, uuid);
if (publisherAPI != null) {
API api = APIMapper.INSTANCE.toApi(publisherAPI);
APIIdentifier apiIdentifier = api.getId();
apiIdentifier.setUuid(uuid);
api.setId(apiIdentifier);
checkAccessControlPermission(userNameWithoutChange, api.getAccessControl(), api.getAccessControlRoles());
// ///////////////// Do processing on the data object//////////
populateRevisionInformation(api, uuid);
populateAPIInformation(uuid, organization, api);
loadMediationPoliciesToAPI(api, organization);
populateAPIStatus(api);
populateDefaultVersion(api);
return api;
} else {
String msg = "Failed to get API. API artifact corresponding to artifactId " + uuid + " does not exist";
throw new APIMgtResourceNotFoundException(msg);
}
} catch (APIPersistenceException e) {
throw new APIManagementException("Failed to get API", e);
} catch (OASPersistenceException e) {
throw new APIManagementException("Error while retrieving the OAS definition", e);
} catch (ParseException e) {
throw new APIManagementException("Error while parsing the OAS definition", e);
} catch (AsyncSpecPersistenceException e) {
throw new APIManagementException("Error while retrieving the Async API definition", e);
}
}
use of org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException in project carbon-apimgt by wso2.
the class APIProviderImpl method createNewAPIVersion.
public API createNewAPIVersion(String existingApiId, String newVersion, Boolean isDefaultVersion, String organization) throws APIManagementException {
API existingAPI = getAPIbyUUID(existingApiId, organization);
if (existingAPI == null) {
throw new APIMgtResourceNotFoundException("API not found for id " + existingApiId, ExceptionCodes.from(ExceptionCodes.API_NOT_FOUND, existingApiId));
}
if (newVersion.equals(existingAPI.getId().getVersion())) {
throw new APIMgtResourceAlreadyExistsException("Version " + newVersion + " exists for api " + existingAPI.getId().getApiName());
}
existingAPI.setOrganization(organization);
APIIdentifier existingAPIId = existingAPI.getId();
String existingAPICreatedTime = existingAPI.getCreatedTime();
String existingAPIStatus = existingAPI.getStatus();
boolean isExsitingAPIdefaultVersion = existingAPI.isDefaultVersion();
String existingContext = existingAPI.getContext();
String existingVersionTimestamp = existingAPI.getVersionTimestamp();
APIIdentifier newApiId = new APIIdentifier(existingAPI.getId().getProviderName(), existingAPI.getId().getApiName(), newVersion);
existingAPI.setUuid(null);
existingAPI.setId(newApiId);
existingAPI.setStatus(APIConstants.CREATED);
existingAPI.setDefaultVersion(isDefaultVersion);
existingAPI.setVersionTimestamp("");
// We need to change the context by setting the new version
// This is a change that is coming with the context version strategy
String existingAPIContextTemplate = existingAPI.getContextTemplate();
existingAPI.setContext(existingAPIContextTemplate.replace("{version}", newVersion));
Map<String, List<OperationPolicy>> operationPoliciesMap = extractAndDropOperationPoliciesFromURITemplate(existingAPI.getUriTemplates());
API newAPI = addAPI(existingAPI);
String newAPIId = newAPI.getUuid();
if (!operationPoliciesMap.isEmpty()) {
// clone common or API specific operation policy.
Map<String, String> clonedOperationPolicyMap = cloneOperationPoliciesToAPI(existingApiId, newAPI, operationPoliciesMap);
// attach policy to uri template.
attachOperationPoliciesToAPI(newAPI, clonedOperationPolicyMap, operationPoliciesMap);
}
// copy docs
List<Documentation> existingDocs = getAllDocumentation(existingApiId, organization);
if (existingDocs != null) {
for (Documentation documentation : existingDocs) {
Documentation newDoc = addDocumentation(newAPIId, documentation, organization);
DocumentationContent content = getDocumentationContent(existingApiId, documentation.getId(), // TODO see whether we can optimize this
organization);
if (content != null) {
addDocumentationContent(newAPIId, newDoc.getId(), organization, content);
}
}
}
// copy icon
ResourceFile icon = getIcon(existingApiId, organization);
if (icon != null) {
setThumbnailToAPI(newAPIId, icon, organization);
}
// copy sequences
List<Mediation> mediationPolicies = getAllApiSpecificMediationPolicies(existingApiId, organization);
if (mediationPolicies != null) {
for (Mediation mediation : mediationPolicies) {
Mediation policy = getApiSpecificMediationPolicyByPolicyId(existingApiId, mediation.getUuid(), organization);
addApiSpecificMediationPolicy(newAPIId, policy, organization);
}
}
// copy wsdl
if (!APIConstants.API_TYPE_SOAPTOREST.equals(existingAPI.getType()) && existingAPI.getWsdlUrl() != null) {
ResourceFile wsdl = getWSDL(existingApiId, organization);
if (wsdl != null) {
addWSDLResource(newAPIId, wsdl, null, organization);
}
}
// copy graphql definition
String graphQLSchema = getGraphqlSchemaDefinition(existingApiId, organization);
if (graphQLSchema != null) {
saveGraphqlSchemaDefinition(newAPIId, graphQLSchema, organization);
}
// update old api
// revert back to old values before update.
existingAPI.setUuid(existingApiId);
existingAPI.setStatus(existingAPIStatus);
existingAPI.setId(existingAPIId);
existingAPI.setContext(existingContext);
existingAPI.setCreatedTime(existingAPICreatedTime);
// update existing api with the original timestamp
existingAPI.setVersionTimestamp(existingVersionTimestamp);
if (isDefaultVersion) {
existingAPI.setDefaultVersion(false);
} else {
existingAPI.setDefaultVersion(isExsitingAPIdefaultVersion);
}
try {
apiPersistenceInstance.updateAPI(new Organization(organization), APIMapper.INSTANCE.toPublisherApi(existingAPI));
} catch (APIPersistenceException e) {
throw new APIManagementException("Error while updating API details", e);
}
return getAPIbyUUID(newAPIId, organization);
}
use of org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException in project carbon-apimgt by wso2.
the class APIProviderImpl method undeployAPIProductRevisionDeployment.
@Override
public void undeployAPIProductRevisionDeployment(String apiProductId, String apiRevisionId, List<APIRevisionDeployment> apiRevisionDeployments) throws APIManagementException {
APIProductIdentifier apiProductIdentifier = APIUtil.getAPIProductIdentifierFromUUID(apiProductId);
if (apiProductIdentifier == null) {
throw new APIMgtResourceNotFoundException("Couldn't retrieve existing API Product with ID: " + apiProductId, ExceptionCodes.from(ExceptionCodes.API_NOT_FOUND, apiProductId));
}
APIRevision apiRevision = apiMgtDAO.getRevisionByRevisionUUID(apiRevisionId);
if (apiRevision == null) {
throw new APIMgtResourceNotFoundException("Couldn't retrieve existing API Revision with Revision UUID: " + apiRevisionId, ExceptionCodes.from(ExceptionCodes.API_REVISION_NOT_FOUND, apiRevisionId));
}
APIProduct product = getAPIProductbyUUID(apiRevisionId, tenantDomain);
product.setUuid(apiProductId);
Set<String> environmentsToRemove = new HashSet<>();
for (APIRevisionDeployment apiRevisionDeployment : apiRevisionDeployments) {
environmentsToRemove.add(apiRevisionDeployment.getDeployment());
}
product.setEnvironments(environmentsToRemove);
removeFromGateway(product, tenantDomain, new HashSet<>(apiRevisionDeployments), Collections.emptySet());
apiMgtDAO.removeAPIRevisionDeployment(apiRevisionId, apiRevisionDeployments);
GatewayArtifactsMgtDAO.getInstance().removePublishedGatewayLabels(apiProductId, apiRevisionId);
}
use of org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException in project carbon-apimgt by wso2.
the class APIProviderImpl method deployAPIRevision.
/**
* Adds a new APIRevisionDeployment to an existing API
*
* @param apiId API UUID
* @param apiRevisionId API Revision UUID
* @param apiRevisionDeployments List of APIRevisionDeployment objects
* @param organization identifier of the organization
* @throws APIManagementException if failed to add APIRevision
*/
@Override
public void deployAPIRevision(String apiId, String apiRevisionId, List<APIRevisionDeployment> apiRevisionDeployments, String organization) throws APIManagementException {
APIIdentifier apiIdentifier = APIUtil.getAPIIdentifierFromUUID(apiId);
if (apiIdentifier == null) {
throw new APIMgtResourceNotFoundException("Couldn't retrieve existing API with API UUID: " + apiId, ExceptionCodes.from(ExceptionCodes.API_NOT_FOUND, apiId));
}
APIRevision apiRevision = apiMgtDAO.getRevisionByRevisionUUID(apiRevisionId);
if (apiRevision == null) {
throw new APIMgtResourceNotFoundException("Couldn't retrieve existing API Revision with Revision UUID: " + apiRevisionId, ExceptionCodes.from(ExceptionCodes.API_REVISION_NOT_FOUND, apiRevisionId));
}
List<APIRevisionDeployment> currentApiRevisionDeploymentList = apiMgtDAO.getAPIRevisionDeploymentsByApiUUID(apiId);
APIGatewayManager gatewayManager = APIGatewayManager.getInstance();
API api = getLightweightAPIByUUID(apiId, organization);
api.setRevisionedApiId(apiRevision.getRevisionUUID());
api.setRevisionId(apiRevision.getId());
api.setUuid(apiId);
api.getId().setUuid(apiId);
api.setOrganization(organization);
Set<String> environmentsToAdd = new HashSet<>();
Map<String, String> gatewayVhosts = new HashMap<>();
Set<APIRevisionDeployment> environmentsToRemove = new HashSet<>();
for (APIRevisionDeployment apiRevisionDeployment : apiRevisionDeployments) {
for (APIRevisionDeployment currentapiRevisionDeployment : currentApiRevisionDeploymentList) {
if (StringUtils.equalsIgnoreCase(currentapiRevisionDeployment.getDeployment(), apiRevisionDeployment.getDeployment())) {
environmentsToRemove.add(currentapiRevisionDeployment);
}
}
environmentsToAdd.add(apiRevisionDeployment.getDeployment());
gatewayVhosts.put(apiRevisionDeployment.getDeployment(), apiRevisionDeployment.getVhost());
}
if (environmentsToRemove.size() > 0) {
apiMgtDAO.removeAPIRevisionDeployment(apiId, environmentsToRemove);
removeFromGateway(api, environmentsToRemove, environmentsToAdd);
}
GatewayArtifactsMgtDAO.getInstance().addAndRemovePublishedGatewayLabels(apiId, apiRevisionId, environmentsToAdd, gatewayVhosts, environmentsToRemove);
apiMgtDAO.addAPIRevisionDeployment(apiRevisionId, apiRevisionDeployments);
if (environmentsToAdd.size() > 0) {
// TODO remove this to organization once the microgateway can build gateway based on organization.
gatewayManager.deployToGateway(api, tenantDomain, environmentsToAdd);
}
String publishedDefaultVersion = getPublishedDefaultVersion(apiIdentifier);
String defaultVersion = getDefaultVersion(apiIdentifier);
apiMgtDAO.updateDefaultAPIPublishedVersion(apiIdentifier);
if (publishedDefaultVersion != null) {
if (apiIdentifier.getVersion().equals(defaultVersion)) {
api.setAsPublishedDefaultVersion(true);
}
if (api.isPublishedDefaultVersion() && !apiIdentifier.getVersion().equals(publishedDefaultVersion)) {
APIIdentifier previousDefaultVersionIdentifier = new APIIdentifier(api.getId().getProviderName(), api.getId().getApiName(), publishedDefaultVersion);
sendUpdateEventToPreviousDefaultVersion(previousDefaultVersionIdentifier, organization);
}
}
}
use of org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException in project carbon-apimgt by wso2.
the class APIProviderImpl method deleteAPIRevision.
/**
* Delete an API Revision
*
* @param apiId API UUID
* @param apiRevisionId API Revision UUID
* @param organization identifier of the organization
* @throws APIManagementException if failed to delete APIRevision
*/
@Override
public void deleteAPIRevision(String apiId, String apiRevisionId, String organization) throws APIManagementException {
APIIdentifier apiIdentifier = APIUtil.getAPIIdentifierFromUUID(apiId);
if (apiIdentifier == null) {
throw new APIMgtResourceNotFoundException("Couldn't retrieve existing API with API UUID: " + apiId, ExceptionCodes.from(ExceptionCodes.API_NOT_FOUND, apiId));
}
APIRevision apiRevision = apiMgtDAO.getRevisionByRevisionUUID(apiRevisionId);
if (apiRevision == null) {
throw new APIMgtResourceNotFoundException("Couldn't retrieve existing API Revision with Revision UUID: " + apiRevisionId, ExceptionCodes.from(ExceptionCodes.API_REVISION_NOT_FOUND, apiRevisionId));
}
List<APIRevisionDeployment> apiRevisionDeploymentsResponse = getAPIRevisionDeploymentList(apiRevisionId);
if (apiRevisionDeploymentsResponse.size() != 0) {
String errorMessage = "Couldn't delete API revision since API revision is currently deployed to a gateway" + "." + "You need to undeploy the API Revision from the gateway before attempting deleting API Revision: " + apiRevision.getRevisionUUID();
throw new APIManagementException(errorMessage, ExceptionCodes.from(ExceptionCodes.EXISTING_API_REVISION_DEPLOYMENT_FOUND, apiRevisionId));
}
apiIdentifier.setUuid(apiId);
try {
apiPersistenceInstance.deleteAPIRevision(new Organization(organization), apiIdentifier.getUUID(), apiRevision.getRevisionUUID(), apiRevision.getId());
} catch (APIPersistenceException e) {
String errorMessage = "Failed to delete registry artifacts";
throw new APIManagementException(errorMessage, ExceptionCodes.from(ExceptionCodes.ERROR_DELETING_API_REVISION, apiRevision.getApiUUID()));
}
apiMgtDAO.deleteAPIRevision(apiRevision);
gatewayArtifactsMgtDAO.deleteGatewayArtifact(apiRevision.getApiUUID(), apiRevision.getRevisionUUID());
if (artifactSaver != null) {
try {
artifactSaver.removeArtifact(apiRevision.getApiUUID(), apiIdentifier.getApiName(), apiIdentifier.getVersion(), apiRevision.getRevisionUUID(), organization);
} catch (ArtifactSynchronizerException e) {
log.error("Error while deleting Runtime artifacts from artifact Store", e);
}
}
}
Aggregations