Example usage for com.google.common.net HttpHeaders CONTENT_ENCODING

List of usage examples for com.google.common.net HttpHeaders CONTENT_ENCODING

Introduction

In this page you can find the example usage for com.google.common.net HttpHeaders CONTENT_ENCODING.

Prototype

String CONTENT_ENCODING

To view the source code for com.google.common.net HttpHeaders CONTENT_ENCODING.

Click Source Link

Document

The HTTP Content-Encoding header field name.

Usage

From source file:com.google.gitiles.doc.DocServlet.java

private void showDoc(HttpServletRequest req, HttpServletResponse res, GitilesView view, Config cfg,
        ImageLoader img, RootNode nav, RootNode doc) throws IOException {
    Map<String, Object> data = new HashMap<>();
    data.putAll(Navbar.bannerSoyData(view, img, nav));
    data.put("pageTitle", MoreObjects.firstNonNull(MarkdownUtil.getTitle(doc), view.getPathPart()));
    data.put("sourceUrl", GitilesView.show().copyFrom(view).toUrl());
    data.put("logUrl", GitilesView.log().copyFrom(view).toUrl());
    data.put("blameUrl", GitilesView.blame().copyFrom(view).toUrl());
    data.put("navbarHtml", new MarkdownToHtml(view, cfg).toSoyHtml(nav));
    data.put("bodyHtml", new MarkdownToHtml(view, cfg).setImageLoader(img).toSoyHtml(doc));

    String page = renderer.render(SOY_TEMPLATE, data);
    byte[] raw = page.getBytes(UTF_8);
    res.setContentType(FormatType.HTML.getMimeType());
    res.setCharacterEncoding(UTF_8.name());
    setCacheHeaders(res);/*  w  w w.ja va  2  s. c o m*/
    if (acceptsGzipEncoding(req)) {
        res.setHeader(HttpHeaders.CONTENT_ENCODING, "gzip");
        raw = gzip(raw);
    }
    res.setContentLength(raw.length);
    res.setStatus(HttpServletResponse.SC_OK);
    res.getOutputStream().write(raw);
}

From source file:be.solidx.hot.web.AsyncStaticResourceServlet.java

private URL getResourceURL(HttpServletRequest servletRequest, HttpServletResponse servletResponse)
        throws Exception {
    URL resourceUrl;//w  w  w.  ja  va 2  s  .  c o m
    String acceptEncoding = servletRequest.getHeader("Accept-Encoding");
    // no resource input => index.html
    if (servletRequest.getPathInfo().equals("/")) {
        if (!hotConfig.isDevMode() && acceptEncoding != null && acceptEncoding.contains("gzip")) {
            resourceUrl = getResource("/index.html.gz");
            if (resourceUrl != null) {
                servletResponse.setHeader(HttpHeaders.CONTENT_ENCODING, "gzip");
                return resourceUrl;
            }
        }
        resourceUrl = getClass().getResource("/index.html");
    } else {
        if (!hotConfig.isDevMode() && acceptEncoding != null && acceptEncoding.contains("gzip")) {
            try {
                String gzPathInfo = servletRequest.getPathInfo() + ".gz";
                resourceUrl = getResource(gzPathInfo);
                if (resourceUrl != null) {
                    servletResponse.setHeader(HttpHeaders.CONTENT_ENCODING, "gzip");
                    return resourceUrl;
                }
            } catch (Exception e) {
                LOGGER.error("Failed to build gz path info", e);
            }
        }
        resourceUrl = getResource(servletRequest.getPathInfo());
    }
    return resourceUrl;
}

From source file:de.fhg.igd.vaadin.util.servlets.OSGiResourcesServlet.java

/**
 * /*w w  w.ja va2s. c  o m*/
 * @param resourcePath
 * @param req
 * @param resp
 * @return
 * @throws MalformedURLException 
 */
private URL lookupPrecompressedFileResource(String resourcePath, HttpServletRequest req,
        HttpServletResponse resp) throws MalformedURLException {

    final String acceptedEncodings = req.getHeader(HttpHeaders.ACCEPT_ENCODING);
    if (acceptedEncodings != null && acceptedEncodings.contains(GZIP_ENCODING)) {
        final URL precompressedResourceURL = lookupFileResource(resourcePath + ".gz", req, resp);
        if (precompressedResourceURL != null) {
            resp.setHeader(HttpHeaders.CONTENT_ENCODING, GZIP_ENCODING);
            log.trace("...using precompressed Resource from servlet context: {}", precompressedResourceURL);
            return precompressedResourceURL;
        }
    }
    // final URL resourcePath.
    return lookupFileResource(resourcePath, req, resp);
}

From source file:org.jclouds.openstack.swift.v1.blobstore.RegionScopedSwiftBlobStore.java

