Search in sources :

Example 16 with ResourceFile

use of org.wso2.carbon.apimgt.api.model.ResourceFile in project carbon-apimgt by wso2.

the class ExportUtils method addThumbnailToArchive.

/**
 * Retrieve thumbnail image for the exporting API or API Product and store it in the archive directory.
 *
 * @param archivePath File path to export the thumbnail image
 * @param identifier  ID of the requesting API or API Product
 * @param apiProvider API Provider
 * @throws APIImportExportException If an error occurs while retrieving image from the registry or
 *                                  storing in the archive directory
 */
public static void addThumbnailToArchive(String archivePath, Identifier identifier, APIProvider apiProvider) throws APIImportExportException, APIManagementException {
    String tenantDomain = RestApiCommonUtil.getLoggedInUserTenantDomain();
    String localImagePath = archivePath + File.separator + ImportExportConstants.IMAGE_RESOURCE;
    try {
        ResourceFile thumbnailResource = apiProvider.getIcon(identifier.getUUID(), tenantDomain);
        if (thumbnailResource != null) {
            String mediaType = thumbnailResource.getContentType();
            String extension = ImportExportConstants.fileExtensionMapping.get(mediaType);
            if (extension != null) {
                CommonUtil.createDirectory(localImagePath);
                try (InputStream imageDataStream = thumbnailResource.getContent();
                    OutputStream outputStream = new FileOutputStream(localImagePath + File.separator + APIConstants.API_ICON_IMAGE + APIConstants.DOT + extension)) {
                    IOUtils.copy(imageDataStream, outputStream);
                    if (log.isDebugEnabled()) {
                        log.debug("Thumbnail image retrieved successfully for API/API Product: " + identifier.getName() + StringUtils.SPACE + APIConstants.API_DATA_VERSION + ": " + identifier.getVersion());
                    }
                }
            } else {
                // api gets imported without thumbnail
                log.error("Unsupported media type for icon " + mediaType + ". Skipping thumbnail export.");
            }
        } else if (log.isDebugEnabled()) {
            log.debug("Thumbnail URL does not exists in registry for API/API Product: " + identifier.getName() + StringUtils.SPACE + APIConstants.API_DATA_VERSION + ": " + identifier.getVersion() + ". Skipping thumbnail export.");
        }
    } catch (IOException e) {
        // Exception is ignored by logging due to the reason that Thumbnail is not essential for
        // an API to be recreated.
        log.error("I/O error while writing API/API Product Thumbnail to file", e);
    }
}
Also used : ResourceFile(org.wso2.carbon.apimgt.api.model.ResourceFile) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException)

Example 17 with ResourceFile

use of org.wso2.carbon.apimgt.api.model.ResourceFile in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisApiIdThumbnailGet.

@Override
public Response apisApiIdThumbnailGet(String apiId, String xWSO2Tenant, String ifNoneMatch, MessageContext messageContext) {
    try {
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        APIConsumer apiConsumer = RestApiCommonUtil.getLoggedInUserConsumer();
        // this will fail if user does not have access to the API or the API does not exist
        apiConsumer.getLightweightAPIByUUID(apiId, organization);
        ResourceFile thumbnailResource = apiConsumer.getIcon(apiId, organization);
        if (thumbnailResource != null) {
            return Response.ok(thumbnailResource.getContent(), MediaType.valueOf(thumbnailResource.getContentType())).build();
        } else {
            return Response.noContent().build();
        }
    } catch (APIManagementException e) {
        // existence of the resource
        if (RestApiUtil.isDueToResourceNotFound(e) || RestApiUtil.isDueToAuthorizationFailure(e)) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, apiId, e, log);
        } else {
            String errorMessage = "Error while retrieving thumbnail of API : " + apiId;
            RestApiUtil.handleInternalServerError(errorMessage, e, log);
        }
    }
    return null;
}
Also used : ResourceFile(org.wso2.carbon.apimgt.api.model.ResourceFile) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer)

Example 18 with ResourceFile

use of org.wso2.carbon.apimgt.api.model.ResourceFile in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method getWSDLOfAPI.

