Skip to content

Commit

Permalink
Merge pull request #1072 from kushalagrawal1/master
Browse files Browse the repository at this point in the history
Issue : #[1070](#1070)
  • Loading branch information
auhlig authored Sep 19, 2017
2 parents b8ede13 + 438f522 commit 044608b
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 104 deletions.
15 changes: 13 additions & 2 deletions connectors/resteasy/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,20 @@
<packaging>jar</packaging>

<properties>
<resteasy-version>2.3.7.Final</resteasy-version>
<resteasy-version>3.1.4.Final</resteasy-version>
</properties>

<dependencies>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<artifactId>resteasy-client</artifactId>
<version>${resteasy-version}</version>
<exclusions>
<exclusion>
<artifactId>httpclient</artifactId>
<groupId>org.apache.httpcomponents</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
Expand All @@ -30,6 +36,11 @@
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
package org.openstack4j.connectors.resteasy;

import java.util.List;
import java.util.Map;

import javax.ws.rs.core.UriBuilder;

import org.jboss.resteasy.client.ClientRequest;
import org.jboss.resteasy.client.ClientResponse;
import org.openstack4j.connectors.resteasy.executors.ApacheHttpClientExecutor;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
import org.openstack4j.connectors.resteasy.executors.ApacheHttpClientEngine;
import org.openstack4j.core.transport.ClientConstants;
import org.openstack4j.core.transport.HttpRequest;
import org.openstack4j.core.transport.functions.EndpointURIFromRequestFunction;

import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;
import java.util.List;
import java.util.Map;

