Permalink
Browse files

httpclient 3.x to httpclient 4.x

  • Loading branch information...
Karthik Kumar
Karthik Kumar committed Dec 30, 2011
1 parent e22c162 commit c1efb918169a018f6ea8240710fc053a61b5f04e
View
@@ -14,11 +14,9 @@
<classpathentry kind="src" path="contrib/krati/test"/>
<classpathentry kind="src" path="contrib/collections/src/java"/>
<classpathentry kind="src" path="contrib/collections/test"/>
- <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="lib/catalina-ant.jar"/>
<classpathentry kind="lib" path="lib/commons-codec-1.3.jar"/>
<classpathentry kind="lib" path="lib/commons-dbcp-1.2.2.jar"/>
- <classpathentry kind="lib" path="lib/commons-httpclient-3.1.jar"/>
<classpathentry kind="lib" path="lib/colt-1.2.0.jar"/>
<classpathentry kind="lib" path="contrib/hadoop-store-builder/lib/commons-cli-2.0-SNAPSHOT.jar"/>
<classpathentry kind="lib" path="contrib/hadoop-store-builder/lib/hadoop-0.20.2-core.jar"/>
@@ -53,5 +51,8 @@
<classpathentry kind="lib" path="lib/compress-lzf-0.9.1.jar"/>
<classpathentry kind="lib" path="lib/snappy-0.2.jar"/>
<classpathentry kind="lib" path="lib/libthrift-0.8.0.jar"/>
+ <classpathentry kind="lib" path="lib/httpclient-4.1.2.jar" sourcepath="/httpclient"/>
+ <classpathentry kind="lib" path="lib/httpcore-4.1.2.jar"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="output" path="classes"/>
</classpath>
@@ -1,4 +1,4 @@
-#Fri Sep 18 15:20:52 BST 2009
+#Fri Dec 30 14:37:10 PST 2011
eclipse.preferences.version=1
org.eclipse.jdt.core.codeComplete.argumentPrefixes=
org.eclipse.jdt.core.codeComplete.argumentSuffixes=
@@ -149,9 +149,12 @@ org.eclipse.jdt.core.formatter.indent_statements_compare_to_body=true
org.eclipse.jdt.core.formatter.indent_switchstatements_compare_to_cases=true
org.eclipse.jdt.core.formatter.indent_switchstatements_compare_to_switch=true
org.eclipse.jdt.core.formatter.indentation.size=4
+org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_field=insert
org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_local_variable=insert
-org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_member=insert
+org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_method=insert
+org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_package=insert
org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_parameter=do not insert
+org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_type=insert
org.eclipse.jdt.core.formatter.insert_new_line_after_opening_brace_in_array_initializer=do not insert
org.eclipse.jdt.core.formatter.insert_new_line_at_end_of_file_if_missing=do not insert
org.eclipse.jdt.core.formatter.insert_new_line_before_catch_in_try_statement=do not insert
View
@@ -193,7 +193,6 @@
<src path="${contrib.root.dir}" />
<classpath refid="main-classpath" />
<classpath refid="contrib-classpath" />
- <bootclasspath refid="bootref-classpath" />
</javac>
<copy todir="${contrib.classes.dir}">
<fileset dir="${contrib.root.dir}/ec2-testing/resources" />
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
@@ -22,15 +22,15 @@
import java.util.Collection;
import java.util.concurrent.TimeUnit;
-import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
-import org.apache.commons.httpclient.HostConfiguration;
-import org.apache.commons.httpclient.HttpClient;
-import org.apache.commons.httpclient.HttpVersion;
-import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
-import org.apache.commons.httpclient.cookie.CookiePolicy;
-import org.apache.commons.httpclient.params.HttpClientParams;
-import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
-import org.apache.commons.httpclient.params.HttpMethodParams;
+import org.apache.http.HttpVersion;
+import org.apache.http.client.params.CookiePolicy;
+import org.apache.http.client.params.HttpClientParams;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
+import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
+import org.apache.http.params.CoreProtocolPNames;
+import org.apache.http.params.HttpConnectionParams;
+import org.apache.http.params.HttpParams;
import voldemort.client.protocol.RequestFormatFactory;
import voldemort.client.protocol.RequestFormatType;
@@ -55,31 +55,28 @@
private static final String VOLDEMORT_USER_AGENT = "vldmrt/0.01";
- private final HttpClient httpClient;
+ private final DefaultHttpClient httpClient;
private final RequestFormatFactory requestFormatFactory;
private final boolean reroute;
public HttpStoreClientFactory(ClientConfig config) {
super(config);
- HostConfiguration hostConfig = new HostConfiguration();
- hostConfig.getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_1);
- MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
- this.httpClient = new HttpClient(connectionManager);
- this.httpClient.setHostConfiguration(hostConfig);
- HttpClientParams clientParams = this.httpClient.getParams();
- clientParams.setConnectionManagerTimeout(config.getConnectionTimeout(TimeUnit.MILLISECONDS));
- clientParams.setSoTimeout(config.getSocketTimeout(TimeUnit.MILLISECONDS));
- clientParams.setParameter(HttpMethodParams.RETRY_HANDLER,
- new DefaultHttpMethodRetryHandler(0, false));
- clientParams.setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
- clientParams.setParameter("http.useragent", VOLDEMORT_USER_AGENT);
- HttpConnectionManagerParams managerParams = this.httpClient.getHttpConnectionManager()
- .getParams();
- managerParams.setConnectionTimeout(config.getConnectionTimeout(TimeUnit.MILLISECONDS));
- managerParams.setMaxTotalConnections(config.getMaxTotalConnections());
- managerParams.setStaleCheckingEnabled(false);
- managerParams.setMaxConnectionsPerHost(httpClient.getHostConfiguration(),
- config.getMaxConnectionsPerNode());
+ ThreadSafeClientConnManager mgr = new ThreadSafeClientConnManager();
+ mgr.setMaxTotal(config.getMaxTotalConnections());
+ mgr.setDefaultMaxPerRoute(config.getMaxConnectionsPerNode());
+
+ this.httpClient = new DefaultHttpClient(mgr);
+ HttpParams clientParams = this.httpClient.getParams();
+ clientParams.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
+ HttpConnectionParams.setConnectionTimeout(clientParams,
+ config.getConnectionTimeout(TimeUnit.MILLISECONDS));
+ HttpConnectionParams.setSoTimeout(clientParams,
+ config.getSocketTimeout(TimeUnit.MILLISECONDS));
+ this.httpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0, false));
+ HttpClientParams.setCookiePolicy(clientParams, CookiePolicy.IGNORE_COOKIES);
+ clientParams.setParameter(CoreProtocolPNames.USER_AGENT, VOLDEMORT_USER_AGENT);
+ HttpConnectionParams.setStaleCheckingEnabled(clientParams, false);
+
this.reroute = config.getRoutingTier().equals(RoutingTier.SERVER);
this.requestFormatFactory = new RequestFormatFactory();
}
@@ -24,10 +24,10 @@
import java.util.List;
import java.util.Map;
-import org.apache.commons.httpclient.HttpClient;
-import org.apache.commons.httpclient.HttpException;
-import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
-import org.apache.commons.httpclient.methods.PostMethod;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.ByteArrayEntity;
import voldemort.VoldemortException;
import voldemort.client.protocol.RequestFormat;
@@ -70,9 +70,9 @@ public HttpStore(String storeName,
public boolean delete(ByteArray key, Version version) throws VoldemortException {
StoreUtils.assertValidKey(key);
- PostMethod method = null;
+ HttpPost method = null;
try {
- method = new PostMethod(this.storeUrl);
+ method = new HttpPost(this.storeUrl);
ByteArrayOutputStream outputBytes = new ByteArrayOutputStream();
requestFormat.writeDeleteRequest(new DataOutputStream(outputBytes),
storeName,
@@ -84,17 +84,14 @@ public boolean delete(ByteArray key, Version version) throws VoldemortException
} catch(IOException e) {
throw new UnreachableStoreException("Could not connect to " + storeUrl + " for "
+ storeName, e);
- } finally {
- if(method != null)
- method.releaseConnection();
}
}
public List<Versioned<byte[]>> get(ByteArray key, byte[] transforms) throws VoldemortException {
StoreUtils.assertValidKey(key);
- PostMethod method = null;
+ HttpPost method = null;
try {
- method = new PostMethod(this.storeUrl);
+ method = new HttpPost(this.storeUrl);
ByteArrayOutputStream outputBytes = new ByteArrayOutputStream();
requestFormat.writeGetRequest(new DataOutputStream(outputBytes),
storeName,
@@ -106,19 +103,16 @@ public boolean delete(ByteArray key, Version version) throws VoldemortException
} catch(IOException e) {
throw new UnreachableStoreException("Could not connect to " + storeUrl + " for "
+ storeName, e);
- } finally {
- if(method != null)
- method.releaseConnection();
}
}
public Map<ByteArray, List<Versioned<byte[]>>> getAll(Iterable<ByteArray> keys,
Map<ByteArray, byte[]> transforms)
throws VoldemortException {
StoreUtils.assertValidKeys(keys);
- PostMethod method = null;
+ HttpPost method = null;
try {
- method = new PostMethod(this.storeUrl);
+ method = new HttpPost(this.storeUrl);
ByteArrayOutputStream outputBytes = new ByteArrayOutputStream();
requestFormat.writeGetAllRequest(new DataOutputStream(outputBytes),
storeName,
@@ -130,18 +124,15 @@ public boolean delete(ByteArray key, Version version) throws VoldemortException
} catch(IOException e) {
throw new UnreachableStoreException("Could not connect to " + storeUrl + " for "
+ storeName, e);
- } finally {
- if(method != null)
- method.releaseConnection();
}
}
public void put(ByteArray key, Versioned<byte[]> versioned, byte[] transforms)
throws VoldemortException {
StoreUtils.assertValidKey(key);
- PostMethod method = null;
+ HttpPost method = null;
try {
- method = new PostMethod(this.storeUrl);
+ method = new HttpPost(this.storeUrl);
ByteArrayOutputStream outputBytes = new ByteArrayOutputStream();
requestFormat.writePutRequest(new DataOutputStream(outputBytes),
storeName,
@@ -155,23 +146,19 @@ public void put(ByteArray key, Versioned<byte[]> versioned, byte[] transforms)
} catch(IOException e) {
throw new UnreachableStoreException("Could not connect to " + storeUrl + " for "
+ storeName, e);
- } finally {
- if(method != null)
- method.releaseConnection();
}
}
- private DataInputStream executeRequest(PostMethod method, ByteArrayOutputStream output) {
+ private DataInputStream executeRequest(HttpPost method, ByteArrayOutputStream output) {
try {
- method.setRequestEntity(new ByteArrayRequestEntity(output.toByteArray()));
- int response = httpClient.executeMethod(method);
- if(response != HttpURLConnection.HTTP_OK)
+ method.setEntity(new ByteArrayEntity(output.toByteArray()));
+ HttpResponse response = httpClient.execute(method);
+ int statusCode = response.getStatusLine().getStatusCode();
+ if(statusCode != HttpURLConnection.HTTP_OK)
throw new UnreachableStoreException("HTTP request to store " + storeName
+ " returned status code " + response + " "
- + method.getStatusText());
- return new DataInputStream(method.getResponseBodyAsStream());
- } catch(HttpException e) {
- throw new VoldemortException(e);
+ + response.getStatusLine().getReasonPhrase());
+ return new DataInputStream(response.getEntity().getContent());
} catch(IOException e) {
throw new UnreachableStoreException("Could not connect to " + storeUrl + " for "
+ storeName, e);
@@ -190,9 +177,9 @@ public Object getCapability(StoreCapabilityType capability) {
public List<Version> getVersions(ByteArray key) {
StoreUtils.assertValidKey(key);
- PostMethod method = null;
+ HttpPost method = null;
try {
- method = new PostMethod(this.storeUrl);
+ method = new HttpPost(this.storeUrl);
ByteArrayOutputStream outputBytes = new ByteArrayOutputStream();
requestFormat.writeGetVersionRequest(new DataOutputStream(outputBytes),
storeName,
@@ -203,9 +190,6 @@ public Object getCapability(StoreCapabilityType capability) {
} catch(IOException e) {
throw new UnreachableStoreException("Could not connect to " + storeUrl + " for "
+ storeName, e);
- } finally {
- if(method != null)
- method.releaseConnection();
}
}
}
Oops, something went wrong.

0 comments on commit c1efb91

Please sign in to comment.