You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@manifoldcf.apache.org by kw...@apache.org on 2012/11/08 01:59:12 UTC

svn commit: r1406879 - in /manifoldcf/branches/CONNECTORS-120/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine: DELETECommand.java GETCommand.java POSTCommand.java PUTCommand.java ScriptParser.java

Author: kwright
Date: Thu Nov  8 00:59:11 2012
New Revision: 1406879

URL: http://svn.apache.org/viewvc?rev=1406879&view=rev
Log:
Update script engine to httpcomponents

Modified:
    manifoldcf/branches/CONNECTORS-120/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine/DELETECommand.java
    manifoldcf/branches/CONNECTORS-120/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine/GETCommand.java
    manifoldcf/branches/CONNECTORS-120/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine/POSTCommand.java
    manifoldcf/branches/CONNECTORS-120/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine/PUTCommand.java
    manifoldcf/branches/CONNECTORS-120/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine/ScriptParser.java

Modified: manifoldcf/branches/CONNECTORS-120/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine/DELETECommand.java
URL: http://svn.apache.org/viewvc/manifoldcf/branches/CONNECTORS-120/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine/DELETECommand.java?rev=1406879&r1=1406878&r2=1406879&view=diff
==============================================================================
--- manifoldcf/branches/CONNECTORS-120/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine/DELETECommand.java (original)
+++ manifoldcf/branches/CONNECTORS-120/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine/DELETECommand.java Thu Nov  8 00:59:11 2012
@@ -18,8 +18,20 @@
 */
 package org.apache.manifoldcf.scriptengine;
 
-import org.apache.commons.httpclient.*;
-import org.apache.commons.httpclient.methods.*;
+import org.apache.manifoldcf.core.interfaces.*;
+import org.apache.http.client.HttpClient;
+import org.apache.http.HttpStatus;
+import org.apache.http.HttpException;
+import org.apache.http.client.methods.HttpDelete;
+import org.apache.http.conn.ConnectTimeoutException;
+import org.apache.http.HttpResponse;
+import org.apache.http.params.BasicHttpParams;
+import org.apache.http.params.HttpParams;
+import org.apache.http.params.CoreConnectionPNames;
+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 java.io.*;
 
 /** DELETE command.  This performs a REST-style DELETE operation, designed to work
@@ -51,22 +63,19 @@ public class DELETECommand implements Co
     try
     {
       HttpClient client = sp.getHttpClient();
-      DeleteMethod method = new DeleteMethod(urlString);
+      HttpDelete method = new HttpDelete(urlString);
       try
       {
-        int resultCode = client.executeMethod(method);
-        byte[] responseData = method.getResponseBody();
-        // We presume that the data is utf-8, since that's what the API
-        // uses throughout.
-        String resultJSON = new String(responseData,"utf-8");
-
+        HttpResponse httpResponse = client.execute(method);
+        int resultCode = httpResponse.getStatusLine().getStatusCode();
+        String resultJSON = sp.convertToString(httpResponse);
         result.setReference(new VariableResult(resultCode,resultJSON));
       
         return false;
       }
       finally
       {
-        method.releaseConnection();
+        //method.releaseConnection();
       }
     }
     catch (IOException e)

Modified: manifoldcf/branches/CONNECTORS-120/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine/GETCommand.java
URL: http://svn.apache.org/viewvc/manifoldcf/branches/CONNECTORS-120/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine/GETCommand.java?rev=1406879&r1=1406878&r2=1406879&view=diff
==============================================================================
--- manifoldcf/branches/CONNECTORS-120/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine/GETCommand.java (original)
+++ manifoldcf/branches/CONNECTORS-120/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine/GETCommand.java Thu Nov  8 00:59:11 2012
@@ -18,8 +18,20 @@
 */
 package org.apache.manifoldcf.scriptengine;
 
