Permalink
Browse files

Replace end of life apache http client 3.1 with 4.1

  • Loading branch information...
1 parent c251928 commit fa6f20bf1d574ab6e693b5b4a51e5a7a4d44e726 @russelldb russelldb committed Jul 11, 2011
Showing with 819 additions and 556 deletions.
  1. +1 −0 TODO.org
  2. +5 −5 pom.xml
  3. +1 −1 src/main/java/com/basho/riak/client/RiakClient.java
  4. +5 −8 src/main/java/com/basho/riak/client/RiakConfig.java
  5. +30 −25 src/main/java/com/basho/riak/client/RiakObject.java
  6. +1 −3 src/main/java/com/basho/riak/client/http/RiakClient.java
  7. +5 −7 src/main/java/com/basho/riak/client/http/RiakConfig.java
  8. +29 −24 src/main/java/com/basho/riak/client/http/RiakObject.java
  9. +9 −2 src/main/java/com/basho/riak/client/http/plain/RiakResponseException.java
  10. +13 −3 src/main/java/com/basho/riak/client/http/request/RequestMeta.java
  11. +24 −9 src/main/java/com/basho/riak/client/http/response/DefaultHttpResponse.java
  12. +7 −2 src/main/java/com/basho/riak/client/http/response/HttpResponse.java
  13. +10 −2 src/main/java/com/basho/riak/client/http/response/HttpResponseDecorator.java
  14. +9 −2 src/main/java/com/basho/riak/client/http/response/RiakResponseRuntimeException.java
  15. +4 −3 src/main/java/com/basho/riak/client/http/response/StreamHandler.java
  16. +95 −54 src/main/java/com/basho/riak/client/http/util/ClientHelper.java
  17. +49 −27 src/main/java/com/basho/riak/client/http/util/ClientUtils.java
  18. +3 −3 src/main/java/com/basho/riak/client/http/util/Multipart.java
  19. +9 −2 src/main/java/com/basho/riak/client/plain/RiakResponseException.java
  20. +2 −3 src/main/java/com/basho/riak/client/raw/http/ConversionUtil.java
  21. +3 −4 src/main/java/com/basho/riak/client/raw/pbc/ConversionUtil.java
  22. +13 −3 src/main/java/com/basho/riak/client/request/RequestMeta.java
  23. +26 −9 src/main/java/com/basho/riak/client/response/DefaultHttpResponse.java
  24. +7 −2 src/main/java/com/basho/riak/client/response/HttpResponse.java
  25. +11 −2 src/main/java/com/basho/riak/client/response/HttpResponseDecorator.java
  26. +9 −2 src/main/java/com/basho/riak/client/response/RiakResponseRuntimeException.java
  27. +3 −3 src/main/java/com/basho/riak/client/response/StreamHandler.java
  28. +111 −59 src/main/java/com/basho/riak/client/util/ClientHelper.java
  29. +62 −40 src/main/java/com/basho/riak/client/util/ClientUtils.java
  30. +4 −0 src/main/java/com/basho/riak/client/util/Constants.java
  31. +3 −3 src/main/java/com/basho/riak/client/util/Multipart.java
  32. +45 −43 src/test/java/com/basho/riak/client/http/TestRiakObject.java
  33. +2 −2 src/test/java/com/basho/riak/client/http/itest/ITestBasic.java
  34. +4 −5 src/test/java/com/basho/riak/client/http/itest/ITestMapReduce.java
  35. +4 −11 src/test/java/com/basho/riak/client/http/itest/Utils.java
  36. +40 −24 src/test/java/com/basho/riak/client/http/response/TestDefaultHttpResponse.java
  37. +2 −2 src/test/java/com/basho/riak/client/http/response/TestFetchResponse.java
  38. +2 −2 src/test/java/com/basho/riak/client/http/response/TestHttpResponseDecorator.java
  39. +142 −94 src/test/java/com/basho/riak/client/http/util/TestClientHelper.java
  40. +11 −10 src/test/java/com/basho/riak/client/http/util/TestClientUtils.java
  41. +4 −7 src/test/java/com/basho/riak/pbc/itest/ITestMapReduce.java
  42. +0 −44 src/test/java/com/basho/riak/pbc/itest/Utils.java