@Override
public Response getWSDLOfAPI(String apiId, String environmentName, String ifNoneMatch, String xWSO2Tenant, MessageContext messageContext) throws APIManagementException {
    String organization = RestApiUtil.getValidatedOrganization(messageContext);
    APIConsumer apiConsumer = RestApiCommonUtil.getLoggedInUserConsumer();
    API api = apiConsumer.getLightweightAPIByUUID(apiId, organization);
    APIIdentifier apiIdentifier = api.getId();
    List<Environment> environments = APIUtil.getEnvironmentsOfAPI(api);
    if (environments != null && environments.size() > 0) {
        if (StringUtils.isEmpty(environmentName)) {
            environmentName = api.getEnvironments().iterator().next();
        }
        Environment selectedEnvironment = null;
        for (Environment environment : environments) {
            if (environment.getName().equals(environmentName)) {
                selectedEnvironment = environment;
                break;
            }
        }
        if (selectedEnvironment == null) {
            throw new APIManagementException(ExceptionCodes.from(ExceptionCodes.INVALID_GATEWAY_ENVIRONMENT, environmentName));
        }
        ResourceFile wsdl = apiConsumer.getWSDL(api, selectedEnvironment.getName(), selectedEnvironment.getType(), organization);
        return RestApiUtil.getResponseFromResourceFile(apiIdentifier.toString(), wsdl);
    } else {
        throw new APIManagementException(ExceptionCodes.from(ExceptionCodes.NO_GATEWAY_ENVIRONMENTS_ADDED, apiIdentifier.toString()));
    }
}
Also used : ResourceFile(org.wso2.carbon.apimgt.api.model.ResourceFile) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Environment(org.wso2.carbon.apimgt.api.model.Environment) API(org.wso2.carbon.apimgt.api.model.API) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer)

Example 19 with ResourceFile

use of org.wso2.carbon.apimgt.api.model.ResourceFile in project carbon-apimgt by wso2.

the class AbstractAPIManager method addResourceFile.

public String addResourceFile(Identifier identifier, String resourcePath, ResourceFile resourceFile) throws APIManagementException {
    try {
        Resource thumb = registry.newResource();
        thumb.setContentStream(resourceFile.getContent());
        thumb.setMediaType(resourceFile.getContentType());
        registry.put(resourcePath, thumb);
        if (MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equalsIgnoreCase(tenantDomain)) {
            return RegistryConstants.PATH_SEPARATOR + "registry" + RegistryConstants.PATH_SEPARATOR + "resource" + RegistryConstants.PATH_SEPARATOR + "_system" + RegistryConstants.PATH_SEPARATOR + "governance" + resourcePath;
        } else {
            return "/t/" + tenantDomain + RegistryConstants.PATH_SEPARATOR + "registry" + RegistryConstants.PATH_SEPARATOR + "resource" + RegistryConstants.PATH_SEPARATOR + "_system" + RegistryConstants.PATH_SEPARATOR + "governance" + resourcePath;
        }
    } catch (RegistryException e) {
        String msg = "Error while adding the resource to the registry";
        throw new APIManagementException(msg, e);
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Resource(org.wso2.carbon.registry.core.Resource) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 20 with ResourceFile

use of org.wso2.carbon.apimgt.api.model.ResourceFile in project carbon-apimgt by wso2.

the class AbstractAPIManagerTestCase method testAddResourceFile.

@Test
public void testAddResourceFile() throws APIManagementException, RegistryException, IOException {
    Identifier identifier = Mockito.mock(Identifier.class);
    AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapper(registry);
    Resource resource = new ResourceImpl();
    Mockito.when(registry.newResource()).thenReturn(resource);
    String resourcePath = "/test";
    String contentType = "sampleType";
    ResourceFile resourceFile = new ResourceFile(null, contentType);
    try {
        abstractAPIManager.addResourceFile(identifier, resourcePath, resourceFile);
        Assert.fail("Registry exception not thrown for error scenario");
    } catch (APIManagementException e) {
        Assert.assertTrue(e.getMessage().contains("Error while adding the resource to the registry"));
    }
    InputStream in = IOUtils.toInputStream("sample content", "UTF-8");
    resourceFile = new ResourceFile(in, contentType);
    String returnedPath = abstractAPIManager.addResourceFile(identifier, resourcePath, resourceFile);
    Assert.assertTrue(returnedPath.contains(resourcePath) && returnedPath.contains("/t/"));
    abstractAPIManager.tenantDomain = SAMPLE_TENANT_DOMAIN;
    returnedPath = abstractAPIManager.addResourceFile(identifier, resourcePath, resourceFile);
    Assert.assertTrue(returnedPath.contains(resourcePath) && !returnedPath.contains("/t/"));
}
Also used : ResourceFile(org.wso2.carbon.apimgt.api.model.ResourceFile) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) Identifier(org.wso2.carbon.apimgt.api.model.Identifier) ResourceImpl(org.wso2.carbon.registry.core.ResourceImpl) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ArrayInputStream(org.apache.derby.iapi.services.io.ArrayInputStream) InputStream(java.io.InputStream) Resource(org.wso2.carbon.registry.core.Resource) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

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