Search in sources :

Example 1 with ObjectExpirationHeaderHandler

use of com.amazonaws.services.s3.internal.ObjectExpirationHeaderHandler in project aws-sdk-android by aws-amplify.

the class AmazonS3Client method completeMultipartUpload.

/*
     * (non-Javadoc)
     * @see
     * com.amazonaws.services.s3.AmazonS3#completeMultipartUpload(com.amazonaws
     * .services.s3.model.CompleteMultipartUploadRequest)
     */
@Override
public CompleteMultipartUploadResult completeMultipartUpload(CompleteMultipartUploadRequest completeMultipartUploadRequest) throws AmazonClientException, AmazonServiceException {
    assertParameterNotNull(completeMultipartUploadRequest, "The request parameter must be specified when completing a multipart upload");
    final String bucketName = completeMultipartUploadRequest.getBucketName();
    final String key = completeMultipartUploadRequest.getKey();
    final String uploadId = completeMultipartUploadRequest.getUploadId();
    assertParameterNotNull(bucketName, "The bucket name parameter must be specified when completing a multipart upload");
    assertParameterNotNull(key, "The key parameter must be specified when completing a multipart upload");
    assertParameterNotNull(uploadId, "The upload ID parameter must be specified when completing a multipart upload");
    assertParameterNotNull(completeMultipartUploadRequest.getPartETags(), "The part ETags parameter must be specified when completing a multipart upload");
    int retries = 0;
    CompleteMultipartUploadHandler handler;
    do {
        final Request<CompleteMultipartUploadRequest> request = createRequest(bucketName, key, completeMultipartUploadRequest, HttpMethodName.POST);
        request.addParameter("uploadId", uploadId);
        populateRequesterPaysHeader(request, completeMultipartUploadRequest.isRequesterPays());
        final byte[] xml = RequestXmlFactory.convertToXmlByteArray(completeMultipartUploadRequest.getPartETags());
        request.addHeader("Content-Type", "application/xml");
        request.addHeader("Content-Length", String.valueOf(xml.length));
        request.setContent(new ByteArrayInputStream(xml));
        @SuppressWarnings("unchecked") final ResponseHeaderHandlerChain<CompleteMultipartUploadHandler> responseHandler = new ResponseHeaderHandlerChain<CompleteMultipartUploadHandler>(// xml payload unmarshaller
        new Unmarshallers.CompleteMultipartUploadResultUnmarshaller(), // header handlers
        new ServerSideEncryptionHeaderHandler<CompleteMultipartUploadHandler>(), new ObjectExpirationHeaderHandler<CompleteMultipartUploadHandler>(), new S3VersionHeaderHandler<CompleteMultipartUploadHandler>(), new S3RequesterChargedHeaderHandler<CompleteMultipartUploadHandler>());
        handler = invoke(request, responseHandler, bucketName, key);
        if (handler.getCompleteMultipartUploadResult() != null) {
            return handler.getCompleteMultipartUploadResult();
        }
    } while (shouldRetryCompleteMultipartUpload(completeMultipartUploadRequest, handler.getAmazonS3Exception(), retries++));
    throw handler.getAmazonS3Exception();
}
Also used : Unmarshallers(com.amazonaws.services.s3.model.transform.Unmarshallers) ByteArrayInputStream(java.io.ByteArrayInputStream) CompleteMultipartUploadHandler(com.amazonaws.services.s3.model.transform.XmlResponsesSaxParser.CompleteMultipartUploadHandler) ResponseHeaderHandlerChain(com.amazonaws.services.s3.internal.ResponseHeaderHandlerChain)

Example 2 with ObjectExpirationHeaderHandler

use of com.amazonaws.services.s3.internal.ObjectExpirationHeaderHandler in project aws-sdk-android by aws-amplify.

the class ExpirationHeaderParsingTest method testParseExpirationHeader1.

@Test
public void testParseExpirationHeader1() {
    MockObjectExpirationResult result = new MockObjectExpirationResult();
    ObjectExpirationHeaderHandler<MockObjectExpirationResult> handler = new ObjectExpirationHeaderHandler<MockObjectExpirationResult>();
    HttpResponse response = HttpResponse.builder().header("x-amz-expiration", "expiry-date=\"Tue, 01 Jan 2013 00:00:00 GMT\", rule-id=\"Test\"").build();
    handler.handle(result, response);
    Assert.assertEquals(1356998400000L, result.getExpirationTime().getTime());
    Assert.assertEquals("Test", result.getExpirationTimeRuleId());
}
Also used : ObjectExpirationHeaderHandler(com.amazonaws.services.s3.internal.ObjectExpirationHeaderHandler) HttpResponse(com.amazonaws.http.HttpResponse) Test(org.junit.Test)

Example 3 with ObjectExpirationHeaderHandler

use of com.amazonaws.services.s3.internal.ObjectExpirationHeaderHandler in project aws-sdk-android by aws-amplify.

the class ExpirationHeaderParsingTest method testParseEmptyExpirationHeader.

@Test
public void testParseEmptyExpirationHeader() {
    MockObjectExpirationResult result = new MockObjectExpirationResult();
    ObjectExpirationHeaderHandler<MockObjectExpirationResult> handler = new ObjectExpirationHeaderHandler<MockObjectExpirationResult>();
    HttpResponse response = HttpResponse.builder().build();
    handler.handle(result, response);
    Assert.assertNull(result.getExpirationTime());
    Assert.assertNull(result.getExpirationTimeRuleId());
}
Also used : ObjectExpirationHeaderHandler(com.amazonaws.services.s3.internal.ObjectExpirationHeaderHandler) HttpResponse(com.amazonaws.http.HttpResponse) Test(org.junit.Test)

