Home | History | Annotate | Download | only in client
      1 /*
      2  * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultHttpRequestRetryHandler.java $
      3  * $Revision: 652726 $
      4  * $Date: 2008-05-01 18:16:51 -0700 (Thu, 01 May 2008) $
      5  *
      6  * ====================================================================
      7  * Licensed to the Apache Software Foundation (ASF) under one
      8  * or more contributor license agreements.  See the NOTICE file
      9  * distributed with this work for additional information
     10  * regarding copyright ownership.  The ASF licenses this file
     11  * to you under the Apache License, Version 2.0 (the
     12  * "License"); you may not use this file except in compliance
     13  * with the License.  You may obtain a copy of the License at
     14  *
     15  *   http://www.apache.org/licenses/LICENSE-2.0
     16  *
     17  * Unless required by applicable law or agreed to in writing,
     18  * software distributed under the License is distributed on an
     19  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     20  * KIND, either express or implied.  See the License for the
     21  * specific language governing permissions and limitations
     22  * under the License.
     23  * ====================================================================
     24  *
     25  * This software consists of voluntary contributions made by many
     26  * individuals on behalf of the Apache Software Foundation.  For more
     27  * information on the Apache Software Foundation, please see
     28  * <http://www.apache.org/>.
     29  *
     30  */
     31 
     32 package org.apache.http.impl.client;
     33 
     34 import java.io.IOException;
     35 import java.io.InterruptedIOException;
     36 import java.net.UnknownHostException;
     37 
     38 import javax.net.ssl.SSLHandshakeException;
     39 
     40 import org.apache.http.NoHttpResponseException;
     41 import org.apache.http.client.HttpRequestRetryHandler;
     42 import org.apache.http.protocol.HttpContext;
     43 import org.apache.http.protocol.ExecutionContext;
     44 
     45 /**
     46  * The default {@link HttpRequestRetryHandler} used by request executors.
     47  *
     48  * @author Michael Becke
     49  * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
     50  *
     51  * @deprecated Please use {@link java.net.URL#openConnection} instead.
     52  *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
     53  *     for further details.
     54  */
     55 @Deprecated
     56 public class DefaultHttpRequestRetryHandler implements HttpRequestRetryHandler {
     57 
     58     /** the number of times a method will be retried */
     59     private final int retryCount;
     60 
     61     /** Whether or not methods that have successfully sent their request will be retried */
     62     private final boolean requestSentRetryEnabled;
     63 
     64     /**
     65      * Default constructor
     66      */
     67     public DefaultHttpRequestRetryHandler(int retryCount, boolean requestSentRetryEnabled) {
     68         super();
     69         this.retryCount = retryCount;
     70         this.requestSentRetryEnabled = requestSentRetryEnabled;
     71     }
     72 
     73     /**
     74      * Default constructor
     75      */
     76     public DefaultHttpRequestRetryHandler() {
     77         this(3, false);
     78     }
     79     /**
     80      * Used <code>retryCount</code> and <code>requestSentRetryEnabled</code> to determine
     81      * if the given method should be retried.
     82      */
     83     public boolean retryRequest(
     84             final IOException exception,
     85             int executionCount,
     86             final HttpContext context) {
     87         if (exception == null) {
     88             throw new IllegalArgumentException("Exception parameter may not be null");
     89         }
     90         if (context == null) {
     91             throw new IllegalArgumentException("HTTP context may not be null");
     92         }
     93         if (executionCount > this.retryCount) {
     94             // Do not retry if over max retry count
     95             return false;
     96         }
     97         if (exception instanceof NoHttpResponseException) {
     98             // Retry if the server dropped connection on us
     99             return true;
    100         }
    101         if (exception instanceof InterruptedIOException) {
    102             // Timeout
    103             return false;
    104         }
    105         if (exception instanceof UnknownHostException) {
    106             // Unknown host
    107             return false;
    108         }
    109         if (exception instanceof SSLHandshakeException) {
    110             // SSL handshake exception
    111             return false;
    112         }
    113         Boolean b = (Boolean)
    114             context.getAttribute(ExecutionContext.HTTP_REQ_SENT);
    115         boolean sent = (b != null && b.booleanValue());
    116         if (!sent || this.requestSentRetryEnabled) {
    117             // Retry if the request has not been sent fully or
    118             // if it's OK to retry methods that have been sent
    119             return true;
    120         }
    121         // otherwise do not retry
    122         return false;
    123     }
    124 
    125     /**
    126      * @return <code>true</code> if this handler will retry methods that have
    127      * successfully sent their request, <code>false</code> otherwise
    128      */
    129     public boolean isRequestSentRetryEnabled() {
    130         return requestSentRetryEnabled;
    131     }
    132 
    133     /**
    134      * @return the maximum number of times a method will be retried
    135      */
    136     public int getRetryCount() {
    137         return retryCount;
    138     }
    139 }
    140