summaryrefslogtreecommitdiffstats
path: root/openstack-client-connectors/http-connector/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'openstack-client-connectors/http-connector/src/main')
-rw-r--r--openstack-client-connectors/http-connector/src/main/java/com/woorea/openstack/connector/HttpClientConnector.java326
-rw-r--r--openstack-client-connectors/http-connector/src/main/java/com/woorea/openstack/connector/HttpClientException.java24
-rw-r--r--openstack-client-connectors/http-connector/src/main/java/com/woorea/openstack/connector/HttpClientRedirectStrategy.java8
-rw-r--r--openstack-client-connectors/http-connector/src/main/java/com/woorea/openstack/connector/HttpClientResponse.java50
4 files changed, 204 insertions, 204 deletions
diff --git a/openstack-client-connectors/http-connector/src/main/java/com/woorea/openstack/connector/HttpClientConnector.java b/openstack-client-connectors/http-connector/src/main/java/com/woorea/openstack/connector/HttpClientConnector.java
index 050a4d9..02d851e 100644
--- a/openstack-client-connectors/http-connector/src/main/java/com/woorea/openstack/connector/HttpClientConnector.java
+++ b/openstack-client-connectors/http-connector/src/main/java/com/woorea/openstack/connector/HttpClientConnector.java
@@ -62,168 +62,168 @@ import com.woorea.openstack.base.client.OpenStackResponseException;
public class HttpClientConnector implements OpenStackClientConnector {
- public static ObjectMapper DEFAULT_MAPPER;
- public static ObjectMapper WRAPPED_MAPPER;
-
- private static Logger LOGGER = Logger.getLogger(HttpClientConnector.class);
-
- static {
- DEFAULT_MAPPER = new ObjectMapper();
-
- DEFAULT_MAPPER.setSerializationInclusion(Inclusion.NON_NULL);
- DEFAULT_MAPPER.disable(SerializationConfig.Feature.INDENT_OUTPUT);
- DEFAULT_MAPPER.enable(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
- DEFAULT_MAPPER.disable(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES);
-
- WRAPPED_MAPPER = new ObjectMapper();
-
- WRAPPED_MAPPER.setSerializationInclusion(Inclusion.NON_NULL);
- WRAPPED_MAPPER.disable(SerializationConfig.Feature.INDENT_OUTPUT);
- WRAPPED_MAPPER.enable(SerializationConfig.Feature.WRAP_ROOT_VALUE);
- WRAPPED_MAPPER.enable(DeserializationConfig.Feature.UNWRAP_ROOT_VALUE);
- WRAPPED_MAPPER.enable(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
- WRAPPED_MAPPER.disable(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES);
- }
-
- protected static <T> ObjectMapper getObjectMapper (Class<T> type) {
- return type.getAnnotation(JsonRootName.class) == null ? DEFAULT_MAPPER : WRAPPED_MAPPER;
- }
-
- @Override
- public <T> OpenStackResponse request(OpenStackRequest<T> request) {
-
- CloseableHttpClient httpClient = null; //HttpClients.createDefault();
- httpClient = HttpClients.custom().setRedirectStrategy(new HttpClientRedirectStrategy()).build();
-
- URI uri = null;
-
- // Build the URI with query params
- try {
- URIBuilder uriBuilder = new URIBuilder(request.endpoint() + request.path());
-
- for(Map.Entry<String, List<Object> > entry : request.queryParams().entrySet()) {
- for (Object o : entry.getValue()) {
- uriBuilder.setParameter(entry.getKey(), String.valueOf(o));
- }
- }
-
- uri = uriBuilder.build();
- } catch (URISyntaxException e) {
- throw new HttpClientException (e);
- }
-
- HttpEntity entity = null;
- if (request.entity() != null) {
- // Flatten the entity to a Json string
-
- try {
- // Get appropriate mapper, based on existence of a root element in Entity class
- ObjectMapper mapper = getObjectMapper (request.entity().getEntity().getClass());
-
- String entityJson = mapper.writeValueAsString (request.entity().getEntity());
- entity = new StringEntity(entityJson, ContentType.create(request.entity().getContentType()));
-
- LOGGER.debug("Openstack query JSON:"+entityJson);
- LOGGER.debug ("Request JSON Body: " + entityJson.replaceAll("\"password\":\"[^\"]*\"", "\"password\":\"***\""));
-
- } catch (JsonProcessingException e) {
- throw new HttpClientException ("Json processing error on request entity", e);
- } catch (IOException e) {
- throw new HttpClientException ("Json IO error on request entity", e);
- }
- }
-
- // Determine the HttpRequest class based on the method
- HttpUriRequest httpRequest;
-
- switch (request.method()) {
- case POST:
- HttpPost post = new HttpPost(uri);
- post.setEntity (entity);
- httpRequest = post;
- break;
-
- case GET:
- httpRequest = new HttpGet(uri);
- break;
-
- case PUT:
- HttpPut put = new HttpPut(uri);
- put.setEntity (entity);
- httpRequest = put;
- break;
-
- case DELETE:
- httpRequest = new HttpDelete(uri);
- break;
-
- default:
- throw new HttpClientException ("Unrecognized HTTP Method: " + request.method());
- }
-
- for (Entry<String, List<Object>> h : request.headers().entrySet()) {
- StringBuilder sb = new StringBuilder();
- for (Object v : h.getValue()) {
- sb.append(String.valueOf(v));
- }
- httpRequest.addHeader(h.getKey(), sb.toString());
- }
-
- LOGGER.debug ("Sending HTTP request: " + httpRequest.toString());
-
- // Get the Response. But don't get the body entity yet, as this response
- // will be wrapped in an HttpClientResponse. The HttpClientResponse
- // buffers the body in constructor, so can close the response here.
- HttpClientResponse httpClientResponse = null;
- CloseableHttpResponse httpResponse = null;
-
- // Catch known HttpClient exceptions, and wrap them in OpenStack Client Exceptions
- // so calling functions can distinguish. Only RuntimeExceptions are allowed.
- try {
- httpResponse = httpClient.execute(httpRequest);
-
- LOGGER.debug ("Response status: " + httpResponse.getStatusLine().getStatusCode());
-
- httpClientResponse = new HttpClientResponse (httpResponse);
-
- int status = httpResponse.getStatusLine().getStatusCode();
- if (status == HttpStatus.SC_OK || status == HttpStatus.SC_CREATED ||
- status == HttpStatus.SC_NO_CONTENT || status == HttpStatus.SC_ACCEPTED)
- {
- return httpClientResponse;
- }
- }
- catch (HttpResponseException e) {
- // What exactly does this mean? It does not appear to get thrown for
- // non-2XX responses as documented.
- throw new OpenStackResponseException(e.getMessage(), e.getStatusCode());
- }
- catch (UnknownHostException e) {
- throw new OpenStackConnectException("Unknown Host: " + e.getMessage());
- }
- catch (IOException e) {
- // Catch all other IOExceptions and throw as OpenStackConnectException
- throw new OpenStackConnectException(e.getMessage());
- }
- catch (Exception e) {
- // Catchall for anything else, must throw as a RuntimeException
- LOGGER.error ("Unexpected client exception: " +e.getMessage());
- throw new RuntimeException("Unexpected client exception", e);
- }
- finally {
- // Have the body. Close the stream
- if (httpResponse != null)
- try {
- httpResponse.close();
- } catch (IOException e) {
- LOGGER.warn("Unable to close HTTP Response: " + e);
- }
- }
-
- // Get here on an error response (4XX-5XX)
- throw new OpenStackResponseException(httpResponse.getStatusLine().getReasonPhrase(),
- httpResponse.getStatusLine().getStatusCode(),
- httpClientResponse);
- }
+ public static ObjectMapper DEFAULT_MAPPER;
+ public static ObjectMapper WRAPPED_MAPPER;
+
+ private static Logger LOGGER = Logger.getLogger(HttpClientConnector.class);
+
+ static {
+ DEFAULT_MAPPER = new ObjectMapper();
+
+ DEFAULT_MAPPER.setSerializationInclusion(Inclusion.NON_NULL);
+ DEFAULT_MAPPER.disable(SerializationConfig.Feature.INDENT_OUTPUT);
+ DEFAULT_MAPPER.enable(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
+ DEFAULT_MAPPER.disable(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES);
+
+ WRAPPED_MAPPER = new ObjectMapper();
+
+ WRAPPED_MAPPER.setSerializationInclusion(Inclusion.NON_NULL);
+ WRAPPED_MAPPER.disable(SerializationConfig.Feature.INDENT_OUTPUT);
+ WRAPPED_MAPPER.enable(SerializationConfig.Feature.WRAP_ROOT_VALUE);
+ WRAPPED_MAPPER.enable(DeserializationConfig.Feature.UNWRAP_ROOT_VALUE);
+ WRAPPED_MAPPER.enable(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
+ WRAPPED_MAPPER.disable(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES);
+ }
+
+ protected static <T> ObjectMapper getObjectMapper (Class<T> type) {
+ return type.getAnnotation(JsonRootName.class) == null ? DEFAULT_MAPPER : WRAPPED_MAPPER;
+ }
+
+ @Override
+ public <T> OpenStackResponse request(OpenStackRequest<T> request) {
+
+ CloseableHttpClient httpClient = null; //HttpClients.createDefault();
+ httpClient = HttpClients.custom().setRedirectStrategy(new HttpClientRedirectStrategy()).build();
+
+ URI uri = null;
+
+ // Build the URI with query params
+ try {
+ URIBuilder uriBuilder = new URIBuilder(request.endpoint() + request.path());
+
+ for(Map.Entry<String, List<Object> > entry : request.queryParams().entrySet()) {
+ for (Object o : entry.getValue()) {
+ uriBuilder.setParameter(entry.getKey(), String.valueOf(o));
+ }
+ }
+
+ uri = uriBuilder.build();
+ } catch (URISyntaxException e) {
+ throw new HttpClientException (e);
+ }
+
+ HttpEntity entity = null;
+ if (request.entity() != null) {
+ // Flatten the entity to a Json string
+
+ try {
+ // Get appropriate mapper, based on existence of a root element in Entity class
+ ObjectMapper mapper = getObjectMapper (request.entity().getEntity().getClass());
+
+ String entityJson = mapper.writeValueAsString (request.entity().getEntity());
+ entity = new StringEntity(entityJson, ContentType.create(request.entity().getContentType()));
+
+ LOGGER.debug("Openstack query JSON:"+entityJson);
+ LOGGER.debug ("Request JSON Body: " + entityJson.replaceAll("\"password\":\"[^\"]*\"", "\"password\":\"***\""));
+
+ } catch (JsonProcessingException e) {
+ throw new HttpClientException ("Json processing error on request entity", e);
+ } catch (IOException e) {
+ throw new HttpClientException ("Json IO error on request entity", e);
+ }
+ }
+
+ // Determine the HttpRequest class based on the method
+ HttpUriRequest httpRequest;
+
+ switch (request.method()) {
+ case POST:
+ HttpPost post = new HttpPost(uri);
+ post.setEntity (entity);
+ httpRequest = post;
+ break;
+
+ case GET:
+ httpRequest = new HttpGet(uri);
+ break;
+
+ case PUT:
+ HttpPut put = new HttpPut(uri);
+ put.setEntity (entity);
+ httpRequest = put;
+ break;
+
+ case DELETE:
+ httpRequest = new HttpDelete(uri);
+ break;
+
+ default:
+ throw new HttpClientException ("Unrecognized HTTP Method: " + request.method());
+ }
+
+ for (Entry<String, List<Object>> h : request.headers().entrySet()) {
+ StringBuilder sb = new StringBuilder();
+ for (Object v : h.getValue()) {
+ sb.append(String.valueOf(v));
+ }
+ httpRequest.addHeader(h.getKey(), sb.toString());
+ }
+
+ LOGGER.debug ("Sending HTTP request: " + httpRequest.toString());
+
+ // Get the Response. But don't get the body entity yet, as this response
+ // will be wrapped in an HttpClientResponse. The HttpClientResponse
+ // buffers the body in constructor, so can close the response here.
+ HttpClientResponse httpClientResponse = null;
+ CloseableHttpResponse httpResponse = null;
+
+ // Catch known HttpClient exceptions, and wrap them in OpenStack Client Exceptions
+ // so calling functions can distinguish. Only RuntimeExceptions are allowed.
+ try {
+ httpResponse = httpClient.execute(httpRequest);
+
+ LOGGER.debug ("Response status: " + httpResponse.getStatusLine().getStatusCode());
+
+ httpClientResponse = new HttpClientResponse (httpResponse);
+
+ int status = httpResponse.getStatusLine().getStatusCode();
+ if (status == HttpStatus.SC_OK || status == HttpStatus.SC_CREATED ||
+ status == HttpStatus.SC_NO_CONTENT || status == HttpStatus.SC_ACCEPTED)
+ {
+ return httpClientResponse;
+ }
+ }
+ catch (HttpResponseException e) {
+ // What exactly does this mean? It does not appear to get thrown for
+ // non-2XX responses as documented.
+ throw new OpenStackResponseException(e.getMessage(), e.getStatusCode());
+ }
+ catch (UnknownHostException e) {
+ throw new OpenStackConnectException("Unknown Host: " + e.getMessage());
+ }
+ catch (IOException e) {
+ // Catch all other IOExceptions and throw as OpenStackConnectException
+ throw new OpenStackConnectException(e.getMessage());
+ }
+ catch (Exception e) {
+ // Catchall for anything else, must throw as a RuntimeException
+ LOGGER.error ("Unexpected client exception: " +e.getMessage());
+ throw new RuntimeException("Unexpected client exception", e);
+ }
+ finally {
+ // Have the body. Close the stream
+ if (httpResponse != null)
+ try {
+ httpResponse.close();
+ } catch (IOException e) {
+ LOGGER.warn("Unable to close HTTP Response: " + e);
+ }
+ }
+
+ // Get here on an error response (4XX-5XX)
+ throw new OpenStackResponseException(httpResponse.getStatusLine().getReasonPhrase(),
+ httpResponse.getStatusLine().getStatusCode(),
+ httpClientResponse);
+ }
}
diff --git a/openstack-client-connectors/http-connector/src/main/java/com/woorea/openstack/connector/HttpClientException.java b/openstack-client-connectors/http-connector/src/main/java/com/woorea/openstack/connector/HttpClientException.java
index 4c51574..ffb7379 100644
--- a/openstack-client-connectors/http-connector/src/main/java/com/woorea/openstack/connector/HttpClientException.java
+++ b/openstack-client-connectors/http-connector/src/main/java/com/woorea/openstack/connector/HttpClientException.java
@@ -45,17 +45,17 @@ package com.woorea.openstack.connector;
*/
public class HttpClientException extends RuntimeException {
- private static final long serialVersionUID = 1L;
+ private static final long serialVersionUID = 1L;
- public HttpClientException (String s) {
- super (s);
- }
-
- public HttpClientException (Exception e) {
- super ("Caught nested exception in HttpClient", e);
- }
-
- public HttpClientException (String s, Exception e) {
- super (s, e);
- }
+ public HttpClientException (String s) {
+ super (s);
+ }
+
+ public HttpClientException (Exception e) {
+ super ("Caught nested exception in HttpClient", e);
+ }
+
+ public HttpClientException (String s, Exception e) {
+ super (s, e);
+ }
}
diff --git a/openstack-client-connectors/http-connector/src/main/java/com/woorea/openstack/connector/HttpClientRedirectStrategy.java b/openstack-client-connectors/http-connector/src/main/java/com/woorea/openstack/connector/HttpClientRedirectStrategy.java
index 2ba17fb..f9aedf5 100644
--- a/openstack-client-connectors/http-connector/src/main/java/com/woorea/openstack/connector/HttpClientRedirectStrategy.java
+++ b/openstack-client-connectors/http-connector/src/main/java/com/woorea/openstack/connector/HttpClientRedirectStrategy.java
@@ -102,7 +102,7 @@ public class HttpClientRedirectStrategy extends DefaultRedirectStrategy {
final HttpRequest request,
final HttpResponse response,
final HttpContext context) throws ProtocolException {
-
+
final URI uri = getLocationURI(request, response, context);
final String method = request.getRequestLine().getMethod();
if (method.equalsIgnoreCase(HttpHead.METHOD_NAME)) {
@@ -113,13 +113,13 @@ public class HttpClientRedirectStrategy extends DefaultRedirectStrategy {
final int status = response.getStatusLine().getStatusCode();
- HttpUriRequest newRequest;
- if (status == HttpStatus.SC_TEMPORARY_REDIRECT || status == HttpStatus.SC_MOVED_TEMPORARILY) {
+ HttpUriRequest newRequest;
+ if (status == HttpStatus.SC_TEMPORARY_REDIRECT || status == HttpStatus.SC_MOVED_TEMPORARILY) {
newRequest = RequestBuilder.copy(request).setUri(uri).build();
} else {
newRequest = new HttpGet(uri);
}
- return newRequest;
+ return newRequest;
}
}
}
diff --git a/openstack-client-connectors/http-connector/src/main/java/com/woorea/openstack/connector/HttpClientResponse.java b/openstack-client-connectors/http-connector/src/main/java/com/woorea/openstack/connector/HttpClientResponse.java
index 74df0e6..108861a 100644
--- a/openstack-client-connectors/http-connector/src/main/java/com/woorea/openstack/connector/HttpClientResponse.java
+++ b/openstack-client-connectors/http-connector/src/main/java/com/woorea/openstack/connector/HttpClientResponse.java
@@ -55,8 +55,8 @@ import java.util.Map;
public class HttpClientResponse implements OpenStackResponse {
- private static Logger LOGGER = Logger.getLogger(HttpClientConnector.class);
-
+ private static Logger LOGGER = Logger.getLogger(HttpClientConnector.class);
+
private HttpResponse response = null;
private String entityBody = null;
@@ -66,34 +66,34 @@ public class HttpClientResponse implements OpenStackResponse {
// Read the body so InputStream can be closed
if (response.getEntity() == null) {
- // No body
- LOGGER.debug ("No Response Body");
- return;
+ // No body
+ LOGGER.debug ("No Response Body");
+ return;
}
- ByteArrayOutputStream responseBody = new ByteArrayOutputStream();
- try {
- response.getEntity().writeTo(responseBody);
- } catch (IOException e) {
- throw new HttpClientException ("Error Reading Response Body", e);
- }
- entityBody = responseBody.toString();
- LOGGER.debug (entityBody);
+ ByteArrayOutputStream responseBody = new ByteArrayOutputStream();
+ try {
+ response.getEntity().writeTo(responseBody);
+ } catch (IOException e) {
+ throw new HttpClientException ("Error Reading Response Body", e);
+ }
+ entityBody = responseBody.toString();
+ LOGGER.debug (entityBody);
}
@Override
- public <T> T getEntity (Class<T> returnType) {
- // Get appropriate mapper, based on existence of a root element
- ObjectMapper mapper = HttpClientConnector.getObjectMapper (returnType);
-
- T resp = null;
- try {
- resp = mapper.readValue(entityBody, returnType);
- } catch (Exception e) {
- throw new HttpClientException ("Caught exception in getEntity", e);
- }
- return resp;
+ public <T> T getEntity (Class<T> returnType) {
+ // Get appropriate mapper, based on existence of a root element
+ ObjectMapper mapper = HttpClientConnector.getObjectMapper (returnType);
+
+ T resp = null;
+ try {
+ resp = mapper.readValue(entityBody, returnType);
+ } catch (Exception e) {
+ throw new HttpClientException ("Caught exception in getEntity", e);
+ }
+ return resp;
}
@Override
@@ -103,7 +103,7 @@ public class HttpClientResponse implements OpenStackResponse {
@Override
public InputStream getInputStream() {
- return new ByteArrayInputStream (entityBody.getBytes());
+ return new ByteArrayInputStream (entityBody.getBytes());
}
@Override