1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.jclouds.http.handlers;
20
21 import javax.inject.Singleton;
22
23 import org.jclouds.http.HttpCommand;
24 import org.jclouds.http.HttpResponse;
25 import org.jclouds.http.HttpRetryHandler;
26 import org.jclouds.http.annotation.ClientError;
27 import org.jclouds.http.annotation.Redirection;
28 import org.jclouds.http.annotation.ServerError;
29
30 import com.google.common.annotations.VisibleForTesting;
31 import com.google.inject.Inject;
32
33
34
35
36
37
38
39 @Singleton
40 public class DelegatingRetryHandler implements HttpRetryHandler {
41
42 @VisibleForTesting
43 @Inject(optional = true)
44 @Redirection
45 HttpRetryHandler redirectionRetryHandler;
46
47 @VisibleForTesting
48 @Inject(optional = true)
49 @ClientError
50 HttpRetryHandler clientErrorRetryHandler;
51
52 @VisibleForTesting
53 @Inject(optional = true)
54 @ServerError
55 HttpRetryHandler serverErrorRetryHandler;
56
57 @Inject
58 public DelegatingRetryHandler(BackoffLimitedRetryHandler backOff,
59 RedirectionRetryHandler redirectionRetryHandler) {
60 this.serverErrorRetryHandler = backOff;
61 this.redirectionRetryHandler = redirectionRetryHandler;
62 this.clientErrorRetryHandler = HttpRetryHandler.NEVER_RETRY;
63 }
64
65 public boolean shouldRetryRequest(HttpCommand command, HttpResponse response) {
66 int statusCode = response.getStatusCode();
67 boolean retryRequest = false;
68 if (statusCode >= 300 && statusCode < 400) {
69 retryRequest = redirectionRetryHandler.shouldRetryRequest(command, response);
70 } else if (statusCode >= 400 && statusCode < 500) {
71 retryRequest = clientErrorRetryHandler.shouldRetryRequest(command, response);
72 } else if (statusCode >= 500) {
73 retryRequest = serverErrorRetryHandler.shouldRetryRequest(command, response);
74 }
75 return retryRequest;
76 }
77
78 public HttpRetryHandler getRedirectionRetryHandler() {
79 return redirectionRetryHandler;
80 }
81
82 public HttpRetryHandler getClientErrorRetryHandler() {
83 return clientErrorRetryHandler;
84 }
85
86 public HttpRetryHandler getServerErrorRetryHandler() {
87 return serverErrorRetryHandler;
88 }
89 }