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.client;
29  
30  import java.io.IOException;
31  import java.io.InterruptedIOException;
32  import java.net.URI;
33  
34  import org.apache.commons.logging.Log;
35  import org.apache.commons.logging.LogFactory;
36  import org.apache.http.HttpHost;
37  import org.apache.http.HttpRequest;
38  import org.apache.http.HttpResponse;
39  import org.apache.http.annotation.Contract;
40  import org.apache.http.annotation.ThreadingBehavior;
41  import org.apache.http.client.HttpClient;
42  import org.apache.http.client.ResponseHandler;
43  import org.apache.http.client.ServiceUnavailableRetryStrategy;
44  import org.apache.http.client.methods.HttpUriRequest;
45  import org.apache.http.conn.ClientConnectionManager;
46  import org.apache.http.params.HttpParams;
47  import org.apache.http.protocol.HttpContext;
48  import org.apache.http.util.Args;
49  import org.apache.http.util.EntityUtils;
50  
51  /**
52   * {@link HttpClient} implementation that can automatically retry the request in case of
53   * a non-2xx response using the {@link ServiceUnavailableRetryStrategy} interface.
54   *
55   * @since 4.2
56   *
57   * @deprecated (4.3) use {@link HttpClientBuilder}.
58   */
59  @Deprecated
60  @Contract(threading = ThreadingBehavior.SAFE_CONDITIONAL)
61  public class AutoRetryHttpClient implements HttpClient {
62  
63      private final HttpClient backend;
64  
65      private final ServiceUnavailableRetryStrategy retryStrategy;
66  
67      private final Log log = LogFactory.getLog(getClass());
68  
69      public AutoRetryHttpClient(
70              final HttpClient client, final ServiceUnavailableRetryStrategy retryStrategy) {
71          super();
72          Args.notNull(client, "HttpClient");
73          Args.notNull(retryStrategy, "ServiceUnavailableRetryStrategy");
74          this.backend = client;
75          this.retryStrategy = retryStrategy;
76      }
77  
78      /**
79       * Constructs a {@code AutoRetryHttpClient} with default caching settings that
80       * stores cache entries in memory and uses a vanilla
81       * {@link DefaultHttpClient} for backend requests.
82       */
83      public AutoRetryHttpClient() {
84          this(new DefaultHttpClient(), new DefaultServiceUnavailableRetryStrategy());
85      }
86  
87      /**
88       * Constructs a {@code AutoRetryHttpClient} with the given caching options that
89       * stores cache entries in memory and uses a vanilla
90       * {@link DefaultHttpClient} for backend requests.
91       *
92       * @param config
93       *            retry configuration module options
94       */
95      public AutoRetryHttpClient(final ServiceUnavailableRetryStrategy config) {
96          this(new DefaultHttpClient(), config);
97      }
98  
99      /**
100      * Constructs a {@code AutoRetryHttpClient} with default caching settings that
101      * stores cache entries in memory and uses the given {@link HttpClient} for
102      * backend requests.
103      *
104      * @param client
105      *            used to make origin requests
106      */
107     public AutoRetryHttpClient(final HttpClient client) {
108         this(client, new DefaultServiceUnavailableRetryStrategy());
109     }
110 
111     @Override
112     public HttpResponse execute(final HttpHost target, final HttpRequest request)
113             throws IOException {
114         final HttpContext defaultContext = null;
115         return execute(target, request, defaultContext);
116     }
117 
118     @Override
119     public <T> T execute(final HttpHost target, final HttpRequest request,
120             final ResponseHandler<? extends T> responseHandler) throws IOException {
121         return execute(target, request, responseHandler, null);
122     }
123 
124     @Override
125     public <T> T execute(final HttpHost target, final HttpRequest request,
126             final ResponseHandler<? extends T> responseHandler, final HttpContext context)
127             throws IOException {
128         final HttpResponse resp = execute(target, request, context);
129         return responseHandler.handleResponse(resp);
130     }
131 
132     @Override
133     public HttpResponse execute(final HttpUriRequest request) throws IOException {
134         final HttpContext context = null;
135         return execute(request, context);
136     }
137 
138     @Override
139     public HttpResponse execute(final HttpUriRequest request, final HttpContext context)
140             throws IOException {
141         final URI uri = request.getURI();
142         final HttpHost httpHost = new HttpHost(uri.getHost(), uri.getPort(),
143                 uri.getScheme());
144         return execute(httpHost, request, context);
145     }
146 
147     @Override
148     public <T> T execute(final HttpUriRequest request,
149             final ResponseHandler<? extends T> responseHandler) throws IOException {
150         return execute(request, responseHandler, null);
151     }
152 
153     @Override
154     public <T> T execute(final HttpUriRequest request,
155             final ResponseHandler<? extends T> responseHandler, final HttpContext context)
156             throws IOException {
157         final HttpResponse resp = execute(request, context);
158         return responseHandler.handleResponse(resp);
159     }
160 
161     @Override
162     public HttpResponse execute(final HttpHost target, final HttpRequest request,
163             final HttpContext context) throws IOException {
164         for (int c = 1;; c++) {
165             final HttpResponse response = backend.execute(target, request, context);
166             try {
167                 if (retryStrategy.retryRequest(response, c, context)) {
168                     EntityUtils.consume(response.getEntity());
169                     final long nextInterval = retryStrategy.getRetryInterval();
170                     try {
171                         log.trace("Wait for " + nextInterval);
172                         Thread.sleep(nextInterval);
173                     } catch (final InterruptedException e) {
174                         Thread.currentThread().interrupt();
175                         throw new InterruptedIOException();
176                     }
177                 } else {
178                     return response;
179                 }
180             } catch (final RuntimeException ex) {
181                 try {
182                     EntityUtils.consume(response.getEntity());
183                 } catch (final IOException ioex) {
184                     log.warn("I/O error consuming response content", ioex);
185                 }
186                 throw ex;
187             }
188         }
189     }
190 
191     @Override
192     public ClientConnectionManager getConnectionManager() {
193         return backend.getConnectionManager();
194     }
195 
196     @Override
197     public HttpParams getParams() {
198         return backend.getParams();
199     }
200 
201 }