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

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

Introduction

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

Prototype

String CONTENT_MD5

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

Click Source Link

Document

The HTTP Content-MD5 header field name.

Usage

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

/**
 * Create Amazon V2 signature.  Reference:
 * http://docs.aws.amazon.com/general/latest/gr/signature-version-2.html
 *///w  ww  . j  av a2  s . c  o  m
static String createAuthorizationSignature(HttpServletRequest request, String uri, String credential) {
    // sort Amazon headers
    SortedSetMultimap<String, String> canonicalizedHeaders = TreeMultimap.create();
    for (String headerName : Collections.list(request.getHeaderNames())) {
        Collection<String> headerValues = Collections.list(request.getHeaders(headerName));
        headerName = headerName.toLowerCase();
        if (!headerName.startsWith("x-amz-")) {
            continue;
        }
        if (headerValues.isEmpty()) {
            canonicalizedHeaders.put(headerName, "");
        }
        for (String headerValue : headerValues) {
            canonicalizedHeaders.put(headerName, Strings.nullToEmpty(headerValue));
        }
    }

    // build string to sign
    StringBuilder builder = new StringBuilder().append(request.getMethod()).append('\n')
            .append(Strings.nullToEmpty(request.getHeader(HttpHeaders.CONTENT_MD5))).append('\n')
            .append(Strings.nullToEmpty(request.getHeader(HttpHeaders.CONTENT_TYPE))).append('\n');
    String expires = request.getParameter("Expires");
    if (expires != null) {
        builder.append(expires);
    } else if (!canonicalizedHeaders.containsKey("x-amz-date")) {
        builder.append(request.getHeader(HttpHeaders.DATE));
    }
    builder.append('\n');
    for (Map.Entry<String, String> entry : canonicalizedHeaders.entries()) {
        builder.append(entry.getKey()).append(':').append(entry.getValue()).append('\n');
    }
    builder.append(uri);

    char separator = '?';
    List<String> subresources = Collections.list(request.getParameterNames());
    Collections.sort(subresources);
    for (String subresource : subresources) {
        if (SIGNED_SUBRESOURCES.contains(subresource)) {
            builder.append(separator).append(subresource);

            String value = request.getParameter(subresource);
            if (!"".equals(value)) {
                builder.append('=').append(value);
            }
            separator = '&';
        }
    }

    String stringToSign = builder.toString();
    logger.trace("stringToSign: {}", stringToSign);

    // sign string
    Mac mac;
    try {
        mac = Mac.getInstance("HmacSHA1");
        mac.init(new SecretKeySpec(credential.getBytes(StandardCharsets.UTF_8), "HmacSHA1"));
    } catch (InvalidKeyException | NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
    return BaseEncoding.base64().encode(mac.doFinal(stringToSign.getBytes(StandardCharsets.UTF_8)));
}

From source file:org.jclouds.glacier.util.AWSRequestSignerV4.java

private static Multimap<String, String> buildCanonicalizedHeadersMap(HttpRequest request) {
    Multimap<String, String> headers = request.getHeaders();
    SortedSetMultimap<String, String> canonicalizedHeaders = TreeMultimap.create();
    for (Entry<String, String> header : headers.entries()) {
        if (header.getKey() == null)
            continue;
        String key = header.getKey().toString().toLowerCase(Locale.getDefault());
        // Ignore any headers that are not particularly interesting.
        if (key.equalsIgnoreCase(HttpHeaders.CONTENT_TYPE) || key.equalsIgnoreCase(HttpHeaders.CONTENT_MD5)
                || key.equalsIgnoreCase(HttpHeaders.HOST) || key.startsWith(HEADER_TAG)) {
            canonicalizedHeaders.put(key, header.getValue());
        }//from   w  w w. j  ava  2  s .  c  o  m
    }
    return canonicalizedHeaders;
}

From source file:org.jclouds.googlecloudstorage.blobstore.GoogleCloudStorageBlobRequestSigner.java

private String createStringToSign(HttpRequest request, long expires) {
    utils.logRequest(signatureLog, request, ">>");
    StringBuilder buffer = new StringBuilder();
    buffer.append(request.getMethod()).append("\n");
    buffer.append(Strings.nullToEmpty(request.getFirstHeaderOrNull(HttpHeaders.CONTENT_MD5))).append("\n");
    buffer.append(Strings.nullToEmpty(request.getFirstHeaderOrNull(HttpHeaders.CONTENT_TYPE))).append("\n");
    buffer.append(String.valueOf(expires)).append("\n");
    // TODO: extension headers
    buffer.append(request.getEndpoint().getPath());
    return buffer.toString();
}

From source file:com.bouncestorage.chaoshttpproxy.ChaosHttpProxyHandler.java

@Override
public void handle(String target, Request baseRequest, HttpServletRequest request,
        HttpServletResponse servletResponse) throws IOException {
    baseRequest.setHandled(true);/*from ww w.j  ava  2 s . c o m*/

    // CONNECT is not supported pending implementation of MITM HTTPS
    if (request.getMethod().equals("CONNECT")) {
        logger.debug("CONNECT is not supported");
        servletResponse.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
        return;
    }

    Failure failure = supplier.get();
    logger.debug("request: {}", request);
    logger.debug("Failure: {}", failure);
    try (InputStream is = request.getInputStream(); OutputStream os = servletResponse.getOutputStream()) {
        HostAndPort hostAndPort = HostAndPort.fromString(request.getHeader(HttpHeaders.HOST));
        String queryString = request.getQueryString();
        URI uri;
        try {
            uri = new URI(request.getScheme(), /*userInfo=*/ null, hostAndPort.getHostText(),
                    hostAndPort.hasPort() ? hostAndPort.getPort() : 80, request.getRequestURI(), queryString,
                    /*fragment=*/ null);
        } catch (URISyntaxException use) {
            throw new IOException(use);
        }
        logger.debug("uri: {}", uri);
        URI redirectedUri = redirects.get(uri);
        if (redirectedUri != null) {
            // TODO: parameters
            uri = redirectedUri;
            logger.debug("redirected uri: {}", uri);
        }

        switch (failure) {
        case HTTP_301:
        case HTTP_302:
        case HTTP_303:
        case HTTP_307:
        case HTTP_308:
            servletResponse.setStatus(failure.getResponseCode());
            URI redirectUri;
            try {
                redirectUri = new URI(request.getScheme(), /*userInfo=*/ null, hostAndPort.getHostText(),
                        hostAndPort.hasPort() ? hostAndPort.getPort() : 80, "/" + UUID.randomUUID().toString(),
                        /*query=*/ null, /*fragment=*/ null);
            } catch (URISyntaxException use) {
                throw new IOException(use);
            }
            redirects.put(redirectUri, uri);
            servletResponse.addHeader(HttpHeaders.LOCATION, redirectUri.toString());
            return;
        case HTTP_408:
        case HTTP_500:
        case HTTP_503:
        case HTTP_504:
            servletResponse.setStatus(failure.getResponseCode());
            return;
        case TIMEOUT:
            Uninterruptibles.sleepUninterruptibly(Long.MAX_VALUE, TimeUnit.DAYS);
            return;
        default:
            break;
        }

        InputStreamResponseListener listener = new InputStreamResponseListener();
        InputStream iss = failure == Failure.PARTIAL_REQUEST ?
        // TODO: random limit
                ByteStreams.limit(is, 1024) : is;
        org.eclipse.jetty.client.api.Request clientRequest = client.newRequest(uri.toString())
                .method(request.getMethod());
        long userContentLength = -1;
        for (String headerName : Collections.list(request.getHeaderNames())) {
            if (headerName.equalsIgnoreCase(HttpHeaders.EXPECT)
                    || headerName.equalsIgnoreCase("Proxy-Connection")) {
                continue;
            }
            String headerValue = request.getHeader(headerName);
            logger.trace("{}: {}", headerName, headerValue);

            if (headerName.equalsIgnoreCase(HttpHeaders.CONTENT_MD5)
                    && failure == Failure.CORRUPT_REQUEST_CONTENT_MD5) {
                headerValue = headerValue.toUpperCase();
            }
            if (headerName.equalsIgnoreCase(HttpHeaders.CONTENT_LENGTH)) {
                userContentLength = Long.parseLong(headerValue);
            }
            clientRequest.header(headerName, headerValue);
        }

        // Work around Jetty bug that strips Content-Length
        // https://bugs.eclipse.org/bugs/show_bug.cgi?id=475613.
        final long length = userContentLength;
        clientRequest.content(new InputStreamContentProvider(iss) {
            @Override
            public long getLength() {
                return length != -1 ? length : super.getLength();
            }
        });
        clientRequest.send(listener);
        if (failure == Failure.PARTIAL_REQUEST) {
            return;
        }

        Response response;
        try {
            response = listener.get(Long.MAX_VALUE, TimeUnit.SECONDS);
        } catch (ExecutionException | InterruptedException | TimeoutException e) {
            throw new IOException(e);
        }
        int status = response.getStatus();
        logger.trace("status: {}", status);
        servletResponse.setStatus(status);
        List<HttpField> headers = Lists.newArrayList(response.getHeaders());
        if (failure == Failure.REORDER_HEADERS) {
            Collections.shuffle(headers);
        }
        for (HttpField field : headers) {
            String header = field.getName();
            String value = field.getValue();
            logger.trace("header: {}: {}", header, value);
            switch (failure) {
            case CHANGE_HEADER_CASE:
                // TODO: randomly change between upper- and lower-case
                header = header.toUpperCase();
                break;
            case CORRUPT_RESPONSE_CONTENT_MD5:
                if (header.equals(HttpHeaders.CONTENT_MD5)) {
                    value = BaseEncoding.base64().encode(new byte[Hashing.md5().bits() / 8]);
                }
                break;
            default:
                break;
            }
            servletResponse.addHeader(header, value);
        }
        try (InputStream responseContent = listener.getInputStream()) {
            switch (failure) {
            case PARTIAL_RESPONSE:
                byte[] array = new byte[1024];
                int count = responseContent.read(array);
                if (count != -1) {
                    // TODO: randomly read n - 1 bytes
                    os.write(array, 0, count / 2);
                    os.flush();
                }
                return;
            case SLOW_RESPONSE:
                for (int i = 0; i < 10; ++i) {
                    int ch = responseContent.read();
                    if (ch == -1) {
                        break;
                    }
                    os.write(ch);
                    os.flush();
                    Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);
                }
                break;
            default:
                break;
            }
            ByteStreams.copy(responseContent, os);
        }
    }
}

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;
        }/*from ww  w  .ja va 2  s. c om*/
    }

    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 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  w ww .  j  a  v a  2s  .c o 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);
    }

    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() + "\"");
    }
}

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 ava  2s. 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

/**
 * Create Amazon V2 signature.  Reference:
 * http://docs.aws.amazon.com/AmazonS3/latest/dev/RESTAuthentication.html
 *///  www  .  j  a  va2 s  . c  o m
private static String createAuthorizationSignature(HttpServletRequest request, String uri, String identity,
        String credential) {
    // sort Amazon headers
    SortedSetMultimap<String, String> canonicalizedHeaders = TreeMultimap.create();
    for (String headerName : Collections.list(request.getHeaderNames())) {
        Collection<String> headerValues = Collections.list(request.getHeaders(headerName));
        headerName = headerName.toLowerCase();
        if (!headerName.startsWith("x-amz-")) {
            continue;
        }
        if (headerValues.isEmpty()) {
            canonicalizedHeaders.put(headerName, "");
        }
        for (String headerValue : headerValues) {
            canonicalizedHeaders.put(headerName, Strings.nullToEmpty(headerValue));
        }
    }

    // build string to sign
    StringBuilder builder = new StringBuilder().append(request.getMethod()).append('\n')
            .append(Strings.nullToEmpty(request.getHeader(HttpHeaders.CONTENT_MD5))).append('\n')
            .append(Strings.nullToEmpty(request.getHeader(HttpHeaders.CONTENT_TYPE))).append('\n');
    String expires = request.getParameter("Expires");
    if (expires != null) {
        builder.append(expires);
    } else if (!canonicalizedHeaders.containsKey("x-amz-date")) {
        builder.append(request.getHeader(HttpHeaders.DATE));
    }
    builder.append('\n');
    for (Map.Entry<String, String> entry : canonicalizedHeaders.entries()) {
        builder.append(entry.getKey()).append(':').append(entry.getValue()).append('\n');
    }
    builder.append(uri);

    char separator = '?';
    List<String> subresources = Collections.list(request.getParameterNames());
    Collections.sort(subresources);
    for (String subresource : subresources) {
        if (SIGNED_SUBRESOURCES.contains(subresource)) {
            builder.append(separator).append(subresource);

            String value = request.getParameter(subresource);
            if (!"".equals(value)) {
                builder.append('=').append(value);
            }
            separator = '&';
        }
    }

    String stringToSign = builder.toString();
    logger.trace("stringToSign: {}", stringToSign);

    // sign string
    Mac mac;
    try {
        mac = Mac.getInstance("HmacSHA1");
        mac.init(new SecretKeySpec(credential.getBytes(StandardCharsets.UTF_8), "HmacSHA1"));
    } catch (InvalidKeyException | NoSuchAlgorithmException e) {
        throw Throwables.propagate(e);
    }
    return BaseEncoding.base64().encode(mac.doFinal(stringToSign.getBytes(StandardCharsets.UTF_8)));
}