Example usage for com.amazonaws SignableRequest addHeader

List of usage examples for com.amazonaws SignableRequest addHeader

Introduction

In this page you can find the example usage for com.amazonaws SignableRequest addHeader.

Prototype

void addHeader(String name, String value);

Source Link

Document

Sets the specified header for this request.

Usage

From source file:com.ibm.og.s3.v4.AWSS3V4Signer.java

License:Open Source License

/**
 * Returns the pre-defined header value and set other necessary headers if the request needs to be
 * chunk-encoded. Otherwise calls the superclass method which calculates the hash of the whole
 * content for signing.//from w w  w  .  j  ava 2s  .  co  m
 */
@Override
protected String calculateContentHash(final SignableRequest<?> request) {
    // To be consistent with other service clients using sig-v4,
    // we just set the header as "required", and AWS4Signer.sign() will be
    // notified to pick up the header value returned by this method.
    request.addHeader(X_AMZ_CONTENT_SHA256, "required");
    final String contentLength = request.getHeaders().get(Headers.CONTENT_LENGTH);
    if (useChunkEncoding(request)) {
        final long originalContentLength;
        if (contentLength != null) {
            originalContentLength = Long.parseLong(contentLength);
        } else {
            /**
             * "Content-Length" header could be missing if the caller is uploading a stream without
             * setting Content-Length in ObjectMetadata. Before using sigv4, we rely on HttpClient to
             * add this header by using BufferedHttpEntity when creating the HttpRequest object. But
             * now, we need this information immediately for the signing process, so we have to cache
             * the stream here.
             */
            try {
                originalContentLength = getContentLength(request);
            } catch (final IOException e) {
                throw new AmazonClientException("Cannot get the content-length of the request content.", e);
            }
        }
        request.addHeader("x-amz-decoded-content-length", Long.toString(originalContentLength));
        // Make sure "Content-Length" header is not empty so that HttpClient
        // won't cache the stream again to recover Content-Length
        request.addHeader(Headers.CONTENT_LENGTH, Long
                .toString(AwsChunkedEncodingInputStream.calculateStreamContentLength(originalContentLength)));
        return CONTENT_SHA_256;
    }

    if (this.digestCache != null) {
        try {
            final long length = contentLength != null ? Long.parseLong(contentLength) : 0;
            return BinaryUtils.toHex(this.digestCache.get(length));
        } catch (final ExecutionException e) {
            throw new RuntimeException(e);
        }
    }
    return super.calculateContentHash(request);
}