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 ch.boye.httpclientandroidlib.impl.execchain; 29 30 import java.io.IOException; 31 import java.io.InterruptedIOException; 32 33 import ch.boye.httpclientandroidlib.androidextra.HttpClientAndroidLog; 34 /* LogFactory removed by HttpClient for Android script. */ 35 import ch.boye.httpclientandroidlib.Header; 36 import ch.boye.httpclientandroidlib.HttpException; 37 import ch.boye.httpclientandroidlib.annotation.Immutable; 38 import ch.boye.httpclientandroidlib.client.ServiceUnavailableRetryStrategy; 39 import ch.boye.httpclientandroidlib.client.methods.CloseableHttpResponse; 40 import ch.boye.httpclientandroidlib.client.methods.HttpExecutionAware; 41 import ch.boye.httpclientandroidlib.client.methods.HttpRequestWrapper; 42 import ch.boye.httpclientandroidlib.client.protocol.HttpClientContext; 43 import ch.boye.httpclientandroidlib.conn.routing.HttpRoute; 44 import ch.boye.httpclientandroidlib.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 * 55 * @since 4.3 56 */ 57 @Immutable 58 public class ServiceUnavailableRetryExec implements ClientExecChain { 59 60 public HttpClientAndroidLog log = new HttpClientAndroidLog(getClass()); 61 62 private final ClientExecChain requestExecutor; 63 private final ServiceUnavailableRetryStrategy retryStrategy; 64 ServiceUnavailableRetryExec( final ClientExecChain requestExecutor, final ServiceUnavailableRetryStrategy retryStrategy)65 public ServiceUnavailableRetryExec( 66 final ClientExecChain requestExecutor, 67 final ServiceUnavailableRetryStrategy retryStrategy) { 68 super(); 69 Args.notNull(requestExecutor, "HTTP request executor"); 70 Args.notNull(retryStrategy, "Retry strategy"); 71 this.requestExecutor = requestExecutor; 72 this.retryStrategy = retryStrategy; 73 } 74 execute( final HttpRoute route, final HttpRequestWrapper request, final HttpClientContext context, final HttpExecutionAware execAware)75 public CloseableHttpResponse execute( 76 final HttpRoute route, 77 final HttpRequestWrapper request, 78 final HttpClientContext context, 79 final HttpExecutionAware execAware) throws IOException, HttpException { 80 final Header[] origheaders = request.getAllHeaders(); 81 for (int c = 1;; c++) { 82 final CloseableHttpResponse response = this.requestExecutor.execute( 83 route, request, context, execAware); 84 try { 85 if (this.retryStrategy.retryRequest(response, c, context)) { 86 response.close(); 87 final long nextInterval = this.retryStrategy.getRetryInterval(); 88 if (nextInterval > 0) { 89 try { 90 this.log.trace("Wait for " + nextInterval); 91 Thread.sleep(nextInterval); 92 } catch (final InterruptedException e) { 93 Thread.currentThread().interrupt(); 94 throw new InterruptedIOException(); 95 } 96 } 97 request.setHeaders(origheaders); 98 } else { 99 return response; 100 } 101 } catch (final RuntimeException ex) { 102 response.close(); 103 throw ex; 104 } 105 } 106 } 107 108 } 109