| 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.inject.Inject; |
| 24 | import javax.inject.Provider; |
| 25 | import javax.inject.Singleton; |
| 26 | import javax.ws.rs.HttpMethod; |
| 27 | import javax.ws.rs.core.HttpHeaders; |
| 28 | import javax.ws.rs.core.UriBuilder; |
| 29 | |
| 30 | import org.jclouds.aws.domain.AWSError; |
| 31 | import org.jclouds.aws.util.AWSUtils; |
| 32 | import org.jclouds.http.HttpCommand; |
| 33 | import org.jclouds.http.HttpResponse; |
| 34 | import org.jclouds.http.handlers.BackoffLimitedRetryHandler; |
| 35 | import org.jclouds.http.handlers.RedirectionRetryHandler; |
| 36 | |
| 37 | /** |
| 38 | * Handles Retryable responses with error codes in the 3xx range |
| 39 | * |
| 40 | * @author Adrian Cole |
| 41 | */ |
| 42 | @Singleton |
| 43 | public class AWSRedirectionRetryHandler extends RedirectionRetryHandler { |
| 44 | private final AWSUtils utils; |
| 45 | |
| 46 | @Inject |
| 47 | public AWSRedirectionRetryHandler(Provider<UriBuilder> uriBuilderProvider, |
| 48 | BackoffLimitedRetryHandler backoffHandler, AWSUtils utils) { |
| 49 | super(uriBuilderProvider, backoffHandler); |
| 50 | this.utils = utils; |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public boolean shouldRetryRequest(HttpCommand command, HttpResponse response) { |
| 55 | if (response.getFirstHeaderOrNull(HttpHeaders.LOCATION) == null |
| 56 | && (response.getStatusCode() == 301 || response.getStatusCode() == 307)) { |
| 57 | if (command.getCurrentRequest().getMethod() == HttpMethod.HEAD) { |
| 58 | command.setCurrentRequest(command.getCurrentRequest().toBuilder().method("GET").build()); |
| 59 | return true; |
| 60 | } else { |
| 61 | command.incrementRedirectCount(); |
| 62 | closeClientButKeepContentStream(response); |
| 63 | AWSError error = utils.parseAWSErrorFromContent(command.getCurrentRequest(), response); |
| 64 | String host = error.getDetails().get("Endpoint"); |
| 65 | if (host != null) { |
| 66 | if (host.equals(command.getCurrentRequest().getEndpoint().getHost())) { |
| 67 | // must be an amazon error related to |
| 68 | // http://developer.amazonwebservices.com/connect/thread.jspa?messageID=72287𑩟 |
| 69 | return backoffHandler.shouldRetryRequest(command, response); |
| 70 | } else { |
| 71 | UriBuilder builder = uriBuilderProvider.get().uri(command.getCurrentRequest().getEndpoint()); |
| 72 | builder.host(host); |
| 73 | command.setCurrentRequest(command.getCurrentRequest().toBuilder().endpoint(builder.build()).build()); |
| 74 | } |
| 75 | return true; |
| 76 | } else { |
| 77 | return false; |
| 78 | } |
| 79 | } |
| 80 | } else { |
| 81 | return super.shouldRetryRequest(command, response); |
| 82 | } |
| 83 | } |
| 84 | } |