public class HttpClientConnector implements OpenStackClientConnector {
- public static ObjectMapper DEFAULT_MAPPER;
- public static ObjectMapper WRAPPED_MAPPER;
-
+ public static final ObjectMapper DEFAULT_MAPPER;
+ public static final ObjectMapper WRAPPED_MAPPER;
+
private static Logger LOGGER = LoggerFactory.getLogger(HttpClientConnector.class);
static {
WRAPPED_MAPPER.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
WRAPPED_MAPPER.disable(DeserializationFeature.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();
+
+ CloseableHttpClient httpClient = null;
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());
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());
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());
+ 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()) {
}
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)
+ status == HttpStatus.SC_NO_CONTENT || status == HttpStatus.SC_ACCEPTED)
{
return httpClientResponse;
}
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);
+ httpResponse.getStatusLine().getStatusCode(),
+ httpClientResponse);
}
-}
+}
\ No newline at end of file
* ============LICENSE_END=========================================================\r
*/\r
\r
-/*\r
- * ============LICENSE_START==========================================\r
- * ===================================================================\r
- * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
- * ===================================================================\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- *\r
- * http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- * ============LICENSE_END============================================\r
- *\r
- * ECOMP and OpenECOMP are trademarks\r
- * and service marks of AT&T Intellectual Property.\r
- *\r
- */\r
\r
package com.woorea.openstack.connector;\r
\r
public HttpClientException (String s) {\r
super (s);\r
}\r
- \r
+\r
public HttpClientException (Exception e) {\r
super ("Caught nested exception in HttpClient", e);\r
}\r
- \r
+\r
public HttpClientException (String s, Exception e) {\r
super (s, e);\r
}\r
-}\r
+}
\ No newline at end of file