use of com.qcloud.cos.internal.ServiceClientHolderInputStream 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;
}
}
use of com.qcloud.cos.internal.ServiceClientHolderInputStream in project cos-java-sdk-v5 by tencentyun.
the class COSClient method selectObjectContent.
@Override
public SelectObjectContentResult selectObjectContent(SelectObjectContentRequest selectRequest) throws CosClientException, CosServiceException {
rejectNull(selectRequest, "The request parameter must be specified");
rejectNull(selectRequest.getBucketName(), "The bucket name parameter must be specified when selecting object content.");
rejectNull(selectRequest.getKey(), "The key parameter must be specified when selecting object content.");
CosHttpRequest<SelectObjectContentRequest> request = createRequest(selectRequest.getBucketName(), selectRequest.getKey(), selectRequest, HttpMethodName.POST);
request.addParameter("select", null);
request.addParameter("select-type", "2");
populateSSE_C(request, selectRequest.getSSECustomerKey());
setContent(request, RequestXmlFactory.convertToXmlByteArray(selectRequest), ContentType.APPLICATION_XML.toString(), true);
COSObject result = invoke(request, new COSObjectResponseHandler());
// Hold a reference to this client while the InputStream is still
// around - otherwise a finalizer in the HttpClient may reset the
// underlying TCP connection out from under us.
SdkFilterInputStream resultStream = new ServiceClientHolderInputStream(result.getObjectContent(), this);
return new SelectObjectContentResult().withPayload(new SelectObjectContentEventStream(resultStream));
}
Aggregations