1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package org.apache.commons.httpclient;
31
32 import java.io.IOException;
33 import java.io.InterruptedIOException;
34
35 /***
36 * The default {@link HttpMethodRetryHandler} used by {@link HttpMethod}s.
37 *
38 * @author Michael Becke
39 * @author <a href="mailto:oleg -at- ural.ru">Oleg Kalnichevski</a>
40 */
41 public class DefaultHttpMethodRetryHandler implements HttpMethodRetryHandler {
42
43
44 private static Class SSL_HANDSHAKE_EXCEPTION = null;
45
46 static {
47 try {
48 SSL_HANDSHAKE_EXCEPTION = Class.forName("javax.net.ssl.SSLHandshakeException");
49 } catch (ClassNotFoundException ignore) {
50 }
51 }
52 /*** the number of times a method will be retried */
53 private int retryCount;
54
55 /*** Whether or not methods that have successfully sent their request will be retried */
56 private boolean requestSentRetryEnabled;
57
58 /***
59 * Default constructor
60 */
61 public DefaultHttpMethodRetryHandler(int retryCount, boolean requestSentRetryEnabled) {
62 super();
63 this.retryCount = retryCount;
64 this.requestSentRetryEnabled = requestSentRetryEnabled;
65 }
66
67 /***
68 * Default constructor
69 */
70 public DefaultHttpMethodRetryHandler() {
71 this(3, false);
72 }
73 /***
74 * Used <code>retryCount</code> and <code>requestSentRetryEnabled</code> to determine
75 * if the given method should be retried.
76 *
77 * @see HttpMethodRetryHandler#retryMethod(HttpMethod, IOException, int)
78 */
79 public boolean retryMethod(
80 final HttpMethod method,
81 final IOException exception,
82 int executionCount) {
83 if (method == null) {
84 throw new IllegalArgumentException("HTTP method may not be null");
85 }
86 if (exception == null) {
87 throw new IllegalArgumentException("Exception parameter may not be null");
88 }
89 if (executionCount > this.retryCount) {
90
91 return false;
92 }
93 if (exception instanceof NoHttpResponseException) {
94
95 return true;
96 }
97 if (exception instanceof InterruptedIOException) {
98
99 return false;
100 }
101 if (SSL_HANDSHAKE_EXCEPTION != null && SSL_HANDSHAKE_EXCEPTION.isInstance(exception)) {
102
103 return false;
104 }
105 if (!method.isRequestSent() || this.requestSentRetryEnabled) {
106
107
108 return true;
109 }
110
111 return false;
112 }
113
114 /***
115 * @return <code>true</code> if this handler will retry methods that have
116 * successfully sent their request, <code>false</code> otherwise
117 */
118 public boolean isRequestSentRetryEnabled() {
119 return requestSentRetryEnabled;
120 }
121
122 /***
123 * @return the maximum number of times a method will be retried
124 */
125 public int getRetryCount() {
126 return retryCount;
127 }
128 }