Search in sources :

Example 26 with ResourceFile

use of org.wso2.carbon.apimgt.persistence.dto.ResourceFile in project carbon-apimgt by wso2.

the class RegistryPersistenceImpl method saveThumbnail.

@Override
public void saveThumbnail(Organization org, String apiId, ResourceFile resourceFile) throws ThumbnailPersistenceException {
    boolean isTenantFlowStarted = false;
    try {
        String tenantDomain = org.getName();
        RegistryHolder holder = getRegistry(tenantDomain);
        Registry registry = holder.getRegistry();
        isTenantFlowStarted = holder.isTenantFlowStarted();
        GenericArtifactManager apiArtifactManager = RegistryPersistenceUtil.getArtifactManager(registry, APIConstants.API_KEY);
        GenericArtifact apiArtifact = apiArtifactManager.getGenericArtifact(apiId);
        if (apiArtifact == null) {
            throw new ThumbnailPersistenceException("API not found. ", ExceptionCodes.API_NOT_FOUND);
        }
        String apiProviderName = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER);
        apiProviderName = RegistryPersistenceUtil.replaceEmailDomain(apiProviderName);
        String apiName = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_NAME);
        String apiVersion = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_VERSION);
        String artifactPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + apiProviderName + RegistryConstants.PATH_SEPARATOR + apiName + RegistryConstants.PATH_SEPARATOR + apiVersion;
        String filePath = artifactPath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_ICON_IMAGE;
        String savedFilePath = addResourceFile(filePath, resourceFile, registry, tenantDomain);
        RegistryPersistenceUtil.setResourcePermissions(apiProviderName, null, null, filePath);
        apiArtifact.setAttribute(APIConstants.API_OVERVIEW_THUMBNAIL_URL, savedFilePath);
        apiArtifactManager.updateGenericArtifact(apiArtifact);
    } catch (APIPersistenceException | GovernanceException | PersistenceException | APIManagementException e) {
        throw new ThumbnailPersistenceException("Error while saving thumbnail for api " + apiId, e);
    } finally {
        if (isTenantFlowStarted) {
            PrivilegedCarbonContext.endTenantFlow();
        }
    }
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ThumbnailPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.ThumbnailPersistenceException) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) PersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.PersistenceException) GraphQLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.GraphQLPersistenceException) MediationPolicyPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.MediationPolicyPersistenceException) ThumbnailPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.ThumbnailPersistenceException) WSDLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.WSDLPersistenceException) OASPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.OASPersistenceException) AsyncSpecPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.AsyncSpecPersistenceException) DocumentationPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.DocumentationPersistenceException) GovernanceException(org.wso2.carbon.governance.api.exception.GovernanceException) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry)

Example 27 with ResourceFile

use of org.wso2.carbon.apimgt.persistence.dto.ResourceFile in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method updateWSDLOfAPI.

/**
 * Update the WSDL of an API
 *
 * @param apiId UUID of the API
 * @param fileInputStream file data as input stream
 * @param fileDetail file details
 * @param url URL of the WSDL
 * @return 200 OK response if the operation is successful. 400 if the provided inputs are invalid. 500 if a server
 *  error occurred.
 * @throws APIManagementException when error occurred while trying to retrieve the WSDL
 */
@Override
public Response updateWSDLOfAPI(String apiId, String ifMatch, InputStream fileInputStream, Attachment fileDetail, String url, MessageContext messageContext) throws APIManagementException {
    APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
    String organization = RestApiUtil.getValidatedOrganization(messageContext);
    // validate if api exists
    APIInfo apiInfo = validateAPIExistence(apiId);
    // validate API update operation permitted based on the LC state
    validateAPIOperationsPerLC(apiInfo.getStatus().toString());
    WSDLValidationResponse validationResponse = validateWSDLAndReset(fileInputStream, fileDetail, url);
    if (StringUtils.isNotBlank(url)) {
        apiProvider.addWSDLResource(apiId, null, url, organization);
    } else {
        ResourceFile wsdlResource;
        if (APIConstants.APPLICATION_ZIP.equals(fileDetail.getContentType().toString()) || APIConstants.APPLICATION_X_ZIP_COMPRESSED.equals(fileDetail.getContentType().toString())) {
            wsdlResource = new ResourceFile(validationResponse.getWsdlProcessor().getWSDL(), APIConstants.APPLICATION_ZIP);
        } else {
            wsdlResource = new ResourceFile(validationResponse.getWsdlProcessor().getWSDL(), fileDetail.getContentType().toString());
        }
        apiProvider.addWSDLResource(apiId, wsdlResource, null, organization);
    }
    return Response.ok().build();
}
Also used : ResourceFile(org.wso2.carbon.apimgt.api.model.ResourceFile) WSDLValidationResponse(org.wso2.carbon.apimgt.impl.wsdl.model.WSDLValidationResponse) APIInfo(org.wso2.carbon.apimgt.api.model.APIInfo) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Example 28 with ResourceFile

