View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.impl.execchain;
29  
30  import java.io.IOException;
31  
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  import org.apache.http.Header;
35  import org.apache.http.HttpException;
36  import org.apache.http.NoHttpResponseException;
37  import org.apache.http.annotation.Contract;
38  import org.apache.http.annotation.ThreadingBehavior;
39  import org.apache.http.client.HttpRequestRetryHandler;
40  import org.apache.http.client.NonRepeatableRequestException;
41  import org.apache.http.client.methods.CloseableHttpResponse;
42  import org.apache.http.client.methods.HttpExecutionAware;
43  import org.apache.http.client.methods.HttpRequestWrapper;
44  import org.apache.http.client.protocol.HttpClientContext;
45  import org.apache.http.conn.routing.HttpRoute;
46  import org.apache.http.util.Args;
47  
48  /**
49   * Request executor in the request execution chain that is responsible
50   * for making a decision whether a request failed due to an I/O error
51   * should be re-executed.
52   * <p>
53   * Further responsibilities such as communication with the opposite
54   * endpoint is delegated to the next executor in the request execution
55   * chain.
56   * </p>
57   *
58   * @since 4.3
59   */
60  @Contract(threading = ThreadingBehavior.IMMUTABLE_CONDITIONAL)
61  public class RetryExec implements ClientExecChain {
62  
63      private final Log log = LogFactory.getLog(getClass());
64  
65      private final ClientExecChain requestExecutor;
66      private final HttpRequestRetryHandler retryHandler;
67  
68      public RetryExec(
69              final ClientExecChain requestExecutor,
70              final HttpRequestRetryHandler retryHandler) {
71          Args.notNull(requestExecutor, "HTTP request executor");
72          Args.notNull(retryHandler, "HTTP request retry handler");
73          this.requestExecutor = requestExecutor;
74          this.retryHandler = retryHandler;
75      }
76  
77      @Override
78      public CloseableHttpResponse execute(
79              final HttpRoute route,
80              final HttpRequestWrapper request,
81              final HttpClientContext context,
82              final HttpExecutionAware execAware) throws IOException, HttpException {
83          Args.notNull(route, "HTTP route");
84          Args.notNull(request, "HTTP request");
85          Args.notNull(context, "HTTP context");
86          final Header[] origheaders = request.getAllHeaders();
87          for (int execCount = 1;; execCount++) {
88              try {
89                  return this.requestExecutor.execute(route, request, context, execAware);
90              } catch (final IOException ex) {
91                  if (execAware != null && execAware.isAborted()) {
92                      this.log.debug("Request has been aborted");
93                      throw ex;
94                  }
95                  if (retryHandler.retryRequest(ex, execCount, context)) {
96                      if (this.log.isInfoEnabled()) {
97                          this.log.info("I/O exception ("+ ex.getClass().getName() +
98                                  ") caught when processing request to "
99                                  + route +
100                                 ": "
101                                 + ex.getMessage());
102                     }
103                     if (this.log.isDebugEnabled()) {
104                         this.log.debug(ex.getMessage(), ex);
105                     }
106                     if (!RequestEntityProxy.isRepeatable(request)) {
107                         this.log.debug("Cannot retry non-repeatable request");
108                         throw new NonRepeatableRequestException("Cannot retry request " +
109                                 "with a non-repeatable request entity", ex);
110                     }
111                     request.setHeaders(origheaders);
112                     if (this.log.isInfoEnabled()) {
113                         this.log.info("Retrying request to " + route);
114                     }
115                 } else {
116                     if (ex instanceof NoHttpResponseException) {
117                         final NoHttpResponseException updatedex = new NoHttpResponseException(
118                                 route.getTargetHost().toHostString() + " failed to respond");
119                         updatedex.setStackTrace(ex.getStackTrace());
120                         throw updatedex;
121                     }
122                     throw ex;
123                 }
124             }
125         }
126     }
127 
128 }