1. /*
  2. * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/DefaultHttpMethodRetryHandler.java,v 1.2 2004/09/18 11:34:12 olegk Exp $
  3. * $Revision: 1.2 $
  4. * $Date: 2004/09/18 11:34:12 $
  5. *
  6. * ====================================================================
  7. *
  8. * Copyright 1999-2004 The Apache Software Foundation
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. * ====================================================================
  22. *
  23. * This software consists of voluntary contributions made by many
  24. * individuals on behalf of the Apache Software Foundation. For more
  25. * information on the Apache Software Foundation, please see
  26. * <http://www.apache.org/>.
  27. *
  28. */
  29. package org.apache.commons.httpclient;
  30. import java.io.IOException;
  31. import java.io.InterruptedIOException;
  32. /**
  33. * The default {@link HttpMethodRetryHandler} used by {@link HttpMethod}s.
  34. *
  35. * @author Michael Becke
  36. * @author <a href="mailto:oleg -at- ural.ru">Oleg Kalnichevski</a>
  37. */
  38. public class DefaultHttpMethodRetryHandler implements HttpMethodRetryHandler {
  39. private static Class SSL_HANDSHAKE_EXCEPTION = null;
  40. static {
  41. try {
  42. SSL_HANDSHAKE_EXCEPTION = Class.forName("javax.net.ssl.SSLHandshakeException");
  43. } catch (ClassNotFoundException ignore) {
  44. }
  45. }
  46. /** the number of times a method will be retried */
  47. private int retryCount;
  48. /** Whether or not methods that have successfully sent their request will be retried */
  49. private boolean requestSentRetryEnabled;
  50. /**
  51. * Default constructor
  52. */
  53. public DefaultHttpMethodRetryHandler(int retryCount, boolean requestSentRetryEnabled) {
  54. super();
  55. this.retryCount = retryCount;
  56. this.requestSentRetryEnabled = requestSentRetryEnabled;
  57. }
  58. /**
  59. * Default constructor
  60. */
  61. public DefaultHttpMethodRetryHandler() {
  62. this(3, false);
  63. }
  64. /**
  65. * Used <code>retryCount</code> and <code>requestSentRetryEnabled</code> to determine
  66. * if the given method should be retried.
  67. *
  68. * @see HttpMethodRetryHandler#retryMethod(HttpMethod, IOException, int)
  69. */
  70. public boolean retryMethod(
  71. final HttpMethod method,
  72. final IOException exception,
  73. int executionCount) {
  74. if (method == null) {
  75. throw new IllegalArgumentException("HTTP method may not be null");
  76. }
  77. if (exception == null) {
  78. throw new IllegalArgumentException("Exception parameter may not be null");
  79. }
  80. if (executionCount >= this.retryCount) {
  81. // Do not retry if over max retry count
  82. return false;
  83. }
  84. if (exception instanceof NoHttpResponseException) {
  85. // Retry if the server dropped connection on us
  86. return true;
  87. }
  88. if (exception instanceof InterruptedIOException) {
  89. // Timeout
  90. return false;
  91. }
  92. if (SSL_HANDSHAKE_EXCEPTION != null && SSL_HANDSHAKE_EXCEPTION.isInstance(exception)) {
  93. // SSL handshake exception
  94. return false;
  95. }
  96. if (!method.isRequestSent() || this.requestSentRetryEnabled) {
  97. // Retry if the request has not been sent fully or
  98. // if it's OK to retry methods that have been sent
  99. return true;
  100. }
  101. // otherwise do not retry
  102. return false;
  103. }
  104. /**
  105. * @return <code>true</code> if this handler will retry methods that have
  106. * successfully sent their request, <code>false</code> otherwise
  107. */
  108. public boolean isRequestSentRetryEnabled() {
  109. return requestSentRetryEnabled;
  110. }
  111. /**
  112. * @return the maximum number of times a method will be retried
  113. */
  114. public int getRetryCount() {
  115. return retryCount;
  116. }
  117. }