Search in sources :

Example 1 with DigestValidationInputStream

use of com.qcloud.cos.internal.DigestValidationInputStream in project cos-java-sdk-v5 by tencentyun.

the class COSClient method getObject.

@Override
public COSObject getObject(GetObjectRequest getObjectRequest) throws CosClientException, CosServiceException {
    rejectNull(getObjectRequest, "The GetObjectRequest parameter must be specified when requesting an object");
    rejectNull(getObjectRequest.getBucketName(), "The bucket name parameter must be specified when requesting an object");
    rejectNull(getObjectRequest.getKey(), "The key parameter must be specified when requesting an object");
    rejectNull(clientConfig.getRegion(), "region is null, region in clientConfig must be specified when requesting an object");
    CosHttpRequest<GetObjectRequest> request = createRequest(getObjectRequest.getBucketName(), getObjectRequest.getKey(), getObjectRequest, HttpMethodName.GET);
    addParameterIfNotNull(request, "versionId", getObjectRequest.getVersionId());
    // Range
    long[] range = getObjectRequest.getRange();
    if (range != null) {
        request.addHeader(Headers.RANGE, "bytes=" + Long.toString(range[0]) + "-" + Long.toString(range[1]));
    }
    addResponseHeaderParameters(request, getObjectRequest.getResponseHeaders());
    addDateHeader(request, Headers.GET_OBJECT_IF_MODIFIED_SINCE, getObjectRequest.getModifiedSinceConstraint());
    addDateHeader(request, Headers.GET_OBJECT_IF_UNMODIFIED_SINCE, getObjectRequest.getUnmodifiedSinceConstraint());
    addStringListHeader(request, Headers.GET_OBJECT_IF_MATCH, getObjectRequest.getMatchingETagConstraints());
    addStringListHeader(request, Headers.GET_OBJECT_IF_NONE_MATCH, getObjectRequest.getNonmatchingETagConstraints());
    // Populate the SSE-C parameters to the request header
    populateSSE_C(request, getObjectRequest.getSSECustomerKey());
    // Populate the traffic limit parameter to the request header
    populateTrafficLimit(request, getObjectRequest.getTrafficLimit());
    try {
        COSObject cosObject = invoke(request, new COSObjectResponseHandler());
        cosObject.setBucketName(getObjectRequest.getBucketName());
        cosObject.setKey(getObjectRequest.getKey());
        InputStream is = cosObject.getObjectContent();
        HttpRequestBase httpRequest = cosObject.getObjectContent().getHttpRequest();
        is = new ServiceClientHolderInputStream(is, this);
        // bytes and complains if what we received doesn't match the Etag.
        if (!skipMd5CheckStrategy.skipClientSideValidation(getObjectRequest, cosObject.getObjectMetadata())) {
            try {
                byte[] serverSideHash = BinaryUtils.fromHex(cosObject.getObjectMetadata().getETag());
                // No content length check is performed when the
                // MD5 check is enabled, since a correct MD5 check would
                // imply a correct content length.
                MessageDigest digest = MessageDigest.getInstance("MD5");
                is = new DigestValidationInputStream(is, digest, serverSideHash);
            } catch (NoSuchAlgorithmException e) {
                log.warn("No MD5 digest algorithm available.  Unable to calculate " + "checksum and verify data integrity.", e);
            } catch (DecoderException e) {
                log.warn("BinaryUtils.fromHex error. Unable to calculate " + "checksum and verify data integrity. etag:" + cosObject.getObjectMetadata().getETag(), e);
            }
        } else {
            // Ensures the data received from COS has the same length as the
            // expected content-length
            is = new LengthCheckInputStream(is, // expected length
            cosObject.getObjectMetadata().getContentLength(), // bytes received from cos are all included even if
            INCLUDE_SKIPPED_BYTES);
        // skipped
        }
        cosObject.setObjectContent(new COSObjectInputStream(is, httpRequest));
        return cosObject;
    } catch (CosServiceException cse) {
        /*
             * If the request failed because one of the specified constraints was not met (ex:
             * matching ETag, modified since date, etc.), then return null, so that users don't have
             * to wrap their code in try/catch blocks and check for this status code if they want to
             * use constraints.
             */
        if (cse.getStatusCode() == 412 || cse.getStatusCode() == 304) {
            return null;
        }
        throw cse;
    }
}
Also used : LengthCheckInputStream(com.qcloud.cos.internal.LengthCheckInputStream) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) DigestValidationInputStream(com.qcloud.cos.internal.DigestValidationInputStream) ReleasableInputStream(com.qcloud.cos.internal.ReleasableInputStream) MD5DigestCalculatingInputStream(com.qcloud.cos.internal.MD5DigestCalculatingInputStream) LengthCheckInputStream(com.qcloud.cos.internal.LengthCheckInputStream) ServiceClientHolderInputStream(com.qcloud.cos.internal.ServiceClientHolderInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ResettableInputStream(com.qcloud.cos.internal.ResettableInputStream) SdkFilterInputStream(com.qcloud.cos.internal.SdkFilterInputStream) InputStream(java.io.InputStream) COSObjectResponseHandler(com.qcloud.cos.internal.COSObjectResponseHandler) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) DecoderException(org.apache.commons.codec.DecoderException) DigestValidationInputStream(com.qcloud.cos.internal.DigestValidationInputStream) CosServiceException(com.qcloud.cos.exception.CosServiceException) MessageDigest(java.security.MessageDigest) ServiceClientHolderInputStream(com.qcloud.cos.internal.ServiceClientHolderInputStream)

Aggregations

CosServiceException (com.qcloud.cos.exception.CosServiceException)1 COSObjectResponseHandler (com.qcloud.cos.internal.COSObjectResponseHandler)1 DigestValidationInputStream (com.qcloud.cos.internal.DigestValidationInputStream)1 LengthCheckInputStream (com.qcloud.cos.internal.LengthCheckInputStream)1 MD5DigestCalculatingInputStream (com.qcloud.cos.internal.MD5DigestCalculatingInputStream)1 ReleasableInputStream (com.qcloud.cos.internal.ReleasableInputStream)1 ResettableInputStream (com.qcloud.cos.internal.ResettableInputStream)1 SdkFilterInputStream (com.qcloud.cos.internal.SdkFilterInputStream)1 ServiceClientHolderInputStream (com.qcloud.cos.internal.ServiceClientHolderInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 MessageDigest (java.security.MessageDigest)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 DecoderException (org.apache.commons.codec.DecoderException)1 HttpRequestBase (org.apache.http.client.methods.HttpRequestBase)1