use of org.wso2.carbon.apimgt.persistence.dto.ResourceFile in project carbon-apimgt by wso2.

the class APIProviderImpl method setThumbnailToAPI.

@Override
public void setThumbnailToAPI(String apiId, ResourceFile resource, String organization) throws APIManagementException {
    try {
        org.wso2.carbon.apimgt.persistence.dto.ResourceFile iconResourceFile = new org.wso2.carbon.apimgt.persistence.dto.ResourceFile(resource.getContent(), resource.getContentType());
        apiPersistenceInstance.saveThumbnail(new Organization(organization), apiId, iconResourceFile);
    } catch (ThumbnailPersistenceException e) {
        if (e.getErrorHandler() == ExceptionCodes.API_NOT_FOUND) {
            throw new APIMgtResourceNotFoundException(e);
        } else {
            throw new APIManagementException("Error while saving thumbnail ", e);
        }
    }
}
Also used : ResourceFile(org.wso2.carbon.apimgt.api.model.ResourceFile) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ThumbnailPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.ThumbnailPersistenceException) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)

Example 29 with ResourceFile

use of org.wso2.carbon.apimgt.persistence.dto.ResourceFile in project carbon-apimgt by wso2.

the class APIProviderImpl method addWSDLResource.

@Override
public void addWSDLResource(String apiId, ResourceFile resource, String url, String organization) throws APIManagementException {
    if (!StringUtils.isEmpty(url)) {
        URL wsdlUrl;
        try {
            wsdlUrl = new URL(url);
        } catch (MalformedURLException e) {
            throw new APIManagementException("Invalid/Malformed WSDL URL : " + url, e, ExceptionCodes.INVALID_WSDL_URL_EXCEPTION);
        }
        // Get the WSDL 1.1 or 2.0 processor and process the content based on the version
        WSDLProcessor wsdlProcessor = APIMWSDLReader.getWSDLProcessorForUrl(wsdlUrl);
        InputStream wsdlContent = wsdlProcessor.getWSDL();
        // wsdlResource.setContentStream(wsdlContent);
        org.wso2.carbon.apimgt.persistence.dto.ResourceFile wsdlResourceFile = new org.wso2.carbon.apimgt.persistence.dto.ResourceFile(wsdlContent, null);
        try {
            apiPersistenceInstance.saveWSDL(new Organization(organization), apiId, wsdlResourceFile);
        } catch (WSDLPersistenceException e) {
            throw new APIManagementException("Error while adding WSDL to api " + apiId, e);
        }
    } else if (resource != null) {
        org.wso2.carbon.apimgt.persistence.dto.ResourceFile wsdlResourceFile = new org.wso2.carbon.apimgt.persistence.dto.ResourceFile(resource.getContent(), resource.getContentType());
        try {
            apiPersistenceInstance.saveWSDL(new Organization(organization), apiId, wsdlResourceFile);
        } catch (WSDLPersistenceException e) {
            throw new APIManagementException("Error while adding WSDL to api " + apiId, e);
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) WSDLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.WSDLPersistenceException) URL(java.net.URL) WSDLProcessor(org.wso2.carbon.apimgt.impl.wsdl.WSDLProcessor) ResourceFile(org.wso2.carbon.apimgt.api.model.ResourceFile) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException)

Example 30 with ResourceFile

use of org.wso2.carbon.apimgt.persistence.dto.ResourceFile 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)

Aggregations

ResourceFile (org.wso2.carbon.apimgt.api.model.ResourceFile)27 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)24 Resource (org.wso2.carbon.registry.core.Resource)13 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)11 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)10 Registry (org.wso2.carbon.registry.core.Registry)9 InputStream (java.io.InputStream)8 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)8 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)7 APIProductResource (org.wso2.carbon.apimgt.api.model.APIProductResource)6 WSDLPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.WSDLPersistenceException)6 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)5 Organization (org.wso2.carbon.apimgt.persistence.dto.Organization)5 ResourceFile (org.wso2.carbon.apimgt.persistence.dto.ResourceFile)4 ThumbnailPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.ThumbnailPersistenceException)4 IOException (java.io.IOException)3 URI (java.net.URI)3 URISyntaxException (java.net.URISyntaxException)3