Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
1 /*
2 * ====================================================================
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 * ====================================================================
20 *
21 * This software consists of voluntary contributions made by many
22 * individuals on behalf of the Apache Software Foundation. For more
23 * information on the Apache Software Foundation, please see
24 * <http://www.apache.org/>.
25 *
26 */
28 package ch.boye.httpclientandroidlib.impl.client;
30 import java.io.IOException;
31 import java.io.InterruptedIOException;
32 import java.net.ConnectException;
33 import java.net.UnknownHostException;
35 import javax.net.ssl.SSLException;
37 import ch.boye.httpclientandroidlib.annotation.Immutable;
39 import ch.boye.httpclientandroidlib.HttpEntityEnclosingRequest;
40 import ch.boye.httpclientandroidlib.HttpRequest;
41 import ch.boye.httpclientandroidlib.client.HttpRequestRetryHandler;
42 import ch.boye.httpclientandroidlib.protocol.HttpContext;
43 import ch.boye.httpclientandroidlib.protocol.ExecutionContext;
45 /**
46 * The default {@link HttpRequestRetryHandler} used by request executors.
47 *
48 *
49 * @since 4.0
50 */
51 @Immutable
52 public class DefaultHttpRequestRetryHandler implements HttpRequestRetryHandler {
54 /** the number of times a method will be retried */
55 private final int retryCount;
57 /** Whether or not methods that have successfully sent their request will be retried */
58 private final boolean requestSentRetryEnabled;
60 /**
61 * Default constructor
62 */
63 public DefaultHttpRequestRetryHandler(int retryCount, boolean requestSentRetryEnabled) {
64 super();
65 this.retryCount = retryCount;
66 this.requestSentRetryEnabled = requestSentRetryEnabled;
67 }
69 /**
70 * Default constructor
71 */
72 public DefaultHttpRequestRetryHandler() {
73 this(3, false);
74 }
75 /**
76 * Used <code>retryCount</code> and <code>requestSentRetryEnabled</code> to determine
77 * if the given method should be retried.
78 */
79 public boolean retryRequest(
80 final IOException exception,
81 int executionCount,
82 final HttpContext context) {
83 if (exception == null) {
84 throw new IllegalArgumentException("Exception parameter may not be null");
85 }
86 if (context == null) {
87 throw new IllegalArgumentException("HTTP context may not be null");
88 }
89 if (executionCount > this.retryCount) {
90 // Do not retry if over max retry count
91 return false;
92 }
93 if (exception instanceof InterruptedIOException) {
94 // Timeout
95 return false;
96 }
97 if (exception instanceof UnknownHostException) {
98 // Unknown host
99 return false;
100 }
101 if (exception instanceof ConnectException) {
102 // Connection refused
103 return false;
104 }
105 if (exception instanceof SSLException) {
106 // SSL handshake exception
107 return false;
108 }
110 HttpRequest request = (HttpRequest)
111 context.getAttribute(ExecutionContext.HTTP_REQUEST);
112 if (handleAsIdempotent(request)) {
113 // Retry if the request is considered idempotent
114 return true;
115 }
117 Boolean b = (Boolean)
118 context.getAttribute(ExecutionContext.HTTP_REQ_SENT);
119 boolean sent = (b != null && b.booleanValue());
121 if (!sent || this.requestSentRetryEnabled) {
122 // Retry if the request has not been sent fully or
123 // if it's OK to retry methods that have been sent
124 return true;
125 }
126 // otherwise do not retry
127 return false;
128 }
130 /**
131 * @return <code>true</code> if this handler will retry methods that have
132 * successfully sent their request, <code>false</code> otherwise
133 */
134 public boolean isRequestSentRetryEnabled() {
135 return requestSentRetryEnabled;
136 }
138 /**
139 * @return the maximum number of times a method will be retried
140 */
141 public int getRetryCount() {
142 return retryCount;
143 }
145 private boolean handleAsIdempotent(final HttpRequest request) {
146 return !(request instanceof HttpEntityEnclosingRequest);
147 }
149 }