Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

upgrade to new HttpClient APIs

Shai Ererac06db19203a3
diff --git a/lucene/replicator/src/java/org/apache/lucene/replicator/http/HttpClientBase.java b/lucene/replicator/src/java/org/apache/lucene/replicator/http/HttpClientBase.java
index 24d35a1fd75..bf4c00f0836 100644
--- a/lucene/replicator/src/java/org/apache/lucene/replicator/http/HttpClientBase.java
+++ b/lucene/replicator/src/java/org/apache/lucene/replicator/http/HttpClientBase.java
@@ -29,12 +29,12 @@ import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
-import org.apache.http.client.HttpClient;
+import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
-import org.apache.http.conn.ClientConnectionManager;
-import org.apache.http.impl.client.DefaultHttpClient;
-import org.apache.http.params.HttpConnectionParams;
+import org.apache.http.conn.HttpClientConnectionManager;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.apache.lucene.store.AlreadyClosedException;
import org.apache.lucene.util.IOUtils;
@@ -46,18 +46,10 @@ import org.apache.lucene.util.IOUtils;
* */
public abstract class HttpClientBase implements Closeable {
- /**
- * Default connection timeout for this client, in milliseconds.
- *
- * @see #setConnectionTimeout(int)
- */
+ /** Default connection timeout for this client, in milliseconds. */
public static final int DEFAULT_CONNECTION_TIMEOUT = 1000;
- /**
- * Default socket timeout for this client, in milliseconds.
- *
- * @see #setSoTimeout(int)
- */
+ /** Default socket timeout for this client, in milliseconds. */
public static final int DEFAULT_SO_TIMEOUT = 60000;
// TODO compression?
@@ -67,38 +59,29 @@ public abstract class HttpClientBase implements Closeable {
private volatile boolean closed = false;
- private final HttpClient httpc;
+ private final CloseableHttpClient httpc;
+ private final RequestConfig defaultConfig;
/**
- * @param conMgr connection manager to use for this http client.
- * <b>NOTE:</b>The provided {@link ClientConnectionManager} will not be
- * {@link ClientConnectionManager#shutdown()} by this class.
+ * @param conMgr
+ * connection manager to use for this http client. <b>NOTE:</b>The
+ * provided {@link HttpClientConnectionManager} will not be
+ * {@link HttpClientConnectionManager#shutdown()} by this class.
+ * @param defaultConfig
+ * the default {@link RequestConfig} to set on the client. If
+ * {@code null} a default config is created w/ the default connection
+ * and socket timeouts.
*/
- protected HttpClientBase(String host, int port, String path, ClientConnectionManager conMgr) {
+ protected HttpClientBase(String host, int port, String path, HttpClientConnectionManager conMgr, RequestConfig defaultConfig) {
url = normalizedURL(host, port, path);
- httpc = new DefaultHttpClient(conMgr);
- setConnectionTimeout(DEFAULT_CONNECTION_TIMEOUT);
- setSoTimeout(DEFAULT_SO_TIMEOUT);
- }
-
- /**
- * Set the connection timeout for this client, in milliseconds. This setting
- * is used to modify {@link HttpConnectionParams#setConnectionTimeout}.
- *
- * @param timeout timeout to set, in millisecopnds
- */
- public void setConnectionTimeout(int timeout) {
- HttpConnectionParams.setConnectionTimeout(httpc.getParams(), timeout);
- }
-
- /**
- * Set the socket timeout for this client, in milliseconds. This setting
- * is used to modify {@link HttpConnectionParams#setSoTimeout}.
- *
- * @param timeout timeout to set, in millisecopnds
- */
- public void setSoTimeout(int timeout) {
- HttpConnectionParams.setSoTimeout(httpc.getParams(), timeout);
+ if (defaultConfig == null) {
+ this.defaultConfig = RequestConfig.custom()
+ .setConnectionRequestTimeout(DEFAULT_CONNECTION_TIMEOUT)
+ .setSocketTimeout(DEFAULT_SO_TIMEOUT).build();
+ } else {
+ this.defaultConfig = defaultConfig;
+ }
+ httpc = HttpClientBuilder.create().setConnectionManager(conMgr).setDefaultRequestConfig(this.defaultConfig).build();
}
/** Throws {@link AlreadyClosedException} if this client is already closed. */
@@ -285,6 +268,7 @@ public abstract class HttpClientBase implements Closeable {
@Override
public void close() throws IOException {
+ httpc.close();
closed = true;
}
diff --git a/lucene/replicator/src/java/org/apache/lucene/replicator/http/HttpReplicator.java b/lucene/replicator/src/java/org/apache/lucene/replicator/http/HttpReplicator.java
index 4d44e7bd8b7..337cd37cee2 100644
--- a/lucene/replicator/src/java/org/apache/lucene/replicator/http/HttpReplicator.java
+++ b/lucene/replicator/src/java/org/apache/lucene/replicator/http/HttpReplicator.java
@@ -23,7 +23,7 @@ import java.io.InputStream;
import java.util.concurrent.Callable;
import org.apache.http.HttpResponse;
-import org.apache.http.conn.ClientConnectionManager;
+import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.lucene.replicator.Replicator;
import org.apache.lucene.replicator.Revision;
import org.apache.lucene.replicator.SessionToken;
@@ -38,8 +38,8 @@ import org.apache.lucene.replicator.http.ReplicationService.ReplicationAction;
public class HttpReplicator extends HttpClientBase implements Replicator {
/** Construct with specified connection manager. */
- public HttpReplicator(String host, int port, String path, ClientConnectionManager conMgr) {
- super(host, port, path, conMgr);
+ public HttpReplicator(String host, int port, String path, HttpClientConnectionManager conMgr) {
+ super(host, port, path, conMgr, null);
}
@Override
diff --git a/lucene/replicator/src/test/org/apache/lucene/replicator/IndexAndTaxonomyRevisionTest.java b/lucene/replicator/src/test/org/apache/lucene/replicator/IndexAndTaxonomyRevisionTest.java
index 583cf56ce42..c618b84c552 100644
--- a/lucene/replicator/src/test/org/apache/lucene/replicator/IndexAndTaxonomyRevisionTest.java
+++ b/lucene/replicator/src/test/org/apache/lucene/replicator/IndexAndTaxonomyRevisionTest.java
@@ -141,6 +141,7 @@ public class IndexAndTaxonomyRevisionTest extends ReplicatorTestCase {
Revision rev = new IndexAndTaxonomyRevision(indexWriter, taxoWriter);
for (Entry<String,List<RevisionFile>> e : rev.getSourceFiles().entrySet()) {
String source = e.getKey();
+ @SuppressWarnings("resource") // silly, both directories are closed in the end
Directory dir = source.equals(IndexAndTaxonomyRevision.INDEX_SOURCE) ? indexDir : taxoDir;
for (RevisionFile file : e.getValue()) {
IndexInput src = dir.openInput(file.fileName, IOContext.READONCE);
diff --git a/lucene/replicator/src/test/org/apache/lucene/replicator/ReplicatorTestCase.java b/lucene/replicator/src/test/org/apache/lucene/replicator/ReplicatorTestCase.java
index f62140804de..aefecbd8ff8 100644
--- a/lucene/replicator/src/test/org/apache/lucene/replicator/ReplicatorTestCase.java
+++ b/lucene/replicator/src/test/org/apache/lucene/replicator/ReplicatorTestCase.java
@@ -19,8 +19,8 @@ package org.apache.lucene.replicator;
import java.util.Random;
-import org.apache.http.conn.ClientConnectionManager;
-import org.apache.http.impl.conn.PoolingClientConnectionManager;
+import org.apache.http.conn.HttpClientConnectionManager;
+import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util.LuceneTestCase.SuppressCodecs;
import org.eclipse.jetty.server.Connector;
@@ -38,7 +38,7 @@ import org.junit.AfterClass;
@SuppressCodecs("Lucene3x")
public abstract class ReplicatorTestCase extends LuceneTestCase {
- private static ClientConnectionManager clientConnectionManager;
+ private static HttpClientConnectionManager clientConnectionManager;
@AfterClass
public static void afterClassReplicatorTestCase() throws Exception {
@@ -144,15 +144,15 @@ public abstract class ReplicatorTestCase extends LuceneTestCase {
}
/**
- * Returns a {@link ClientConnectionManager}.
+ * Returns a {@link HttpClientConnectionManager}.
* <p>
- * <b>NOTE:</b> do not {@link ClientConnectionManager#shutdown()} this
+ * <b>NOTE:</b> do not {@link HttpClientConnectionManager#shutdown()} this
* connection manager, it will be shutdown automatically after all tests have
* finished.
*/
- public static synchronized ClientConnectionManager getClientConnectionManager() {
+ public static synchronized HttpClientConnectionManager getClientConnectionManager() {
if (clientConnectionManager == null) {
- PoolingClientConnectionManager ccm = new PoolingClientConnectionManager();
+ PoolingHttpClientConnectionManager ccm = new PoolingHttpClientConnectionManager();
ccm.setDefaultMaxPerRoute(128);
ccm.setMaxTotal(128);
clientConnectionManager = ccm;
diff --git a/lucene/replicator/src/test/org/apache/lucene/replicator/http/HttpReplicatorTest.java b/lucene/replicator/src/test/org/apache/lucene/replicator/http/HttpReplicatorTest.java
index ca7cbc4a52d..94205c119e4 100644
--- a/lucene/replicator/src/test/org/apache/lucene/replicator/http/HttpReplicatorTest.java
+++ b/lucene/replicator/src/test/org/apache/lucene/replicator/http/HttpReplicatorTest.java
@@ -21,7 +21,7 @@ import java.io.File;
import java.io.IOException;
import java.util.Collections;
-import org.apache.http.impl.conn.BasicClientConnectionManager;
+import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
@@ -130,7 +130,7 @@ public class HttpReplicatorTest extends ReplicatorTestCase {
public void testServerErrors() throws Exception {
// tests the behaviour of the client when the server sends an error
// must use BasicClientConnectionManager to test whether the client is closed correctly
- BasicClientConnectionManager conMgr = new BasicClientConnectionManager();
+ BasicHttpClientConnectionManager conMgr = new BasicHttpClientConnectionManager();
Replicator replicator = new HttpReplicator(host, port, ReplicationService.REPLICATION_CONTEXT + "/s1", conMgr);
ReplicationClient client = new ReplicationClient(replicator, new IndexReplicationHandler(handlerIndexDir, null),
new PerSessionDirectoryFactory(clientWorkDir));