Add connection timeout, and abort connection on error.

git-svn-id: https://svn.apache.org/repos/asf/manifoldcf/integration/solr-4.x/trunk@1453815 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/mcf/src/java/org/apache/solr/mcf/ManifoldCFQParserPlugin.java b/mcf/src/java/org/apache/solr/mcf/ManifoldCFQParserPlugin.java
index 0bad2f0..fc4dd86 100644
--- a/mcf/src/java/org/apache/solr/mcf/ManifoldCFQParserPlugin.java
+++ b/mcf/src/java/org/apache/solr/mcf/ManifoldCFQParserPlugin.java
@@ -49,7 +49,7 @@
 import org.apache.http.util.EntityUtils;
 import org.apache.http.impl.client.DefaultHttpClient;
 import org.apache.http.impl.client.DefaultRedirectStrategy;
-import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
+import org.apache.http.impl.conn.PoolingClientConnectionManager;
 import org.slf4j.*;
 
 import java.io.*;
@@ -85,9 +85,10 @@
   String fieldDenyDocument = null;
   String fieldAllowShare = null;
   String fieldDenyShare = null;
+  int connectionTimeOut;
   int socketTimeOut;
   Integer connectionManagerSynchronizer = new Integer(0);
-  ThreadSafeClientConnManager httpConnectionManager = null;
+  PoolingClientConnectionManager httpConnectionManager = null;
   DefaultHttpClient client = null;
   int poolSize;
   
@@ -102,6 +103,8 @@
     authorityBaseURL = (String)args.get("AuthorityServiceBaseURL");
     if (authorityBaseURL == null)
       authorityBaseURL = "http://localhost:8345/mcf-authority-service";
+    Integer cTimeOut = (Integer)args.get("ConnectionTimeOut");
+    connectionTimeOut = cTimeOut == null ? 60000 : cTimeOut;
     Integer timeOut = (Integer)args.get("SocketTimeOut");
     socketTimeOut = timeOut == null ? 300000 : timeOut;
     String allowAttributePrefix = (String)args.get("AllowAttributePrefix");
@@ -125,13 +128,14 @@
       if (client == null)
       {
         // Initialize the connection pool
-        httpConnectionManager = new ThreadSafeClientConnManager();
+        httpConnectionManager = new PoolingClientConnectionManager();
         httpConnectionManager.setMaxTotal(poolSize);
         httpConnectionManager.setDefaultMaxPerRoute(poolSize);
         BasicHttpParams params = new BasicHttpParams();
         params.setBooleanParameter(CoreConnectionPNames.TCP_NODELAY,true);
         params.setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK,false);
         params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT,socketTimeOut);
+        params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,connectionTimeOut);
         client = new DefaultHttpClient(httpConnectionManager,params);
         client.setRedirectStrategy(new DefaultRedirectStrategy());
         core.addCloseHook(new CloseHandler());
@@ -278,7 +282,7 @@
         int rval = httpResponse.getStatusLine().getStatusCode();
         if (rval != 200)
         {
-          String response = EntityUtils.toString(httpResponse.getEntity(),null);
+          String response = EntityUtils.toString(httpResponse.getEntity(),"utf-8");
           throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,"Couldn't fetch user's access tokens from ManifoldCF authority service: "+Integer.toString(rval)+"; "+response);
         }
         InputStream is = httpResponse.getEntity().getContent();
@@ -329,7 +333,7 @@
       }
       finally
       {
-        //method.releaseConnection();
+        method.abort();
       }
     }
   }
diff --git a/mcf/src/java/org/apache/solr/mcf/ManifoldCFSearchComponent.java b/mcf/src/java/org/apache/solr/mcf/ManifoldCFSearchComponent.java
index 032075c..5565393 100644
--- a/mcf/src/java/org/apache/solr/mcf/ManifoldCFSearchComponent.java
+++ b/mcf/src/java/org/apache/solr/mcf/ManifoldCFSearchComponent.java
@@ -41,7 +41,7 @@
 import org.apache.http.util.EntityUtils;
 import org.apache.http.impl.client.DefaultHttpClient;
 import org.apache.http.impl.client.DefaultRedirectStrategy;
-import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
+import org.apache.http.impl.conn.PoolingClientConnectionManager;
 import org.slf4j.*;
 
 import java.io.*;
@@ -78,8 +78,9 @@
   String fieldDenyDocument = null;
   String fieldAllowShare = null;
   String fieldDenyShare = null;
+  int connectionTimeOut;
   int socketTimeOut;
-  ThreadSafeClientConnManager httpConnectionManager = null;
+  PoolingClientConnectionManager httpConnectionManager = null;
   DefaultHttpClient client = null;
   int poolSize;
   
@@ -98,6 +99,8 @@
       System.out.println("USING DEFAULT BASE URL!!");
       authorityBaseURL = "http://localhost:8345/mcf-authority-service";
     }
+    Integer cTimeOut = (Integer)args.get("ConnectionTimeOut");
+    connectionTimeOut = cTimeOut == null ? 60000 : cTimeOut;
     Integer timeOut = (Integer)args.get("SocketTimeOut");
     socketTimeOut = timeOut == null ? 300000 : timeOut;
     String allowAttributePrefix = (String)args.get("AllowAttributePrefix");
@@ -114,13 +117,14 @@
     poolSize = (connectionPoolSize==null)?50:connectionPoolSize.intValue();
 
     // Initialize the connection pool
-    httpConnectionManager = new ThreadSafeClientConnManager();
+    httpConnectionManager = new PoolingClientConnectionManager();
     httpConnectionManager.setMaxTotal(poolSize);
     httpConnectionManager.setDefaultMaxPerRoute(poolSize);
     BasicHttpParams params = new BasicHttpParams();
     params.setBooleanParameter(CoreConnectionPNames.TCP_NODELAY,true);
     params.setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK,false);
     params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT,socketTimeOut);
+    params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,connectionTimeOut);
     client = new DefaultHttpClient(httpConnectionManager,params);
     client.setRedirectStrategy(new DefaultRedirectStrategy());
   }
@@ -301,7 +305,7 @@
       int rval = httpResponse.getStatusLine().getStatusCode();
       if (rval != 200)
       {
-        String response = EntityUtils.toString(httpResponse.getEntity(),null);
+        String response = EntityUtils.toString(httpResponse.getEntity(),"utf-8");
         throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,"Couldn't fetch user's access tokens from ManifoldCF authority service: "+Integer.toString(rval)+"; "+response);
       }
       InputStream is = httpResponse.getEntity().getContent();
@@ -352,7 +356,7 @@
     }
     finally
     {
-      //method.releaseConnection();
+      method.abort();
     }
   }