View Javadoc

1   /*
2    * (C)opyright 2010, Nikolaos Georgosopoulos
3    *
4    * This file is part of URLChecker.
5   
6       URLChecker is free software: you can redistribute it and/or modify
7       it under the terms of the Lesser General Public License as published by
8       the Free Software Foundation, either version 3 of the License, or
9       (at your option) any later version.
10  
11      URLChecker is distributed in the hope that it will be useful,
12      but WITHOUT ANY WARRANTY; without even the implied warranty of
13      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14      GNU General Public License for more details.
15  
16      You should have received a copy of the Lesser General Public License
17      along with URLChecker.  If not, see <http://www.gnu.org/licenses/>.
18  
19   */
20  package net.sf.urlchecker.commands;
21  
22  import java.io.IOException;
23  
24  import net.sf.urlchecker.communication.CommunicationFactory;
25  import net.sf.urlchecker.exception.handling.ExceptionHandler;
26  
27  import org.apache.commons.httpclient.HttpMethod;
28  import org.apache.commons.httpclient.HttpMethodRetryHandler;
29  
30  /**
31   * The RetryHandler Class that controls the old client's retry method. This
32   * class is control from the configuration for the maximum number of retries and
33   * the Exception handling.
34   * 
35   * <p>
36   * <b> $Id: RetryHandler.java 181 2010-12-12 23:39:00Z georgosn $</b>
37   * </p>
38   * 
39   * @author $LastChangedBy: georgosn $
40   * @version $LastChangedRevision: 181 $
41   */
42  public class RetryHandler implements HttpMethodRetryHandler {
43  
44      /*
45       * (non-Javadoc)
46       * 
47       * @see
48       * org.apache.commons.httpclient.HttpMethodRetryHandler#retryMethod(org.
49       * apache.commons.httpclient.HttpMethod, java.io.IOException, int)
50       */
51      /** {@inheritDoc} */
52      public boolean retryMethod(final HttpMethod method,
53              final IOException exception, final int executionCount) {
54          if (executionCount >= CommunicationFactory.getInstance()
55                  .getMaxretries()) {
56              // Do not retry if over max retry count
57              return false;
58          }
59          final ExceptionHandler handler = CommunicationFactory.getInstance()
60                  .getExceptionHandlers().get(exception.getClass().getName());
61          if (null != handler) {
62              return handler.handle(method, exception, executionCount);
63          }
64          if (!method.isRequestSent()) {
65              // Retry if the request has not been sent fully or
66              // if it's OK to retry methods that have been sent
67              return true;
68          }
69          // otherwise do not retry
70          return false;
71  
72      }
73  }