Example usage for com.liferay.portal.kernel.servlet HttpHeaders CONTENT_ENCODING

List of usage examples for com.liferay.portal.kernel.servlet HttpHeaders CONTENT_ENCODING

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.servlet HttpHeaders CONTENT_ENCODING.

Prototype

String CONTENT_ENCODING

To view the source code for com.liferay.portal.kernel.servlet HttpHeaders CONTENT_ENCODING.

Click Source Link

Usage

From source file:org.eclipse.sw360.portal.common.AttachmentPortletUtils.java

License:Open Source License

private void serveAttachmentBundle(List<AttachmentContent> attachments, ResourceRequest request,
        ResourceResponse response, Optional<String> downloadFileName) {
    String filename;// w  w w  . j  a  v a2s  .c om
    String contentType;
    if (attachments.size() == 1) {
        filename = attachments.get(0).getFilename();
        contentType = attachments.get(0).getContentType();
        if (contentType.equalsIgnoreCase("application/gzip")) {
            // In case of downloads with gzip file extension (e.g. *.tar.gz)
            // the client should receive the origin file.
            // Set http header 'Content-Encoding' to 'identity'
            // to prevent auto encoding content (chrome).
            response.setProperty(HttpHeaders.CONTENT_ENCODING, "identity");
        }
    } else {
        filename = downloadFileName.orElse(DEFAULT_ATTACHMENT_BUNDLE_NAME);
        contentType = "application/zip";
    }

    User user = UserCacheHolder.getUserFromRequest(request);
    try {
        Optional<Object> context = getContextFromRequest(request, user);

        if (context.isPresent()) {
            try (InputStream attachmentStream = getStreamToServeAFile(attachments, user, context.get())) {
                PortletResponseUtil.sendFile(request, response, filename, attachmentStream, contentType);
            } catch (IOException e) {
                log.error("cannot finish writing response", e);
                response.setProperty(ResourceResponse.HTTP_STATUS_CODE, "500");
            }
        } else {
            log.warn("The user=[" + user.getEmail() + "] tried to download attachment=["
                    + CommonUtils.joinStrings(
                            attachments.stream().map(AttachmentContent::getId).collect(Collectors.toList()))
                    + "] in context=[" + request.getParameter(PortalConstants.CONTEXT_ID)
                    + "] without read permissions");
            response.setProperty(ResourceResponse.HTTP_STATUS_CODE, "404");
        }
    } catch (SW360Exception e) {
        log.error("Context was not set properly.", e);
        response.setProperty(ResourceResponse.HTTP_STATUS_CODE, "400");
    } catch (TException e) {
        log.error("Problem getting the attachment content from the backend", e);
        response.setProperty(ResourceResponse.HTTP_STATUS_CODE, "500");
    }
}