summaryrefslogtreecommitdiffstats
path: root/mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/execchain/BackoffStrategyExec.java
diff options
context:
space:
mode:
Diffstat (limited to 'mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/execchain/BackoffStrategyExec.java')
-rw-r--r--mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/execchain/BackoffStrategyExec.java104
1 files changed, 104 insertions, 0 deletions
diff --git a/mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/execchain/BackoffStrategyExec.java b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/execchain/BackoffStrategyExec.java
new file mode 100644
index 000000000..a639da934
--- /dev/null
+++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/execchain/BackoffStrategyExec.java
@@ -0,0 +1,104 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package ch.boye.httpclientandroidlib.impl.execchain;
+
+import java.io.IOException;
+import java.lang.reflect.UndeclaredThrowableException;
+
+import ch.boye.httpclientandroidlib.HttpException;
+import ch.boye.httpclientandroidlib.annotation.Immutable;
+import ch.boye.httpclientandroidlib.client.BackoffManager;
+import ch.boye.httpclientandroidlib.client.ConnectionBackoffStrategy;
+import ch.boye.httpclientandroidlib.client.methods.CloseableHttpResponse;
+import ch.boye.httpclientandroidlib.client.methods.HttpExecutionAware;
+import ch.boye.httpclientandroidlib.client.methods.HttpRequestWrapper;
+import ch.boye.httpclientandroidlib.client.protocol.HttpClientContext;
+import ch.boye.httpclientandroidlib.conn.routing.HttpRoute;
+import ch.boye.httpclientandroidlib.util.Args;
+
+/**
+ * @since 4.3
+ */
+@Immutable
+public class BackoffStrategyExec implements ClientExecChain {
+
+ private final ClientExecChain requestExecutor;
+ private final ConnectionBackoffStrategy connectionBackoffStrategy;
+ private final BackoffManager backoffManager;
+
+ public BackoffStrategyExec(
+ final ClientExecChain requestExecutor,
+ final ConnectionBackoffStrategy connectionBackoffStrategy,
+ final BackoffManager backoffManager) {
+ super();
+ Args.notNull(requestExecutor, "HTTP client request executor");
+ Args.notNull(connectionBackoffStrategy, "Connection backoff strategy");
+ Args.notNull(backoffManager, "Backoff manager");
+ this.requestExecutor = requestExecutor;
+ this.connectionBackoffStrategy = connectionBackoffStrategy;
+ this.backoffManager = backoffManager;
+ }
+
+ public CloseableHttpResponse execute(
+ final HttpRoute route,
+ final HttpRequestWrapper request,
+ final HttpClientContext context,
+ final HttpExecutionAware execAware) throws IOException, HttpException {
+ Args.notNull(route, "HTTP route");
+ Args.notNull(request, "HTTP request");
+ Args.notNull(context, "HTTP context");
+ CloseableHttpResponse out = null;
+ try {
+ out = this.requestExecutor.execute(route, request, context, execAware);
+ } catch (final Exception ex) {
+ if (out != null) {
+ out.close();
+ }
+ if (this.connectionBackoffStrategy.shouldBackoff(ex)) {
+ this.backoffManager.backOff(route);
+ }
+ if (ex instanceof RuntimeException) {
+ throw (RuntimeException) ex;
+ }
+ if (ex instanceof HttpException) {
+ throw (HttpException) ex;
+ }
+ if (ex instanceof IOException) {
+ throw (IOException) ex;
+ }
+ throw new UndeclaredThrowableException(ex);
+ }
+ if (this.connectionBackoffStrategy.shouldBackoff(out)) {
+ this.backoffManager.backOff(route);
+ } else {
+ this.backoffManager.probe(route);
+ }
+ return out;
+ }
+
+}