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  import java.io.InterruptedIOException;
32  
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  import org.apache.http.Header;
36  import org.apache.http.HttpException;
37  import org.apache.http.annotation.Immutable;
38  import org.apache.http.client.ServiceUnavailableRetryStrategy;
39  import org.apache.http.client.methods.CloseableHttpResponse;
40  import org.apache.http.client.methods.HttpExecutionAware;
41  import org.apache.http.client.methods.HttpRequestWrapper;
42  import org.apache.http.client.protocol.HttpClientContext;
43  import org.apache.http.conn.routing.HttpRoute;
44  import org.apache.http.util.Args;
45  
46  /**
47   * Request executor in the request execution chain that is responsible
48   * for making a decision whether a request that received a non-2xx response
49   * from the target server should be re-executed.
50   * <p>
51   * Further responsibilities such as communication with the opposite
52   * endpoint is delegated to the next executor in the request execution
53   * chain.
54   * </p>
55   *
56   * @since 4.3
57   */
58  @Immutable
59  public class ServiceUnavailableRetryExec implements ClientExecChain {
60  
61      private final Log log = LogFactory.getLog(getClass());
62  
63      private final ClientExecChain requestExecutor;
64      private final ServiceUnavailableRetryStrategy retryStrategy;
65  
66      public ServiceUnavailableRetryExec(
67              final ClientExecChain requestExecutor,
68              final ServiceUnavailableRetryStrategy retryStrategy) {
69          super();
70          Args.notNull(requestExecutor, "HTTP request executor");
71          Args.notNull(retryStrategy, "Retry strategy");
72          this.requestExecutor = requestExecutor;
73          this.retryStrategy = retryStrategy;
74      }
75  
76      @Override
77      public CloseableHttpResponse execute(
78              final HttpRoute route,
79              final HttpRequestWrapper request,
80              final HttpClientContext context,
81              final HttpExecutionAware execAware) throws IOException, HttpException {
82          final Header[] origheaders = request.getAllHeaders();
83          for (int c = 1;; c++) {
84              final CloseableHttpResponse response = this.requestExecutor.execute(
85                      route, request, context, execAware);
86              try {
87                  if (this.retryStrategy.retryRequest(response, c, context)) {
88                      response.close();
89                      final long nextInterval = this.retryStrategy.getRetryInterval();
90                      if (nextInterval > 0) {
91                          try {
92                              this.log.trace("Wait for " + nextInterval);
93                              Thread.sleep(nextInterval);
94                          } catch (final InterruptedException e) {
95                              Thread.currentThread().interrupt();
96                              throw new InterruptedIOException();
97                          }
98                      }
99                      request.setHeaders(origheaders);
100                 } else {
101                     return response;
102                 }
103             } catch (final RuntimeException ex) {
104                 response.close();
105                 throw ex;
106             }
107         }
108     }
109 
110 }