-import org.apache.commons.httpclient.*;
-import org.apache.commons.httpclient.methods.*;
+import org.apache.manifoldcf.core.interfaces.*;
+import org.apache.http.client.HttpClient;
+import org.apache.http.HttpStatus;
+import org.apache.http.HttpException;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.conn.ConnectTimeoutException;
+import org.apache.http.HttpResponse;
+import org.apache.http.params.BasicHttpParams;
+import org.apache.http.params.HttpParams;
+import org.apache.http.params.CoreConnectionPNames;
+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 java.io.*;
 
 /** GET command.  This performs a REST-style GET operation, designed to work
@@ -51,22 +63,19 @@ public class GETCommand implements Comma
     try
     {
       HttpClient client = sp.getHttpClient();
-      GetMethod method = new GetMethod(urlString);
+      HttpGet method = new HttpGet(urlString);
       try
       {
-        int resultCode = client.executeMethod(method);
-        byte[] responseData = method.getResponseBody();
-        // We presume that the data is utf-8, since that's what the API
-        // uses throughout.
-        String resultJSON = new String(responseData,"utf-8");
-
+        HttpResponse httpResponse = client.execute(method);
+        int resultCode = httpResponse.getStatusLine().getStatusCode();
+        String resultJSON = sp.convertToString(httpResponse);
         result.setReference(new VariableResult(resultCode,resultJSON));
       
         return false;
       }
       finally
       {
-        method.releaseConnection();
+        //method.releaseConnection();
       }
     }
     catch (IOException e)

Modified: manifoldcf/branches/CONNECTORS-120/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine/POSTCommand.java
URL: http://svn.apache.org/viewvc/manifoldcf/branches/CONNECTORS-120/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine/POSTCommand.java?rev=1406879&r1=1406878&r2=1406879&view=diff
==============================================================================
--- manifoldcf/branches/CONNECTORS-120/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine/POSTCommand.java (original)
+++ manifoldcf/branches/CONNECTORS-120/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine/POSTCommand.java Thu Nov  8 00:59:11 2012
@@ -19,8 +19,21 @@
 package org.apache.manifoldcf.scriptengine;
 
 import org.apache.manifoldcf.core.interfaces.*;
-import org.apache.commons.httpclient.*;
-import org.apache.commons.httpclient.methods.*;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.entity.ContentType;
+import org.apache.http.client.HttpClient;
+import org.apache.http.HttpStatus;
+import org.apache.http.HttpException;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.conn.ConnectTimeoutException;
+import org.apache.http.HttpResponse;
+import org.apache.http.params.BasicHttpParams;
+import org.apache.http.params.HttpParams;
+import org.apache.http.params.CoreConnectionPNames;
+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 java.io.*;
 
 /** POST command.  This performs a REST-style POST operation, designed to work
@@ -61,24 +74,20 @@ public class POSTCommand implements Comm
     {
       String json = configuration.toJSON();
       HttpClient client = sp.getHttpClient();
-      PostMethod method = new PostMethod(urlString);
+      HttpPost method = new HttpPost(urlString);
       try
       {
-        method.setRequestHeader("Content-type", "text/plain; charset=UTF-8");
-        method.setRequestBody(json);
-        int resultCode = client.executeMethod(method);
-        byte[] responseData = method.getResponseBody();
-        // We presume that the data is utf-8, since that's what the API
-        // uses throughout.
-        String resultJSON = new String(responseData,"utf-8");
-
+        method.setEntity(new StringEntity(json,ContentType.create("text/plain","UTF-8")));
+        HttpResponse httpResponse = client.execute(method);
+        int resultCode = httpResponse.getStatusLine().getStatusCode();
+        String resultJSON = sp.convertToString(httpResponse);
         result.setReference(new VariableResult(resultCode,resultJSON));
       
         return false;
       }
       finally
       {
-        method.releaseConnection();
+        //method.releaseConnection();
       }
     }
     catch (ManifoldCFException e)

Modified: manifoldcf/branches/CONNECTORS-120/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine/PUTCommand.java
URL: http://svn.apache.org/viewvc/manifoldcf/branches/CONNECTORS-120/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine/PUTCommand.java?rev=1406879&r1=1406878&r2=1406879&view=diff
==============================================================================
--- manifoldcf/branches/CONNECTORS-120/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine/PUTCommand.java (original)
+++ manifoldcf/branches/CONNECTORS-120/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine/PUTCommand.java Thu Nov  8 00:59:11 2012
@@ -19,8 +19,21 @@
 package org.apache.manifoldcf.scriptengine;
 
 import org.apache.manifoldcf.core.interfaces.*;
-import org.apache.commons.httpclient.*;
-import org.apache.commons.httpclient.methods.*;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.entity.ContentType;
+import org.apache.http.client.HttpClient;
+import org.apache.http.HttpStatus;
+import org.apache.http.HttpException;
+import org.apache.http.client.methods.HttpPut;
+import org.apache.http.conn.ConnectTimeoutException;
+import org.apache.http.HttpResponse;
+import org.apache.http.params.BasicHttpParams;
+import org.apache.http.params.HttpParams;
+import org.apache.http.params.CoreConnectionPNames;
+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 java.io.*;
 
 /** PUT command.  This performs a REST-style PUT operation, designed to work
@@ -61,24 +74,20 @@ public class PUTCommand implements Comma
     {
       String json = configuration.toJSON();
       HttpClient client = sp.getHttpClient();
-      PutMethod method = new PutMethod(urlString);
+      HttpPut method = new HttpPut(urlString);
       try
       {
-        method.setRequestHeader("Content-type", "text/plain; charset=UTF-8");
-        method.setRequestBody(json);
-        int resultCode = client.executeMethod(method);
-        byte[] responseData = method.getResponseBody();
-        // We presume that the data is utf-8, since that's what the API
-        // uses throughout.
-        String resultJSON = new String(responseData,"utf-8");
-
+        method.setEntity(new StringEntity(json,ContentType.create("text/plain","UTF-8")));
+        HttpResponse httpResponse = client.execute(method);
+        int resultCode = httpResponse.getStatusLine().getStatusCode();
+        String resultJSON = sp.convertToString(httpResponse);
         result.setReference(new VariableResult(resultCode,resultJSON));
       
         return false;
       }
       finally
       {
-        method.releaseConnection();
+        //method.releaseConnection();
       }
     }
     catch (ManifoldCFException e)

Modified: manifoldcf/branches/CONNECTORS-120/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine/ScriptParser.java
URL: http://svn.apache.org/viewvc/manifoldcf/branches/CONNECTORS-120/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine/ScriptParser.java?rev=1406879&r1=1406878&r2=1406879&view=diff
==============================================================================
--- manifoldcf/branches/CONNECTORS-120/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine/ScriptParser.java (original)
+++ manifoldcf/branches/CONNECTORS-120/framework/script-engine/src/main/java/org/apache/manifoldcf/scriptengine/ScriptParser.java Thu Nov  8 00:59:11 2012
@@ -19,7 +19,20 @@
 
 package org.apache.manifoldcf.scriptengine;
 
-import org.apache.commons.httpclient.*;
+import org.apache.http.client.HttpClient;
+import org.apache.http.HttpStatus;
+import org.apache.http.HttpException;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.conn.ConnectTimeoutException;
+import org.apache.http.HttpResponse;
+import org.apache.http.params.BasicHttpParams;
+import org.apache.http.params.HttpParams;
+import org.apache.http.params.CoreConnectionPNames;
+import org.apache.http.util.EntityUtils;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.impl.client.DefaultRedirectStrategy;
+import org.apache.http.conn.ClientConnectionManager;
+import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
 import java.util.*;
 import java.io.*;
 
@@ -28,7 +41,7 @@ import java.io.*;
 public class ScriptParser
 {
   /** The connection manager. */
