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:32:52 UTC

svn commit: r1374964 - in /lucene/dev/trunk/solr/solrj/src: java/org/apache/solr/client/solrj/impl/HttpClientConfigurer.java java/org/apache/solr/client/solrj/impl/HttpClientUtil.java test/org/apache/solr/client/solrj/impl/HttpClientUtilTest.java

Author: siren
Date: Mon Aug 20 10:32:52 2012
New Revision: 1374964

URL: http://svn.apache.org/viewvc?rev=1374964&view=rev
Log:
SOLR-3726 allow more flexibility in configuring solr http clients

Added:
    lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpClientConfigurer.java   (with props)
Modified:
    lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpClientUtil.java
    lucene/dev/trunk/solr/solrj/src/test/org/apache/solr/client/solrj/impl/HttpClientUtilTest.java

Added: lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpClientConfigurer.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpClientConfigurer.java?rev=1374964&view=auto
==============================================================================
--- lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpClientConfigurer.java (added)
+++ lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpClientConfigurer.java Mon Aug 20 10:32:52 2012
@@ -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));
+    }
+  }
+}

Modified: lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpClientUtil.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpClientUtil.java?rev=1374964&r1=1374963&r2=1374964&view=diff
==============================================================================
--- lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpClientUtil.java (original)
+++ lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpClientUtil.java Mon Aug 20 10:32:52 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/trunk/solr/solrj/src/test/org/apache/solr/client/solrj/impl/HttpClientUtilTest.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/solrj/src/test/org/apache/solr/client/solrj/impl/HttpClientUtilTest.java?rev=1374964&r1=1374963&r2=1374964&view=diff
==============================================================================
--- lucene/dev/trunk/solr/solrj/src/test/org/apache/solr/client/solrj/impl/HttpClientUtilTest.java (original)
+++ lucene/dev/trunk/solr/solrj/src/test/org/apache/solr/client/solrj/impl/HttpClientUtilTest.java Mon Aug 20 10:32:52 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());
+    }
+
+  }
+  
 }