Search in sources :

Example 1 with CopyObjectResultHandler

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

the class COSClient method copyObject.

@Override
public CopyObjectResult copyObject(CopyObjectRequest copyObjectRequest) throws CosClientException, CosServiceException {
    rejectNull(copyObjectRequest, "The CopyObjectRequest parameter must be specified when copying an object");
    rejectNull(copyObjectRequest.getSourceBucketName(), "The source bucket name must be specified when copying an object");
    rejectNull(copyObjectRequest.getSourceKey(), "The source object key must be specified when copying an object");
    rejectNull(copyObjectRequest.getDestinationBucketName(), "The destination bucket name must be specified when copying an object");
    rejectNull(copyObjectRequest.getDestinationKey(), "The destination object key must be specified when copying an object");
    rejectNull(clientConfig.getRegion(), "region is null, region in clientConfig must be specified when copying an object");
    String destinationKey = copyObjectRequest.getDestinationKey();
    String destinationBucketName = copyObjectRequest.getDestinationBucketName();
    CosHttpRequest<CopyObjectRequest> request = createRequest(destinationBucketName, destinationKey, copyObjectRequest, HttpMethodName.PUT);
    populateRequestWithCopyObjectParameters(request, copyObjectRequest);
    /*
         * We can't send a non-zero length Content-Length header if the user specified it, otherwise
         * it messes up the HTTP connection when the remote server thinks there's more data to pull.
         */
    setZeroContentLength(request);
    CopyObjectResultHandler copyObjectResultHandler = null;
    try {
        @SuppressWarnings("unchecked") ResponseHeaderHandlerChain<CopyObjectResultHandler> handler = new ResponseHeaderHandlerChain<CopyObjectResultHandler>(// xml payload unmarshaller
        new Unmarshallers.CopyObjectUnmarshaller(), // header handlers
        new ServerSideEncryptionHeaderHandler<CopyObjectResultHandler>(), new ObjectExpirationHeaderHandler<CopyObjectResultHandler>(), new VIDResultHandler<CopyObjectResultHandler>());
        copyObjectResultHandler = invoke(request, handler);
    } 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() == Constants.FAILED_PRECONDITION_STATUS_CODE) {
            return null;
        }
        throw cse;
    }
    /*
         * CopyObject has two failure modes: 1 - An HTTP error code is returned and the error is
         * processed like any other error response. 2 - An HTTP 200 OK code is returned, but the
         * response content contains an XML error response.
         *
         * This makes it very difficult for the client runtime to cleanly detect this case and
         * handle it like any other error response. We could extend the runtime to have a more
         * flexible/customizable definition of success/error (per request), but it's probably
         * overkill for this one special case.
         */
    if (copyObjectResultHandler.getErrorCode() != null) {
        String errorCode = copyObjectResultHandler.getErrorCode();
        String errorMessage = copyObjectResultHandler.getErrorMessage();
        String requestId = copyObjectResultHandler.getErrorRequestId();
        CosServiceException cse = new CosServiceException(errorMessage);
        cse.setErrorCode(errorCode);
        cse.setRequestId(requestId);
        cse.setStatusCode(200);
        throw cse;
    }
    CopyObjectResult copyObjectResult = new CopyObjectResult();
    copyObjectResult.setETag(copyObjectResultHandler.getETag());
    copyObjectResult.setLastModifiedDate(copyObjectResultHandler.getLastModified());
    copyObjectResult.setVersionId(copyObjectResultHandler.getVersionId());
    copyObjectResult.setSSEAlgorithm(copyObjectResultHandler.getSSEAlgorithm());
    copyObjectResult.setSSECustomerAlgorithm(copyObjectResultHandler.getSSECustomerAlgorithm());
    copyObjectResult.setSSECustomerKeyMd5(copyObjectResultHandler.getSSECustomerKeyMd5());
    copyObjectResult.setExpirationTime(copyObjectResultHandler.getExpirationTime());
    copyObjectResult.setExpirationTimeRuleId(copyObjectResultHandler.getExpirationTimeRuleId());
    copyObjectResult.setDateStr(copyObjectResultHandler.getDateStr());
    copyObjectResult.setCrc64Ecma(copyObjectResultHandler.getCrc64Ecma());
    copyObjectResult.setRequestId(copyObjectResultHandler.getRequestId());
    return copyObjectResult;
}
Also used : Unmarshallers(com.qcloud.cos.internal.Unmarshallers) CosServiceException(com.qcloud.cos.exception.CosServiceException) CopyObjectResultHandler(com.qcloud.cos.internal.XmlResponsesSaxParser.CopyObjectResultHandler) ResponseHeaderHandlerChain(com.qcloud.cos.internal.ResponseHeaderHandlerChain)

Example 2 with CopyObjectResultHandler

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

the class COSClient method copyPart.

