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

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

Introduction

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

Prototype

String ETAG

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

Click Source Link

Document

The HTTP ETag header field name.

Usage

From source file:org.sonatype.nexus.repository.proxy.ProxyFacetSupport.java

/**
 * Extract ETag from response if possible, or {@code null}.
 *///from ww  w .j a  v  a 2s .  c  o m
@Nullable
private String extractETag(final HttpResponse response) {
    final Header etagHeader = response.getLastHeader(HttpHeaders.ETAG);
    if (etagHeader != null) {
        final String etag = etagHeader.getValue();
        if (!Strings.isNullOrEmpty(etag)) {
            if (etag.startsWith("\"") && etag.endsWith("\"")) {
                return etag.substring(1, etag.length() - 1);
            } else {
                return etag;
            }
        }
    }
    return null;
}

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

private void handlePutBlob(HttpServletRequest request, HttpServletResponse response, BlobStore blobStore,
        String containerName, String blobName) throws IOException, S3Exception {
    // Flag headers present since HttpServletResponse.getHeader returns
    // null for empty headers values.
    String contentLengthString = null;
    String contentMD5String = null;
    for (String headerName : Collections.list(request.getHeaderNames())) {
        String headerValue = Strings.nullToEmpty(request.getHeader(headerName));
        if (headerName.equalsIgnoreCase(HttpHeaders.CONTENT_LENGTH)) {
            contentLengthString = headerValue;
        } else if (headerName.equalsIgnoreCase(HttpHeaders.CONTENT_MD5)) {
            contentMD5String = headerValue;
        }/*  www .  j  av  a 2  s . co m*/
    }

    HashCode contentMD5 = null;
    if (contentMD5String != null) {
        try {
            contentMD5 = HashCode.fromBytes(BaseEncoding.base64().decode(contentMD5String));
        } catch (IllegalArgumentException iae) {
            throw new S3Exception(S3ErrorCode.INVALID_DIGEST, iae);
        }
        if (contentMD5.bits() != Hashing.md5().bits()) {
            throw new S3Exception(S3ErrorCode.INVALID_DIGEST);
        }
    }

    if (contentLengthString == null) {
        throw new S3Exception(S3ErrorCode.MISSING_CONTENT_LENGTH);
    }
    long contentLength;
    try {
        contentLength = Long.parseLong(contentLengthString);
    } catch (NumberFormatException nfe) {
        throw new S3Exception(S3ErrorCode.INVALID_ARGUMENT, nfe);
    }
    if (contentLength < 0) {
        throw new S3Exception(S3ErrorCode.INVALID_ARGUMENT);
    }

    try (InputStream is = request.getInputStream()) {
        BlobBuilder.PayloadBlobBuilder builder = blobStore.blobBuilder(blobName).payload(is)
                .contentLength(request.getContentLength());
        addContentMetdataFromHttpRequest(builder, request);
        if (contentMD5 != null) {
            builder = builder.contentMD5(contentMD5);
        }

        PutOptions options = new PutOptions();
        String blobStoreType = getBlobStoreType(blobStore);
        if (blobStoreType.equals("azureblob") && contentLength > 64 * 1024 * 1024) {
            options.multipart(true);
        }
        String eTag;
        try {
            eTag = blobStore.putBlob(containerName, builder.build(), options);
        } catch (HttpResponseException hre) {
            HttpResponse hr = hre.getResponse();
            if (hr == null) {
                return;
            }
            int status = hr.getStatusCode();
            switch (status) {
            case HttpServletResponse.SC_BAD_REQUEST:
            case 422: // Swift returns 422 Unprocessable Entity
                throw new S3Exception(S3ErrorCode.BAD_DIGEST);
            default:
                // TODO: emit hre.getContent() ?
                response.sendError(status);
                break;
            }
            return;
        } catch (RuntimeException re) {
            if (Throwables2.getFirstThrowableOfType(re, TimeoutException.class) != null) {
                throw new S3Exception(S3ErrorCode.REQUEST_TIMEOUT, re);
            } else {
                throw re;
            }
        }

        // S3 quotes ETag while Swift does not
        if (!eTag.startsWith("\"") && !eTag.endsWith("\"")) {
            eTag = '"' + eTag + '"';
        }
        response.addHeader(HttpHeaders.ETAG, eTag);
    }

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

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 ava2s .  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 void handleUploadPart(HttpServletRequest request, HttpServletResponse response, BlobStore blobStore,
        String containerName, String blobName, String uploadId) throws IOException, S3Exception {
    // TODO: duplicated from handlePutBlob
    String contentLengthString = null;
    String contentMD5String = null;
    for (String headerName : Collections.list(request.getHeaderNames())) {
        String headerValue = Strings.nullToEmpty(request.getHeader(headerName));
        if (headerName.equalsIgnoreCase(HttpHeaders.CONTENT_LENGTH)) {
            contentLengthString = headerValue;
        } else if (headerName.equalsIgnoreCase(HttpHeaders.CONTENT_MD5)) {
            contentMD5String = headerValue;
        }//from  ww  w .java 2s .com
    }

    HashCode contentMD5 = null;
    if (contentMD5String != null) {
        try {
            contentMD5 = HashCode.fromBytes(BaseEncoding.base64().decode(contentMD5String));
        } catch (IllegalArgumentException iae) {
            throw new S3Exception(S3ErrorCode.INVALID_DIGEST, iae);
        }
        if (contentMD5.bits() != Hashing.md5().bits()) {
            throw new S3Exception(S3ErrorCode.INVALID_DIGEST);
        }
    }

    if (contentLengthString == null) {
        throw new S3Exception(S3ErrorCode.MISSING_CONTENT_LENGTH);
    }
    long contentLength;
    try {
        contentLength = Long.parseLong(contentLengthString);
    } catch (NumberFormatException nfe) {
        throw new S3Exception(S3ErrorCode.INVALID_ARGUMENT, nfe);
    }
    if (contentLength < 0) {
        throw new S3Exception(S3ErrorCode.INVALID_ARGUMENT);
    }

    String partNumberString = request.getParameter("partNumber");
    if (partNumberString == null) {
        throw new S3Exception(S3ErrorCode.INVALID_ARGUMENT);
    }
    int partNumber;
    try {
        partNumber = Integer.parseInt(partNumberString);
    } catch (NumberFormatException nfe) {
        throw new S3Exception(S3ErrorCode.INVALID_ARGUMENT,
                "Part number must be an integer between 1 and 10000" + ", inclusive", nfe,
                ImmutableMap.of("ArgumentName", "partNumber", "ArgumentValue", partNumberString));
    }
    if (partNumber < 1 || partNumber > 10_000) {
        throw new S3Exception(S3ErrorCode.INVALID_ARGUMENT,
                "Part number must be an integer between 1 and 10000" + ", inclusive", (Throwable) null,
                ImmutableMap.of("ArgumentName", "partNumber", "ArgumentValue", partNumberString));
    }

    // TODO: how to reconstruct original mpu?
    MultipartUpload mpu = MultipartUpload.create(containerName, blobName, uploadId,
            createFakeBlobMetadata(blobStore));

    try (InputStream is = request.getInputStream()) {
        Payload payload = Payloads.newInputStreamPayload(is);
        payload.getContentMetadata().setContentLength(contentLength);
        if (contentMD5 != null) {
            payload.getContentMetadata().setContentMD5(contentMD5);
        }

        MultipartPart part = blobStore.uploadMultipartPart(mpu, partNumber, payload);
        response.addHeader(HttpHeaders.ETAG, "\"" + part.partETag() + "\"");
    }
}