Skip to content

Apache Lucene open-source search software

SOLR-3726 allow more flexibility in configuring solr http clients

Sami Siren96767652d77a
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpClientConfigurer.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpClientConfigurer.java
new file mode 100644
index 00000000000..2388e2598b8
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpClientConfigurer.java
@@ -0,0 +1,73 @@
+package org.apache.solr.client.solrj.impl;
+
+/*
+ * 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.
+ */
+
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.solr.common.params.SolrParams;
+
+/**
+ * The default http client configurer. If the behaviour needs to be customized a
+ * new HttpCilentConfigurer can be set by calling
+ * {@link HttpClientUtil#setConfigurer(HttpClientConfigurer)}
+ */
+public class HttpClientConfigurer {
+
+ protected void configure(DefaultHttpClient httpClient, SolrParams config) {
+
+ if (config.get(HttpClientUtil.PROP_MAX_CONNECTIONS) != null) {
+ HttpClientUtil.setMaxConnections(httpClient,
+ config.getInt(HttpClientUtil.PROP_MAX_CONNECTIONS));
+ }
+
+ if (config.get(HttpClientUtil.PROP_MAX_CONNECTIONS_PER_HOST) != null) {
+ HttpClientUtil.setMaxConnectionsPerHost(httpClient,
+ config.getInt(HttpClientUtil.PROP_MAX_CONNECTIONS_PER_HOST));
+ }
+
+ if (config.get(HttpClientUtil.PROP_CONNECTION_TIMEOUT) != null) {
+ HttpClientUtil.setConnectionTimeout(httpClient,
+ config.getInt(HttpClientUtil.PROP_CONNECTION_TIMEOUT));
+ }
+
+ if (config.get(HttpClientUtil.PROP_SO_TIMEOUT) != null) {
+ HttpClientUtil.setSoTimeout(httpClient,
+ config.getInt(HttpClientUtil.PROP_SO_TIMEOUT));
+ }
+
+ if (config.get(HttpClientUtil.PROP_USE_RETRY) != null) {
+ HttpClientUtil.setUseRetry(httpClient,
+ config.getBool(HttpClientUtil.PROP_USE_RETRY));
+ }
+
+ if (config.get(HttpClientUtil.PROP_FOLLOW_REDIRECTS) != null) {
+ HttpClientUtil.setFollowRedirects(httpClient,
+ config.getBool(HttpClientUtil.PROP_FOLLOW_REDIRECTS));
+ }
+
+ final String basicAuthUser = config
+ .get(HttpClientUtil.PROP_BASIC_AUTH_USER);
+ final String basicAuthPass = config
+ .get(HttpClientUtil.PROP_BASIC_AUTH_PASS);
+ HttpClientUtil.setBasicAuth(httpClient, basicAuthUser, basicAuthPass);
+
+ if (config.get(HttpClientUtil.PROP_ALLOW_COMPRESSION) != null) {
+ HttpClientUtil.setAllowCompression(httpClient,
+ config.getBool(HttpClientUtil.PROP_ALLOW_COMPRESSION));
+ }
+ }
+}
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpClientUtil.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpClientUtil.java
index 757ae92c355..5b863b50bef 100644
--- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpClientUtil.java
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpClientUtil.java
@@ -78,8 +78,17 @@ public class HttpClientUtil {
static final DefaultHttpRequestRetryHandler NO_RETRY = new DefaultHttpRequestRetryHandler(
0, false);
+ private static HttpClientConfigurer configurer = new HttpClientConfigurer();
+
private HttpClientUtil(){}
+ /**
+ * Replace the {@link HttpClientConfigurer} class used in configuring the http
+ * clients with a custom implementation.
+ */
+ public static void setConfigurer(HttpClientConfigurer newConfigurer) {
+ configurer = newConfigurer;
+ }
/**
* Creates new http client by using the provided configuration.
*
@@ -103,38 +112,7 @@ public class HttpClientUtil {
*/
public static void configureClient(final DefaultHttpClient httpClient,
SolrParams config) {
-
- if (config.get(PROP_MAX_CONNECTIONS) != null) {
- setMaxConnections(httpClient, config.getInt(PROP_MAX_CONNECTIONS));
- }
-
- if (config.get(PROP_MAX_CONNECTIONS_PER_HOST) != null) {
- setMaxConnectionsPerHost(httpClient, config.getInt(PROP_MAX_CONNECTIONS_PER_HOST));
- }
-
- if (config.get(PROP_CONNECTION_TIMEOUT) != null) {
- setConnectionTimeout(httpClient, config.getInt(PROP_CONNECTION_TIMEOUT));
- }
-
- if (config.get(PROP_SO_TIMEOUT) != null) {
- setSoTimeout(httpClient, config.getInt(PROP_SO_TIMEOUT));
- }
-
- if (config.get(PROP_USE_RETRY) != null) {
- setUseRetry(httpClient, config.getBool(PROP_USE_RETRY));
- }
-
- if (config.get(PROP_FOLLOW_REDIRECTS) != null) {
- setFollowRedirects(httpClient, config.getBool(PROP_FOLLOW_REDIRECTS));
- }
-
- final String basicAuthUser = config.get(PROP_BASIC_AUTH_USER);
- final String basicAuthPass = config.get(PROP_BASIC_AUTH_PASS);
- setBasicAuth(httpClient, basicAuthUser, basicAuthPass);
-
- if (config.get(PROP_ALLOW_COMPRESSION) != null) {
- setAllowCompression(httpClient, config.getBool(PROP_ALLOW_COMPRESSION));
- }
+ configurer.configure(httpClient, config);
}
/**
diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/impl/HttpClientUtilTest.java b/solr/solrj/src/test/org/apache/solr/client/solrj/impl/HttpClientUtilTest.java
index 2436911d013..6d136c1a57e 100644
--- a/solr/solrj/src/test/org/apache/solr/client/solrj/impl/HttpClientUtilTest.java
+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/impl/HttpClientUtilTest.java
@@ -18,6 +18,8 @@ package org.apache.solr.client.solrj.impl;
import static org.junit.Assert.assertEquals;
+import java.util.concurrent.atomic.AtomicInteger;
+
import org.apache.http.auth.AuthScope;
import org.apache.http.client.HttpClient;
import org.apache.http.client.params.ClientPNames;
@@ -25,6 +27,7 @@ import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.HttpConnectionParams;
import org.apache.solr.common.params.ModifiableSolrParams;
+import org.apache.solr.common.params.SolrParams;
import org.junit.Test;
public class HttpClientUtilTest {
@@ -60,4 +63,31 @@ public class HttpClientUtilTest {
client.getConnectionManager().shutdown();
}
+ @Test
+ public void testReplaceConfigurer(){
+
+ try {
+ final AtomicInteger counter = new AtomicInteger();
+ HttpClientConfigurer custom = new HttpClientConfigurer(){
+ @Override
+ protected void configure(DefaultHttpClient httpClient, SolrParams config) {
+ super.configure(httpClient, config);
+ counter.set(config.getInt("custom-param", -1));
+ }
+
+ };
+
+ HttpClientUtil.setConfigurer(custom);
+
+ ModifiableSolrParams params = new ModifiableSolrParams();
+ params.set("custom-param", 5);
+ HttpClientUtil.createClient(params).getConnectionManager().shutdown();
+ assertEquals(5, counter.get());
+ } finally {
+ //restore default configurer
+ HttpClientUtil.setConfigurer(new HttpClientConfigurer());
+ }
+
+ }
+
}