You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by si...@apache.org on 2012/08/20 12:45:08 UTC

svn commit: r1374966 - in /lucene/dev/branches/branch_4x/solr/solrj/src: java/org/apache/solr/client/solrj/impl/ test/org/apache/solr/client/solrj/impl/

Author: siren
Date: Mon Aug 20 10:45:07 2012
New Revision: 1374966

URL: http://svn.apache.org/viewvc?rev=1374966&view=rev
Log:
SOLR-3726 allow more flexibility in configuring solr http clients, merge from trunk (r1374964)

Added:
    lucene/dev/branches/branch_4x/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpClientConfigurer.java
      - copied unchanged from r1374964, lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpClientConfigurer.java
Modified:
    lucene/dev/branches/branch_4x/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpClientUtil.java
    lucene/dev/branches/branch_4x/solr/solrj/src/test/org/apache/solr/client/solrj/impl/HttpClientUtilTest.java

Modified: lucene/dev/branches/branch_4x/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpClientUtil.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpClientUtil.java?rev=1374966&r1=1374965&r2=1374966&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpClientUtil.java (original)
+++ lucene/dev/branches/branch_4x/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpClientUtil.java Mon Aug 20 10:45:07 2012
@@ -78,9 +78,18 @@ 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.
    * 
    * @param params
@@ -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);
   }
 
   /**

Modified: lucene/dev/branches/branch_4x/solr/solrj/src/test/org/apache/solr/client/solrj/impl/HttpClientUtilTest.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/solr/solrj/src/test/org/apache/solr/client/solrj/impl/HttpClientUtilTest.java?rev=1374966&r1=1374965&r2=1374966&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/solr/solrj/src/test/org/apache/solr/client/solrj/impl/HttpClientUtilTest.java (original)
+++ lucene/dev/branches/branch_4x/solr/solrj/src/test/org/apache/solr/client/solrj/impl/HttpClientUtilTest.java Mon Aug 20 10:45:07 2012
@@ -18,6 +18,8 @@ package org.apache.solr.client.solrj.imp
 
 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.Defau
 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());
+    }
+
+  }
+  
 }