View
1 TODO.org
@@ -6,6 +6,7 @@
Include things like TCP_NODELAY etc
* TODO Push retrier down the stack to Raw level
* TODO Conversion needs more thought, especially around the key
+** TODO separate convert to and convert from domain object
* TODO Have store/fetch/delete Object operations use the pass through non converter if the type they are working with is IRiakObject
* TODO Have key as a parameter to convert?
* TODO remove vclock as param to convert?
View
10 pom.xml
@@ -61,11 +61,11 @@
<dependencies>
<!-- Apache Commons -->
- <dependency>
- <groupId>commons-httpclient</groupId>
- <artifactId>commons-httpclient</artifactId>
- <version>3.1</version>
- </dependency>
+ <dependency>
+ <groupId>org.apache.httpcomponents</groupId>
+ <artifactId>httpclient</artifactId>
+ <version>4.1.1</version>
+ </dependency>
<!-- Protocol BUffers -->
<dependency>
<groupId>com.google.protobuf</groupId>
View
2 src/main/java/com/basho/riak/client/RiakClient.java
@@ -17,7 +17,7 @@
import java.util.Map;
import java.util.Set;
-import org.apache.commons.httpclient.HttpClient;
+import org.apache.http.client.HttpClient;
import org.json.JSONException;
import org.json.JSONObject;
View
13 src/main/java/com/basho/riak/client/RiakConfig.java
@@ -16,11 +16,8 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
-import org.apache.commons.httpclient.HttpClient;
-import org.apache.commons.httpclient.HttpMethodRetryHandler;
-import org.apache.commons.httpclient.params.HttpClientParams;
-import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
-
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.HttpRequestRetryHandler;
/**
* Configuration settings for connecting to a Riak instance such as the base
* Riak URL and HttpClient settings. A pre-constructed HttpClient can also be
@@ -45,7 +42,7 @@
private HttpClient httpClient = null;
private Integer timeout = null;
private Integer maxConnections = null;
- private HttpMethodRetryHandler retryHandler = null;
+ private HttpRequestRetryHandler retryHandler = null;
public RiakConfig() {}
@@ -184,11 +181,11 @@ public Integer getMaxConnections() {
*
* @see org.apache.commons.httpclient.DefaultHttpMethodRetryHandler
*/
- public HttpMethodRetryHandler getRetryHandler() {
+ public HttpRequestRetryHandler getRetryHandler() {
return retryHandler;
}
- public void setRetryHandler(HttpMethodRetryHandler retryHandler) {
+ public void setRetryHandler(HttpRequestRetryHandler retryHandler) {
this.retryHandler = retryHandler;
}
}
View
55 src/main/java/com/basho/riak/client/RiakObject.java
@@ -24,13 +24,15 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
-import org.apache.commons.httpclient.HttpMethod;
-import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
-import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
-import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
-import org.apache.commons.httpclient.util.DateParseException;
-import org.apache.commons.httpclient.util.DateUtil;
-
+import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
+import org.apache.http.client.methods.HttpRequestBase;
+import org.apache.http.entity.AbstractHttpEntity;
+import org.apache.http.entity.ByteArrayEntity;
+import org.apache.http.entity.InputStreamEntity;
+import org.apache.http.impl.cookie.DateParseException;
+import org.apache.http.impl.cookie.DateUtils;
+
+import com.basho.riak.client.http.util.ClientUtils;
import com.basho.riak.client.request.RequestMeta;
import com.basho.riak.client.request.RiakWalkSpec;
import com.basho.riak.client.response.FetchResponse;
@@ -572,7 +574,7 @@ public String getLastmod() {
*/
public Date getLastmodAsDate() {
try {
- return DateUtil.parseDate(lastmod);
+ return DateUtils.parseDate(lastmod);
} catch (DateParseException e) {
return null;
}
@@ -739,39 +741,42 @@ public LinkBuilder walk(boolean keep) {
* user-defined metadata and vclock as HTTP headers and the value as the
* body. Used by {@link RiakClient} to create PUT requests.
*/
- public void writeToHttpMethod(HttpMethod httpMethod) {
+ public void writeToHttpMethod(HttpRequestBase httpMethod) {
// Serialize headers
String basePath = getBasePathFromHttpMethod(httpMethod);
writeLinks(httpMethod, basePath);
for (String name : userMetaData.keySet()) {
- httpMethod.setRequestHeader(Constants.HDR_USERMETA_REQ_PREFIX + name, userMetaData.get(name));
+ httpMethod.addHeader(Constants.HDR_USERMETA_REQ_PREFIX + name, userMetaData.get(name));
}
if (vclock != null) {
- httpMethod.setRequestHeader(Constants.HDR_VCLOCK, vclock);
+ httpMethod.addHeader(Constants.HDR_VCLOCK, vclock);
}
// Serialize body
- if (httpMethod instanceof EntityEnclosingMethod) {
- EntityEnclosingMethod entityEnclosingMethod = (EntityEnclosingMethod) httpMethod;
-
+ if (httpMethod instanceof HttpEntityEnclosingRequestBase) {
+ HttpEntityEnclosingRequestBase entityEnclosingMethod = (HttpEntityEnclosingRequestBase) httpMethod;
+ AbstractHttpEntity entity = null;
// Any value set using setValueAsStream() has precedent over value
// set using setValue()
if (valueStream != null) {
if (valueStreamLength != null && valueStreamLength >= 0) {
- entityEnclosingMethod.setRequestEntity(new InputStreamRequestEntity(valueStream, valueStreamLength,
- contentType));
+ entity = new InputStreamEntity(valueStream, valueStreamLength);
} else {
- entityEnclosingMethod.setRequestEntity(new InputStreamRequestEntity(valueStream, contentType));
+ // since apache http client 4.1 no longer supports buffering stream entities, but we can't change API
+ // behaviour, here we have to buffer the whole content
+ entity = new ByteArrayEntity(ClientUtils.bufferStream(valueStream));
}
} else if (value != null) {
- entityEnclosingMethod.setRequestEntity(new ByteArrayRequestEntity(value, contentType));
+ entity = new ByteArrayEntity(value);
} else {
- entityEnclosingMethod.setRequestEntity(new ByteArrayRequestEntity(EMPTY, contentType));
+ entity = new ByteArrayEntity(EMPTY);
}
+ entity.setContentType(contentType);
+ entityEnclosingMethod.setEntity(entity);
}
}
- private void writeLinks(HttpMethod httpMethod, String basePath) {
+ private void writeLinks(HttpRequestBase httpMethod, String basePath) {
StringBuilder linkHeader = new StringBuilder();
for (RiakLink link : this.links) {
@@ -793,20 +798,20 @@ private void writeLinks(HttpMethod httpMethod, String basePath) {
// To avoid (MochiWeb) problems with too long headers, flush if
// it grows too big:
if (linkHeader.length() > 2000) {
- httpMethod.addRequestHeader(Constants.HDR_LINK, linkHeader.toString());
+ httpMethod.addHeader(Constants.HDR_LINK, linkHeader.toString());
linkHeader = new StringBuilder();
}
}
if (linkHeader.length() > 0) {
- httpMethod.addRequestHeader(Constants.HDR_LINK, linkHeader.toString());
+ httpMethod.addHeader(Constants.HDR_LINK, linkHeader.toString());
}
}
- String getBasePathFromHttpMethod(HttpMethod httpMethod) {
- if (httpMethod == null || httpMethod.getPath() == null)
+ String getBasePathFromHttpMethod(HttpRequestBase httpMethod) {
+ if (httpMethod == null || httpMethod.getURI() == null)
return "";
- String path = httpMethod.getPath();
+ String path = httpMethod.getURI().getPath();
int idx = path.length() - 1;
// ignore any trailing slash
View
4 src/main/java/com/basho/riak/client/http/RiakClient.java
@@ -17,11 +17,10 @@
import java.util.Map;
import java.util.Set;
-import org.apache.commons.httpclient.HttpClient;
+import org.apache.http.client.HttpClient;
import org.json.JSONException;
import org.json.JSONObject;
-import com.basho.riak.client.RiakLink;
import com.basho.riak.client.http.request.MapReduceBuilder;
import com.basho.riak.client.http.request.RequestMeta;
import com.basho.riak.client.http.request.RiakWalkSpec;
@@ -37,7 +36,6 @@
import com.basho.riak.client.http.response.StreamHandler;
import com.basho.riak.client.http.response.WalkResponse;
import com.basho.riak.client.http.util.ClientHelper;
-import com.basho.riak.client.http.util.ClientUtils;
import com.basho.riak.client.http.util.Constants;
/**
View
12 src/main/java/com/basho/riak/client/http/RiakConfig.java
@@ -16,10 +16,8 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
-import org.apache.commons.httpclient.HttpClient;
-import org.apache.commons.httpclient.HttpMethodRetryHandler;
-import org.apache.commons.httpclient.params.HttpClientParams;
-import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.HttpRequestRetryHandler;
/**
* Configuration settings for connecting to a Riak instance such as the base
@@ -36,7 +34,7 @@
private HttpClient httpClient = null;
private Integer timeout = null;
private Integer maxConnections = null;
- private HttpMethodRetryHandler retryHandler = null;
+ private HttpRequestRetryHandler retryHandler = null;
public RiakConfig() {}
@@ -175,11 +173,11 @@ public Integer getMaxConnections() {
*
* @see org.apache.commons.httpclient.DefaultHttpMethodRetryHandler
*/
- public HttpMethodRetryHandler getRetryHandler() {
+ public HttpRequestRetryHandler getRetryHandler() {
return retryHandler;
}
- public void setRetryHandler(HttpMethodRetryHandler retryHandler) {
+ public void setRetryHandler(HttpRequestRetryHandler retryHandler) {
this.retryHandler = retryHandler;
}
}
View
53 src/main/java/com/basho/riak/client/http/RiakObject.java
@@ -25,12 +25,13 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
-import org.apache.commons.httpclient.HttpMethod;
-import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
-import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
-import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
-import org.apache.commons.httpclient.util.DateParseException;
-import org.apache.commons.httpclient.util.DateUtil;
+import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
+import org.apache.http.client.methods.HttpRequestBase;
+import org.apache.http.entity.AbstractHttpEntity;
+import org.apache.http.entity.ByteArrayEntity;
+import org.apache.http.entity.InputStreamEntity;
+import org.apache.http.impl.cookie.DateParseException;
+import org.apache.http.impl.cookie.DateUtils;
import com.basho.riak.client.http.request.RequestMeta;
import com.basho.riak.client.http.request.RiakWalkSpec;
@@ -40,6 +41,7 @@
import com.basho.riak.client.http.response.RiakResponseRuntimeException;
import com.basho.riak.client.http.response.StoreResponse;
import com.basho.riak.client.http.response.WalkResponse;
+import com.basho.riak.client.http.util.ClientUtils;
import com.basho.riak.client.http.util.Constants;
/**
@@ -522,7 +524,7 @@ public String getLastmod() {
*/
public Date getLastmodAsDate() {
try {
- return DateUtil.parseDate(lastmod);
+ return DateUtils.parseDate(lastmod);
} catch (DateParseException e) {
return null;
}
@@ -669,39 +671,42 @@ public LinkBuilder walk(boolean keep) {
/* (non-Javadoc)
* @see com.basho.riak.client.HttpRiakObject#writeToHttpMethod(org.apache.commons.httpclient.HttpMethod)
*/
- public void writeToHttpMethod(HttpMethod httpMethod) {
+ public void writeToHttpMethod(HttpRequestBase httpMethod) {
// Serialize headers
String basePath = getBasePathFromHttpMethod(httpMethod);
writeLinks(httpMethod, basePath);
for (String name : userMetaData.keySet()) {
- httpMethod.setRequestHeader(Constants.HDR_USERMETA_REQ_PREFIX + name, userMetaData.get(name));
+ httpMethod.addHeader(Constants.HDR_USERMETA_REQ_PREFIX + name, userMetaData.get(name));
}
if (vclock != null) {
- httpMethod.setRequestHeader(Constants.HDR_VCLOCK, vclock);
+ httpMethod.addHeader(Constants.HDR_VCLOCK, vclock);
}
// Serialize body
- if (httpMethod instanceof EntityEnclosingMethod) {
- EntityEnclosingMethod entityEnclosingMethod = (EntityEnclosingMethod) httpMethod;
-
+ if (httpMethod instanceof HttpEntityEnclosingRequestBase) {
+ HttpEntityEnclosingRequestBase entityEnclosingMethod = (HttpEntityEnclosingRequestBase) httpMethod;
+ AbstractHttpEntity entity = null;
// Any value set using setValueAsStream() has precedent over value
// set using setValue()
if (valueStream != null) {
if (valueStreamLength != null && valueStreamLength >= 0) {
- entityEnclosingMethod.setRequestEntity(new InputStreamRequestEntity(valueStream, valueStreamLength,
- contentType));
+ entity = new InputStreamEntity(valueStream, valueStreamLength);
} else {
- entityEnclosingMethod.setRequestEntity(new InputStreamRequestEntity(valueStream, contentType));
+ // since apache http client 4.1 no longer supports buffering stream entities, but we can't change API
+ // behaviour, here we have to buffer the whole content
+ entity = new ByteArrayEntity(ClientUtils.bufferStream(valueStream));
}
} else if (value != null) {
- entityEnclosingMethod.setRequestEntity(new ByteArrayRequestEntity(value, contentType));
+ entity = new ByteArrayEntity(value);
} else {
- entityEnclosingMethod.setRequestEntity(new ByteArrayRequestEntity(EMPTY, contentType));
+ entity = new ByteArrayEntity(EMPTY);
}
+ entity.setContentType(contentType);
+ entityEnclosingMethod.setEntity(entity);
}
}
- private void writeLinks(HttpMethod httpMethod, String basePath) {
+ private void writeLinks(HttpRequestBase httpMethod, String basePath) {
StringBuilder linkHeader = new StringBuilder();
for (RiakLink link : this.links) {
@@ -723,20 +728,20 @@ private void writeLinks(HttpMethod httpMethod, String basePath) {
// To avoid (MochiWeb) problems with too long headers, flush if
// it grows too big:
if (linkHeader.length() > 2000) {
- httpMethod.addRequestHeader(Constants.HDR_LINK, linkHeader.toString());
+ httpMethod.addHeader(Constants.HDR_LINK, linkHeader.toString());
linkHeader = new StringBuilder();
}
}
if (linkHeader.length() > 0) {
- httpMethod.addRequestHeader(Constants.HDR_LINK, linkHeader.toString());
+ httpMethod.addHeader(Constants.HDR_LINK, linkHeader.toString());
}
}
- String getBasePathFromHttpMethod(HttpMethod httpMethod) {
- if (httpMethod == null || httpMethod.getPath() == null)
+ String getBasePathFromHttpMethod(HttpRequestBase httpMethod) {
+ if (httpMethod == null || httpMethod.getURI() == null)
return "";
- String path = httpMethod.getPath();
+ String path = httpMethod.getURI().getPath();
int idx = path.length() - 1;
// ignore any trailing slash
View
11 src/main/java/com/basho/riak/client/http/plain/RiakResponseException.java
@@ -16,7 +16,7 @@
import java.io.InputStream;
import java.util.Map;
-import org.apache.commons.httpclient.HttpMethod;
+import org.apache.http.client.methods.HttpRequestBase;
import com.basho.riak.client.http.response.HttpResponse;
import com.basho.riak.client.http.response.RiakResponseRuntimeException;
@@ -58,7 +58,7 @@ public String getBucket() {
return impl.getHttpHeaders();
}
- public HttpMethod getHttpMethod() {
+ public HttpRequestBase getHttpMethod() {
return impl.getHttpMethod();
}
@@ -81,4 +81,11 @@ public boolean isSuccess() {
public void close() {
impl.close();
}
+
+ /* (non-Javadoc)
+ * @see com.basho.riak.client.http.response.HttpResponse#getHttpResponse()
+ */
+ public org.apache.http.HttpResponse getHttpResponse() {
+ return impl.getHttpResponse();
+ }
}
View
16 src/main/java/com/basho/riak/client/http/request/RequestMeta.java
@@ -18,7 +18,7 @@
import java.util.LinkedHashMap;
import java.util.Map;
-import org.apache.commons.httpclient.util.DateUtil;
+import org.apache.http.impl.cookie.DateUtils;
import com.basho.riak.client.http.util.ClientUtils;
import com.basho.riak.client.http.util.Constants;
@@ -168,6 +168,16 @@ public String getQueryParams() {
return qp.toString();
}
+ /**
+ * Get a copy of the query params in a map
+ *
+ * @return a {@link LinkedHashMap} of the query params (copy, does not
+ * read/write through)
+ */
+ public Map<String, String> getQueryParamMap() {
+ return new LinkedHashMap<String, String>(queryParams);
+ }
+
/** Convenience method for the X-Riak-ClientId HTTP header */
public String getClientId() {
return getHeader(Constants.HDR_CLIENT_ID);
@@ -187,7 +197,7 @@ public RequestMeta setIfModifiedSince(String lastmod) {
}
public RequestMeta setIfModifiedSince(Date lastmod) {
- return setHeader(Constants.HDR_IF_MODIFIED_SINCE, DateUtil.formatDate(lastmod));
+ return setHeader(Constants.HDR_IF_MODIFIED_SINCE, DateUtils.formatDate(lastmod));
}
/** Convenience method for the If-Unmodified-Since HTTP header */
@@ -200,7 +210,7 @@ public RequestMeta setIfUnmodifiedSince(String lastmod) {
}
public RequestMeta setIfUnmodifiedSince(Date lastmod) {
- return setHeader(Constants.HDR_IF_UNMODIFIED_SINCE, DateUtil.formatDate(lastmod));
+ return setHeader(Constants.HDR_IF_UNMODIFIED_SINCE, DateUtils.formatDate(lastmod));
}
/** Convenience method for the If-Match HTTP header */
View
33 src/main/java/com/basho/riak/client/http/response/DefaultHttpResponse.java
@@ -16,11 +16,13 @@
import static com.basho.riak.client.util.CharsetUtils.asString;
import static com.basho.riak.client.util.CharsetUtils.getCharset;
+import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
-import org.apache.commons.httpclient.HttpMethod;
+import org.apache.http.client.methods.HttpRequestBase;
+import org.apache.http.util.EntityUtils;
import com.basho.riak.client.http.util.Constants;
@@ -36,10 +38,11 @@
private Map<String, String> headers = null;
private byte[] body = null;
private InputStream stream = null;
- private HttpMethod httpMethod = null;
+ private org.apache.http.HttpResponse httpResponse = null;
+ private HttpRequestBase httpMethod = null;
public DefaultHttpResponse(String bucket, String key, int status, Map<String, String> headers, byte[] body,
- InputStream stream, HttpMethod httpMethod) {
+ InputStream stream, org.apache.http.HttpResponse httpResponse, HttpRequestBase httpMethod) {
if (headers == null) {
headers = new HashMap<String, String>();
}
@@ -52,6 +55,7 @@ public DefaultHttpResponse(String bucket, String key, int status, Map<String, St
this.body = body.clone();
}
this.stream = stream;
+ this.httpResponse = httpResponse;
this.httpMethod = httpMethod;
}
@@ -93,14 +97,14 @@ public boolean isStreamed() {
return stream != null;
}
- public HttpMethod getHttpMethod() {
+ public HttpRequestBase getHttpMethod() {
return httpMethod;
}
public boolean isSuccess() {
String method = null;
if (httpMethod != null) {
- method = httpMethod.getName();
+ method = httpMethod.getMethod();
}
return (status >= 200 && status < 300) ||
@@ -112,16 +116,27 @@ public boolean isSuccess() {
public boolean isError() {
String method = null;
- if (httpMethod != null) {
- method = httpMethod.getName();
+ if (httpResponse != null) {
+ method = httpMethod.getMethod();
}
return (status < 100 || status >= 400) && !((status == 404) && Constants.HTTP_DELETE_METHOD.equals(method));
}
public void close() {
- if (httpMethod != null) {
- httpMethod.releaseConnection();
+ if (httpResponse != null) {
+ try {
+ EntityUtils.consume(httpResponse.getEntity());
+ } catch (IOException e) {
+ throw new RiakIORuntimeException(e);
+ }
}
}
+
+ /* (non-Javadoc)
+ * @see com.basho.riak.client.http.response.HttpResponse#getHttpResponse()
+ */
+ public org.apache.http.HttpResponse getHttpResponse() {
+ return httpResponse;
+ }
}
View
9 src/main/java/com/basho/riak/client/http/response/HttpResponse.java
@@ -16,7 +16,7 @@
import java.io.InputStream;
import java.util.Map;
-import org.apache.commons.httpclient.HttpMethod;
+import org.apache.http.client.methods.HttpRequestBase;
/**
* HTTP response information resulting from some HTTP operation
@@ -66,7 +66,7 @@
* note that the connection will already be closed, so calling
* getHttpMethod().getResponseBodyAsStream() will return null.
*/
- public HttpMethod getHttpMethod();
+ public HttpRequestBase getHttpMethod();
/**
* Whether the HTTP response is considered a success. Generally this
@@ -84,4 +84,9 @@
* Releases the underlying the HTTP connection when the response is streamed
*/
public void close();
+
+ /**
+ * @return
+ */
+ public org.apache.http.HttpResponse getHttpResponse();
}
View
12 src/main/java/com/basho/riak/client/http/response/HttpResponseDecorator.java
@@ -17,7 +17,8 @@
import java.util.HashMap;
import java.util.Map;
-import org.apache.commons.httpclient.HttpMethod;
+import org.apache.http.client.methods.HttpRequestBase;
+
/**
* A default decorator implementation for HttpResponse
@@ -72,7 +73,7 @@ public boolean isStreamed() {
return impl.getHttpHeaders();
}
- public HttpMethod getHttpMethod() {
+ public HttpRequestBase getHttpMethod() {
if (impl == null)
return null;
return impl.getHttpMethod();
@@ -101,4 +102,11 @@ public void close() {
impl.close();
}
}
+
+ /* (non-Javadoc)
+ * @see com.basho.riak.client.http.response.HttpResponse#getHttpResponse()
+ */
+ public org.apache.http.HttpResponse getHttpResponse() {
+ return impl.getHttpResponse();
+ }
}
View
11 src/main/java/com/basho/riak/client/http/response/RiakResponseRuntimeException.java
@@ -16,7 +16,7 @@
import java.io.InputStream;
import java.util.Map;
-import org.apache.commons.httpclient.HttpMethod;
+import org.apache.http.client.methods.HttpRequestBase;
/**
* Thrown when the Riak server returns a malformed response. The HTTP response
@@ -83,7 +83,7 @@ public String getBucket() {
return response.getHttpHeaders();
}
- public HttpMethod getHttpMethod() {
+ public HttpRequestBase getHttpMethod() {
if (response == null)
return null;
return response.getHttpMethod();
@@ -113,4 +113,11 @@ public void close() {
if (response != null)
response.close();
}
+
+ /* (non-Javadoc)
+ * @see com.basho.riak.client.http.response.HttpResponse#getHttpResponse()
+ */
+ public org.apache.http.HttpResponse getHttpResponse() {
+ return response.getHttpResponse();
+ }
}
View
7 src/main/java/com/basho/riak/client/http/response/StreamHandler.java
@@ -16,7 +16,8 @@
import java.io.InputStream;
import java.util.Map;
-import org.apache.commons.httpclient.HttpMethod;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.HttpRequestBase;
/**
* Used with RiakClient.stream() to process the HTTP responses for fetch
@@ -38,11 +39,11 @@
* @param in
* InputStream of the object's value (body)
* @param httpMethod
- * The original {@link HttpMethod} used to make the request. Its
+ * The original {@link HttpRequestBase} used to make the request. Its
* connection is still open and will be closed by the caller on
* return.
* @return true if the object was processed; false otherwise
*/
public boolean process(String bucket, String key, int status, Map<String, String> headers, InputStream in,
- HttpMethod httpMethod);
+ HttpResponse httpMethod);
}
View
149 src/main/java/com/basho/riak/client/http/util/ClientHelper.java
@@ -17,18 +17,28 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.LinkedList;
+import java.util.List;
import java.util.Map;
import org.apache.commons.codec.binary.Base64;
-import org.apache.commons.httpclient.HttpClient;
-import org.apache.commons.httpclient.HttpMethod;
-import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
-import org.apache.commons.httpclient.methods.DeleteMethod;
-import org.apache.commons.httpclient.methods.GetMethod;
-import org.apache.commons.httpclient.methods.HeadMethod;
-import org.apache.commons.httpclient.methods.PostMethod;
-import org.apache.commons.httpclient.methods.PutMethod;
-import org.apache.commons.httpclient.methods.StringRequestEntity;
+import org.apache.http.HttpEntity;
+import org.apache.http.NameValuePair;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpDelete;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpHead;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.client.methods.HttpPut;
+import org.apache.http.client.methods.HttpRequestBase;
+import org.apache.http.client.utils.URIUtils;
+import org.apache.http.client.utils.URLEncodedUtils;
+import org.apache.http.entity.ByteArrayEntity;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.message.BasicNameValuePair;
+import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import com.basho.riak.client.http.RiakClient;
@@ -42,6 +52,7 @@
import com.basho.riak.client.http.response.RiakIORuntimeException;
import com.basho.riak.client.http.response.RiakResponseRuntimeException;
import com.basho.riak.client.http.response.StreamHandler;
+import com.basho.riak.client.util.CharsetUtils;
/**
* This class performs the actual HTTP requests underlying the operations in
@@ -95,8 +106,10 @@ public HttpResponse setBucketSchema(String bucket, JSONObject schema, RequestMet
meta.setHeader(Constants.HDR_ACCEPT, Constants.CTYPE_JSON);
- PutMethod put = new PutMethod(ClientUtils.makeURI(config, bucket));
- put.setRequestEntity(new ByteArrayRequestEntity(utf8StringToBytes(schema.toString()), Constants.CTYPE_JSON));
+ HttpPut put = new HttpPut(ClientUtils.makeURI(config, bucket));
+ ByteArrayEntity entity = new ByteArrayEntity(utf8StringToBytes(schema.toString()));
+ entity.setContentType(Constants.CTYPE_JSON_UTF8);
+ put.setEntity(entity);
return executeMethod(bucket, null, put, meta);
}
@@ -124,7 +137,7 @@ public HttpResponse getBucketSchema(String bucket, RequestMeta meta) {
public HttpResponse listBuckets() {
final RequestMeta meta = new RequestMeta();
meta.setQueryParam(Constants.QP_BUCKETS, Constants.LIST_BUCKETS);
- GetMethod get = new GetMethod(config.getUrl());
+ HttpGet get = new HttpGet(config.getUrl());
return executeMethod(null, null, get, meta);
}
@@ -151,7 +164,7 @@ public HttpResponse listBucket(String bucket, RequestMeta meta, boolean streamRe
meta.setHeader(Constants.HDR_ACCEPT, Constants.CTYPE_JSON);
}
- GetMethod get = new GetMethod(ClientUtils.makeURI(config, bucket));
+ HttpGet get = new HttpGet(ClientUtils.makeURI(config, bucket));
return executeMethod(bucket, null, get, meta, streamResponse);
}
@@ -172,7 +185,7 @@ public HttpResponse store(RiakObject object, RequestMeta meta) {
String bucket = object.getBucket();
String key = object.getKey();
String url = ClientUtils.makeURI(config, bucket, key);
- PutMethod put = new PutMethod(url);
+ HttpPut put = new HttpPut(url);
object.writeToHttpMethod(put);
return executeMethod(bucket, key, put, meta);
@@ -188,7 +201,7 @@ public HttpResponse fetchMeta(String bucket, String key, RequestMeta meta) {
if (meta.getQueryParam(Constants.QP_R) == null) {
meta.setQueryParam(Constants.QP_R, Constants.DEFAULT_R.toString());
}
- HeadMethod head = new HeadMethod(ClientUtils.makeURI(config, bucket, key));
+ HttpHead head = new HttpHead(ClientUtils.makeURI(config, bucket, key));
return executeMethod(bucket, key, head, meta);
}
@@ -216,7 +229,7 @@ public HttpResponse fetch(String bucket, String key, RequestMeta meta, boolean s
if (meta.getQueryParam(Constants.QP_R) == null) {
meta.setQueryParam(Constants.QP_R, Constants.DEFAULT_R.toString());
}
- GetMethod get = new GetMethod(ClientUtils.makeURI(config, bucket, key));
+ HttpGet get = new HttpGet(ClientUtils.makeURI(config, bucket, key));
return executeMethod(bucket, key, get, meta, streamResponse);
}
@@ -234,16 +247,24 @@ public boolean stream(String bucket, String key, StreamHandler handler, RequestM
if (meta.getQueryParam(Constants.QP_R) == null) {
meta.setQueryParam(Constants.QP_R, Constants.DEFAULT_R.toString());
}
- GetMethod get = new GetMethod(ClientUtils.makeURI(config, bucket, key));
+ HttpGet get = new HttpGet(ClientUtils.makeURI(config, bucket, key));
try {
- int status = httpClient.executeMethod(get);
- if (handler == null)
- return true;
-
- return handler.process(bucket, key, status, ClientUtils.asHeaderMap(get.getResponseHeaders()),
- get.getResponseBodyAsStream(), get);
- } finally {
- get.releaseConnection();
+ org.apache.http.HttpResponse response = httpClient.execute(get);
+ HttpEntity entity = response.getEntity();
+
+ boolean result = true;
+ if (handler != null) {
+
+ result = handler.process(bucket, key, response.getStatusLine().getStatusCode(),
+ ClientUtils.asHeaderMap(response.getAllHeaders()), entity.getContent(),
+ response);
+ }
+ EntityUtils.consume(entity);
+
+ return result;
+ } catch (IOException e) {
+ get.abort();
+ throw e;
}
}
@@ -255,27 +276,28 @@ public HttpResponse delete(String bucket, String key, RequestMeta meta) {
meta = new RequestMeta();
}
String url = ClientUtils.makeURI(config, bucket, key);
- DeleteMethod delete = new DeleteMethod(url);
+ HttpDelete delete = new HttpDelete(url);
return executeMethod(bucket, key, delete, meta);
}
/**
* Same as {@link RiakClient}, except only returning the HTTP response
*/
public HttpResponse walk(String bucket, String key, String walkSpec, RequestMeta meta) {
- GetMethod get = new GetMethod(ClientUtils.makeURI(config, bucket, key, walkSpec));
+ HttpGet get = new HttpGet(ClientUtils.makeURI(config, bucket, key, walkSpec));
return executeMethod(bucket, key, get, meta);
}
/**
* Same as {@link RiakClient}, except only returning the HTTP response
*/
public HttpResponse mapReduce(String job, RequestMeta meta) {
- PostMethod post = new PostMethod(config.getMapReduceUrl());
+ HttpPost post = new HttpPost(config.getMapReduceUrl());
try {
- post.setRequestEntity(new StringRequestEntity(job, Constants.CTYPE_JSON, null));
+ StringEntity entity = new StringEntity(job, Constants.CTYPE_JSON_UTF8, CharsetUtils.UTF_8.name());
+ post.setEntity(entity);
} catch (UnsupportedEncodingException e) {
- throw new IllegalStateException("StringRequestEntity should always support no charset", e);
+ throw new IllegalStateException("StringEntity should always support UTF-8 charset", e);
}
return executeMethod(null, null, post, meta);
}
@@ -303,15 +325,15 @@ public void setExceptionHandler(RiakExceptionHandler exceptionHandler) {
public HttpResponse toss(RiakIORuntimeException e) {
if (exceptionHandler != null) {
exceptionHandler.handle(e);
- return new DefaultHttpResponse(null, null, 0, null, null, null, null);
+ return new DefaultHttpResponse(null, null, 0, null, null, null, null, null);
} else
throw e;
}
public HttpResponse toss(RiakResponseRuntimeException e) {
if (exceptionHandler != null) {
exceptionHandler.handle(e);
- return new DefaultHttpResponse(null, null, 0, null, null, null, null);
+ return new DefaultHttpResponse(null, null, 0, null, null, null, null, null);
} else
throw e;
}
@@ -358,54 +380,73 @@ public RiakConfig getConfig() {
* If an error occurs during communication with the Riak server
* (i.e. HttpClient threw an IOException)
*/
- HttpResponse executeMethod(String bucket, String key, HttpMethod httpMethod, RequestMeta meta,
+ HttpResponse executeMethod(String bucket, String key, HttpRequestBase httpMethod, RequestMeta meta,
boolean streamResponse) {
if (meta != null) {
Map<String, String> headers = meta.getHeaders();
for (String header : headers.keySet()) {
- httpMethod.setRequestHeader(header, headers.get(header));
+ httpMethod.addHeader(header, headers.get(header));
}
- String queryParams = meta.getQueryParams();
- if (queryParams != null && (queryParams.length() != 0)) {
- String currentQuery = httpMethod.getQueryString();
- if (currentQuery != null && (currentQuery.length() != 0)) {
- httpMethod.setQueryString(currentQuery + "&" + queryParams);
- } else {
- httpMethod.setQueryString(queryParams);
+ Map<String, String> queryParams = meta.getQueryParamMap();
+ if (!queryParams.isEmpty()) {
+ URI originalURI = httpMethod.getURI();
+ List<NameValuePair> currentQuery = URLEncodedUtils.parse(originalURI, CharsetUtils.UTF_8.name());
+ List<NameValuePair> newQuery = new LinkedList<NameValuePair>(currentQuery);
+
+ for(Map.Entry<String, String> qp : queryParams.entrySet()) {
+ newQuery.add(new BasicNameValuePair(qp.getKey(), qp.getValue()));
+ }
+
+ // For this, HC4.1 authors, I hate you
+ URI newURI;
+ try {
+ newURI = URIUtils.createURI(originalURI.getScheme(),
+ originalURI.getHost(),
+ originalURI.getPort(),
+ originalURI.getPath(),
+ URLEncodedUtils.format(newQuery, "UTF-8"), null);
+ } catch (URISyntaxException e) {
+ throw new RiakIORuntimeException(e);
}
+ httpMethod.setURI(newURI);
}
}
-
+ HttpEntity entity;
try {
- httpClient.executeMethod(httpMethod);
+ org.apache.http.HttpResponse response = httpClient.execute(httpMethod);
int status = 0;
- if (httpMethod.getStatusLine() != null) {
- status = httpMethod.getStatusCode();
+ if (response.getStatusLine() != null) {
+ status = response.getStatusLine().getStatusCode();
}
- Map<String, String> headers = ClientUtils.asHeaderMap(httpMethod.getResponseHeaders());
+ Map<String, String> headers = ClientUtils.asHeaderMap(response.getAllHeaders());
byte[] body = null;
InputStream stream = null;
+ entity = response.getEntity();
+
if (streamResponse) {
- stream = httpMethod.getResponseBodyAsStream();
+ stream = entity.getContent();
} else {
- body = httpMethod.getResponseBody();
+ if(null != entity) {
+ body = EntityUtils.toByteArray(entity);
+ }
}
- return new DefaultHttpResponse(bucket, key, status, headers, body, stream, httpMethod);
- } catch (IOException e) {
- return toss(new RiakIORuntimeException(e));
- } finally {
if (!streamResponse) {
- httpMethod.releaseConnection();
+ EntityUtils.consume(entity);
}
+
+ return new DefaultHttpResponse(bucket, key, status, headers, body, stream, response, httpMethod);
+ } catch (IOException e) {
+ httpMethod.abort();
+ return toss(new RiakIORuntimeException(e));
}
}
- HttpResponse executeMethod(String bucket, String key, HttpMethod httpMethod, RequestMeta meta) {
+ HttpResponse executeMethod(String bucket, String key, HttpRequestBase httpMethod, RequestMeta meta) {
return executeMethod(bucket, key, httpMethod, meta, false);
}
}
View
76 src/main/java/com/basho/riak/client/http/util/ClientUtils.java
@@ -13,6 +13,7 @@
*/
package com.basho.riak.client.http.util;
+import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@@ -28,14 +29,13 @@
import java.util.Map.Entry;
import org.apache.commons.codec.binary.Base64;
-import org.apache.commons.httpclient.Header;
-import org.apache.commons.httpclient.HostConfiguration;
-import org.apache.commons.httpclient.HttpClient;
-import org.apache.commons.httpclient.HttpConnectionManager;
-import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
-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.Header;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.params.AllClientPNames;
+import org.apache.http.conn.ClientConnectionManager;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
+import org.apache.http.params.HttpParams;
import org.json.JSONArray;
import org.json.JSONObject;
@@ -44,6 +44,7 @@
import com.basho.riak.client.http.RiakLink;
import com.basho.riak.client.http.RiakObject;
import com.basho.riak.client.http.response.RiakExceptionHandler;
+import com.basho.riak.client.http.response.RiakIORuntimeException;
import com.basho.riak.client.util.CharsetUtils;
/**
@@ -65,29 +66,27 @@
public static HttpClient newHttpClient(RiakConfig config) {
HttpClient http = config.getHttpClient();
- HttpConnectionManager m;
+ ClientConnectionManager m;
if (http == null) {
- m = new MultiThreadedHttpConnectionManager();
- http = new HttpClient(m);
- } else {
- m = http.getHttpConnectionManager();
- }
+ m = new ThreadSafeClientConnManager();
+ if (config.getMaxConnections() != null) {
+ ((ThreadSafeClientConnManager) m).setMaxTotal(config.getMaxConnections());
+ ((ThreadSafeClientConnManager) m).setDefaultMaxPerRoute(config.getMaxConnections());
+ }
+ http = new DefaultHttpClient(m);
- HttpConnectionManagerParams mp = m.getParams();
- if (config.getMaxConnections() != null) {
- mp.setMaxTotalConnections(config.getMaxConnections());
- mp.setMaxConnectionsPerHost(HostConfiguration.ANY_HOST_CONFIGURATION, config.getMaxConnections());
+ if (config.getRetryHandler() != null) {
+ ((DefaultHttpClient) http).setHttpRequestRetryHandler(config.getRetryHandler());
+ }
+ } else {
+ m = http.getConnectionManager();
}
- HttpClientParams cp = http.getParams();
+ HttpParams cp = http.getParams();
if (config.getTimeout() != null) {
- mp.setConnectionTimeout(config.getTimeout().intValue());
- cp.setConnectionManagerTimeout(config.getTimeout());
- cp.setSoTimeout(config.getTimeout().intValue());
- }
- if (config.getRetryHandler() != null) {
- cp.setParameter(HttpMethodParams.RETRY_HANDLER, config.getRetryHandler());
+ cp.setIntParameter(AllClientPNames.CONNECTION_TIMEOUT, config.getTimeout());
+ cp.setIntParameter(AllClientPNames.SO_TIMEOUT, config.getTimeout());
}
return http;
@@ -375,7 +374,7 @@ private static RiakLink parseOneLink(String url, Map<String, String> params) {
public static Map<String, String> parseUsermeta(Map<String, String> headers) {
Map<String, String> usermeta = new HashMap<String, String>();
if (headers != null) {
- for (Entry<String, String> e : headers.entrySet()) {
+ for (Entry<String, String> e : headers.entrySet()) {
String header = e.getKey();
if (header != null && header.toLowerCase().startsWith(Constants.HDR_USERMETA_PREFIX)) {
usermeta.put(header.substring(Constants.HDR_USERMETA_PREFIX.length()), e.getValue());
@@ -467,10 +466,33 @@ private static RiakLink parseOneLink(String url, Map<String, String> params) {
public static void throwChecked(final Throwable exception) {
new CheckedThrower<RuntimeException>().throwChecked(exception);
}
+
+ /**
+ * Buffers an input stream into a byte array
+ * @param valueStream the stream to read into an array
+ * @return the byte array of the consumed stream
+ */
+ public static byte[] bufferStream(InputStream valueStream) {
+ if (valueStream == null) {
+ return new byte[] {};
+ }
+
+ try {
+ ByteArrayOutputStream tmp = new ByteArrayOutputStream();
+ byte[] data = new byte[4096];
+ int l = 0;
+ while ((l = valueStream.read(data)) >= 0) {
+ tmp.write(data, 0, l);
+ }
+ return tmp.toByteArray();
+ } catch (IOException e) {
+ throw new RiakIORuntimeException(e);
+ }
+ }
}
class CheckedThrower<T extends Throwable> {
@SuppressWarnings("unchecked") public void throwChecked(Throwable exception) throws T {
throw (T) exception;
}
-}
+}
View
6 src/main/java/com/basho/riak/client/http/util/Multipart.java
@@ -13,8 +13,6 @@
*/
package com.basho.riak.client.http.util;
-import org.apache.commons.httpclient.util.EncodingUtil;
-
import com.basho.riak.client.util.CharsetUtils;
import java.io.ByteArrayInputStream;
@@ -23,6 +21,8 @@
import java.io.InputStream;
import java.util.*;
+import org.apache.http.util.EncodingUtils;
+
/**
* Represents a multipart entity as described here:
*
@@ -125,7 +125,7 @@ private static int indexOf(byte[] text, byte[] pattern, int fromIndex) {
bodyStart = end;
}
- Map<String, String> partHeaders = parseHeaders(EncodingUtil.getAsciiString(copyOfRange(body, start, headerEnd)));
+ Map<String, String> partHeaders = parseHeaders(EncodingUtils.getAsciiString(copyOfRange(body, start, headerEnd)));
parts.add(new Part(partHeaders, copyOfRange(body, bodyStart, end)));
pos = end;
View
11 src/main/java/com/basho/riak/client/plain/RiakResponseException.java
@@ -16,7 +16,7 @@
import java.io.InputStream;
import java.util.Map;
-import org.apache.commons.httpclient.HttpMethod;
+import org.apache.http.client.methods.HttpRequestBase;
import com.basho.riak.client.response.HttpResponse;
import com.basho.riak.client.response.RiakResponseRuntimeException;
@@ -67,7 +67,7 @@ public String getBucket() {
return impl.getHttpHeaders();
}
- public HttpMethod getHttpMethod() {
+ public HttpRequestBase getHttpMethod() {
return impl.getHttpMethod();
}
@@ -90,4 +90,11 @@ public boolean isSuccess() {
public void close() {
impl.close();
}
+
+ /* (non-Javadoc)
+ * @see com.basho.riak.client.http.response.HttpResponse#getHttpResponse()
+ */
+ public org.apache.http.HttpResponse getHttpResponse() {
+ return impl.getHttpResponse();
+ }
}
View
5 src/main/java/com/basho/riak/client/raw/http/ConversionUtil.java
@@ -24,12 +24,11 @@
import java.util.Map;
import java.util.Map.Entry;
-import org.apache.commons.httpclient.util.DateUtil;
+import org.apache.http.impl.cookie.DateUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.type.TypeFactory;
import com.basho.riak.client.IRiakObject;
-import com.basho.riak.client.RiakException;
import com.basho.riak.client.RiakLink;
import com.basho.riak.client.bucket.BucketProperties;
import com.basho.riak.client.builders.BucketPropertiesBuilder;
@@ -203,7 +202,7 @@ static String formatDate(Date lastModified) {
if (lastModified == null) {
return null;
}
- return DateUtil.formatDate(lastModified);
+ return DateUtils.formatDate(lastModified);
}
/**
View
7 src/main/java/com/basho/riak/client/raw/pbc/ConversionUtil.java
@@ -27,8 +27,8 @@
import java.util.SortedMap;
import java.util.TreeMap;
-import org.apache.commons.httpclient.util.DateParseException;
-import org.apache.commons.httpclient.util.DateUtil;
+import org.apache.http.impl.cookie.DateParseException;
+import org.apache.http.impl.cookie.DateUtils;
import com.basho.riak.client.IRiakObject;
import com.basho.riak.client.bucket.BucketProperties;
@@ -45,7 +45,6 @@
import com.basho.riak.client.util.UnmodifiableIterator;
import com.basho.riak.pbc.MapReduceResponseSource;
import com.basho.riak.pbc.RequestMeta;
-import com.basho.riak.pbc.mapreduce.MapReduceResponse;
import com.google.protobuf.ByteString;
/**
@@ -306,7 +305,7 @@ static boolean linkAccumulateToLinkPhaseKeep(Accumulate accumulate, boolean isFi
b.withVtag((String) meta.get("X-Riak-VTag"));
try {
- Date lastModDate = DateUtil.parseDate((String) meta.get("X-Riak-Last-Modified"));
+ Date lastModDate = DateUtils.parseDate((String) meta.get("X-Riak-Last-Modified"));
b.withLastModified(lastModDate.getTime());
} catch (DateParseException e) {
// NO-OP
View
16 src/main/java/com/basho/riak/client/request/RequestMeta.java
@@ -18,7 +18,7 @@
import java.util.LinkedHashMap;
import java.util.Map;
-import org.apache.commons.httpclient.util.DateUtil;
+import org.apache.http.impl.cookie.DateUtils;
import com.basho.riak.client.util.ClientUtils;
import com.basho.riak.client.util.Constants;
@@ -161,6 +161,16 @@ public RequestMeta setQueryParam(String param, String value) {
}
/**
+ * Get a copy of the query params in a map
+ *
+ * @return a {@link LinkedHashMap} of the query params (copy, does not
+ * read/write through)
+ */
+ public Map<String, String> getQueryParamMap() {
+ return new LinkedHashMap<String, String>(queryParams);
+ }
+
+ /**
* A string containing all the specified query parameters in this
* {@link RequestMeta} in the form: p1=v1&p2=v2
*/
@@ -196,7 +206,7 @@ public RequestMeta setIfModifiedSince(String lastmod) {
}
public RequestMeta setIfModifiedSince(Date lastmod) {
- return setHeader(Constants.HDR_IF_MODIFIED_SINCE, DateUtil.formatDate(lastmod));
+ return setHeader(Constants.HDR_IF_MODIFIED_SINCE, DateUtils.formatDate(lastmod));
}
/** Convenience method for the If-Unmodified-Since HTTP header */
@@ -209,7 +219,7 @@ public RequestMeta setIfUnmodifiedSince(String lastmod) {
}
public RequestMeta setIfUnmodifiedSince(Date lastmod) {
- return setHeader(Constants.HDR_IF_UNMODIFIED_SINCE, DateUtil.formatDate(lastmod));
+ return setHeader(Constants.HDR_IF_UNMODIFIED_SINCE, DateUtils.formatDate(lastmod));
}
/** Convenience method for the If-Match HTTP header */
View
35 src/main/java/com/basho/riak/client/response/DefaultHttpResponse.java
@@ -16,12 +16,16 @@
import static com.basho.riak.client.util.CharsetUtils.asString;
import static com.basho.riak.client.util.CharsetUtils.getCharset;
+import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
-import org.apache.commons.httpclient.HttpMethod;
+import org.apache.http.client.methods.HttpRequestBase;
+import org.apache.http.util.EntityUtils;
+import com.basho.riak.client.response.HttpResponse;
+import com.basho.riak.client.response.RiakIORuntimeException;
import com.basho.riak.client.util.Constants;
/**
@@ -45,10 +49,11 @@
private Map<String, String> headers = null;
private byte[] body = null;
private InputStream stream = null;
- private HttpMethod httpMethod = null;
+ private org.apache.http.HttpResponse httpResponse = null;
+ private HttpRequestBase httpMethod = null;
public DefaultHttpResponse(String bucket, String key, int status, Map<String, String> headers, byte[] body,
- InputStream stream, HttpMethod httpMethod) {
+ InputStream stream, org.apache.http.HttpResponse httpResponse, HttpRequestBase httpMethod) {
if (headers == null) {
headers = new HashMap<String, String>();
}
@@ -61,6 +66,7 @@ public DefaultHttpResponse(String bucket, String key, int status, Map<String, St
this.body = body.clone();
}
this.stream = stream;
+ this.httpResponse = httpResponse;
this.httpMethod = httpMethod;
}
@@ -102,14 +108,14 @@ public boolean isStreamed() {
return stream != null;
}
- public HttpMethod getHttpMethod() {
+ public HttpRequestBase getHttpMethod() {
return httpMethod;
}
public boolean isSuccess() {
String method = null;
if (httpMethod != null) {
- method = httpMethod.getName();
+ method = httpMethod.getMethod();
}
return (status >= 200 && status < 300) ||
@@ -121,16 +127,27 @@ public boolean isSuccess() {
public boolean isError() {
String method = null;
- if (httpMethod != null) {
- method = httpMethod.getName();
+ if (httpResponse != null) {
+ method = httpMethod.getMethod();
}
return (status < 100 || status >= 400) && !((status == 404) && Constants.HTTP_DELETE_METHOD.equals(method));
}
public void close() {
- if (httpMethod != null) {
- httpMethod.releaseConnection();
+ if (httpResponse != null) {
+ try {
+ EntityUtils.consume(httpResponse.getEntity());
+ } catch (IOException e) {
+ throw new RiakIORuntimeException(e);
+ }
}
}
+
+ /* (non-Javadoc)
+ * @see com.basho.riak.client.http.response.HttpResponse#getHttpResponse()
+ */
+ public org.apache.http.HttpResponse getHttpResponse() {
+ return httpResponse;
+ }
}
View
9 src/main/java/com/basho/riak/client/response/HttpResponse.java
@@ -16,7 +16,7 @@
import java.io.InputStream;
import java.util.Map;
-import org.apache.commons.httpclient.HttpMethod;
+import org.apache.http.client.methods.HttpRequestBase;
/**
* HTTP response information resulting from some HTTP operation
@@ -75,7 +75,7 @@
* note that the connection will already be closed, so calling
* getHttpMethod().getResponseBodyAsStream() will return null.
*/
- public HttpMethod getHttpMethod();
+ public HttpRequestBase getHttpMethod();
/**
* Whether the HTTP response is considered a success. Generally this
@@ -93,4 +93,9 @@
* Releases the underlying the HTTP connection when the response is streamed
*/
public void close();
+
+ /**
+ * @return
+ */
+ public org.apache.http.HttpResponse getHttpResponse();
}
View
13 src/main/java/com/basho/riak/client/response/HttpResponseDecorator.java
@@ -17,7 +17,7 @@
import java.util.HashMap;
import java.util.Map;
-import org.apache.commons.httpclient.HttpMethod;
+import org.apache.http.client.methods.HttpRequestBase;
/**
* A default decorator implementation for HttpResponse
@@ -81,7 +81,7 @@ public boolean isStreamed() {
return impl.getHttpHeaders();
}
- public HttpMethod getHttpMethod() {
+ public HttpRequestBase getHttpMethod() {
if (impl == null)
return null;
return impl.getHttpMethod();
@@ -110,4 +110,13 @@ public void close() {
impl.close();
}
}
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see com.basho.riak.client.http.response.HttpResponse#getHttpResponse()
+ */
+ public org.apache.http.HttpResponse getHttpResponse() {
+ return impl.getHttpResponse();
+ }
}
View
11 src/main/java/com/basho/riak/client/response/RiakResponseRuntimeException.java
@@ -16,7 +16,7 @@
import java.io.InputStream;
import java.util.Map;
-import org.apache.commons.httpclient.HttpMethod;
+import org.apache.http.client.methods.HttpRequestBase;
/**
* Thrown when the Riak server returns a malformed response. The HTTP response
@@ -92,7 +92,7 @@ public String getBucket() {
return response.getHttpHeaders();
}
- public HttpMethod getHttpMethod() {
+ public HttpRequestBase getHttpMethod() {
if (response == null)
return null;
return response.getHttpMethod();
@@ -122,4 +122,11 @@ public void close() {
if (response != null)
response.close();
}
+
+ /* (non-Javadoc)
+ * @see com.basho.riak.client.http.response.HttpResponse#getHttpResponse()
+ */
+ public org.apache.http.HttpResponse getHttpResponse() {
+ return response.getHttpResponse();
+ }
}
View
6 src/main/java/com/basho/riak/client/response/StreamHandler.java
@@ -16,7 +16,7 @@
import java.io.InputStream;
import java.util.Map;
-import org.apache.commons.httpclient.HttpMethod;
+import org.apache.http.HttpResponse;
/**
* Used with RiakClient.stream() to process the HTTP responses for fetch
@@ -47,11 +47,11 @@
* @param in
* InputStream of the object's value (body)
* @param httpMethod
- * The original {@link HttpMethod} used to make the request. Its
+ * The original {@link HttpResponse} used to make the request. Its
* connection is still open and will be closed by the caller on
* return.
* @return true if the object was processed; false otherwise
*/
public boolean process(String bucket, String key, int status, Map<String, String> headers, InputStream in,
- HttpMethod httpMethod);
+ HttpResponse httpMethod);
}
View
170 src/main/java/com/basho/riak/client/util/ClientHelper.java
@@ -13,21 +13,34 @@
*/
package com.basho.riak.client.util;
+import static com.basho.riak.client.util.CharsetUtils.utf8StringToBytes;
+
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.LinkedList;
+import java.util.List;
import java.util.Map;
import org.apache.commons.codec.binary.Base64;
-import org.apache.commons.httpclient.HttpClient;
-import org.apache.commons.httpclient.HttpMethod;
-import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
-import org.apache.commons.httpclient.methods.DeleteMethod;
-import org.apache.commons.httpclient.methods.GetMethod;
-import org.apache.commons.httpclient.methods.HeadMethod;
-import org.apache.commons.httpclient.methods.PostMethod;
-import org.apache.commons.httpclient.methods.PutMethod;
-import org.apache.commons.httpclient.methods.StringRequestEntity;
+import org.apache.http.HttpEntity;
+import org.apache.http.NameValuePair;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpDelete;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpHead;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.client.methods.HttpPut;
+import org.apache.http.client.methods.HttpRequestBase;
+import org.apache.http.client.utils.URIUtils;
+import org.apache.http.client.utils.URLEncodedUtils;
+import org.apache.http.entity.ByteArrayEntity;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.message.BasicNameValuePair;
+import org.apache.http.util.EntityUtils;
+
import org.json.JSONObject;
import com.basho.riak.client.RiakClient;
@@ -78,11 +91,7 @@ void setHttpClient(HttpClient httpClient) {
* See {@link RiakClient#getClientId()}
*/
public byte[] getClientId() {
- try {
- return Base64.decodeBase64(clientId.getBytes("UTF-8"));
- } catch (UnsupportedEncodingException e) {
- throw new IllegalStateException("UTF-8 support required in JVM");
- }
+ return Base64.decodeBase64(utf8StringToBytes(clientId));
}
public void setClientId(String clientId) {
@@ -95,7 +104,7 @@ public void setClientId(String clientId) {
/**
* See
- * {@link RiakClient#setBucketSchema(String, com.basho.riak.client.RiakBucketInfo, RequestMeta)}
+ * {@link RiakClient#setBucketSchema(String, com.basho.riak.client.http.RiakBucketInfo, RequestMeta)}
*/
public HttpResponse setBucketSchema(String bucket, JSONObject schema, RequestMeta meta) {
if (schema == null) {
@@ -107,8 +116,10 @@ public HttpResponse setBucketSchema(String bucket, JSONObject schema, RequestMet
meta.setHeader(Constants.HDR_ACCEPT, Constants.CTYPE_JSON);
- PutMethod put = new PutMethod(ClientUtils.makeURI(config, bucket));
- put.setRequestEntity(new ByteArrayRequestEntity(CharsetUtils.utf8StringToBytes(schema.toString()), Constants.CTYPE_JSON));
+ HttpPut put = new HttpPut(ClientUtils.makeURI(config, bucket));
+ ByteArrayEntity entity = new ByteArrayEntity(utf8StringToBytes(schema.toString()));
+ entity.setContentType(Constants.CTYPE_JSON);
+ put.setEntity(entity);
return executeMethod(bucket, null, put, meta);
}
@@ -128,6 +139,19 @@ public HttpResponse getBucketSchema(String bucket, RequestMeta meta) {
}
/**
+ * List the buckets in Riak
+ *
+ * @return an {@link HttpResponse} whose body should be the result of asking
+ * Riak to list buckets.
+ */
+ public HttpResponse listBuckets() {
+ final RequestMeta meta = new RequestMeta();
+ meta.setQueryParam(Constants.QP_BUCKETS, Constants.LIST_BUCKETS);
+ HttpGet get = new HttpGet(config.getUrl());
+ return executeMethod(null, null, get, meta);
+ }
+
+ /**
* Same as {@link RiakClient}, except only returning the HTTP response, and
* if streamResponse==true, the response will be streamed back, so the user
* is responsible for calling {@link BucketResponse#close()}
@@ -150,7 +174,7 @@ public HttpResponse listBucket(String bucket, RequestMeta meta, boolean streamRe
meta.setHeader(Constants.HDR_ACCEPT, Constants.CTYPE_JSON);
}
- GetMethod get = new GetMethod(ClientUtils.makeURI(config, bucket));
+ HttpGet get = new HttpGet(ClientUtils.makeURI(config, bucket));
return executeMethod(bucket, null, get, meta, streamResponse);
}
@@ -171,7 +195,7 @@ public HttpResponse store(RiakObject object, RequestMeta meta) {
String bucket = object.getBucket();
String key = object.getKey();
String url = ClientUtils.makeURI(config, bucket, key);
- PutMethod put = new PutMethod(url);
+ HttpPut put = new HttpPut(url);
object.writeToHttpMethod(put);
return executeMethod(bucket, key, put, meta);
@@ -187,7 +211,7 @@ public HttpResponse fetchMeta(String bucket, String key, RequestMeta meta) {
if (meta.getQueryParam(Constants.QP_R) == null) {
meta.setQueryParam(Constants.QP_R, Constants.DEFAULT_R.toString());
}
- HeadMethod head = new HeadMethod(ClientUtils.makeURI(config, bucket, key));
+ HttpHead head = new HttpHead(ClientUtils.makeURI(config, bucket, key));
return executeMethod(bucket, key, head, meta);
}
@@ -215,7 +239,7 @@ public HttpResponse fetch(String bucket, String key, RequestMeta meta, boolean s
if (meta.getQueryParam(Constants.QP_R) == null) {
meta.setQueryParam(Constants.QP_R, Constants.DEFAULT_R.toString());
}
- GetMethod get = new GetMethod(ClientUtils.makeURI(config, bucket, key));
+ HttpGet get = new HttpGet(ClientUtils.makeURI(config, bucket, key));
return executeMethod(bucket, key, get, meta, streamResponse);
}
@@ -233,16 +257,24 @@ public boolean stream(String bucket, String key, StreamHandler handler, RequestM
if (meta.getQueryParam(Constants.QP_R) == null) {
meta.setQueryParam(Constants.QP_R, Constants.DEFAULT_R.toString());
}
- GetMethod get = new GetMethod(ClientUtils.makeURI(config, bucket, key));
+ HttpGet get = new HttpGet(ClientUtils.makeURI(config, bucket, key));
try {
- int status = httpClient.executeMethod(get);
- if (handler == null)
- return true;
-
- return handler.process(bucket, key, status, ClientUtils.asHeaderMap(get.getResponseHeaders()),
- get.getResponseBodyAsStream(), get);
- } finally {
- get.releaseConnection();
+ org.apache.http.HttpResponse response = httpClient.execute(get);
+ HttpEntity entity = response.getEntity();
+
+ boolean result = true;
+ if (handler != null) {
+
+ result = handler.process(bucket, key, response.getStatusLine().getStatusCode(),
+ ClientUtils.asHeaderMap(response.getAllHeaders()), entity.getContent(),
+ response);
+ }
+ EntityUtils.consume(entity);
+
+ return result;
+ } catch (IOException e) {
+ get.abort();
+ throw e;
}
}
@@ -254,27 +286,28 @@ public HttpResponse delete(String bucket, String key, RequestMeta meta) {
meta = new RequestMeta();
}
String url = ClientUtils.makeURI(config, bucket, key);
- DeleteMethod delete = new DeleteMethod(url);
+ HttpDelete delete = new HttpDelete(url);
return executeMethod(bucket, key, delete, meta);
}
/**
* Same as {@link RiakClient}, except only returning the HTTP response
*/
public HttpResponse walk(String bucket, String key, String walkSpec, RequestMeta meta) {
- GetMethod get = new GetMethod(ClientUtils.makeURI(config, bucket, key, walkSpec));
+ HttpGet get = new HttpGet(ClientUtils.makeURI(config, bucket, key, walkSpec));
return executeMethod(bucket, key, get, meta);
}
/**
* Same as {@link RiakClient}, except only returning the HTTP response
*/
public HttpResponse mapReduce(String job, RequestMeta meta) {
- PostMethod post = new PostMethod(config.getMapReduceUrl());
+ HttpPost post = new HttpPost(config.getMapReduceUrl());
try {
- post.setRequestEntity(new StringRequestEntity(job, Constants.CTYPE_JSON, null));
+ StringEntity entity = new StringEntity(job, Constants.CTYPE_JSON, CharsetUtils.UTF_8.name());
+ post.setEntity(entity);
} catch (UnsupportedEncodingException e) {
- throw new IllegalStateException("StringRequestEntity should always support no charset", e);
+ throw new IllegalStateException("StringEntity should always support UTF-8 charset", e);
}
return executeMethod(null, null, post, meta);
}
@@ -302,15 +335,15 @@ public void setExceptionHandler(RiakExceptionHandler exceptionHandler) {
public HttpResponse toss(RiakIORuntimeException e) {
if (exceptionHandler != null) {
exceptionHandler.handle(e);
- return new DefaultHttpResponse(null, null, 0, null, null, null, null);
+ return new DefaultHttpResponse(null, null, 0, null, null, null, null, null);
} else
throw e;
}
public HttpResponse toss(RiakResponseRuntimeException e) {
if (exceptionHandler != null) {
exceptionHandler.handle(e);
- return new DefaultHttpResponse(null, null, 0, null, null, null, null);
+ return new DefaultHttpResponse(null, null, 0, null, null, null, null, null);
} else
throw e;
}
@@ -357,54 +390,73 @@ public RiakConfig getConfig() {
* If an error occurs during communication with the Riak server
* (i.e. HttpClient threw an IOException)
*/
- HttpResponse executeMethod(String bucket, String key, HttpMethod httpMethod, RequestMeta meta,
+ HttpResponse executeMethod(String bucket, String key, HttpRequestBase httpMethod, RequestMeta meta,
boolean streamResponse) {
if (meta != null) {
Map<String, String> headers = meta.getHeaders();
for (String header : headers.keySet()) {
- httpMethod.setRequestHeader(header, headers.get(header));
+ httpMethod.addHeader(header, headers.get(header));
}
- String queryParams = meta.getQueryParams();
- if (queryParams != null && (queryParams.length() != 0)) {
- String currentQuery = httpMethod.getQueryString();
- if (currentQuery != null && (currentQuery.length() != 0)) {
- httpMethod.setQueryString(currentQuery + "&" + queryParams);
- } else {
- httpMethod.setQueryString(queryParams);
+ Map<String, String> queryParams = meta.getQueryParamMap();
+ if (!queryParams.isEmpty()) {
+ URI originalURI = httpMethod.getURI();
+ List<NameValuePair> currentQuery = URLEncodedUtils.parse(originalURI, CharsetUtils.UTF_8.name());
+ List<NameValuePair> newQuery = new LinkedList<NameValuePair>(currentQuery);
+
+ for(Map.Entry<String, String> qp : queryParams.entrySet()) {
+ newQuery.add(new BasicNameValuePair(qp.getKey(), qp.getValue()));
}
+
+ // For this, HC4.1 authors, I hate you
+ URI newURI;
+ try {
+ newURI = URIUtils.createURI(originalURI.getScheme(),
+ originalURI.getHost(),
+ originalURI.getPort(),
+ originalURI.getPath(),
+ URLEncodedUtils.format(newQuery, "UTF-8"), null);
+ } catch (URISyntaxException e) {
+ throw new RiakIORuntimeException(e);
+ }
+ httpMethod.setURI(newURI);
}
}
-
+ HttpEntity entity;
try {
- httpClient.executeMethod(httpMethod);
+ org.apache.http.HttpResponse response = httpClient.execute(httpMethod);
int status = 0;
- if (httpMethod.getStatusLine() != null) {
- status = httpMethod.getStatusCode();
+ if (response.getStatusLine() != null) {
+ status = response.getStatusLine().getStatusCode();
}
- Map<String, String> headers = ClientUtils.asHeaderMap(httpMethod.getResponseHeaders());
+ Map<String, String> headers = ClientUtils.asHeaderMap(response.getAllHeaders());
byte[] body = null;
InputStream stream = null;
+ entity = response.getEntity();
+
if (streamResponse) {
- stream = httpMethod.getResponseBodyAsStream();
+ stream = entity.getContent();
} else {
- body = httpMethod.getResponseBody();
+ if(null != entity) {
+ body = EntityUtils.toByteArray(entity);
+ }
}
- return new DefaultHttpResponse(bucket, key, status, headers, body, stream, httpMethod);
- } catch (IOException e) {
- return toss(new RiakIORuntimeException(e));
- } finally {
if (!streamResponse) {
- httpMethod.releaseConnection();
+ EntityUtils.consume(entity);
}
+
+ return new DefaultHttpResponse(bucket, key, status, headers, body, stream, response, httpMethod);
+ } catch (IOException e) {
+ httpMethod.abort();
+ return toss(new RiakIORuntimeException(e));
}
}
- HttpResponse executeMethod(String bucket, String key, HttpMethod httpMethod, RequestMeta meta) {
+ HttpResponse executeMethod(String bucket, String key, HttpRequestBase httpMethod, RequestMeta meta) {
return executeMethod(bucket, key, httpMethod, meta, false);
}
}
View
102 src/main/java/com/basho/riak/client/util/ClientUtils.java
@@ -13,6 +13,7 @@
*/
package com.basho.riak.client.util;
+import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@@ -28,14 +29,13 @@
import java.util.Map.Entry;
import org.apache.commons.codec.binary.Base64;
-import org.apache.commons.httpclient.Header;
-import org.apache.commons.httpclient.HostConfiguration;
-import org.apache.commons.httpclient.HttpClient;
-import org.apache.commons.httpclient.HttpConnectionManager;
-import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
-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.Header;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.params.AllClientPNames;
+import org.apache.http.conn.ClientConnectionManager;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
+import org.apache.http.params.HttpParams;
import org.json.JSONArray;
import org.json.JSONObject;
@@ -44,6 +44,7 @@
import com.basho.riak.client.RiakLink;
import com.basho.riak.client.RiakObject;
import com.basho.riak.client.response.RiakExceptionHandler;
+import com.basho.riak.client.response.RiakIORuntimeException;
/**
* Utility functions.
@@ -73,29 +74,27 @@
public static HttpClient newHttpClient(RiakConfig config) {
HttpClient http = config.getHttpClient();
- HttpConnectionManager m;
+ ClientConnectionManager m;
if (http == null) {
- m = new MultiThreadedHttpConnectionManager();
- http = new HttpClient(m);
- } else {
- m = http.getHttpConnectionManager();
- }
+ m = new ThreadSafeClientConnManager();
+ if (config.getMaxConnections() != null) {
+ ((ThreadSafeClientConnManager) m).setMaxTotal(config.getMaxConnections());
+ ((ThreadSafeClientConnManager) m).setDefaultMaxPerRoute(config.getMaxConnections());
+ }
+ http = new DefaultHttpClient(m);
- HttpConnectionManagerParams mp = m.getParams();
- if (config.getMaxConnections() != null) {
- mp.setMaxTotalConnections(config.getMaxConnections());
- mp.setMaxConnectionsPerHost(HostConfiguration.ANY_HOST_CONFIGURATION, config.getMaxConnections());
+ if (config.getRetryHandler() != null) {
+ ((DefaultHttpClient) http).setHttpRequestRetryHandler(config.getRetryHandler());
+ }
+ } else {
+ m = http.getConnectionManager();
}
- HttpClientParams cp = http.getParams();
+ HttpParams cp = http.getParams();
if (config.getTimeout() != null) {
- mp.setConnectionTimeout(config.getTimeout().intValue());
- cp.setConnectionManagerTimeout(config.getTimeout());
- cp.setSoTimeout(config.getTimeout().intValue());
- }