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.Immutable;
38  import org.apache.http.client.HttpRequestRetryHandler;
39  import org.apache.http.client.NonRepeatableRequestException;
40  import org.apache.http.client.methods.CloseableHttpResponse;
41  import org.apache.http.client.methods.HttpExecutionAware;
42  import org.apache.http.client.methods.HttpRequestWrapper;
43  import org.apache.http.client.protocol.HttpClientContext;
44  import org.apache.http.conn.routing.HttpRoute;
45  import org.apache.http.util.Args;
46  
47  /**
48   * Request executor in the request execution chain that is responsible
49   * for making a decision whether a request failed due to an I/O error
50   * should be re-executed.
51   * <p/>
52   * Further responsibilities such as communication with the opposite
53   * endpoint is delegated to the next executor in the request execution
54   * chain.
55   *
56   * @since 4.3
57   */
58  @Immutable
59  public class RetryExec implements ClientExecChain {
60  
61      private final Log log = LogFactory.getLog(getClass());
62  
63      private final ClientExecChain requestExecutor;
64      private final HttpRequestRetryHandler retryHandler;
65  
66      public RetryExec(
67              final ClientExecChain requestExecutor,
68              final HttpRequestRetryHandler retryHandler) {
69          Args.notNull(requestExecutor, "HTTP request executor");
70          Args.notNull(retryHandler, "HTTP request retry handler");
71          this.requestExecutor = requestExecutor;
72          this.retryHandler = retryHandler;
73      }
74  
75      public CloseableHttpResponse execute(
76              final HttpRoute route,
77              final HttpRequestWrapper request,
78              final HttpClientContext context,
79              final HttpExecutionAware execAware) throws IOException, HttpException {
80          Args.notNull(route, "HTTP route");
81          Args.notNull(request, "HTTP request");
82          Args.notNull(context, "HTTP context");
83          final Header[] origheaders = request.getAllHeaders();
84          for (int execCount = 1;; execCount++) {
85              try {
86                  return this.requestExecutor.execute(route, request, context, execAware);
87              } catch (final IOException ex) {
88                  if (execAware != null && execAware.isAborted()) {
89                      this.log.debug("Request has been aborted");
90                      throw ex;
91                  }
92                  if (retryHandler.retryRequest(ex, execCount, context)) {
93                      if (this.log.isInfoEnabled()) {
94                          this.log.info("I/O exception ("+ ex.getClass().getName() +
95                                  ") caught when processing request to "
96                                  + route +
97                                  ": "
98                                  + ex.getMessage());
99                      }
100                     if (this.log.isDebugEnabled()) {
101                         this.log.debug(ex.getMessage(), ex);
102                     }
103                     if (!Proxies.isRepeatable(request)) {
104                         this.log.debug("Cannot retry non-repeatable request");
105                         throw new NonRepeatableRequestException("Cannot retry request " +
106                                 "with a non-repeatable request entity", ex);
107                     }
108                     request.setHeaders(origheaders);
109                     if (this.log.isInfoEnabled()) {
110                         this.log.info("Retrying request to " + route);
111                     }
112                 } else {
113                     if (ex instanceof NoHttpResponseException) {
114                         final NoHttpResponseException updatedex = new NoHttpResponseException(
115                                 route.getTargetHost().toHostString() + " failed to respond");
116                         updatedex.setStackTrace(ex.getStackTrace());
117                         throw updatedex;
118                     } else {
119                         throw ex;
120                     }
121                 }
122             }
123         }
124     }
125 
126 }