Author: stefanegli
Date: Mon Jan 26 09:21:36 2015
New Revision: 1654747
URL: http://svn.apache.org/r1654747
Log:
SLING-4131 : migrate to httpclient 4.x (from 3)
Modified:
sling/trunk/bundles/extensions/discovery/impl/pom.xml
sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/topology/connector/TopologyConnectorClient.java
sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/topology/connector/TopologyRequestValidator.java
sling/trunk/bundles/extensions/discovery/impl/src/test/java/org/apache/sling/discovery/impl/topology/connector/TopologyRequestValidatorTest.java
Modified: sling/trunk/bundles/extensions/discovery/impl/pom.xml
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/discovery/impl/pom.xml?rev=1654747&r1=1654746&r2=1654747&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/discovery/impl/pom.xml (original)
+++ sling/trunk/bundles/extensions/discovery/impl/pom.xml Mon Jan 26 09:21:36
2015
@@ -154,9 +154,9 @@
<artifactId>servlet-api</artifactId>
</dependency>
<dependency>
- <groupId>commons-httpclient</groupId>
- <artifactId>commons-httpclient</artifactId>
- <version>3.1</version>
+ <groupId>org.apache.httpcomponents</groupId>
+ <artifactId>httpclient-osgi</artifactId>
+ <version>4.3.5</version>
<scope>provided</scope>
</dependency>
<dependency>
Modified:
sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/topology/connector/TopologyConnectorClient.java
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/topology/connector/TopologyConnectorClient.java?rev=1654747&r1=1654746&r2=1654747&view=diff
==============================================================================
---
sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/topology/connector/TopologyConnectorClient.java
(original)
+++
sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/topology/connector/TopologyConnectorClient.java
Mon Jan 26 09:21:36 2015
@@ -28,18 +28,22 @@ import java.util.zip.GZIPOutputStream;
import javax.servlet.http.HttpServletResponse;
-import org.apache.commons.httpclient.Credentials;
-import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
-import org.apache.commons.httpclient.Header;
-import org.apache.commons.httpclient.HttpClient;
-import org.apache.commons.httpclient.URIException;
-import org.apache.commons.httpclient.UsernamePasswordCredentials;
-import org.apache.commons.httpclient.auth.AuthScope;
-import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
-import org.apache.commons.httpclient.methods.DeleteMethod;
-import org.apache.commons.httpclient.methods.PutMethod;
-import org.apache.commons.httpclient.methods.StringRequestEntity;
-import org.apache.commons.httpclient.params.HttpMethodParams;
+import org.apache.http.Header;
+import org.apache.http.auth.AuthScope;
+import org.apache.http.auth.Credentials;
+import org.apache.http.auth.UsernamePasswordCredentials;
+import org.apache.http.client.config.RequestConfig;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpDelete;
+import org.apache.http.client.methods.HttpPut;
+import org.apache.http.client.protocol.HttpClientContext;
+import org.apache.http.config.SocketConfig;
+import org.apache.http.entity.ByteArrayEntity;
+import org.apache.http.entity.ContentType;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
+import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.discovery.ClusterView;
import org.apache.sling.discovery.InstanceDescription;
@@ -154,15 +158,23 @@ public class TopologyConnectorClient imp
if (logger.isDebugEnabled()) {
logger.debug("ping: connectorUrl=" + connectorUrl + ", complete
uri=" + uri);
}
- HttpClient httpClient = new HttpClient();
- final PutMethod method = new PutMethod(uri);
+ final HttpClientContext clientContext = HttpClientContext.create();
+ final CloseableHttpClient httpClient = createHttpClient();
+ final HttpPut putRequest = new HttpPut(uri);
+
+ // setting the connection timeout (idle connection, configured in
seconds)
+ putRequest.setConfig(RequestConfig.
+ custom().
+ setConnectTimeout(1000*config.getConnectionTimeout()).
+ build());
+
Announcement resultingAnnouncement = null;
try {
String userInfo = connectorUrl.getUserInfo();
if (userInfo != null) {
Credentials c = new UsernamePasswordCredentials(userInfo);
- httpClient.getState().setCredentials(
- new AuthScope(method.getURI().getHost(), method
+ clientContext.getCredentialsProvider().setCredentials(
+ new AuthScope(putRequest.getURI().getHost(), putRequest
.getURI().getPort()), c);
}
@@ -201,47 +213,44 @@ public class TopologyConnectorClient imp
if (logger.isDebugEnabled()) {
logger.debug("ping: topologyAnnouncement json is: " + p);
}
- requestValidator.trustMessage(method, p);
+ requestValidator.trustMessage(putRequest, p);
if (config.isGzipConnectorRequestsEnabled()) {
// tell the server that the content is gzipped:
- method.addRequestHeader("Content-Encoding", "gzip");
+ putRequest.addHeader("Content-Encoding", "gzip");
// and gzip the body:
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final GZIPOutputStream gzipOut = new GZIPOutputStream(baos);
gzipOut.write(p.getBytes("UTF-8"));
gzipOut.close();
final byte[] gzippedEncodedJson = baos.toByteArray();
- method.setRequestEntity(new
ByteArrayRequestEntity(gzippedEncodedJson, "application/json"));
+ putRequest.setEntity(new ByteArrayEntity(gzippedEncodedJson,
ContentType.APPLICATION_JSON));
lastRequestEncoding = "gzip";
} else {
// otherwise plaintext:
- method.setRequestEntity(new StringRequestEntity(p,
"application/json", "UTF-8"));
+ final StringEntity plaintext = new StringEntity(p, "UTF-8");
+
plaintext.setContentType(ContentType.APPLICATION_JSON.getMimeType());
+ putRequest.setEntity(plaintext);
lastRequestEncoding = "plaintext";
}
// independent of request-gzipping, we do accept the response to
be gzipped,
// so indicate this to the server:
- method.addRequestHeader("Accept-Encoding", "gzip");
- DefaultHttpMethodRetryHandler retryhandler = new
DefaultHttpMethodRetryHandler(0, false);
-
httpClient.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
retryhandler);
-
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(1000*config.getConnectionTimeout());
-
httpClient.getHttpConnectionManager().getParams().setSoTimeout(1000*config.getSoTimeout());
- method.getParams().setSoTimeout(1000*config.getSoTimeout());
- httpClient.executeMethod(method);
+ putRequest.addHeader("Accept-Encoding", "gzip");
+ final CloseableHttpResponse response =
httpClient.execute(putRequest, clientContext);
if (logger.isDebugEnabled()) {
- logger.debug("ping: done. code=" + method.getStatusCode() +
" - "
- + method.getStatusText());
+ logger.debug("ping: done. code=" +
response.getStatusLine().getStatusCode() + " - "
+ + response.getStatusLine().getReasonPhrase());
}
- lastStatusCode = method.getStatusCode();
+ lastStatusCode = response.getStatusLine().getStatusCode();
lastResponseEncoding = null;
- if (method.getStatusCode()==HttpServletResponse.SC_OK) {
- final Header contentEncoding =
method.getResponseHeader("Content-Encoding");
+ if
(response.getStatusLine().getStatusCode()==HttpServletResponse.SC_OK) {
+ final Header contentEncoding =
response.getFirstHeader("Content-Encoding");
if (contentEncoding!=null && contentEncoding.getValue()!=null
&&
contentEncoding.getValue().contains("gzip")) {
lastResponseEncoding = "gzip";
} else {
lastResponseEncoding = "plaintext";
}
- String responseBody = requestValidator.decodeMessage(method);
// limiting to 16MB, should be way enough
+ final String responseBody =
requestValidator.decodeMessage(putRequest.getURI().getPath(), response); //
limiting to 16MB, should be way enough
if (logger.isDebugEnabled()) {
logger.debug("ping: response body=" + responseBody);
}
@@ -294,9 +303,6 @@ public class TopologyConnectorClient imp
}
// SLING-2882 : reset suppressPingWarnings_ flag in success case
suppressPingWarnings_ = false;
- } catch (URIException e) {
- logger.warn("ping: Got URIException: " + e + ", uri=" + uri);
- statusDetails = e.toString();
} catch (IOException e) {
// SLING-2882 : set/check the suppressPingWarnings_ flag
if (suppressPingWarnings_) {
@@ -315,12 +321,29 @@ public class TopologyConnectorClient imp
logger.warn("ping: got RuntimeException: " + re, re);
statusDetails = re.toString();
} finally {
- method.releaseConnection();
+ putRequest.releaseConnection();
lastInheritedAnnouncement = resultingAnnouncement;
lastPingedAt = System.currentTimeMillis();
+ try {
+ httpClient.close();
+ } catch (IOException e) {
+ logger.error("disconnect: could not close
httpClient: "+e, e);
+ }
}
}
+ private CloseableHttpClient createHttpClient() {
+ final HttpClientBuilder builder = HttpClientBuilder.create();
+ // setting the SoTimeout (which is configured in seconds)
+ builder.setDefaultSocketConfig(SocketConfig.
+ custom().
+ setSoTimeout(1000*config.getSoTimeout()).
+ build());
+ builder.setRetryHandler(new DefaultHttpRequestRetryHandler(0,
false));
+
+ return builder.build();
+ }
+
public int getStatusCode() {
return lastStatusCode;
}
@@ -423,34 +446,43 @@ public class TopologyConnectorClient imp
.getOwnerId());
}
- HttpClient httpClient = new HttpClient();
- final DeleteMethod method = new DeleteMethod(uri);
+ final HttpClientContext clientContext = HttpClientContext.create();
+ final CloseableHttpClient httpClient = createHttpClient();
+ final HttpDelete deleteRequest = new HttpDelete(uri);
+ // setting the connection timeout (idle connection, configured in
seconds)
+ deleteRequest.setConfig(RequestConfig.
+ custom().
+ setConnectTimeout(1000*config.getConnectionTimeout()).
+ build());
try {
String userInfo = connectorUrl.getUserInfo();
if (userInfo != null) {
Credentials c = new UsernamePasswordCredentials(userInfo);
- httpClient.getState().setCredentials(
- new AuthScope(method.getURI().getHost(), method
+ clientContext.getCredentialsProvider().setCredentials(
+ new AuthScope(deleteRequest.getURI().getHost(),
deleteRequest
.getURI().getPort()), c);
}
- requestValidator.trustMessage(method, null);
- httpClient.executeMethod(method);
+ requestValidator.trustMessage(deleteRequest, null);
+ final CloseableHttpResponse response =
httpClient.execute(deleteRequest, clientContext);
if (logger.isDebugEnabled()) {
- logger.debug("disconnect: done. code=" +
method.getStatusCode()
- + " - " + method.getStatusText());
+ logger.debug("disconnect: done. code=" +
response.getStatusLine().getStatusCode()
+ + " - " +
response.getStatusLine().getReasonPhrase());
}
// ignoring the actual statuscode though as there's little we can
// do about it after this point
- } catch (URIException e) {
- logger.warn("disconnect: Got URIException: " + e);
} catch (IOException e) {
logger.warn("disconnect: got IOException: " + e);
} catch (RuntimeException re) {
logger.error("disconnect: got RuntimeException: " + re, re);
} finally {
- method.releaseConnection();
+ deleteRequest.releaseConnection();
+ try {
+ httpClient.close();
+ } catch (IOException e) {
+ logger.error("disconnect: could not close
httpClient: "+e, e);
+ }
}
}
}
Modified:
sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/topology/connector/TopologyRequestValidator.java
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/topology/connector/TopologyRequestValidator.java?rev=1654747&r1=1654746&r2=1654747&view=diff
==============================================================================
---
sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/topology/connector/TopologyRequestValidator.java
(original)
+++
sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/topology/connector/TopologyRequestValidator.java
Mon Jan 26 09:21:36 2015
@@ -50,10 +50,10 @@ import javax.servlet.http.HttpServletReq
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.codec.binary.Base64;
-import org.apache.commons.httpclient.Header;
-import org.apache.commons.httpclient.HttpMethod;
-import org.apache.commons.httpclient.HttpMethodBase;
import org.apache.commons.io.IOUtils;
+import org.apache.http.Header;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.HttpUriRequest;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
@@ -183,14 +183,14 @@ public class TopologyRequestValidator {
/**
* Decode a response from the server.
*
- * @param method the method that received the response.
+ * @param response the response.
* @return the message in clear text.
* @throws IOException if there was a problem decoding the message.
*/
- public String decodeMessage(HttpMethod method) throws IOException {
+ public String decodeMessage(String uri, HttpResponse response) throws
IOException {
checkActive();
- return decodeMessage("response:", method.getPath(),
getResponseBody(method),
- getResponseHeader(method, HASH_HEADER));
+ return decodeMessage("response:", uri, getResponseBody(response),
+ getResponseHeader(response, HASH_HEADER));
}
/**
@@ -258,14 +258,14 @@ public class TopologyRequestValidator {
/**
* Is the response from the server to be trusted by the client.
*
- * @param method the client method.
+ * @param response the response
* @return true if trusted, or true if this component is disabled.
*/
- public boolean isTrusted(HttpMethod method) {
+ public boolean isTrusted(HttpResponse response) {
checkActive();
if (trustEnabled) {
- return checkTrustHeader(getResponseHeader(method, HASH_HEADER),
- getResponseHeader(method, SIG_HEADER));
+ return checkTrustHeader(getResponseHeader(response, HASH_HEADER),
+ getResponseHeader(response, SIG_HEADER));
}
return false;
}
@@ -276,12 +276,12 @@ public class TopologyRequestValidator {
* @param method the method which will have headers set after the call.
* @param body the body.
*/
- public void trustMessage(HttpMethod method, String body) {
+ public void trustMessage(HttpUriRequest method, String body) {
checkActive();
if (trustEnabled) {
- String bodyHash = hash("request:" + method.getPath() + ":" + body);
- method.setRequestHeader(HASH_HEADER, bodyHash);
- method.setRequestHeader(SIG_HEADER, createTrustHeader(bodyHash));
+ String bodyHash = hash("request:" + method.getURI().getPath() +
":" + body);
+ method.setHeader(HASH_HEADER, bodyHash);
+ method.setHeader(SIG_HEADER, createTrustHeader(bodyHash));
}
}
@@ -516,12 +516,12 @@ public class TopologyRequestValidator {
/**
* Get the value of a response header.
*
- * @param method the method
+ * @param response the response
* @param name the name of the response header.
* @return the value of the response header, null if none.
*/
- private String getResponseHeader(HttpMethod method, String name) {
- Header h = method.getResponseHeader(name);
+ private String getResponseHeader(HttpResponse response, String name) {
+ Header h = response.getFirstHeader(name);
if (h == null) {
return null;
}
@@ -550,25 +550,22 @@ public class TopologyRequestValidator {
}
/**
- * @param method the response method.
+ * @param response the response
* @return the body of the response from the server.
* @throws IOException
*/
- private String getResponseBody(HttpMethod method) throws IOException {
- final Header contentEncoding =
method.getResponseHeader("Content-Encoding");
+ private String getResponseBody(HttpResponse response) throws IOException {
+ final Header contentEncoding =
response.getFirstHeader("Content-Encoding");
if (contentEncoding!=null && contentEncoding.getValue()!=null &&
contentEncoding.getValue().contains("gzip")) {
// then the server sent gzip - treat it so:
- final GZIPInputStream gzipIn = new
GZIPInputStream(method.getResponseBodyAsStream());
+ final GZIPInputStream gzipIn = new
GZIPInputStream(response.getEntity().getContent());
final String gunzippedEncodedJson = IOUtils.toString(gzipIn);
gzipIn.close();
return gunzippedEncodedJson;
} else {
- // otherwise the server sent plaintext:
- if (method instanceof HttpMethodBase) {
- return ((HttpMethodBase) method).getResponseBodyAsString(16 *
1024 * 1024);
- }
- return method.getResponseBodyAsString();
+ // otherwise the server sent plaintext:
+ return IOUtils.toString(response.getEntity().getContent(),
"UTF-8");
}
}
Modified:
sling/trunk/bundles/extensions/discovery/impl/src/test/java/org/apache/sling/discovery/impl/topology/connector/TopologyRequestValidatorTest.java
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/discovery/impl/src/test/java/org/apache/sling/discovery/impl/topology/connector/TopologyRequestValidatorTest.java?rev=1654747&r1=1654746&r2=1654747&view=diff
==============================================================================
---
sling/trunk/bundles/extensions/discovery/impl/src/test/java/org/apache/sling/discovery/impl/topology/connector/TopologyRequestValidatorTest.java
(original)
+++
sling/trunk/bundles/extensions/discovery/impl/src/test/java/org/apache/sling/discovery/impl/topology/connector/TopologyRequestValidatorTest.java
Mon Jan 26 09:21:36 2015
@@ -18,6 +18,7 @@
package org.apache.sling.discovery.impl.topology.connector;
import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringReader;
import java.lang.reflect.Field;
@@ -27,9 +28,10 @@ import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-import org.apache.commons.httpclient.Header;
-import org.apache.commons.httpclient.HttpMethod;
-import org.apache.commons.httpclient.methods.PutMethod;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.HttpPut;
+import org.apache.http.message.BasicHeader;
import org.apache.sling.discovery.impl.Config;
import org.hamcrest.Description;
import org.jmock.Expectations;
@@ -67,33 +69,33 @@ public class TopologyRequestValidatorTes
@Test
public void testTrustRequest() throws IOException {
- final PutMethod method = new PutMethod("/TestUri");
+ final HttpPut method = new HttpPut("/TestUri");
String clearMessage = "TestMessage";
final String message =
topologyRequestValidator.encodeMessage(clearMessage);
Assert.assertNotNull(message);
Assert.assertNotEquals(message, clearMessage);
topologyRequestValidator.trustMessage(method, message);
-
Assert.assertNotNull(method.getRequestHeader(TopologyRequestValidator.HASH_HEADER));
-
Assert.assertNotNull(method.getRequestHeader(TopologyRequestValidator.HASH_HEADER).getValue());
-
Assert.assertTrue(method.getRequestHeader(TopologyRequestValidator.HASH_HEADER).getValue().length()
> 0);
-
Assert.assertNotNull(method.getRequestHeader(TopologyRequestValidator.SIG_HEADER));
-
Assert.assertNotNull(method.getRequestHeader(TopologyRequestValidator.SIG_HEADER).getValue());
-
Assert.assertTrue(method.getRequestHeader(TopologyRequestValidator.SIG_HEADER).getValue().length()
> 0);
+
Assert.assertNotNull(method.getFirstHeader(TopologyRequestValidator.HASH_HEADER));
+
Assert.assertNotNull(method.getFirstHeader(TopologyRequestValidator.HASH_HEADER).getValue());
+
Assert.assertTrue(method.getFirstHeader(TopologyRequestValidator.HASH_HEADER).getValue().length()
> 0);
+
Assert.assertNotNull(method.getFirstHeader(TopologyRequestValidator.SIG_HEADER));
+
Assert.assertNotNull(method.getFirstHeader(TopologyRequestValidator.SIG_HEADER).getValue());
+
Assert.assertTrue(method.getFirstHeader(TopologyRequestValidator.SIG_HEADER).getValue().length()
> 0);
final HttpServletRequest request =
context.mock(HttpServletRequest.class);
context.checking(new Expectations() {
{
allowing(request).getHeader(with(TopologyRequestValidator.HASH_HEADER));
-
will(returnValue(method.getRequestHeader(TopologyRequestValidator.HASH_HEADER).getValue()));
+
will(returnValue(method.getFirstHeader(TopologyRequestValidator.HASH_HEADER).getValue()));
allowing(request).getHeader(with(TopologyRequestValidator.SIG_HEADER));
-
will(returnValue(method.getRequestHeader(TopologyRequestValidator.SIG_HEADER).getValue()));
+
will(returnValue(method.getFirstHeader(TopologyRequestValidator.SIG_HEADER).getValue()));
allowing(request).getHeader(with("Content-Encoding"));
will(returnValue(""));
allowing(request).getRequestURI();
- will(returnValue(method.getPath()));
+ will(returnValue(method.getURI().getPath()));
allowing(request).getReader();
will(returnValue(new BufferedReader(new
StringReader(message))));
@@ -140,30 +142,35 @@ public class TopologyRequestValidatorTes
final String message =
topologyRequestValidator.encodeMessage(clearMessage);
topologyRequestValidator.trustMessage(response, request, message);
- final HttpMethod method = context.mock(HttpMethod.class);
+ final HttpEntity responseEntity = context.mock(HttpEntity.class);
+ context.checking(new Expectations() {
+ {
+ allowing(responseEntity).getContent();
+ will(returnValue(new
ByteArrayInputStream(message.getBytes())));
+ }
+ });
+
+ final HttpResponse resp = context.mock(HttpResponse.class);
context.checking(new Expectations(){
{
- allowing(method).getResponseHeader(with(any(String.class)));
+ allowing(resp).getFirstHeader(with(any(String.class)));
will(new Action() {
public void describeTo(Description desc) {
- desc.appendText("Getting header ");
+ desc.appendText("Getting (first) header ");
}
public Object invoke(Invocation invocation) throws
Throwable {
- return new Header((String)invocation.getParameter(0),
(String)headers.get(invocation.getParameter(0)));
+ return new
BasicHeader((String)invocation.getParameter(0),
(String)headers.get(invocation.getParameter(0)));
}
});
- allowing(method).getPath();
- will(returnValue("/Test/Uri2"));
-
- allowing(method).getResponseBodyAsString();
- will(returnValue(message));
+ allowing(resp).getEntity();
+ will(returnValue(responseEntity));
}
});
- topologyRequestValidator.isTrusted(method);
- topologyRequestValidator.decodeMessage(method);
+ topologyRequestValidator.isTrusted(resp);
+ topologyRequestValidator.decodeMessage("/Test/Uri2", resp);
}