Example 4 with ObjectExpirationHeaderHandler

use of com.amazonaws.services.s3.internal.ObjectExpirationHeaderHandler in project aws-sdk-android by aws-amplify.

the class ExpirationHeaderParsingTest method testParseExpirationHeader2.

@Test
public void testParseExpirationHeader2() {
    MockObjectExpirationResult result = new MockObjectExpirationResult();
    ObjectExpirationHeaderHandler<MockObjectExpirationResult> handler = new ObjectExpirationHeaderHandler<MockObjectExpirationResult>();
    HttpResponse response = HttpResponse.builder().header("x-amz-expiration", "rule-id=\"Test\", expiry-date=\"Tue, 01 Jan 2013 00:00:00 GMT\"").build();
    handler.handle(result, response);
    Assert.assertEquals(1356998400000L, result.getExpirationTime().getTime());
    Assert.assertEquals("Test", result.getExpirationTimeRuleId());
}
Also used : ObjectExpirationHeaderHandler(com.amazonaws.services.s3.internal.ObjectExpirationHeaderHandler) HttpResponse(com.amazonaws.http.HttpResponse) Test(org.junit.Test)

Example 5 with ObjectExpirationHeaderHandler

use of com.amazonaws.services.s3.internal.ObjectExpirationHeaderHandler in project aws-sdk-android by aws-amplify.

the class AmazonS3Client method copyObject.

/*
     * (non-Javadoc)
     * @see
     * com.amazonaws.services.s3.AmazonS3#copyObject(com.amazonaws.services.
     * s3.model.CopyObjectRequest)
     */
@Override
public CopyObjectResult copyObject(CopyObjectRequest copyObjectRequest) throws AmazonClientException, AmazonServiceException {
    assertParameterNotNull(copyObjectRequest.getSourceBucketName(), "The source bucket name must be specified when copying an object");
    assertParameterNotNull(copyObjectRequest.getSourceKey(), "The source object key must be specified when copying an object");
    assertParameterNotNull(copyObjectRequest.getDestinationBucketName(), "The destination bucket name must be specified when copying an object");
    assertParameterNotNull(copyObjectRequest.getDestinationKey(), "The destination object key must be specified when copying an object");
    final String destinationKey = copyObjectRequest.getDestinationKey();
    final String destinationBucketName = copyObjectRequest.getDestinationBucketName();
    final Request<CopyObjectRequest> request = createRequest(destinationBucketName, destinationKey, copyObjectRequest, HttpMethodName.PUT);
    populateRequestWithCopyObjectParameters(request, copyObjectRequest);
    // Populate the SSE AWS KMS parameters to the request header
    populateSSE_KMS(request, copyObjectRequest.getSSEAwsKeyManagementParams());
    /*
         * 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") final ResponseHeaderHandlerChain<CopyObjectResultHandler> handler = new ResponseHeaderHandlerChain<CopyObjectResultHandler>(// xml payload unmarshaller
        new Unmarshallers.CopyObjectUnmarshaller(), // header handlers
        new ServerSideEncryptionHeaderHandler<CopyObjectResultHandler>(), new S3VersionHeaderHandler<CopyObjectResultHandler>(), new ObjectExpirationHeaderHandler<CopyObjectResultHandler>(), new S3RequesterChargedHeaderHandler<CopyObjectResultHandler>());
        copyObjectResultHandler = invoke(request, handler, destinationBucketName, destinationKey);
    } catch (final AmazonS3Exception ase) {
        /*
             * 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 (ase.getStatusCode() == Constants.FAILED_PRECONDITION_STATUS_CODE) {
            return null;
        }
        throw ase;
    }
    /*
         * 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) {
        final String errorCode = copyObjectResultHandler.getErrorCode();
        final String errorMessage = copyObjectResultHandler.getErrorMessage();
        final String requestId = copyObjectResultHandler.getErrorRequestId();
        final String hostId = copyObjectResultHandler.getErrorHostId();
        final AmazonS3Exception ase = new AmazonS3Exception(errorMessage);
        ase.setErrorCode(errorCode);
        ase.setErrorType(ErrorType.Service);
        ase.setRequestId(requestId);
        ase.setExtendedRequestId(hostId);
        ase.setServiceName(request.getServiceName());
        ase.setStatusCode(200);
        throw ase;
    }
    // TODO: Might be nice to create this in our custom
    // S3VersionHeaderHandler
    final 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.setRequesterCharged(copyObjectResultHandler.isRequesterCharged());
    return copyObjectResult;
}
Also used : Unmarshallers(com.amazonaws.services.s3.model.transform.Unmarshallers) CopyObjectResultHandler(com.amazonaws.services.s3.model.transform.XmlResponsesSaxParser.CopyObjectResultHandler) ResponseHeaderHandlerChain(com.amazonaws.services.s3.internal.ResponseHeaderHandlerChain)

Aggregations

HttpResponse (com.amazonaws.http.HttpResponse)3 ObjectExpirationHeaderHandler (com.amazonaws.services.s3.internal.ObjectExpirationHeaderHandler)3 Test (org.junit.Test)3 ResponseHeaderHandlerChain (com.amazonaws.services.s3.internal.ResponseHeaderHandlerChain)2 Unmarshallers (com.amazonaws.services.s3.model.transform.Unmarshallers)2 CompleteMultipartUploadHandler (com.amazonaws.services.s3.model.transform.XmlResponsesSaxParser.CompleteMultipartUploadHandler)1 CopyObjectResultHandler (com.amazonaws.services.s3.model.transform.XmlResponsesSaxParser.CopyObjectResultHandler)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1