Skip to content

Commit

Permalink
Merge pull request #11 from ContainX/master
Browse files Browse the repository at this point in the history
merge
  • Loading branch information
zizhongwei authored Oct 26, 2017
2 parents 2f9d38a + 18ca998 commit 75f8590
Show file tree
Hide file tree
Showing 297 changed files with 14,364 additions and 579 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ pom-shade.xml
*.versionsBackup
*/bin/*
*.iml
*.ipr
*.iws
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ OpenStack4j version 2.0.0+ is now modular. One of the benefits to this is the a
<dependency>
<groupId>org.pacesys</groupId>
<artifactId>openstack4j</artifactId>
<version>3.0.4</version>
<version>3.1.0</version>
</dependency>
```

Expand All @@ -54,7 +54,7 @@ See notes above about connectors (same rules apply) to development branches.
<dependency>
<groupId>org.pacesys</groupId>
<artifactId>openstack4j</artifactId>
<version>3.0.5-SNAPSHOT</version>
<version>3.1.1-SNAPSHOT</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion connectors/http-connector/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.pacesys.openstack4j.connectors</groupId>
<artifactId>openstack4j-connectors</artifactId>
<version>3.0.5-SNAPSHOT</version>
<version>3.1.1-SNAPSHOT</version>
</parent>
<name>OpenStack4j HttpURL Connector</name>
<artifactId>openstack4j-http-connector</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion connectors/httpclient/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<parent>
<groupId>org.pacesys.openstack4j.connectors</groupId>
<artifactId>openstack4j-connectors</artifactId>
<version>3.0.5-SNAPSHOT</version>
<version>3.1.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>openstack4j-httpclient</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion connectors/jersey2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<parent>
<groupId>org.pacesys.openstack4j.connectors</groupId>
<artifactId>openstack4j-connectors</artifactId>
<version>3.0.5-SNAPSHOT</version>
<version>3.1.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>openstack4j-jersey2</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion connectors/okhttp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<parent>
<groupId>org.pacesys.openstack4j.connectors</groupId>
<artifactId>openstack4j-connectors</artifactId>
<version>3.0.5-SNAPSHOT</version>
<version>3.1.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>openstack4j-okhttp</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ public <R> HttpResponse execute(HttpRequest<R> request) {
catch (ResponseException re) {
throw re;
}
catch (RuntimeException e) {
throw e;
}
catch (Exception e) {
e.printStackTrace();
return null;
throw new RuntimeException(e);
}
}

Expand Down
2 changes: 1 addition & 1 deletion connectors/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<parent>
<groupId>org.pacesys</groupId>
<artifactId>openstack4j-parent</artifactId>
<version>3.0.5-SNAPSHOT</version>
<version>3.1.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>org.pacesys.openstack4j.connectors</groupId>
Expand Down
17 changes: 14 additions & 3 deletions connectors/resteasy/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<parent>
<groupId>org.pacesys.openstack4j.connectors</groupId>
<artifactId>openstack4j-connectors</artifactId>
<version>3.0.5-SNAPSHOT</version>
<version>3.1.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>openstack4j-resteasy</artifactId>
Expand All @@ -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
Loading

0 comments on commit 75f8590

Please sign in to comment.