Search in sources :

Example 11 with ResourceFile

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

the class ApisApiServiceImpl method getAPIThumbnail.

/**
 * Retrieves the thumbnail image of an API specified by API identifier
 *
 * @param apiId           API Id
 * @param ifNoneMatch     If-None-Match header value
 * @param messageContext If-Modified-Since header value
 * @return Thumbnail image of the API
 */
@Override
public Response getAPIThumbnail(String apiId, String ifNoneMatch, MessageContext messageContext) {
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        // this will fail if user does not have access to the API or the API does not exist
        // APIIdentifier apiIdentifier = APIMappingUtil.getAPIIdentifierFromUUID(apiId, tenantDomain);
        ResourceFile thumbnailResource = apiProvider.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 if (isAuthorizationFailure(e)) {
            RestApiUtil.handleAuthorizationFailure("Authorization failure while retrieving thumbnail of 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) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Example 12 with ResourceFile

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

the class ApisApiServiceImpl method getWSDLOfAPI.

/**
 * Retrieve the WSDL of an API
 *
 * @param apiId UUID of the API
 * @param ifNoneMatch If-None-Match header value
 * @return the WSDL of the API (can be a file or zip archive)
 * @throws APIManagementException when error occurred while trying to retrieve the WSDL
 */
@Override
public Response getWSDLOfAPI(String apiId, String ifNoneMatch, MessageContext messageContext) throws APIManagementException {
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        // this will fail if user does not have access to the API or the API does not exist
        // APIIdentifier apiIdentifier = APIMappingUtil.getAPIIdentifierFromUUID(apiId, organization);
        ResourceFile resource = apiProvider.getWSDL(apiId, organization);
        return RestApiUtil.getResponseFromResourceFile(resource.getName(), resource);
    } catch (APIManagementException e) {
        // to expose the existence of the resource
        if (RestApiUtil.isDueToResourceNotFound(e) || RestApiUtil.isDueToAuthorizationFailure(e)) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, apiId, e, log);
        } else if (isAuthorizationFailure(e)) {
            RestApiUtil.handleAuthorizationFailure("Authorization failure while retrieving wsdl of API: " + apiId, e, log);
        } else {
            throw e;
        }
    }
    return null;
}
Also used : ResourceFile(org.wso2.carbon.apimgt.api.model.ResourceFile) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Example 13 with ResourceFile

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

the class PublisherCommonUtils method addDocumentationContentForFile.

/**
 * Add documentation content of files.
 *
 * @param inputStream  Input Stream
 * @param mediaType    Media type of the document
 * @param filename     File name
 * @param apiProvider  API Provider
 * @param apiId        API/API Product UUID
 * @param documentId   Document ID
 * @param organization organization of the API
 * @throws APIManagementException If an error occurs while adding the documentation file
 */
public static void addDocumentationContentForFile(InputStream inputStream, String mediaType, String filename, APIProvider apiProvider, String apiId, String documentId, String organization) throws APIManagementException {
    DocumentationContent content = new DocumentationContent();
    ResourceFile resourceFile = new ResourceFile(inputStream, mediaType);
    resourceFile.setName(filename);
    content.setResourceFile(resourceFile);
    content.setSourceType(DocumentationContent.ContentSourceType.FILE);
    apiProvider.addDocumentationContent(apiId, documentId, organization, content);
}
Also used : DocumentationContent(org.wso2.carbon.apimgt.api.model.DocumentationContent) ResourceFile(org.wso2.carbon.apimgt.api.model.ResourceFile)

Example 14 with ResourceFile

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

the class PublisherCommonUtils method addWsdl.

/**
 * Add WSDL file of an API.
 *
 * @param fileContentType Content type of the file
 * @param fileInputStream Input Stream
 * @param api             API to which the WSDL belongs to
 * @param apiProvider     API Provider
 * @param tenantDomain    Tenant domain of the API
 * @throws APIManagementException If an error occurs while adding the WSDL resource
 */
public static void addWsdl(String fileContentType, InputStream fileInputStream, API api, APIProvider apiProvider, String tenantDomain) throws APIManagementException {
    ResourceFile wsdlResource;
    if (APIConstants.APPLICATION_ZIP.equals(fileContentType) || APIConstants.APPLICATION_X_ZIP_COMPRESSED.equals(fileContentType)) {
        wsdlResource = new ResourceFile(fileInputStream, APIConstants.APPLICATION_ZIP);
    } else {
        wsdlResource = new ResourceFile(fileInputStream, fileContentType);
    }
    api.setWsdlResource(wsdlResource);
    apiProvider.addWSDLResource(api.getUuid(), wsdlResource, null, tenantDomain);
}
Also used : ResourceFile(org.wso2.carbon.apimgt.api.model.ResourceFile)

Example 15 with ResourceFile

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

the class ExportUtils method addWSDLtoArchive.

/**
 * Retrieve WSDL for the exporting API and store it in the archive directory.
 *
 * @param archivePath   File path to export the WSDL
 * @param apiIdentifier ID of the requesting API
 * @throws APIImportExportException If an error occurs while retrieving WSDL from the registry or
 *                                  storing in the archive directory
 */
public static void addWSDLtoArchive(String archivePath, APIIdentifier apiIdentifier, APIProvider apiProvider) throws APIImportExportException {
    String tenantDomain = RestApiCommonUtil.getLoggedInUserTenantDomain();
    String wsdlPath = APIConstants.API_WSDL_RESOURCE_LOCATION + apiIdentifier.getProviderName() + "--" + apiIdentifier.getApiName() + apiIdentifier.getVersion() + APIConstants.WSDL_FILE_EXTENSION;
    try {
        ResourceFile wsdlResource = apiProvider.getWSDL(apiIdentifier.getUUID(), tenantDomain);
        if (wsdlResource != null) {
            CommonUtil.createDirectory(archivePath + File.separator + "WSDL");
            try (InputStream wsdlStream = wsdlResource.getContent();
                OutputStream outputStream = new FileOutputStream(archivePath + File.separator + "WSDL" + File.separator + apiIdentifier.getApiName() + "-" + apiIdentifier.getVersion() + APIConstants.WSDL_FILE_EXTENSION)) {
                IOUtils.copy(wsdlStream, outputStream);
                if (log.isDebugEnabled()) {
                    log.debug("WSDL file: " + wsdlPath + " retrieved successfully");
                }
            }
        } else if (log.isDebugEnabled()) {
            log.debug("WSDL resource does not exists in path: " + wsdlPath + ". Skipping WSDL export.");
        }
    } catch (IOException | APIManagementException e) {
        throw new APIImportExportException("I/O error while writing WSDL: " + wsdlPath + " to file", e);
    }
}
Also used : ResourceFile(org.wso2.carbon.apimgt.api.model.ResourceFile) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) APIImportExportException(org.wso2.carbon.apimgt.impl.importexport.APIImportExportException) IOException(java.io.IOException)

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