@Override
public String copyBlob(String fromContainer, String fromName, String toContainer, String toName,
        CopyOptions options) {/*from  w ww.j  a va  2s  .c  om*/
    ObjectApi objectApi = api.getObjectApi(regionId, toContainer);

    org.jclouds.openstack.swift.v1.options.CopyOptions swiftOptions = new org.jclouds.openstack.swift.v1.options.CopyOptions();

    if (options.ifMatch() != null) {
        swiftOptions.ifMatch(options.ifMatch());
    }
    if (options.ifNoneMatch() != null) {
        throw new UnsupportedOperationException("Swift does not support ifNoneMatch");
    }
    if (options.ifModifiedSince() != null) {
        swiftOptions.ifModifiedSince(options.ifModifiedSince());
    }
    if (options.ifUnmodifiedSince() != null) {
        swiftOptions.ifUnmodifiedSince(options.ifUnmodifiedSince());
    }

    Map<String, String> systemMetadata = Maps.newHashMap();
    ContentMetadata contentMetadata = options.contentMetadata();
    Map<String, String> userMetadata = options.userMetadata();

    if (contentMetadata != null || userMetadata != null) {
        if (contentMetadata != null) {
            String contentDisposition = contentMetadata.getContentDisposition();
            if (contentDisposition != null) {
                systemMetadata.put(HttpHeaders.CONTENT_DISPOSITION, contentDisposition);
            }

            String contentEncoding = contentMetadata.getContentEncoding();
            if (contentEncoding != null) {
                systemMetadata.put(HttpHeaders.CONTENT_ENCODING, contentEncoding);
            }

            String contentLanguage = contentMetadata.getContentLanguage();
            if (contentLanguage != null) {
                systemMetadata.put(HttpHeaders.CONTENT_LANGUAGE, contentLanguage);
            }

            String contentType = contentMetadata.getContentType();
            if (contentType != null) {
                systemMetadata.put(HttpHeaders.CONTENT_TYPE, contentType);
            }
        }
        if (userMetadata == null) {
            userMetadata = Maps.newHashMap();
        }
    } else {
        SwiftObject metadata = api.getObjectApi(regionId, fromContainer).getWithoutBody(fromName);
        if (metadata == null) {
            throw new KeyNotFoundException(fromContainer, fromName,
                    "Swift could not find the specified source key");
        }
        contentMetadata = metadata.getPayload().getContentMetadata();
        String contentDisposition = contentMetadata.getContentDisposition();
        if (contentDisposition != null) {
            systemMetadata.put(HttpHeaders.CONTENT_DISPOSITION, contentDisposition);
        }
        String contentEncoding = contentMetadata.getContentEncoding();
        if (contentEncoding != null) {
            systemMetadata.put(HttpHeaders.CONTENT_ENCODING, contentEncoding);
        }
        String contentLanguage = contentMetadata.getContentLanguage();
        if (contentLanguage != null) {
            systemMetadata.put(HttpHeaders.CONTENT_LANGUAGE, contentLanguage);
        }
        String contentType = contentMetadata.getContentType();
        if (contentType != null) {
            systemMetadata.put(HttpHeaders.CONTENT_TYPE, contentType);
        }
        userMetadata = metadata.getMetadata();
    }

    objectApi.copy(toName, fromContainer, fromName, userMetadata, systemMetadata, swiftOptions);

    // TODO: Swift copy object *appends* user metadata, does not overwrite
    return objectApi.getWithoutBody(toName).getETag();
}

From source file:org.gaul.s3proxy.S3ProxyHandler.java