/**
* HttpCommand is responsible for executing the actual request driven by the HttpExecutor.
*
* HttpCommand is responsible for executing the actual request driven by the HttpExecutor.
*
* @param <R>
*/
public final class HttpCommand<R> {

private HttpRequest<R> request;
private ClientRequest client;
private ResteasyWebTarget resteasyWebTarget;
private int retries;
private Invocation.Builder resteasyRequest ;

private HttpCommand(HttpRequest<R> request) {
this.request = request;
Expand All @@ -33,37 +36,39 @@ private HttpCommand(HttpRequest<R> request) {
* @return the command
*/
public static <R> HttpCommand<R> create(HttpRequest<R> request) {
HttpCommand<R> command = new HttpCommand<R>(request);
HttpCommand<R> command = new HttpCommand<>(request);
command.initialize();
return command;
}

private void initialize() {
client = new ClientRequest(UriBuilder.fromUri(new EndpointURIFromRequestFunction().apply(request)),
ApacheHttpClientExecutor.create(request.getConfig()), ResteasyClientFactory.getInstance());

client.followRedirects(true);

resteasyWebTarget = new ResteasyClientBuilder().httpEngine(ApacheHttpClientEngine.create(request.getConfig()))
.providerFactory(ResteasyClientFactory.getInstance()).build()
.target(UriBuilder.fromUri(new EndpointURIFromRequestFunction().apply(request)));

populateQueryParams(request);
resteasyRequest = resteasyWebTarget.request();
populateHeaders(request);
}

/**
* Executes the command and returns the Response
*
*
* @return the response
* @throws Exception
*/
public ClientResponse<R> execute() throws Exception {

public Response execute(){

Invocation webRequest;
if (request.getEntity() != null) {
client.body(request.getContentType(), request.getEntity());
webRequest = resteasyRequest.build(request.getMethod().name(), Entity.entity(request.getEntity(), request.getContentType()));
} else if (request.hasJson()) {
webRequest= resteasyRequest.build(request.getMethod().name() , Entity.entity(request.getJson(),ClientConstants.CONTENT_TYPE_JSON));
}else{
webRequest = resteasyRequest.build(request.getMethod().name());
}
else if(request.hasJson()) {
client.body(ClientConstants.CONTENT_TYPE_JSON, request.getJson());
}
ClientResponse<R> response = client.httpMethod(request.getMethod().name(), request.getReturnType());
return response;

return webRequest.invoke();
}

/**
Expand All @@ -72,21 +77,21 @@ else if(request.hasJson()) {
public boolean hasEntity() {
return request.getEntity() != null;
}

/**
* @return current retry execution count for this command
*/
public int getRetries() {
return retries;
}

/**
* @return incremement's the retry count and returns self
*/
public HttpCommand<R> incrementRetriesAndReturn() {
initialize();
retries++;
return this;
initialize();
retries++;
return this;
}

public HttpRequest<R> getRequest() {
Expand All @@ -97,9 +102,9 @@ private void populateQueryParams(HttpRequest<R> request) {

if (!request.hasQueryParams()) return;

for(Map.Entry<String, List<Object> > entry : request.getQueryParams().entrySet()) {
for (Map.Entry<String, List<Object>> entry : request.getQueryParams().entrySet()) {
for (Object o : entry.getValue()) {
client = client.queryParameter(entry.getKey(), o);
resteasyWebTarget = resteasyWebTarget.queryParam(entry.getKey(), o);
}
}
}
Expand All @@ -108,8 +113,8 @@ private void populateHeaders(HttpRequest<R> request) {

if (!request.hasHeaders()) return;

for(Map.Entry<String, Object> h : request.getHeaders().entrySet()) {
client.header(h.getKey(), h.getValue());
for (Map.Entry<String, Object> h : request.getHeaders().entrySet()) {
resteasyRequest.header(h.getKey(), h.getValue());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.openstack4j.connectors.resteasy;

import org.jboss.resteasy.client.ClientResponse;
import org.openstack4j.api.exceptions.ConnectionException;
import org.openstack4j.api.exceptions.ResponseException;
import org.openstack4j.core.transport.ClientConstants;
Expand All @@ -10,6 +9,8 @@
import org.openstack4j.openstack.internal.OSAuthenticator;
import org.openstack4j.openstack.internal.OSClientSession;

import javax.ws.rs.core.Response;

/**
* HttpExecutor is the default implementation for HttpExecutorService which is responsible for interfacing with Resteasy and mapping common status codes, requests and responses
* back to the common API
Expand Down Expand Up @@ -58,7 +59,7 @@ private <R> HttpResponse invoke(HttpRequest<R> request) throws Exception {
}

private <R> HttpResponse invokeRequest(HttpCommand<R> command) throws Exception {
ClientResponse<R> response = command.execute();
Response response = command.execute();
if (command.getRetries() == 0 && response.getStatus() == 401 && !command.getRequest().getHeaders().containsKey(ClientConstants.HEADER_OS4J_AUTH))
{
OSAuthenticator.reAuthenticate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@
import java.util.Map;

import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;

import org.jboss.resteasy.client.ClientResponse;
import org.openstack4j.core.transport.ClientConstants;
import org.openstack4j.core.transport.ExecutionOptions;
import org.openstack4j.core.transport.HttpEntityHandler;
import org.openstack4j.core.transport.HttpResponse;

public class HttpResponseImpl implements HttpResponse {

private final ClientResponse<?> response;
private final Response response;

private HttpResponseImpl(ClientResponse<?> response) {
private HttpResponseImpl(Response response) {
this.response = response;
}

Expand All @@ -27,7 +27,7 @@ private HttpResponseImpl(ClientResponse<?> response) {
* @param response the response
* @return the HttpResponse
*/
public static HttpResponseImpl wrap(ClientResponse<?> response) {
public static HttpResponseImpl wrap(Response response) {
return new HttpResponseImpl(response);
}

Expand All @@ -36,7 +36,7 @@ public static HttpResponseImpl wrap(ClientResponse<?> response) {
*
* @return the response
*/
public ClientResponse<?> unwrap() {
public Response unwrap() {
return response;
}

Expand Down Expand Up @@ -77,14 +77,15 @@ public int getStatus() {
*/
@Override
public String getStatusMessage() {
return response.getResponseStatus().getReasonPhrase();
return response.getStatusInfo().getReasonPhrase();
}

/**
* @return the input stream
*/
public InputStream getInputStream() {
return response.getEntity(InputStream.class);
response.bufferEntity();
return response.readEntity(InputStream.class);
}

/**
Expand All @@ -94,31 +95,32 @@ public InputStream getInputStream() {
* @return the header as a String or null if not found
*/
public String header(String name) {
return response.getHeaders().getFirst(name);
return response.getStringHeaders().getFirst(name);
}

/**
* @return the a Map of Header Name to Header Value
*/
public Map<String, String> headers() {
Map<String, String> headers = new HashMap<String, String>();
MultivaluedMap<String, String> responseHeaders = response.getHeaders();
Map<String, String> headers = new HashMap<>();
MultivaluedMap<String, String> responseHeaders = response.getStringHeaders();

for (String key : responseHeaders.keySet()) {
headers.put(key, responseHeaders.getFirst(key).toString());
headers.put(key, responseHeaders.getFirst(key));
}

return headers;
}

@Override
public <T> T readEntity(Class<T> typeToReadAs) {
return response.getEntity(typeToReadAs);
response.bufferEntity();
return response.readEntity(typeToReadAs);
}

@Override
public void close() throws IOException {
response.releaseConnection();
response.close();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package org.openstack4j.connectors.resteasy.executors;

import org.apache.http.HttpHost;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.impl.client.DefaultRedirectStrategy;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine;
import org.openstack4j.core.transport.Config;
import org.openstack4j.core.transport.UntrustedSSL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.MalformedURLException;
import java.net.URL;

/**
* Default Apache HttpClient based Executor
*
* @author Jeremy Unruh
*/
public class ApacheHttpClientEngine extends ApacheHttpClient4Engine {

ApacheHttpClientEngine(HttpClient client) {
super(client);
}
private static final Logger LOGGER = LoggerFactory.getLogger(ApacheHttpClientEngine.class);

public static ApacheHttpClientEngine create(Config config) {

RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();

if (config.getReadTimeout() > 0) {
requestConfigBuilder.setConnectionRequestTimeout(config.getReadTimeout());
}

if (config.getConnectTimeout() > 0) {
requestConfigBuilder.setConnectTimeout(config.getConnectTimeout());
}

/*HttpClient client*/

HttpClientBuilder httpClientBuilder = HttpClients.custom();

if (config.isIgnoreSSLVerification()) {
httpClientBuilder.setSSLContext(UntrustedSSL.getSSLContext());
httpClientBuilder.setSSLHostnameVerifier(new NoopHostnameVerifier());
}

if (config.getHostNameVerifier() != null) {
httpClientBuilder.setSSLHostnameVerifier(config.getHostNameVerifier());
}

if (config.getProxy() != null) {
try {
URL url = new URL(config.getProxy().getHost());
HttpHost proxy = new HttpHost(url.getHost(), config.getProxy().getPort(), url.getProtocol());
requestConfigBuilder.setProxy(proxy);
} catch (MalformedURLException e) {
LOGGER.error("Invalid proxy Url :%s",config.getProxy(),e);
}
}

httpClientBuilder.setDefaultRequestConfig(requestConfigBuilder.build());
httpClientBuilder.setRedirectStrategy(new DefaultRedirectStrategy() {
@Override
protected boolean isRedirectable(String method)
{
return true;
}
});

HttpClient client = httpClientBuilder.build();
return new ApacheHttpClientEngine(client);
}

}
Loading

0 comments on commit 044608b

Please sign in to comment.