michael@0: /* michael@0: * ==================================================================== michael@0: * Licensed to the Apache Software Foundation (ASF) under one michael@0: * or more contributor license agreements. See the NOTICE file michael@0: * distributed with this work for additional information michael@0: * regarding copyright ownership. The ASF licenses this file michael@0: * to you under the Apache License, Version 2.0 (the michael@0: * "License"); you may not use this file except in compliance michael@0: * with the License. You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, michael@0: * software distributed under the License is distributed on an michael@0: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY michael@0: * KIND, either express or implied. See the License for the michael@0: * specific language governing permissions and limitations michael@0: * under the License. michael@0: * ==================================================================== michael@0: * michael@0: * This software consists of voluntary contributions made by many michael@0: * individuals on behalf of the Apache Software Foundation. For more michael@0: * information on the Apache Software Foundation, please see michael@0: * . michael@0: * michael@0: */ michael@0: michael@0: package ch.boye.httpclientandroidlib.impl.client; michael@0: michael@0: import java.io.IOException; michael@0: import java.io.InterruptedIOException; michael@0: import java.net.ConnectException; michael@0: import java.net.UnknownHostException; michael@0: michael@0: import javax.net.ssl.SSLException; michael@0: michael@0: import ch.boye.httpclientandroidlib.annotation.Immutable; michael@0: michael@0: import ch.boye.httpclientandroidlib.HttpEntityEnclosingRequest; michael@0: import ch.boye.httpclientandroidlib.HttpRequest; michael@0: import ch.boye.httpclientandroidlib.client.HttpRequestRetryHandler; michael@0: import ch.boye.httpclientandroidlib.protocol.HttpContext; michael@0: import ch.boye.httpclientandroidlib.protocol.ExecutionContext; michael@0: michael@0: /** michael@0: * The default {@link HttpRequestRetryHandler} used by request executors. michael@0: * michael@0: * michael@0: * @since 4.0 michael@0: */ michael@0: @Immutable michael@0: public class DefaultHttpRequestRetryHandler implements HttpRequestRetryHandler { michael@0: michael@0: /** the number of times a method will be retried */ michael@0: private final int retryCount; michael@0: michael@0: /** Whether or not methods that have successfully sent their request will be retried */ michael@0: private final boolean requestSentRetryEnabled; michael@0: michael@0: /** michael@0: * Default constructor michael@0: */ michael@0: public DefaultHttpRequestRetryHandler(int retryCount, boolean requestSentRetryEnabled) { michael@0: super(); michael@0: this.retryCount = retryCount; michael@0: this.requestSentRetryEnabled = requestSentRetryEnabled; michael@0: } michael@0: michael@0: /** michael@0: * Default constructor michael@0: */ michael@0: public DefaultHttpRequestRetryHandler() { michael@0: this(3, false); michael@0: } michael@0: /** michael@0: * Used retryCount and requestSentRetryEnabled to determine michael@0: * if the given method should be retried. michael@0: */ michael@0: public boolean retryRequest( michael@0: final IOException exception, michael@0: int executionCount, michael@0: final HttpContext context) { michael@0: if (exception == null) { michael@0: throw new IllegalArgumentException("Exception parameter may not be null"); michael@0: } michael@0: if (context == null) { michael@0: throw new IllegalArgumentException("HTTP context may not be null"); michael@0: } michael@0: if (executionCount > this.retryCount) { michael@0: // Do not retry if over max retry count michael@0: return false; michael@0: } michael@0: if (exception instanceof InterruptedIOException) { michael@0: // Timeout michael@0: return false; michael@0: } michael@0: if (exception instanceof UnknownHostException) { michael@0: // Unknown host michael@0: return false; michael@0: } michael@0: if (exception instanceof ConnectException) { michael@0: // Connection refused michael@0: return false; michael@0: } michael@0: if (exception instanceof SSLException) { michael@0: // SSL handshake exception michael@0: return false; michael@0: } michael@0: michael@0: HttpRequest request = (HttpRequest) michael@0: context.getAttribute(ExecutionContext.HTTP_REQUEST); michael@0: if (handleAsIdempotent(request)) { michael@0: // Retry if the request is considered idempotent michael@0: return true; michael@0: } michael@0: michael@0: Boolean b = (Boolean) michael@0: context.getAttribute(ExecutionContext.HTTP_REQ_SENT); michael@0: boolean sent = (b != null && b.booleanValue()); michael@0: michael@0: if (!sent || this.requestSentRetryEnabled) { michael@0: // Retry if the request has not been sent fully or michael@0: // if it's OK to retry methods that have been sent michael@0: return true; michael@0: } michael@0: // otherwise do not retry michael@0: return false; michael@0: } michael@0: michael@0: /** michael@0: * @return true if this handler will retry methods that have michael@0: * successfully sent their request, false otherwise michael@0: */ michael@0: public boolean isRequestSentRetryEnabled() { michael@0: return requestSentRetryEnabled; michael@0: } michael@0: michael@0: /** michael@0: * @return the maximum number of times a method will be retried michael@0: */ michael@0: public int getRetryCount() { michael@0: return retryCount; michael@0: } michael@0: michael@0: private boolean handleAsIdempotent(final HttpRequest request) { michael@0: return !(request instanceof HttpEntityEnclosingRequest); michael@0: } michael@0: michael@0: }