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.http.handlers; |
20 | |
21 | import static java.util.Collections.singletonList; |
22 | import static javax.ws.rs.core.HttpHeaders.HOST; |
23 | import static org.jclouds.http.HttpUtils.closeClientButKeepContentStream; |
24 | |
25 | import java.net.URI; |
26 | |
27 | import javax.annotation.Resource; |
28 | import javax.inject.Named; |
29 | import javax.inject.Provider; |
30 | import javax.inject.Singleton; |
31 | import javax.ws.rs.core.HttpHeaders; |
32 | import javax.ws.rs.core.UriBuilder; |
33 | |
34 | import org.jclouds.Constants; |
35 | import org.jclouds.http.HttpCommand; |
36 | import org.jclouds.http.HttpRequest; |
37 | import org.jclouds.http.HttpResponse; |
38 | import org.jclouds.http.HttpRetryHandler; |
39 | import org.jclouds.http.utils.ModifyRequest; |
40 | import org.jclouds.logging.Logger; |
41 | |
42 | import com.google.inject.Inject; |
43 | |
44 | /** |
45 | * Handles Retryable responses with error codes in the 3xx range |
46 | * |
47 | * @author Adrian Cole |
48 | */ |
49 | @Singleton |
50 | public class RedirectionRetryHandler implements HttpRetryHandler { |
51 | @Inject(optional = true) |
52 | @Named(Constants.PROPERTY_MAX_REDIRECTS) |
53 | protected int retryCountLimit = 5; |
54 | |
55 | @Resource |
56 | protected Logger logger = Logger.NULL; |
57 | |
58 | protected final BackoffLimitedRetryHandler backoffHandler; |
59 | protected final Provider<UriBuilder> uriBuilderProvider; |
60 | |
61 | @Inject |
62 | protected RedirectionRetryHandler(Provider<UriBuilder> uriBuilderProvider, BackoffLimitedRetryHandler backoffHandler) { |
63 | this.backoffHandler = backoffHandler; |
64 | this.uriBuilderProvider = uriBuilderProvider; |
65 | } |
66 | |
67 | public boolean shouldRetryRequest(HttpCommand command, HttpResponse response) { |
68 | closeClientButKeepContentStream(response); |
69 | String hostHeader = response.getFirstHeaderOrNull(HttpHeaders.LOCATION); |
70 | if (command.incrementRedirectCount() < retryCountLimit && hostHeader != null) { |
71 | URI redirectionUrl = URI.create(hostHeader); |
72 | |
73 | // if you are sent the same uri, assume there's a transient problem and retry. |
74 | HttpRequest currentRequest = command.getCurrentRequest(); |
75 | if (redirectionUrl.equals(currentRequest.getEndpoint())) |
76 | return backoffHandler.shouldRetryRequest(command, response); |
77 | |
78 | assert redirectionUrl.getPath() != null : "no path in redirect header from: " + response; |
79 | if (!redirectionUrl.isAbsolute()) { |
80 | UriBuilder builder = uriBuilderProvider.get().uri(currentRequest.getEndpoint()); |
81 | builder.replacePath(redirectionUrl.getPath()); |
82 | if (redirectionUrl.getQuery() != null) |
83 | builder.replaceQuery(redirectionUrl.getQuery()); |
84 | redirectionUrl = builder.build(); |
85 | } |
86 | |
87 | if (currentRequest.getFirstHeaderOrNull(HOST) != null && redirectionUrl.getHost() != null) { |
88 | command.setCurrentRequest(ModifyRequest.replaceHeader(currentRequest, HOST, |
89 | singletonList(redirectionUrl.getHost())).toBuilder().endpoint(redirectionUrl).build()); |
90 | } else { |
91 | command.setCurrentRequest(currentRequest.toBuilder().endpoint(redirectionUrl).build()); |
92 | } |
93 | return true; |
94 | } else { |
95 | return false; |
96 | } |
97 | } |
98 | |
99 | } |