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.v2.communication;
21  
22  import java.io.IOException;
23  
24  import javax.net.ssl.SSLHandshakeException;
25  
26  import net.sf.urlchecker.communication.CommunicationFactory;
27  
28  import org.apache.http.HttpEntityEnclosingRequest;
29  import org.apache.http.HttpRequest;
30  import org.apache.http.NoHttpResponseException;
31  import org.apache.http.client.HttpRequestRetryHandler;
32  import org.apache.http.protocol.ExecutionContext;
33  import org.apache.http.protocol.HttpContext;
34  
35  /**
36   * The Class RequestRetryHandler for the new version of the HttpClient. This
37   * Handler is much like the default. It is here for future enhancements.
38   * 
39   * <p>
40   * <b> $Id: RequestRetryHandler.java 183 2010-12-17 19:10:21Z georgosn $</b>
41   * </p>
42   * 
43   * @author $LastChangedBy: georgosn $
44   * @version $LastChangedRevision: 183 $
45   */
46  public class RequestRetryHandler implements HttpRequestRetryHandler {
47  
48      /*
49       * (non-Javadoc)
50       * 
51       * @see org.apache.http.client.HttpRequestRetryHandler#retryRequest(java.io.
52       * IOException, int, org.apache.http.protocol.HttpContext)
53       */
54      /** {@inheritDoc} */
55      public boolean retryRequest(IOException exception, int executionCount,
56              HttpContext context) {
57          if (executionCount >= CommunicationFactory.getInstance()
58                  .getMaxretries()) {
59              // Do not retry if over max retry count
60              return false;
61          }
62  
63          if (executionCount >= 5) {
64              // Do not retry if over max retry count
65              return false;
66          }
67          if (exception instanceof NoHttpResponseException) {
68              // Retry if the server dropped connection on us
69              return true;
70          }
71          if (exception instanceof SSLHandshakeException) {
72              // Do not retry on SSL handshake exception
73              return false;
74          }
75          final HttpRequest request = (HttpRequest) context
76                  .getAttribute(ExecutionContext.HTTP_REQUEST);
77          final boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
78          if (idempotent) {
79              // Retry if the request is considered idempotent
80              return true;
81          }
82  
83          return false;
84      }
85  
86  }