Search in sources :

Example 86 with APIProductIdentifier

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);
    }
}
Also used : ResourceFile(org.wso2.carbon.apimgt.api.model.ResourceFile) PublisherAPIProduct(org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct) APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException)

Example 87 with APIProductIdentifier

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();
    }
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 88 with APIProductIdentifier

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);
    }
}
Also used : PublisherAPIProduct(org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct) APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 89 with APIProductIdentifier

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);
}
Also used : APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) DeployedAPIRevision(org.wso2.carbon.apimgt.api.model.DeployedAPIRevision) APIRevision(org.wso2.carbon.apimgt.api.model.APIRevision) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)

Example 90 with APIProductIdentifier

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);
    }
}
Also used : APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) QName(javax.xml.namespace.QName) UserStoreException(org.wso2.carbon.user.api.UserStoreException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Aggregations

APIProductIdentifier (org.wso2.carbon.apimgt.api.model.APIProductIdentifier)91 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)48 APIProduct (org.wso2.carbon.apimgt.api.model.APIProduct)33 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)22 PreparedStatement (java.sql.PreparedStatement)19 APIProductResource (org.wso2.carbon.apimgt.api.model.APIProductResource)19 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)19 Connection (java.sql.Connection)18 ArrayList (java.util.ArrayList)18 ResultSet (java.sql.ResultSet)17 SQLException (java.sql.SQLException)17 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)17 API (org.wso2.carbon.apimgt.api.model.API)14 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)14 HashSet (java.util.HashSet)13 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)13 URITemplate (org.wso2.carbon.apimgt.api.model.URITemplate)13 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)13 Tier (org.wso2.carbon.apimgt.api.model.Tier)12 PublisherAPIProduct (org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct)12