1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/client/DefaultHttpRequestRetryHandler.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,149 @@ 1.4 +/* 1.5 + * ==================================================================== 1.6 + * Licensed to the Apache Software Foundation (ASF) under one 1.7 + * or more contributor license agreements. See the NOTICE file 1.8 + * distributed with this work for additional information 1.9 + * regarding copyright ownership. The ASF licenses this file 1.10 + * to you under the Apache License, Version 2.0 (the 1.11 + * "License"); you may not use this file except in compliance 1.12 + * with the License. You may obtain a copy of the License at 1.13 + * 1.14 + * http://www.apache.org/licenses/LICENSE-2.0 1.15 + * 1.16 + * Unless required by applicable law or agreed to in writing, 1.17 + * software distributed under the License is distributed on an 1.18 + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 1.19 + * KIND, either express or implied. See the License for the 1.20 + * specific language governing permissions and limitations 1.21 + * under the License. 1.22 + * ==================================================================== 1.23 + * 1.24 + * This software consists of voluntary contributions made by many 1.25 + * individuals on behalf of the Apache Software Foundation. For more 1.26 + * information on the Apache Software Foundation, please see 1.27 + * <http://www.apache.org/>. 1.28 + * 1.29 + */ 1.30 + 1.31 +package ch.boye.httpclientandroidlib.impl.client; 1.32 + 1.33 +import java.io.IOException; 1.34 +import java.io.InterruptedIOException; 1.35 +import java.net.ConnectException; 1.36 +import java.net.UnknownHostException; 1.37 + 1.38 +import javax.net.ssl.SSLException; 1.39 + 1.40 +import ch.boye.httpclientandroidlib.annotation.Immutable; 1.41 + 1.42 +import ch.boye.httpclientandroidlib.HttpEntityEnclosingRequest; 1.43 +import ch.boye.httpclientandroidlib.HttpRequest; 1.44 +import ch.boye.httpclientandroidlib.client.HttpRequestRetryHandler; 1.45 +import ch.boye.httpclientandroidlib.protocol.HttpContext; 1.46 +import ch.boye.httpclientandroidlib.protocol.ExecutionContext; 1.47 + 1.48 +/** 1.49 + * The default {@link HttpRequestRetryHandler} used by request executors. 1.50 + * 1.51 + * 1.52 + * @since 4.0 1.53 + */ 1.54 +@Immutable 1.55 +public class DefaultHttpRequestRetryHandler implements HttpRequestRetryHandler { 1.56 + 1.57 + /** the number of times a method will be retried */ 1.58 + private final int retryCount; 1.59 + 1.60 + /** Whether or not methods that have successfully sent their request will be retried */ 1.61 + private final boolean requestSentRetryEnabled; 1.62 + 1.63 + /** 1.64 + * Default constructor 1.65 + */ 1.66 + public DefaultHttpRequestRetryHandler(int retryCount, boolean requestSentRetryEnabled) { 1.67 + super(); 1.68 + this.retryCount = retryCount; 1.69 + this.requestSentRetryEnabled = requestSentRetryEnabled; 1.70 + } 1.71 + 1.72 + /** 1.73 + * Default constructor 1.74 + */ 1.75 + public DefaultHttpRequestRetryHandler() { 1.76 + this(3, false); 1.77 + } 1.78 + /** 1.79 + * Used <code>retryCount</code> and <code>requestSentRetryEnabled</code> to determine 1.80 + * if the given method should be retried. 1.81 + */ 1.82 + public boolean retryRequest( 1.83 + final IOException exception, 1.84 + int executionCount, 1.85 + final HttpContext context) { 1.86 + if (exception == null) { 1.87 + throw new IllegalArgumentException("Exception parameter may not be null"); 1.88 + } 1.89 + if (context == null) { 1.90 + throw new IllegalArgumentException("HTTP context may not be null"); 1.91 + } 1.92 + if (executionCount > this.retryCount) { 1.93 + // Do not retry if over max retry count 1.94 + return false; 1.95 + } 1.96 + if (exception instanceof InterruptedIOException) { 1.97 + // Timeout 1.98 + return false; 1.99 + } 1.100 + if (exception instanceof UnknownHostException) { 1.101 + // Unknown host 1.102 + return false; 1.103 + } 1.104 + if (exception instanceof ConnectException) { 1.105 + // Connection refused 1.106 + return false; 1.107 + } 1.108 + if (exception instanceof SSLException) { 1.109 + // SSL handshake exception 1.110 + return false; 1.111 + } 1.112 + 1.113 + HttpRequest request = (HttpRequest) 1.114 + context.getAttribute(ExecutionContext.HTTP_REQUEST); 1.115 + if (handleAsIdempotent(request)) { 1.116 + // Retry if the request is considered idempotent 1.117 + return true; 1.118 + } 1.119 + 1.120 + Boolean b = (Boolean) 1.121 + context.getAttribute(ExecutionContext.HTTP_REQ_SENT); 1.122 + boolean sent = (b != null && b.booleanValue()); 1.123 + 1.124 + if (!sent || this.requestSentRetryEnabled) { 1.125 + // Retry if the request has not been sent fully or 1.126 + // if it's OK to retry methods that have been sent 1.127 + return true; 1.128 + } 1.129 + // otherwise do not retry 1.130 + return false; 1.131 + } 1.132 + 1.133 + /** 1.134 + * @return <code>true</code> if this handler will retry methods that have 1.135 + * successfully sent their request, <code>false</code> otherwise 1.136 + */ 1.137 + public boolean isRequestSentRetryEnabled() { 1.138 + return requestSentRetryEnabled; 1.139 + } 1.140 + 1.141 + /** 1.142 + * @return the maximum number of times a method will be retried 1.143 + */ 1.144 + public int getRetryCount() { 1.145 + return retryCount; 1.146 + } 1.147 + 1.148 + private boolean handleAsIdempotent(final HttpRequest request) { 1.149 + return !(request instanceof HttpEntityEnclosingRequest); 1.150 + } 1.151 + 1.152 +}