use of org.wso2.carbon.apimgt.api.model.APIProductIdentifier in project carbon-apimgt by wso2.
the class APIProviderImpl method addFileToProductDocumentation.
/**
* Add a file to a product document of source type FILE
*
* @param productId APIProduct identifier the document belongs to
* @param documentation document
* @param filename name of the file
* @param content content of the file as an Input Stream
* @param contentType content type of the file
* @throws APIManagementException if failed to add the file
*/
public void addFileToProductDocumentation(APIProductIdentifier productId, Documentation documentation, String filename, InputStream content, String contentType) throws APIManagementException {
if (Documentation.DocumentSourceType.FILE.equals(documentation.getSourceType())) {
contentType = "application/force-download";
ResourceFile icon = new ResourceFile(content, contentType);
String filePath = APIUtil.getDocumentationFilePath(productId, filename);
APIProduct apiProduct;
try {
apiProduct = getAPIProduct(productId);
String visibleRolesList = apiProduct.getVisibleRoles();
String[] visibleRoles = new String[0];
if (visibleRolesList != null) {
visibleRoles = visibleRolesList.split(",");
}
APIUtil.setResourcePermissions(apiProduct.getId().getProviderName(), apiProduct.getVisibility(), visibleRoles, filePath, registry);
documentation.setFilePath(addResourceFile(productId, filePath, icon));
APIUtil.setFilePermission(filePath);
} catch (APIManagementException e) {
handleException("Failed to add file to product document " + documentation.getName(), e);
}
} else {
String errorMsg = "Cannot add file to the Product Document. Document " + documentation.getName() + "'s Source type is not FILE.";
handleException(errorMsg);
}
}
use of org.wso2.carbon.apimgt.api.model.APIProductIdentifier in project carbon-apimgt by wso2.
the class APIProviderImpl method changeLifeCycleStatusToPublish.
private void changeLifeCycleStatusToPublish(APIProductIdentifier apiIdentifier) throws APIManagementException {
try {
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(this.username);
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(this.tenantDomain, true);
String productArtifactId = registry.get(APIUtil.getAPIProductPath(apiIdentifier)).getUUID();
GenericArtifactManager artifactManager = APIUtil.getArtifactManager(registry, APIConstants.API_KEY);
GenericArtifact apiArtifact = artifactManager.getGenericArtifact(productArtifactId);
if (apiArtifact != null) {
apiArtifact.invokeAction("Publish", APIConstants.API_LIFE_CYCLE);
if (log.isDebugEnabled()) {
String logMessage = "API Product Status changed successfully. API Product Name: " + apiIdentifier.getName();
log.debug(logMessage);
}
}
} catch (RegistryException e) {
throw new APIManagementException("Error while Changing Lifecycle status of API Product " + apiIdentifier.getName(), e);
} finally {
PrivilegedCarbonContext.endTenantFlow();
}
}
use of org.wso2.carbon.apimgt.api.model.APIProductIdentifier in project carbon-apimgt by wso2.
the class APIProviderImpl method updateDocumentation.
/**
* Updates a given api product documentation
*
* @param productId APIProductIdentifier
* @param documentation Documentation
* @throws org.wso2.carbon.apimgt.api.APIManagementException if failed to update docs
*/
public void updateDocumentation(APIProductIdentifier productId, Documentation documentation) throws APIManagementException {
String productPath = APIUtil.getAPIProductPath(productId);
APIProduct product = getAPIProduct(productPath);
String docPath = APIUtil.getProductDocPath(productId) + documentation.getName();
try {
String docArtifactId = registry.get(docPath).getUUID();
GenericArtifactManager artifactManager = APIUtil.getArtifactManager(registry, APIConstants.DOCUMENTATION_KEY);
GenericArtifact artifact = artifactManager.getGenericArtifact(docArtifactId);
String docVisibility = documentation.getVisibility().name();
String[] authorizedRoles = new String[0];
String visibleRolesList = product.getVisibleRoles();
if (visibleRolesList != null) {
authorizedRoles = visibleRolesList.split(",");
}
String visibility = product.getVisibility();
if (docVisibility != null) {
if (APIConstants.DOC_SHARED_VISIBILITY.equalsIgnoreCase(docVisibility)) {
authorizedRoles = null;
visibility = APIConstants.DOC_SHARED_VISIBILITY;
} else if (APIConstants.DOC_OWNER_VISIBILITY.equalsIgnoreCase(docVisibility)) {
authorizedRoles = null;
visibility = APIConstants.DOC_OWNER_VISIBILITY;
}
}
GenericArtifact updateDocArtifact = APIUtil.createDocArtifactContent(artifact, productId, documentation);
artifactManager.updateGenericArtifact(updateDocArtifact);
APIUtil.clearResourcePermissions(docPath, productId, ((UserRegistry) registry).getTenantId());
APIUtil.setResourcePermissions(product.getId().getProviderName(), visibility, authorizedRoles, artifact.getPath(), registry);
String docFilePath = artifact.getAttribute(APIConstants.DOC_FILE_PATH);
if (docFilePath != null && !"".equals(docFilePath)) {
// The docFilePatch comes as
// /t/tenanatdoman/registry/resource/_system/governance/apimgt/applicationdata..
// We need to remove the
// /t/tenanatdoman/registry/resource/_system/governance section
// to set permissions.
int startIndex = docFilePath.indexOf(APIConstants.GOVERNANCE) + (APIConstants.GOVERNANCE).length();
String filePath = docFilePath.substring(startIndex, docFilePath.length());
APIUtil.setResourcePermissions(product.getId().getProviderName(), visibility, authorizedRoles, filePath, registry);
}
} catch (RegistryException e) {
handleException("Failed to update documentation", e);
}
}
use of org.wso2.carbon.apimgt.api.model.APIProductIdentifier in project carbon-apimgt by wso2.
the class APIProviderImpl method restoreAPIProductRevision.
@Override
public void restoreAPIProductRevision(String apiProductId, String apiRevisionId, String organization) 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));
}
apiProductIdentifier.setUUID(apiProductId);
try {
apiPersistenceInstance.restoreAPIRevision(new Organization(organization), apiProductIdentifier.getUUID(), apiRevision.getRevisionUUID(), apiRevision.getId());
} catch (APIPersistenceException e) {
String errorMessage = "Failed to restore registry artifacts";
throw new APIManagementException(errorMessage, ExceptionCodes.from(ExceptionCodes.ERROR_RESTORING_API_REVISION, apiRevision.getApiUUID()));
}
apiMgtDAO.restoreAPIProductRevision(apiRevision);
}
use of org.wso2.carbon.apimgt.api.model.APIProductIdentifier in project carbon-apimgt by wso2.
the class APIProviderImpl method createDocumentation.
/**
* Create a product documentation
*
* @param product APIProduct
* @param documentation Documentation
* @throws APIManagementException if failed to add documentation
*/
private void createDocumentation(APIProduct product, Documentation documentation) throws APIManagementException {
try {
APIProductIdentifier productId = product.getId();
GenericArtifactManager artifactManager = new GenericArtifactManager(registry, APIConstants.DOCUMENTATION_KEY);
GenericArtifact artifact = artifactManager.newGovernanceArtifact(new QName(documentation.getName()));
artifactManager.addGenericArtifact(APIUtil.createDocArtifactContent(artifact, productId, documentation));
String productPath = APIUtil.getAPIProductPath(productId);
// Adding association from api to documentation . (API Product -----> doc)
registry.addAssociation(productPath, artifact.getPath(), APIConstants.DOCUMENTATION_ASSOCIATION);
String docVisibility = documentation.getVisibility().name();
String[] authorizedRoles = getAuthorizedRoles(productPath);
String visibility = product.getVisibility();
if (docVisibility != null) {
if (APIConstants.DOC_SHARED_VISIBILITY.equalsIgnoreCase(docVisibility)) {
authorizedRoles = null;
visibility = APIConstants.DOC_SHARED_VISIBILITY;
} else if (APIConstants.DOC_OWNER_VISIBILITY.equalsIgnoreCase(docVisibility)) {
authorizedRoles = null;
visibility = APIConstants.DOC_OWNER_VISIBILITY;
}
}
APIUtil.setResourcePermissions(product.getId().getProviderName(), visibility, authorizedRoles, artifact.getPath(), registry);
String docFilePath = artifact.getAttribute(APIConstants.DOC_FILE_PATH);
if (docFilePath != null && !StringUtils.EMPTY.equals(docFilePath)) {
// The docFilePatch comes as /t/tenanatdoman/registry/resource/_system/governance/apimgt/applicationdata..
// We need to remove the /t/tenanatdoman/registry/resource/_system/governance section to set permissions.
int startIndex = docFilePath.indexOf(APIConstants.GOVERNANCE) + (APIConstants.GOVERNANCE).length();
String filePath = docFilePath.substring(startIndex, docFilePath.length());
APIUtil.setResourcePermissions(product.getId().getProviderName(), visibility, authorizedRoles, filePath, registry);
registry.addAssociation(artifact.getPath(), filePath, APIConstants.DOCUMENTATION_FILE_ASSOCIATION);
}
documentation.setId(artifact.getId());
} catch (RegistryException e) {
handleException("Failed to add documentation", e);
} catch (UserStoreException e) {
handleException("Failed to add documentation", e);
}
}
Aggregations