| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.jclouds.aws.handlers; |
| 20 | |
| 21 | import static org.jclouds.http.HttpUtils.closeClientButKeepContentStream; |
| 22 | |
| 23 | import javax.annotation.Resource; |
| 24 | import javax.inject.Named; |
| 25 | |
| 26 | import org.jclouds.Constants; |
| 27 | import org.jclouds.aws.domain.AWSError; |
| 28 | import org.jclouds.aws.util.AWSUtils; |
| 29 | import org.jclouds.http.HttpCommand; |
| 30 | import org.jclouds.http.HttpResponse; |
| 31 | import org.jclouds.http.HttpRetryHandler; |
| 32 | import org.jclouds.logging.Logger; |
| 33 | |
| 34 | import com.google.inject.Inject; |
| 35 | |
| 36 | /** |
| 37 | * Handles Retryable responses with error codes in the 4xx range |
| 38 | * |
| 39 | * @author Adrian Cole |
| 40 | */ |
| 41 | public class AWSClientErrorRetryHandler implements HttpRetryHandler { |
| 42 | |
| 43 | @Inject(optional = true) |
| 44 | @Named(Constants.PROPERTY_MAX_RETRIES) |
| 45 | private int retryCountLimit = 5; |
| 46 | |
| 47 | private final AWSUtils utils; |
| 48 | |
| 49 | @Resource |
| 50 | protected Logger logger = Logger.NULL; |
| 51 | |
| 52 | @Inject |
| 53 | public AWSClientErrorRetryHandler(AWSUtils utils) { |
| 54 | this.utils = utils; |
| 55 | } |
| 56 | |
| 57 | public boolean shouldRetryRequest(HttpCommand command, HttpResponse response) { |
| 58 | if (command.getFailureCount() > retryCountLimit) |
| 59 | return false; |
| 60 | if (response.getStatusCode() == 400 || response.getStatusCode() == 403 |
| 61 | || response.getStatusCode() == 409) { |
| 62 | command.incrementFailureCount(); |
| 63 | // Content can be null in the case of HEAD requests |
| 64 | if (response.getPayload() != null) { |
| 65 | closeClientButKeepContentStream(response); |
| 66 | AWSError error = utils.parseAWSErrorFromContent(command.getCurrentRequest(), response); |
| 67 | if (error != null |
| 68 | && ("RequestTimeout".equals(error.getCode()) |
| 69 | || "OperationAborted".equals(error.getCode()) || "SignatureDoesNotMatch" |
| 70 | .equals(error.getCode()))) { |
| 71 | return true; |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | } |