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