Skip to content

Commit

Permalink
[WISE-287] Updated from Apache httpclient 3.1 to httpclient 4.5 version
Browse files Browse the repository at this point in the history
  • Loading branch information
rsearls committed Aug 13, 2016
1 parent ff8f8dd commit 28d3334
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,37 @@
*/
package org.jboss.wise.core.client.jaxrs.impl;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import net.jcip.annotations.ThreadSafe;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethodBase;
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
import org.apache.commons.httpclient.methods.DeleteMethod;
import org.apache.commons.httpclient.methods.FileRequestEntity;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
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.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.FileEntity;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.client.methods.HttpRequestBase;
import org.jboss.wise.core.client.InvocationResult;
import org.jboss.wise.core.client.impl.reflection.InvocationResultImpl;
import org.jboss.wise.core.client.jaxrs.RSDynamicClient;
import org.jboss.wise.core.mapper.WiseMapper;
import org.jboss.logging.Logger;

/*
* TODO:
Expand All @@ -59,6 +66,8 @@
@ThreadSafe
public class RSDynamicClientImpl implements RSDynamicClient {

private static Logger log = Logger.getLogger(RSDynamicClientImpl.class);

private final String resourceURI;
private String user;
private String password;
Expand All @@ -85,7 +94,7 @@ public RSDynamicClientImpl( String resourceURI,
this.consumeMediaTypes = consumeMediaTypes;
this.httpMethod = httpMethod;

this.httpClient = new HttpClient();
this.httpClient = HttpClientBuilder.create().build();
}

public RSDynamicClientImpl( String resourceURI,
Expand All @@ -99,7 +108,7 @@ public RSDynamicClientImpl( String resourceURI,
this.httpMethod = httpMethod;
this.requestHeaders = requestHeaders;

this.httpClient = new HttpClient();
this.httpClient = HttpClientBuilder.create().build();
}

public String getResourceURI() {
Expand Down Expand Up @@ -135,48 +144,48 @@ public InvocationResult invoke( Map<String, Object> inputObjects,

public InvocationResult invoke( InputStream request,
WiseMapper mapper ) {
return invoke(new InputStreamRequestEntity(request), mapper);
return invoke(new InputStreamEntity(request), mapper);
}

public InvocationResult invoke( String request,
WiseMapper mapper ) {
RequestEntity requestEntity = null;
try {
requestEntity = new StringRequestEntity(request, produceMediaTypes, "UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO:
}
StringEntity requestEntity = null;
requestEntity = new StringEntity(request, "UTF-8");
requestEntity.setContentType(produceMediaTypes);
return invoke(requestEntity, mapper);
}

public InvocationResult invoke( byte[] request,
WiseMapper mapper ) {
return invoke(new ByteArrayRequestEntity(request), mapper);
return invoke(new ByteArrayEntity(request), mapper);
}

public InvocationResult invoke( File request,
WiseMapper mapper ) {
return invoke(new FileRequestEntity(request, produceMediaTypes), mapper);
return invoke(new FileEntity(request, ContentType.create(produceMediaTypes)), mapper);
}

public InvocationResult invoke() {
RequestEntity requestEntity = null;
HttpEntity requestEntity = null;
return invoke(requestEntity, null);
}

public InvocationResult invoke( RequestEntity requestEntity,
public InvocationResult invoke( HttpEntity requestEntity,
WiseMapper mapper ) {
InvocationResult result = null;
Map<String, Object> responseHolder = new HashMap<String, Object>();

if (HttpMethod.GET == httpMethod) {
GetMethod get = new GetMethod(resourceURI);
HttpGet get = new HttpGet(resourceURI);
setRequestHeaders(get);

try {
int statusCode = httpClient.executeMethod(get);
URL url = get.getURI().toURL();
HttpHost targetHost = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
HttpResponse hResponse = httpClient.execute(targetHost, get);
int statusCode = hResponse.getStatusLine().getStatusCode();
// TODO: Use InputStream
String response = get.getResponseBodyAsString();
String response = getResponseBodyAsString(hResponse);
responseHolder.put(InvocationResult.STATUS, Integer.valueOf(statusCode));

result = new InvocationResultImpl(InvocationResult.RESPONSE, null, response, responseHolder);
Expand All @@ -188,14 +197,17 @@ public InvocationResult invoke( RequestEntity requestEntity,
get.releaseConnection();
}
} else if (HttpMethod.POST == httpMethod) {
PostMethod post = new PostMethod(resourceURI);
HttpPost post = new HttpPost(resourceURI);
setRequestHeaders(post);

post.setRequestEntity(requestEntity);
post.setEntity(requestEntity);

try {
int statusCode = httpClient.executeMethod(post);
String response = post.getResponseBodyAsString();
URL url = post.getURI().toURL();
HttpHost targetHost = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
HttpResponse hResponse = httpClient.execute(targetHost, post);
int statusCode = hResponse.getStatusLine().getStatusCode();
String response = getResponseBodyAsString(hResponse);
responseHolder.put(InvocationResult.STATUS, Integer.valueOf(statusCode));

result = new InvocationResultImpl(InvocationResult.RESPONSE, null, response, responseHolder);
Expand All @@ -207,14 +219,17 @@ public InvocationResult invoke( RequestEntity requestEntity,
post.releaseConnection();
}
} else if (HttpMethod.PUT == httpMethod) {
PutMethod put = new PutMethod(resourceURI);
HttpPut put = new HttpPut(resourceURI);
setRequestHeaders(put);

put.setRequestEntity(requestEntity);
put.setEntity(requestEntity);

try {
int statusCode = httpClient.executeMethod(put);
String response = put.getResponseBodyAsString();
URL url = put.getURI().toURL();
HttpHost targetHost = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
HttpResponse hResponse = httpClient.execute(targetHost, put);
int statusCode = hResponse.getStatusLine().getStatusCode();
String response = getResponseBodyAsString(hResponse);
responseHolder.put(InvocationResult.STATUS, Integer.valueOf(statusCode));

result = new InvocationResultImpl(InvocationResult.RESPONSE, null, response, responseHolder);
Expand All @@ -226,12 +241,15 @@ public InvocationResult invoke( RequestEntity requestEntity,
put.releaseConnection();
}
} else if (HttpMethod.DELETE == httpMethod) {
DeleteMethod delete = new DeleteMethod(resourceURI);
HttpDelete delete = new HttpDelete(resourceURI);
setRequestHeaders(delete);

try {
int statusCode = httpClient.executeMethod(delete);
String response = delete.getResponseBodyAsString();
URL url = delete.getURI().toURL();
HttpHost targetHost = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
HttpResponse hResponse = httpClient.execute(targetHost, delete);
int statusCode = hResponse.getStatusLine().getStatusCode();
String response = getResponseBodyAsString(hResponse);
responseHolder.put(InvocationResult.STATUS, Integer.valueOf(statusCode));

result = new InvocationResultImpl(InvocationResult.RESPONSE, null, response, responseHolder);
Expand All @@ -247,20 +265,37 @@ public InvocationResult invoke( RequestEntity requestEntity,
return result;
}

private void setRequestHeaders( HttpMethodBase method ) {
private void setRequestHeaders( HttpRequestBase method ) {
if (produceMediaTypes != null) {
method.setRequestHeader("Content-Type", produceMediaTypes);
method.addHeader("Content-Type", produceMediaTypes);
}

if (consumeMediaTypes != null) {
method.setRequestHeader("Accept", consumeMediaTypes);
method.addHeader("Accept", consumeMediaTypes);
}

for (String headerName : requestHeaders.keySet()) {
String headerValue = requestHeaders.get(headerName);

method.setRequestHeader(headerName, headerValue);
method.addHeader(headerName, headerValue);
}
}

private String getResponseBodyAsString(HttpResponse httpResponse) {
StringBuffer buffer = new StringBuffer();
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(httpResponse.getEntity().getContent()));
String dataLine = null;

while ((dataLine = reader.readLine()) != null) {
buffer.append(dataLine);
}
} catch (IOException e) {
log.error(e);
}

return buffer.toString();
}

}
4 changes: 2 additions & 2 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
</dependency>

<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>

<dependency>
Expand Down
4 changes: 2 additions & 2 deletions integration-testsuite/common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@
<artifactId>commons-logging</artifactId>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.wise</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public static WebArchive createDeploymentB() {
.addAsLibrary(resolver.loadPomFromFile(getTestResourcesDir() + "/../../../pom.xml").resolve("commons-collections:commons-collections").withoutTransitivity().asSingleFile())
.addAsLibrary(resolver.loadPomFromFile(getTestResourcesDir() + "/../../../pom.xml").resolve("commons-pool:commons-pool").withoutTransitivity().asSingleFile())
.addAsLibrary(resolver.loadPomFromFile(getTestResourcesDir() + "/../../../pom.xml").resolve("commons-cli:commons-cli").withoutTransitivity().asSingleFile())
.addAsLibrary(resolver.loadPomFromFile(getTestResourcesDir() + "/../../../pom.xml").resolve("commons-httpclient:commons-httpclient").withoutTransitivity().asSingleFile())
.addAsLibrary(resolver.loadPomFromFile(getTestResourcesDir() + "/../../../pom.xml").resolve("org.apache.httpcomponents:httpclient").withoutTransitivity().asSingleFile())
.addAsLibrary(resolver.loadPomFromFile(getTestResourcesDir() + "/../../../pom.xml").resolve("commons-codec:commons-codec").withoutTransitivity().asSingleFile())
.addAsLibrary(resolver.loadPomFromFile(getTestResourcesDir() + "/../../../pom.xml").resolve("commons-lang:commons-lang").withoutTransitivity().asSingleFile())
.addAsLibrary(resolver.loadPomFromFile(getTestResourcesDir() + "/../../../pom.xml").resolve("org.apache.commons:commons-lang3").withoutTransitivity().asSingleFile())
Expand Down
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
<cxf.version>2.6.4</cxf.version>
<jaxb.impl.version>2.2.5</jaxb.impl.version>
<compiler.fork>false</compiler.fork>
<commons-httpclient.version>3.1</commons-httpclient.version>
<commons-httpclient.version>4.5</commons-httpclient.version>
<commons-codec.version>1.2</commons-codec.version>
<commons-logging.version>1.0.4</commons-logging.version>
<commons-io.version>1.4</commons-io.version>
Expand Down Expand Up @@ -159,8 +159,8 @@
</dependency>

<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${commons-httpclient.version}</version>
</dependency>

Expand Down

0 comments on commit 28d3334

Please sign in to comment.