-  protected HttpConnectionManager connectionManager = null;
+  protected ClientConnectionManager connectionManager = null;
   
   /** The client instance */
   protected HttpClient httpClient = null;
@@ -1141,14 +1154,54 @@ public class ScriptParser
       t.throwException(message + ": "+t);
   }
   
+  public static String convertToString(HttpResponse httpResponse)
+    throws IOException
+  {
+    InputStream is = httpResponse.getEntity().getContent();
+    String charSet = EntityUtils.getContentCharSet(httpResponse.getEntity());
+    if (charSet == null)
+      charSet = "utf-8";
+    char[] buffer = new char[65536];
+    Reader r = new InputStreamReader(is,charSet);
+    try
+    {
+      Writer w = new StringWriter();
+      try
+      {
+        while (true)
+        {
+          int amt = r.read(buffer);
+          if (amt == -1)
+            break;
+          w.write(buffer,0,amt);
+        }
+      }
+      finally
+      {
+        w.flush();
+      }
+      return w.toString();
+    }
+    finally
+    {
+      r.close();
+    }
+  }
+  
   public HttpClient getHttpClient()
   {
     synchronized (httpClientLock)
     {
       if (httpClient == null)
       {
-        connectionManager = new MultiThreadedHttpConnectionManager();
-        httpClient = new HttpClient(connectionManager);
+        connectionManager = new ThreadSafeClientConnManager();
+        BasicHttpParams params = new BasicHttpParams();
+        params.setBooleanParameter(CoreConnectionPNames.TCP_NODELAY,true);
+        params.setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK,false);
+        //params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT,socketTimeOut);
+        DefaultHttpClient localHttpClient = new DefaultHttpClient(connectionManager,params);
+        localHttpClient.setRedirectStrategy(new DefaultRedirectStrategy());
+        httpClient = localHttpClient;
       }
     }
     return httpClient;