001: /*
002: * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/DefaultHttpMethodRetryHandler.java,v 1.3 2004/12/20 11:47:46 olegk Exp $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: *
006: * ====================================================================
007: *
008: * Licensed to the Apache Software Foundation (ASF) under one or more
009: * contributor license agreements. See the NOTICE file distributed with
010: * this work for additional information regarding copyright ownership.
011: * The ASF licenses this file to You under the Apache License, Version 2.0
012: * (the "License"); you may not use this file except in compliance with
013: * the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing, software
018: * distributed under the License is distributed on an "AS IS" BASIS,
019: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020: * See the License for the specific language governing permissions and
021: * limitations under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: */
030:
031: package org.apache.commons.httpclient;
032:
033: import java.io.IOException;
034: import java.io.InterruptedIOException;
035: import java.net.NoRouteToHostException;
036: import java.net.UnknownHostException;
037:
038: /**
039: * The default {@link HttpMethodRetryHandler} used by {@link HttpMethod}s.
040: *
041: * @author Michael Becke
042: * @author <a href="mailto:oleg -at- ural.ru">Oleg Kalnichevski</a>
043: */
044: public class DefaultHttpMethodRetryHandler implements
045: HttpMethodRetryHandler {
046:
047: private static Class SSL_HANDSHAKE_EXCEPTION = null;
048:
049: static {
050: try {
051: SSL_HANDSHAKE_EXCEPTION = Class
052: .forName("javax.net.ssl.SSLHandshakeException");
053: } catch (ClassNotFoundException ignore) {
054: }
055: }
056: /** the number of times a method will be retried */
057: private int retryCount;
058:
059: /** Whether or not methods that have successfully sent their request will be retried */
060: private boolean requestSentRetryEnabled;
061:
062: /**
063: * Creates a new DefaultHttpMethodRetryHandler.
064: * @param retryCount the number of times a method will be retried
065: * @param requestSentRetryEnabled if true, methods that have successfully sent their request will be retried
066: */
067: public DefaultHttpMethodRetryHandler(int retryCount,
068: boolean requestSentRetryEnabled) {
069: super ();
070: this .retryCount = retryCount;
071: this .requestSentRetryEnabled = requestSentRetryEnabled;
072: }
073:
074: /**
075: * Creates a new DefaultHttpMethodRetryHandler that retries up to 3 times
076: * but does not retry methods that have successfully sent their requests.
077: */
078: public DefaultHttpMethodRetryHandler() {
079: this (3, false);
080: }
081:
082: /**
083: * Used <code>retryCount</code> and <code>requestSentRetryEnabled</code> to determine
084: * if the given method should be retried.
085: *
086: * @see HttpMethodRetryHandler#retryMethod(HttpMethod, IOException, int)
087: */
088: public boolean retryMethod(final HttpMethod method,
089: final IOException exception, int executionCount) {
090: if (method == null) {
091: throw new IllegalArgumentException(
092: "HTTP method may not be null");
093: }
094: if (exception == null) {
095: throw new IllegalArgumentException(
096: "Exception parameter may not be null");
097: }
098: // HttpMethod interface is the WORST thing ever done to HttpClient
099: if (method instanceof HttpMethodBase) {
100: if (((HttpMethodBase) method).isAborted()) {
101: return false;
102: }
103: }
104: if (executionCount > this .retryCount) {
105: // Do not retry if over max retry count
106: return false;
107: }
108: if (exception instanceof NoHttpResponseException) {
109: // Retry if the server dropped connection on us
110: return true;
111: }
112: if (exception instanceof InterruptedIOException) {
113: // Timeout
114: return false;
115: }
116: if (exception instanceof UnknownHostException) {
117: // Unknown host
118: return false;
119: }
120: if (exception instanceof NoRouteToHostException) {
121: // Host unreachable
122: return false;
123: }
124: if (SSL_HANDSHAKE_EXCEPTION != null
125: && SSL_HANDSHAKE_EXCEPTION.isInstance(exception)) {
126: // SSL handshake exception
127: return false;
128: }
129: if (!method.isRequestSent() || this .requestSentRetryEnabled) {
130: // Retry if the request has not been sent fully or
131: // if it's OK to retry methods that have been sent
132: return true;
133: }
134: // otherwise do not retry
135: return false;
136: }
137:
138: /**
139: * @return <code>true</code> if this handler will retry methods that have
140: * successfully sent their request, <code>false</code> otherwise
141: */
142: public boolean isRequestSentRetryEnabled() {
143: return requestSentRetryEnabled;
144: }
145:
146: /**
147: * @return the maximum number of times a method will be retried
148: */
149: public int getRetryCount() {
150: return retryCount;
151: }
152: }
|