@Override
public CopyPartResult copyPart(CopyPartRequest copyPartRequest) throws CosClientException, CosServiceException {
    rejectNull(copyPartRequest, "The CopyPartRequest parameter must be specified when copying a part");
    rejectNull(copyPartRequest.getSourceBucketName(), "The source bucket name must be specified when copying a part");
    rejectNull(copyPartRequest.getSourceKey(), "The source object key must be specified when copying a part");
    rejectNull(copyPartRequest.getDestinationBucketName(), "The destination bucket name must be specified when copying a part");
    rejectNull(copyPartRequest.getUploadId(), "The upload id must be specified when copying a part");
    rejectNull(copyPartRequest.getDestinationKey(), "The destination object key must be specified when copying a part");
    rejectNull(copyPartRequest.getPartNumber(), "The part number must be specified when copying a part");
    rejectNull(clientConfig.getRegion(), "region is null, region in clientConfig must be specified when copying a part");
    String destinationKey = copyPartRequest.getDestinationKey();
    String destinationBucketName = copyPartRequest.getDestinationBucketName();
    CosHttpRequest<CopyPartRequest> request = createRequest(destinationBucketName, destinationKey, copyPartRequest, HttpMethodName.PUT);
    populateRequestWithCopyPartParameters(request, copyPartRequest);
    request.addParameter("uploadId", copyPartRequest.getUploadId());
    request.addParameter("partNumber", Integer.toString(copyPartRequest.getPartNumber()));
    /*
         * We can't send a non-zero length Content-Length header if the user
         * specified it, otherwise it messes up the HTTP connection when the
         * remote server thinks there's more data to pull.
         */
    setZeroContentLength(request);
    CopyObjectResultHandler copyObjectResultHandler = null;
    try {
        @SuppressWarnings("unchecked") ResponseHeaderHandlerChain<CopyObjectResultHandler> handler = new ResponseHeaderHandlerChain<CopyObjectResultHandler>(// xml payload unmarshaller
        new Unmarshallers.CopyObjectUnmarshaller(), // header handlers
        new ServerSideEncryptionHeaderHandler<CopyObjectResultHandler>(), new COSVersionHeaderHandler());
        copyObjectResultHandler = invoke(request, handler);
    } 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() == Constants.FAILED_PRECONDITION_STATUS_CODE) {
            return null;
        }
        throw cse;
    }
    /*
         * CopyPart has two failure modes: 1 - An HTTP error code is returned
         * and the error is processed like any other error response. 2 - An HTTP
         * 200 OK code is returned, but the response content contains an XML
         * error response.
         *
         * This makes it very difficult for the client runtime to cleanly detect
         * this case and handle it like any other error response. We could
         * extend the runtime to have a more flexible/customizable definition of
         * success/error (per request), but it's probably overkill for this one
         * special case.
         */
    if (copyObjectResultHandler.getErrorCode() != null) {
        String errorCode = copyObjectResultHandler.getErrorCode();
        String errorMessage = copyObjectResultHandler.getErrorMessage();
        String requestId = copyObjectResultHandler.getErrorRequestId();
        CosServiceException cse = new CosServiceException(errorMessage);
        cse.setErrorCode(errorCode);
        cse.setErrorType(ErrorType.Service);
        cse.setRequestId(requestId);
        cse.setStatusCode(200);
        throw cse;
    }
    CopyPartResult copyPartResult = new CopyPartResult();
    copyPartResult.setETag(copyObjectResultHandler.getETag());
    copyPartResult.setPartNumber(copyPartRequest.getPartNumber());
    copyPartResult.setLastModifiedDate(copyObjectResultHandler.getLastModified());
    copyPartResult.setVersionId(copyObjectResultHandler.getVersionId());
    copyPartResult.setSSEAlgorithm(copyObjectResultHandler.getSSEAlgorithm());
    copyPartResult.setSSECustomerAlgorithm(copyObjectResultHandler.getSSECustomerAlgorithm());
    copyPartResult.setSSECustomerKeyMd5(copyObjectResultHandler.getSSECustomerKeyMd5());
    copyPartResult.setCrc64Ecma(copyObjectResultHandler.getCrc64Ecma());
    return copyPartResult;
}
Also used : Unmarshallers(com.qcloud.cos.internal.Unmarshallers) COSVersionHeaderHandler(com.qcloud.cos.internal.COSVersionHeaderHandler) CosServiceException(com.qcloud.cos.exception.CosServiceException) CopyObjectResultHandler(com.qcloud.cos.internal.XmlResponsesSaxParser.CopyObjectResultHandler) ResponseHeaderHandlerChain(com.qcloud.cos.internal.ResponseHeaderHandlerChain)

Aggregations

CosServiceException (com.qcloud.cos.exception.CosServiceException)2 ResponseHeaderHandlerChain (com.qcloud.cos.internal.ResponseHeaderHandlerChain)2 Unmarshallers (com.qcloud.cos.internal.Unmarshallers)2 CopyObjectResultHandler (com.qcloud.cos.internal.XmlResponsesSaxParser.CopyObjectResultHandler)2 COSVersionHeaderHandler (com.qcloud.cos.internal.COSVersionHeaderHandler)1