diff options
Diffstat (limited to 'openstack-client-connectors/http-connector/src')
2 files changed, 50 insertions, 41 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 9b2f2e2..d43e6c9 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 @@ -29,7 +29,6 @@ import java.net.URI; import java.net.URISyntaxException; import java.net.UnknownHostException; import java.util.List; -import java.util.Map; import java.util.Map.Entry; import org.apache.http.HttpEntity; @@ -66,7 +65,7 @@ public class HttpClientConnector implements OpenStackClientConnector { public static final ObjectMapper DEFAULT_MAPPER; public static final ObjectMapper WRAPPED_MAPPER; - private static Logger LOGGER = LoggerFactory.getLogger(HttpClientConnector.class); + private static Logger logger = LoggerFactory.getLogger(HttpClientConnector.class); static { DEFAULT_MAPPER = new ObjectMapper(); @@ -102,11 +101,7 @@ public class HttpClientConnector implements OpenStackClientConnector { 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)); - } - } + setUriBuilderParams(request, uriBuilder); uri = uriBuilder.build(); } catch (URISyntaxException e) { @@ -124,7 +119,7 @@ public class HttpClientConnector implements OpenStackClientConnector { String entityJson = mapper.writeValueAsString (request.entity().getEntity()); entity = new StringEntity(entityJson, ContentType.create(request.entity().getContentType())); - LOGGER.debug ("Request JSON Body: " + entityJson.replaceAll("\"password\":\"[^\"]*\"", "\"password\":\"***\"")); + logger.debug ("Request JSON Body: " + entityJson.replaceAll("\"password\":\"[^\"]*\"", "\"password\":\"***\"")); } catch (JsonProcessingException e) { throw new HttpClientException ("Json processing error on request entity", e); @@ -136,30 +131,7 @@ public class HttpClientConnector implements OpenStackClientConnector { // 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()); - } + httpRequest = getHttpUriRequest(request, uri, entity); for (Entry<String, List<Object>> h : request.headers().entrySet()) { StringBuilder sb = new StringBuilder(); @@ -169,7 +141,7 @@ public class HttpClientConnector implements OpenStackClientConnector { httpRequest.addHeader(h.getKey(), sb.toString()); } - LOGGER.debug ("Sending HTTP request: " + httpRequest.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 @@ -182,7 +154,7 @@ public class HttpClientConnector implements OpenStackClientConnector { try { httpResponse = httpClient.execute(httpRequest); - LOGGER.debug ("Response status: " + httpResponse.getStatusLine().getStatusCode()); + logger.debug ("Response status: " + httpResponse.getStatusLine().getStatusCode()); httpClientResponse = new HttpClientResponse (httpResponse); @@ -207,7 +179,7 @@ public class HttpClientConnector implements OpenStackClientConnector { } catch (Exception e) { // Catchall for anything else, must throw as a RuntimeException - LOGGER.error ("Unexpected client exception: " +e.getMessage()); + logger.error ("Unexpected client exception: " +e.getMessage()); throw new RuntimeException("Unexpected client exception", e); } finally { @@ -216,7 +188,7 @@ public class HttpClientConnector implements OpenStackClientConnector { try { httpResponse.close(); } catch (IOException e) { - LOGGER.warn("Unable to close HTTP Response: " + e); + logger.warn("Unable to close HTTP Response: " + e); } } @@ -226,4 +198,41 @@ public class HttpClientConnector implements OpenStackClientConnector { httpClientResponse); } + private <T> HttpUriRequest getHttpUriRequest(OpenStackRequest<T> request, URI uri, HttpEntity entity) { + 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()); + } + return httpRequest; + } + + private <T> void setUriBuilderParams(OpenStackRequest<T> request, URIBuilder uriBuilder) { + for(Entry<String, List<Object>> entry : request.queryParams().entrySet()) { + for (Object o : entry.getValue()) { + uriBuilder.setParameter(entry.getKey(), String.valueOf(o)); + } + } + } + }
\ No newline at end of file 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 468e859..f1c8c78 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 @@ -34,7 +34,7 @@ import java.util.Map; public class HttpClientResponse implements OpenStackResponse { - private static Logger LOGGER = LoggerFactory.getLogger(HttpClientConnector.class); + private static Logger logger = LoggerFactory.getLogger(HttpClientConnector.class); private HttpResponse response = null; private String entityBody = null; @@ -46,7 +46,7 @@ public class HttpClientResponse implements OpenStackResponse { // Read the body so InputStream can be closed if (response.getEntity() == null) { // No body - LOGGER.debug ("No Response Body"); + logger.debug ("No Response Body"); return; } @@ -57,7 +57,7 @@ public class HttpClientResponse implements OpenStackResponse { throw new HttpClientException ("Error Reading Response Body", e); } entityBody = responseBody.toString(); - LOGGER.debug (entityBody); + logger.debug (entityBody); } @@ -92,9 +92,9 @@ public class HttpClientResponse implements OpenStackResponse { @Override public Map<String, String> headers() { - Map<String, String> headers = new HashMap<String, String>(); + Map<String, String> headers = new HashMap<>(); - Header responseHeaders[] = response.getAllHeaders(); + Header[] responseHeaders = response.getAllHeaders(); for (Header h : responseHeaders) { headers.put(h.getName(), h.getValue()); } |