Search in sources :

Example 1 with FileInfoDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.FileInfoDTO in project carbon-apimgt by wso2.

the class CompositeApisApiServiceImpl method compositeApisApiIdImplementationPut.

@Override
public Response compositeApisApiIdImplementationPut(String apiId, InputStream apiImplementationInputStream, FileInfo apiImplementationDetail, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIStore apiStore = RestApiUtil.getConsumer(username);
        String existingFingerprint = compositeApisApiIdImplementationGetFingerprint(apiId, null, null, request);
        if (!StringUtils.isEmpty(ifMatch) && !StringUtils.isEmpty(existingFingerprint) && ifMatch.contains(existingFingerprint)) {
            return Response.notModified().build();
        }
        apiStore.updateCompositeApiImplementation(apiId, apiImplementationInputStream);
        String uriString = RestApiConstants.RESOURCE_PATH_IMPLEMENTATION.replace(RestApiConstants.APIID_PARAM, apiId);
        FileInfoDTO infoDTO = new FileInfoDTO();
        infoDTO.setRelativePath(uriString);
        infoDTO.setMediaType(MediaType.APPLICATION_OCTET_STREAM);
        String newFingerprint = compositeApisApiIdImplementationGetFingerprint(apiId, null, null, request);
        return Response.ok().header(HttpHeaders.ETAG, "\"" + newFingerprint + "\"").entity(infoDTO).build();
    } catch (APIManagementException e) {
        HashMap<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) FileInfoDTO(org.wso2.carbon.apimgt.rest.api.store.dto.FileInfoDTO)

Example 2 with FileInfoDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.FileInfoDTO in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisApiIdThumbnailPost.

/**
 * Updates the thumbnail image of an API
 *
 * @param apiId             UUID of API
 * @param fileInputStream   Image data stream
 * @param fileDetail        meta information of the image
 * @param ifMatch           If-Match header value
 * @param ifUnmodifiedSince If-Unmodified-Since header value
 * @param request           msf4j request object
 * @return meta info about the updated thumbnail image
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response apisApiIdThumbnailPost(String apiId, InputStream fileInputStream, FileInfo fileDetail, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIPublisher apiPublisher = RestAPIPublisherUtil.getApiPublisher(username);
        String existingFingerprint = apisApiIdThumbnailGetFingerprint(apiId, null, null, request);
        if (!StringUtils.isEmpty(ifMatch) && !StringUtils.isEmpty(existingFingerprint) && !ifMatch.contains(existingFingerprint)) {
            return Response.status(Response.Status.PRECONDITION_FAILED).build();
        }
        apiPublisher.saveThumbnailImage(apiId, fileInputStream, fileDetail.getFileName());
        String uriString = RestApiConstants.RESOURCE_PATH_THUMBNAIL.replace(RestApiConstants.APIID_PARAM, apiId);
        FileInfoDTO infoDTO = new FileInfoDTO();
        infoDTO.setRelativePath(uriString);
        infoDTO.setMediaType(MediaType.APPLICATION_OCTET_STREAM);
        String newFingerprint = apisApiIdThumbnailGetFingerprint(apiId, null, null, request);
        return Response.status(Response.Status.CREATED).entity(infoDTO).header(HttpHeaders.ETAG, "\"" + newFingerprint + "\"").build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while uploading image" + apiId;
        HashMap<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher) FileInfoDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.FileInfoDTO)

Example 3 with FileInfoDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.FileInfoDTO in project carbon-apimgt by wso2.

the class ApiProductsApiServiceImpl method updateAPIProductThumbnail.

@Override
public Response updateAPIProductThumbnail(String apiProductId, InputStream fileInputStream, Attachment fileDetail, String ifMatch, MessageContext messageContext) {
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        String tenantDomain = RestApiCommonUtil.getLoggedInUserTenantDomain();
        String fileName = fileDetail.getDataHandler().getName();
        String extension = FilenameUtils.getExtension(fileName);
        if (!RestApiConstants.ALLOWED_THUMBNAIL_EXTENSIONS.contains(extension.toLowerCase())) {
            RestApiUtil.handleBadRequest("Unsupported Thumbnail File Extension. Supported extensions are .jpg, .png, .jpeg .svg " + "and .gif", log);
        }
        String fileContentType = URLConnection.guessContentTypeFromName(fileName);
        if (org.apache.commons.lang3.StringUtils.isBlank(fileContentType)) {
            fileContentType = fileDetail.getContentType().toString();
        }
        // this will fail if user does not have access to the API or the API does not exist
        APIProduct apiProduct = apiProvider.getAPIProductbyUUID(apiProductId, tenantDomain);
        ResourceFile apiImage = new ResourceFile(fileInputStream, fileContentType);
        apiProvider.setThumbnailToAPI(apiProductId, apiImage, tenantDomain);
        /*
            String thumbPath = APIUtil.getProductIconPath(apiProduct.getId());
            String thumbnailUrl = apiProvider.addProductResourceFile(apiProduct.getId(), thumbPath, apiImage);
            apiProduct.setThumbnailUrl(APIUtil.prependTenantPrefix(thumbnailUrl, apiProduct.getId().getProviderName()));
            APIUtil.setResourcePermissions(apiProduct.getId().getProviderName(), null, null, thumbPath);

            //need to set product resource mappings before updating product, otherwise existing mappings will be lost
            List<APIProductResource> resources = apiProvider.getResourcesOfAPIProduct(apiProduct.getId());
            apiProduct.setProductResources(resources);
            apiProvider.updateAPIProduct(apiProduct);
            */
        String uriString = RestApiConstants.RESOURCE_PATH_THUMBNAIL.replace(RestApiConstants.APIID_PARAM, apiProductId);
        URI uri = new URI(uriString);
        FileInfoDTO infoDTO = new FileInfoDTO();
        infoDTO.setRelativePath(uriString);
        infoDTO.setMediaType(apiImage.getContentType());
        return Response.created(uri).entity(infoDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while updating API Product : " + apiProductId;
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    } catch (URISyntaxException e) {
        String errorMessage = "Error while retrieving thumbnail location of API Product : " + apiProductId;
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    }
    return null;
}
Also used : APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) ResourceFile(org.wso2.carbon.apimgt.api.model.ResourceFile) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) URISyntaxException(java.net.URISyntaxException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) URI(java.net.URI) FileInfoDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.FileInfoDTO)