private void handleCopyBlob(HttpServletRequest request, HttpServletResponse response, BlobStore blobStore,
        String destContainerName, String destBlobName) throws IOException, S3Exception {
    String copySourceHeader = request.getHeader("x-amz-copy-source");
    copySourceHeader = URLDecoder.decode(copySourceHeader, "UTF-8");
    if (copySourceHeader.startsWith("/")) {
        // Some clients like boto do not include the leading slash
        copySourceHeader = copySourceHeader.substring(1);
    }//from   ww w . j av a 2 s  .c o m
    String[] path = copySourceHeader.split("/", 2);
    if (path.length != 2) {
        throw new S3Exception(S3ErrorCode.INVALID_REQUEST);
    }
    String sourceContainerName = path[0];
    String sourceBlobName = path[1];
    boolean replaceMetadata = "REPLACE".equalsIgnoreCase(request.getHeader("x-amz-metadata-directive"));

    if (sourceContainerName.equals(destContainerName) && sourceBlobName.equals(destBlobName)
            && !replaceMetadata) {
        throw new S3Exception(S3ErrorCode.INVALID_REQUEST);
    }

    CopyOptions.Builder options = CopyOptions.builder();
    if (replaceMetadata) {
        ContentMetadataBuilder contentMetadata = ContentMetadataBuilder.create();
        ImmutableMap.Builder<String, String> userMetadata = ImmutableMap.builder();
        for (String headerName : Collections.list(request.getHeaderNames())) {
            String headerValue = Strings.nullToEmpty(request.getHeader(headerName));
            if (headerName.equalsIgnoreCase(HttpHeaders.CONTENT_DISPOSITION)) {
                contentMetadata.contentDisposition(headerValue);
            } else if (headerName.equalsIgnoreCase(HttpHeaders.CONTENT_ENCODING)) {
                contentMetadata.contentEncoding(headerValue);
            } else if (headerName.equalsIgnoreCase(HttpHeaders.CONTENT_LANGUAGE)) {
                contentMetadata.contentLanguage(headerValue);
            } else if (headerName.equalsIgnoreCase(HttpHeaders.CONTENT_TYPE)) {
                contentMetadata.contentType(headerValue);
            } else if (headerName.toLowerCase().startsWith(USER_METADATA_PREFIX)) {
                userMetadata.put(headerName.substring(USER_METADATA_PREFIX.length()), headerValue);
            }
            // TODO: Expires
        }
        options.contentMetadata(contentMetadata.build());
        options.userMetadata(userMetadata.build());
    }

    String eTag;
    try {
        eTag = blobStore.copyBlob(sourceContainerName, sourceBlobName, destContainerName, destBlobName,
                options.build());
    } catch (KeyNotFoundException knfe) {
        throw new S3Exception(S3ErrorCode.NO_SUCH_KEY, knfe);
    }

    // TODO: jclouds should include this in CopyOptions
    String cannedAcl = request.getHeader("x-amz-acl");
    if (cannedAcl != null && !cannedAcl.equalsIgnoreCase("private")) {
        handleSetBlobAcl(request, response, blobStore, destContainerName, destBlobName);
    }

    BlobMetadata blobMetadata = blobStore.blobMetadata(destContainerName, destBlobName);
    try (Writer writer = response.getWriter()) {
        XMLStreamWriter xml = xmlOutputFactory.createXMLStreamWriter(writer);
        xml.writeStartDocument();
        xml.writeStartElement("CopyObjectResult");
        xml.writeDefaultNamespace(AWS_XMLNS);

        writeSimpleElement(xml, "LastModified",
                blobStore.getContext().utils().date().iso8601DateFormat(blobMetadata.getLastModified()));
        writeSimpleElement(xml, "ETag", "\"" + eTag + "\"");

        xml.writeEndElement();
        xml.flush();
    } catch (XMLStreamException xse) {
        throw new IOException(xse);
    }
}

From source file:org.gaul.s3proxy.S3ProxyHandler.java

private static void addMetadataToResponse(HttpServletResponse response, BlobMetadata metadata) {
    ContentMetadata contentMetadata = metadata.getContentMetadata();
    response.addHeader(HttpHeaders.CONTENT_DISPOSITION, contentMetadata.getContentDisposition());
    response.addHeader(HttpHeaders.CONTENT_ENCODING, contentMetadata.getContentEncoding());
    response.addHeader(HttpHeaders.CONTENT_LANGUAGE, contentMetadata.getContentLanguage());
    response.addHeader(HttpHeaders.CONTENT_LENGTH, contentMetadata.getContentLength().toString());
    response.setContentType(contentMetadata.getContentType());
    HashCode contentMd5 = contentMetadata.getContentMD5AsHashCode();
    if (contentMd5 != null) {
        byte[] contentMd5Bytes = contentMd5.asBytes();
        response.addHeader(HttpHeaders.CONTENT_MD5, BaseEncoding.base64().encode(contentMd5Bytes));
        response.addHeader(HttpHeaders.ETAG,
                "\"" + BaseEncoding.base16().lowerCase().encode(contentMd5Bytes) + "\"");
    }//from ww  w  .  j  a v  a2 s  . c o  m
    Date expires = contentMetadata.getExpires();
    if (expires != null) {
        response.addDateHeader(HttpHeaders.EXPIRES, expires.getTime());
    }
    response.addDateHeader(HttpHeaders.LAST_MODIFIED, metadata.getLastModified().getTime());
    for (Map.Entry<String, String> entry : metadata.getUserMetadata().entrySet()) {
        response.addHeader(USER_METADATA_PREFIX + entry.getKey(), entry.getValue());
    }
}

From source file:org.gaul.s3proxy.S3ProxyHandler.java

private static void addContentMetdataFromHttpRequest(BlobBuilder.PayloadBlobBuilder builder,
        HttpServletRequest request) {//from w  w  w .  j  a  v  a 2 s .c  o  m
    ImmutableMap.Builder<String, String> userMetadata = ImmutableMap.builder();
    for (String headerName : Collections.list(request.getHeaderNames())) {
        if (headerName.toLowerCase().startsWith(USER_METADATA_PREFIX)) {
            userMetadata.put(headerName.substring(USER_METADATA_PREFIX.length()),
                    Strings.nullToEmpty(request.getHeader(headerName)));
        }
    }
    builder.contentDisposition(request.getHeader(HttpHeaders.CONTENT_DISPOSITION))
            .contentEncoding(request.getHeader(HttpHeaders.CONTENT_ENCODING))
            .contentLanguage(request.getHeader(HttpHeaders.CONTENT_LANGUAGE))
            .userMetadata(userMetadata.build());
    String contentType = request.getContentType();
    if (contentType != null) {
        builder.contentType(contentType);
    }
    long expires = request.getDateHeader(HttpHeaders.EXPIRES);
    if (expires != -1) {
        builder.expires(new Date(expires));
    }
}