use of org.wso2.carbon.apimgt.api.model.APIProductIdentifier in project carbon-apimgt by wso2.
the class RegistryPersistenceImpl method updateAPIProduct.
@Override
public PublisherAPIProduct updateAPIProduct(Organization org, PublisherAPIProduct publisherAPIProduct) throws APIPersistenceException {
String requestedTenantDomain = org.getName();
boolean isTenantFlowStarted = false;
boolean transactionCommitted = false;
APIProduct apiProduct;
Registry registry = null;
try {
RegistryHolder holder = getRegistry(requestedTenantDomain);
registry = holder.getRegistry();
isTenantFlowStarted = holder.isTenantFlowStarted();
registry.beginTransaction();
GenericArtifactManager artifactManager = RegistryPersistenceUtil.getArtifactManager(registry, APIConstants.API_KEY);
if (artifactManager == null) {
String errorMessage = "Artifact manager is null when updating API Product with artifact ID " + publisherAPIProduct.getId();
log.error(errorMessage);
throw new APIManagementException(errorMessage);
}
GenericArtifact artifact = artifactManager.getGenericArtifact(publisherAPIProduct.getId());
apiProduct = APIProductMapper.INSTANCE.toApiProduct(publisherAPIProduct);
APIProductIdentifier id = new APIProductIdentifier(publisherAPIProduct.getProviderName(), publisherAPIProduct.getApiProductName(), publisherAPIProduct.getVersion());
apiProduct.setID(id);
GenericArtifact updateApiProductArtifact = RegistryPersistenceUtil.createAPIProductArtifactContent(artifact, apiProduct);
String artifactPath = GovernanceUtils.getArtifactPath(registry, updateApiProductArtifact.getId());
artifactManager.updateGenericArtifact(updateApiProductArtifact);
String visibleRolesList = apiProduct.getVisibleRoles();
String[] visibleRoles = new String[0];
if (visibleRolesList != null) {
visibleRoles = visibleRolesList.split(",");
}
org.wso2.carbon.registry.core.Tag[] oldTags = registry.getTags(artifactPath);
if (oldTags != null) {
for (org.wso2.carbon.registry.core.Tag tag : oldTags) {
registry.removeTag(artifactPath, tag.getTagName());
}
}
Set<String> tagSet = apiProduct.getTags();
if (tagSet != null) {
for (String tag : tagSet) {
registry.applyTag(artifactPath, tag);
}
}
String publisherAccessControlRoles = apiProduct.getAccessControlRoles();
updateRegistryResources(registry, artifactPath, publisherAccessControlRoles, apiProduct.getAccessControl(), apiProduct.getAdditionalProperties());
RegistryPersistenceUtil.setResourcePermissions(apiProduct.getId().getProviderName(), apiProduct.getVisibility(), visibleRoles, artifactPath, registry);
registry.commitTransaction();
transactionCommitted = true;
return publisherAPIProduct;
} catch (Exception e) {
try {
registry.rollbackTransaction();
} catch (RegistryException re) {
// Throwing an error from this level will mask the original exception
log.error("Error while rolling back the transaction for API Product: " + publisherAPIProduct.getApiProductName(), re);
}
throw new APIPersistenceException("Error while performing registry transaction operation", e);
} finally {
try {
if (!transactionCommitted) {
registry.rollbackTransaction();
}
} catch (RegistryException ex) {
throw new APIPersistenceException("Error occurred while rolling back the transaction.", ex);
}
if (isTenantFlowStarted) {
PrivilegedCarbonContext.endTenantFlow();
}
}
}
use of org.wso2.carbon.apimgt.api.model.APIProductIdentifier in project carbon-apimgt by wso2.
the class RegistryPersistenceImpl method addAPIProduct.
@Override
public PublisherAPIProduct addAPIProduct(Organization org, PublisherAPIProduct publisherAPIProduct) throws APIPersistenceException {
Registry registry = null;
boolean isTenantFlowStarted = false;
boolean transactionCommitted = false;
APIProduct apiProduct;
try {
String tenantDomain = org.getName();
RegistryHolder holder = getRegistry(tenantDomain);
registry = holder.getRegistry();
isTenantFlowStarted = holder.isTenantFlowStarted();
registry.beginTransaction();
GenericArtifactManager artifactManager = RegistryPersistenceUtil.getArtifactManager(registry, APIConstants.API_KEY);
GenericArtifact genericArtifact = artifactManager.newGovernanceArtifact(new QName(publisherAPIProduct.getApiProductName()));
apiProduct = APIProductMapper.INSTANCE.toApiProduct(publisherAPIProduct);
APIProductIdentifier id = new APIProductIdentifier(publisherAPIProduct.getProviderName(), publisherAPIProduct.getApiProductName(), publisherAPIProduct.getVersion());
apiProduct.setID(id);
if (genericArtifact == null) {
String errorMessage = "Generic artifact is null when creating API Product" + apiProduct.getId().getName();
log.error(errorMessage);
throw new APIManagementException(errorMessage);
}
GenericArtifact artifact = RegistryPersistenceUtil.createAPIProductArtifactContent(genericArtifact, apiProduct);
artifactManager.addGenericArtifact(artifact);
artifact.attachLifecycle(APIConstants.API_LIFE_CYCLE);
String artifactPath = GovernanceUtils.getArtifactPath(registry, artifact.getId());
String providerPath = APIConstants.API_LOCATION + RegistryConstants.PATH_SEPARATOR + id.getProviderName();
// provider ------provides----> APIProduct
registry.addAssociation(providerPath, artifactPath, APIConstants.PROVIDER_ASSOCIATION);
String apiProductStatus = apiProduct.getState();
saveAPIStatus(registry, artifactPath, apiProductStatus);
Set<String> tagSet = apiProduct.getTags();
if (tagSet != null) {
for (String tag : tagSet) {
registry.applyTag(artifactPath, tag);
}
}
String visibleRolesList = apiProduct.getVisibleRoles();
String[] visibleRoles = new String[0];
if (visibleRolesList != null) {
visibleRoles = visibleRolesList.split(",");
}
String publisherAccessControlRoles = apiProduct.getAccessControlRoles();
updateRegistryResources(registry, artifactPath, publisherAccessControlRoles, apiProduct.getAccessControl(), apiProduct.getAdditionalProperties());
RegistryPersistenceUtil.setResourcePermissions(apiProduct.getId().getProviderName(), apiProduct.getVisibility(), visibleRoles, artifactPath, registry);
registry.commitTransaction();
transactionCommitted = true;
if (log.isDebugEnabled()) {
String logMessage = "API Product Name: " + apiProduct.getId().getName() + ", API Product Version " + apiProduct.getId().getVersion() + " created";
log.debug(logMessage);
}
publisherAPIProduct.setCreatedTime(String.valueOf(new Date().getTime()));
publisherAPIProduct.setId(artifact.getId());
return publisherAPIProduct;
} catch (RegistryException e) {
try {
registry.rollbackTransaction();
} catch (RegistryException re) {
// Throwing an error here would mask the original exception
log.error("Error while rolling back the transaction for API Product : " + publisherAPIProduct.getApiProductName(), re);
}
throw new APIPersistenceException("Error while performing registry transaction operation", e);
} catch (APIManagementException e) {
throw new APIPersistenceException("Error while creating API Product", e);
} finally {
try {
if (!transactionCommitted) {
registry.rollbackTransaction();
}
} catch (RegistryException ex) {
throw new APIPersistenceException("Error while rolling back the transaction for API Product : " + publisherAPIProduct.getApiProductName(), ex);
}
if (isTenantFlowStarted) {
PrivilegedCarbonContext.endTenantFlow();
}
}
}
use of org.wso2.carbon.apimgt.api.model.APIProductIdentifier in project carbon-apimgt by wso2.
the class RegistryPersistenceUtil method updateAPIProductDependencies.
public static void updateAPIProductDependencies(API api, Registry registry) throws APIManagementException {
for (URITemplate uriTemplate : api.getUriTemplates()) {
Set<APIProductIdentifier> usedByProducts = uriTemplate.retrieveUsedByProducts();
for (APIProductIdentifier usedByProduct : usedByProducts) {
String apiProductPath = RegistryPersistenceUtil.getAPIProductPath(usedByProduct);
usedByProduct.setUUID(apiProductPath);
}
}
}
use of org.wso2.carbon.apimgt.api.model.APIProductIdentifier in project carbon-apimgt by wso2.
the class OASParserUtil method getAPIDefinition.
/**
* This method returns api definition json for given api
*
* @param apiIdentifier api identifier
* @param registry user registry
* @return api definition json as json string
* @throws APIManagementException
*/
public static String getAPIDefinition(Identifier apiIdentifier, Registry registry) throws APIManagementException {
String resourcePath = "";
if (apiIdentifier instanceof APIIdentifier) {
APIRevision apiRevision = ApiMgtDAO.getInstance().checkAPIUUIDIsARevisionUUID(apiIdentifier.getUUID());
if (apiRevision != null && apiRevision.getApiUUID() != null) {
resourcePath = APIUtil.getRevisionPath(apiRevision.getApiUUID(), apiRevision.getId());
} else {
resourcePath = APIUtil.getOpenAPIDefinitionFilePath(apiIdentifier.getName(), apiIdentifier.getVersion(), apiIdentifier.getProviderName());
}
} else if (apiIdentifier instanceof APIProductIdentifier) {
resourcePath = APIUtil.getAPIProductOpenAPIDefinitionFilePath(apiIdentifier.getName(), apiIdentifier.getVersion(), apiIdentifier.getProviderName());
}
JSONParser parser = new JSONParser();
String apiDocContent = null;
try {
if (registry.resourceExists(resourcePath + APIConstants.API_OAS_DEFINITION_RESOURCE_NAME)) {
Resource apiDocResource = registry.get(resourcePath + APIConstants.API_OAS_DEFINITION_RESOURCE_NAME);
apiDocContent = new String((byte[]) apiDocResource.getContent(), Charset.defaultCharset());
parser.parse(apiDocContent);
} else {
if (log.isDebugEnabled()) {
log.debug("Resource " + APIConstants.API_OAS_DEFINITION_RESOURCE_NAME + " not found at " + resourcePath);
}
}
} catch (RegistryException e) {
handleException("Error while retrieving OpenAPI v2.0 or v3.0.0 Definition for " + apiIdentifier.getName() + '-' + apiIdentifier.getVersion(), e);
} catch (ParseException e) {
handleException("Error while parsing OpenAPI v2.0 or v3.0.0 Definition for " + apiIdentifier.getName() + '-' + apiIdentifier.getVersion() + " in " + resourcePath, e);
}
return apiDocContent;
}
use of org.wso2.carbon.apimgt.api.model.APIProductIdentifier in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method updateAPIClientCertificateByAlias.
@Override
public Response updateAPIClientCertificateByAlias(String alias, String apiId, InputStream certificateInputStream, Attachment certificateDetail, String tier, MessageContext messageContext) {
try {
// validate if api exists
validateAPIExistence(apiId);
ContentDisposition contentDisposition;
String fileName;
String base64EncodedCert = null;
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
String organization = RestApiUtil.getValidatedOrganization(messageContext);
API api = apiProvider.getAPIbyUUID(apiId, organization);
api.setOrganization(organization);
// validate API update operation permitted based on the LC state
validateAPIOperationsPerLC(api.getStatus());
String userName = RestApiCommonUtil.getLoggedInUsername();
int tenantId = APIUtil.getInternalOrganizationId(organization);
ClientCertificateDTO clientCertificateDTO = CertificateRestApiUtils.preValidateClientCertificate(alias, api.getId(), organization);
if (certificateDetail != null) {
contentDisposition = certificateDetail.getContentDisposition();
fileName = contentDisposition.getParameter(RestApiConstants.CONTENT_DISPOSITION_FILENAME);
if (StringUtils.isNotBlank(fileName)) {
base64EncodedCert = CertificateRestApiUtils.generateEncodedCertificate(certificateInputStream);
}
}
if (StringUtils.isEmpty(base64EncodedCert) && StringUtils.isEmpty(tier)) {
return Response.ok().entity("Client Certificate is not updated for alias " + alias).build();
}
int responseCode = apiProvider.updateClientCertificate(base64EncodedCert, alias, clientCertificateDTO.getApiIdentifier(), tier, tenantId, organization);
if (ResponseCode.SUCCESS.getResponseCode() == responseCode) {
// Handle api product case.
if (API_PRODUCT_TYPE.equals(api.getType())) {
APIIdentifier apiIdentifier = api.getId();
APIProductIdentifier apiProductIdentifier = new APIProductIdentifier(apiIdentifier.getProviderName(), apiIdentifier.getApiName(), apiIdentifier.getVersion());
APIProduct apiProduct = apiProvider.getAPIProduct(apiProductIdentifier);
apiProduct.setOrganization(organization);
apiProvider.updateAPIProduct(apiProduct);
} else {
apiProvider.updateAPI(api);
}
ClientCertMetadataDTO clientCertMetadataDTO = new ClientCertMetadataDTO();
clientCertMetadataDTO.setAlias(alias);
clientCertMetadataDTO.setApiId(api.getUUID());
clientCertMetadataDTO.setTier(clientCertificateDTO.getTierName());
URI updatedCertUri = new URI(RestApiConstants.CLIENT_CERTS_BASE_PATH + "?alias=" + alias);
return Response.ok(updatedCertUri).entity(clientCertMetadataDTO).build();
} else if (ResponseCode.INTERNAL_SERVER_ERROR.getResponseCode() == responseCode) {
RestApiUtil.handleInternalServerError("Error while updating the client certificate for the alias " + alias + " due to an internal " + "server error", log);
} else if (ResponseCode.CERTIFICATE_NOT_FOUND.getResponseCode() == responseCode) {
RestApiUtil.handleResourceNotFoundError("", log);
} else if (ResponseCode.CERTIFICATE_EXPIRED.getResponseCode() == responseCode) {
RestApiUtil.handleBadRequest("Error while updating the client certificate for the alias " + alias + " Certificate Expired.", log);
}
} catch (APIManagementException e) {
RestApiUtil.handleInternalServerError("Error while updating the client certificate for the alias " + alias + " due to an internal " + "server error", e, log);
} catch (IOException e) {
RestApiUtil.handleInternalServerError("Error while encoding client certificate for the alias " + alias, e, log);
} catch (URISyntaxException e) {
RestApiUtil.handleInternalServerError("Error while generating the resource location URI for alias '" + alias + "'", e, log);
} catch (FaultGatewaysException e) {
RestApiUtil.handleInternalServerError("Error while publishing the certificate change to gateways for the alias " + alias, e, log);
}
return null;
}
Aggregations