Example 4 with FileInfoDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.FileInfoDTO in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method updateAPIThumbnail.

@Override
public Response updateAPIThumbnail(String apiId, InputStream fileInputStream, Attachment fileDetail, String ifMatch, MessageContext messageContext) {
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        // validate if api exists
        APIInfo apiInfo = validateAPIExistence(apiId);
        // validate API update operation permitted based on the LC state
        validateAPIOperationsPerLC(apiInfo.getStatus().toString());
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        String fileName = fileDetail.getDataHandler().getName();
        String extension = FilenameUtils.getExtension(fileName);
        if (!RestApiConstants.ALLOWED_THUMBNAIL_EXTENSIONS.contains(extension.toLowerCase())) {
            RestApiUtil.handleBadRequest("Unsupported Thumbnail File Extension. Supported extensions are .jpg, .png, .jpeg .svg " + "and .gif", log);
        }
        String fileContentType = URLConnection.guessContentTypeFromName(fileName);
        if (org.apache.commons.lang3.StringUtils.isBlank(fileContentType)) {
            fileContentType = fileDetail.getContentType().toString();
        }
        PublisherCommonUtils.updateThumbnail(fileInputStream, fileContentType, apiProvider, apiId, organization);
        String uriString = RestApiConstants.RESOURCE_PATH_THUMBNAIL.replace(RestApiConstants.APIID_PARAM, apiId);
        URI uri = new URI(uriString);
        FileInfoDTO infoDTO = new FileInfoDTO();
        infoDTO.setRelativePath(uriString);
        infoDTO.setMediaType(fileContentType);
        return Response.created(uri).entity(infoDTO).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 adding thumbnail for API : " + apiId, e, log);
        } else {
            String errorMessage = "Error while retrieving thumbnail of API : " + apiId;
            RestApiUtil.handleInternalServerError(errorMessage, e, log);
        }
    } catch (URISyntaxException e) {
        String errorMessage = "Error while updating thumbnail of API: " + apiId;
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    } finally {
        IOUtils.closeQuietly(fileInputStream);
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIInfo(org.wso2.carbon.apimgt.api.model.APIInfo) URISyntaxException(java.net.URISyntaxException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) URI(java.net.URI) FileInfoDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.FileInfoDTO)

Aggregations

URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 HashMap (java.util.HashMap)2 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)2 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)2 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)2 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)2 FileInfoDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.FileInfoDTO)2 APIInfo (org.wso2.carbon.apimgt.api.model.APIInfo)1 APIProduct (org.wso2.carbon.apimgt.api.model.APIProduct)1 ResourceFile (org.wso2.carbon.apimgt.api.model.ResourceFile)1 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)1 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)1 FileInfoDTO (org.wso2.carbon.apimgt.rest.api.publisher.dto.FileInfoDTO)1 FileInfoDTO (org.wso2.carbon.apimgt.rest.api.store.dto.FileInfoDTO)1