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

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

Introduction

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

Prototype

String CONTENT_LANGUAGE

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

Click Source Link

Document

The HTTP Content-Language header field name.

Usage

From source file:org.jclouds.s3.binders.BindObjectMetadataToRequest.java

@SuppressWarnings("unchecked")
@Override//from   ww w.  j  av a  2  s .c o  m
public <R extends HttpRequest> R bindToRequest(R request, Object input) {
    checkArgument(checkNotNull(input, "input") instanceof ObjectMetadata,
            "this binder is only valid for ObjectMetadata!");
    checkNotNull(request, "request");

    ObjectMetadata md = ObjectMetadata.class.cast(input);
    checkArgument(md.getKey() != null, "objectMetadata.getKey() must be set!");

    request = metadataPrefixer.bindToRequest(request, md.getUserMetadata());

    Builder<String, String> headers = ImmutableMultimap.builder();
    if (md.getContentMetadata().getCacheControl() != null) {
        headers.put(HttpHeaders.CACHE_CONTROL, md.getContentMetadata().getCacheControl());
    }

    if (md.getContentMetadata().getContentDisposition() != null) {
        headers.put("Content-Disposition", md.getContentMetadata().getContentDisposition());
    }

    if (md.getContentMetadata().getContentEncoding() != null) {
        headers.put("Content-Encoding", md.getContentMetadata().getContentEncoding());
    }

    String contentLanguage = md.getContentMetadata().getContentLanguage();
    if (contentLanguage != null) {
        headers.put(HttpHeaders.CONTENT_LANGUAGE, contentLanguage);
    }

    if (md.getContentMetadata().getContentType() != null) {
        headers.put(HttpHeaders.CONTENT_TYPE, md.getContentMetadata().getContentType());
    } else {
        headers.put(HttpHeaders.CONTENT_TYPE, "binary/octet-stream");
    }

    if (md.getContentMetadata().getContentMD5() != null) {
        headers.put("Content-MD5", base64().encode(md.getContentMetadata().getContentMD5()));
    }

    return (R) request.toBuilder().replaceHeaders(headers.build()).build();
}

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  w w .j  a va 2s.  c o m*/
    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);
    }/* w  ww.java 2s .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   w ww  .j a  v  a2s .  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   ww w. ja v  a2s .  c om
    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));
    }
}