1 /*
2  * Copyright 2015-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  *  http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */

15 package com.amazonaws.services.s3.internal;
16
17 import com.amazonaws.AmazonClientException;
18 import com.amazonaws.AmazonWebServiceRequest;
19 import com.amazonaws.annotation.SdkTestInternalApi;
20 import com.amazonaws.internal.SdkPredicate;
21 import com.amazonaws.retry.RetryPolicy;
22 import com.amazonaws.services.s3.model.AmazonS3Exception;
23 import com.amazonaws.util.ValidationUtils;
24
25 public class CompleteMultipartUploadRetryCondition implements RetryPolicy.RetryCondition {
26
27     private static final int MAX_RETRY_ATTEMPTS = 3;
28
29     private final SdkPredicate<AmazonS3Exception>
30             completeMultipartRetryablePredicate;
31
32     private final int maxCompleteMultipartUploadRetries;
33
34     public CompleteMultipartUploadRetryCondition() {
35         this(new CompleteMultipartUploadRetryablePredicate(), MAX_RETRY_ATTEMPTS);
36     }
37
38     /**
39      * For testing purposes.
40      */

41     @SdkTestInternalApi
42     CompleteMultipartUploadRetryCondition(SdkPredicate<AmazonS3Exception>
43                                                   predicate, int maxRetryAttempts) {
44         ValidationUtils.assertNotNull(predicate, "sdk predicate");
45         this.completeMultipartRetryablePredicate = predicate;
46         this.maxCompleteMultipartUploadRetries = maxRetryAttempts;
47     }
48
49     @Override
50     public boolean shouldRetry(AmazonWebServiceRequest originalRequest,
51                                AmazonClientException exception, int retriesAttempted) {
52
53         if (exception instanceof AmazonS3Exception) {
54             return completeMultipartRetryablePredicate.test((AmazonS3Exception) exception)
55                     && retriesAttempted < maxCompleteMultipartUploadRetries;
56         }
57         return false;
58     }
59 }
60