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.java89
-rw-r--r--openstack-client-connectors/http-connector/src/main/java/com/woorea/openstack/connector/HttpClientException.java78
-rw-r--r--openstack-client-connectors/http-connector/src/main/java/com/woorea/openstack/connector/HttpClientRedirectStrategy.java195
-rw-r--r--openstack-client-connectors/http-connector/src/main/java/com/woorea/openstack/connector/HttpClientResponse.java21
4 files changed, 177 insertions, 206 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 27bbe87..418268b 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
@@ -1,27 +1,20 @@
-/*
- * ONAP-SO
- * ============LICENSE_START==========================================
- * ===================================================================
- * Copyright (c) 2017 AT&T Intellectual Property. All rights reserved.
- * ===================================================================
+/*-
+ * ============LICENSE_START=======================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
- * ============LICENSE_END============================================
- *
- * ECOMP and OpenECOMP are trademarks
- * and service marks of AT&T Intellectual Property.
- *
+ * ============LICENSE_END=========================================================
*/
+
package com.woorea.openstack.connector;
import java.io.IOException;
@@ -30,7 +23,6 @@ import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.util.List;
import java.util.Map.Entry;
-
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpResponseException;
@@ -47,7 +39,6 @@ import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonRootName;
import com.fasterxml.jackson.core.JsonProcessingException;
@@ -85,7 +76,7 @@ public class HttpClientConnector implements OpenStackClientConnector {
WRAPPED_MAPPER.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
}
- protected static <T> ObjectMapper getObjectMapper (Class<T> type) {
+ protected static <T> ObjectMapper getObjectMapper(Class<T> type) {
return type.getAnnotation(JsonRootName.class) == null ? DEFAULT_MAPPER : WRAPPED_MAPPER;
}
@@ -105,7 +96,7 @@ public class HttpClientConnector implements OpenStackClientConnector {
uri = uriBuilder.build();
} catch (URISyntaxException e) {
- throw new HttpClientException (e);
+ throw new HttpClientException(e);
}
HttpEntity entity = null;
@@ -114,17 +105,18 @@ public class HttpClientConnector implements OpenStackClientConnector {
try {
// Get appropriate mapper, based on existence of a root element in Entity class
- ObjectMapper mapper = getObjectMapper (request.entity().getEntity().getClass());
+ ObjectMapper mapper = getObjectMapper(request.entity().getEntity().getClass());
- String entityJson = mapper.writeValueAsString (request.entity().getEntity());
+ 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);
+ throw new HttpClientException("Json processing error on request entity", e);
} catch (IOException e) {
- throw new HttpClientException ("Json IO error on request entity", e);
+ throw new HttpClientException("Json IO error on request entity", e);
}
}
@@ -141,52 +133,46 @@ 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
+ // 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.
+ // so calling functions can distinguish. Only RuntimeExceptions are allowed.
try {
httpResponse = httpClient.execute(httpRequest);
- logger.debug ("Response status: " + httpResponse.getStatusLine().getStatusCode());
+ logger.debug("Response status: " + httpResponse.getStatusLine().getStatusCode());
- httpClientResponse = new HttpClientResponse (httpResponse);
+ 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)
- {
+ 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
+ } catch (HttpResponseException e) {
+ // What exactly does this mean? It does not appear to get thrown for
// non-2XX responses as documented.
- logger.error ("HttpResponseException: " +e.getMessage());
+ logger.error("HttpResponseException: " + e.getMessage());
throw new OpenStackResponseException(e.getMessage(), e.getStatusCode());
- }
- catch (UnknownHostException e) {
- logger.error ("Unknown Host: " +e.getMessage());
+ } catch (UnknownHostException e) {
+ logger.error("Unknown Host: " + e.getMessage());
throw new OpenStackConnectException("Unknown Host: " + e.getMessage());
- }
- catch (IOException e) {
- logger.error ("IOException: " +e.getMessage());
+ } catch (IOException e) {
+ logger.error("IOException: " + e.getMessage());
// Catch all other IOExceptions and throw as OpenStackConnectException
throw new OpenStackConnectException(e.getMessage());
- }
- catch (Exception e) {
+ } 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 {
- // Have the body. Close the stream
+ } finally {
+ // Have the body. Close the stream
if (httpResponse != null)
try {
httpResponse.close();
@@ -197,8 +183,7 @@ public class HttpClientConnector implements OpenStackClientConnector {
// Get here on an error response (4XX-5XX)
throw new OpenStackResponseException(httpResponse.getStatusLine().getReasonPhrase(),
- httpResponse.getStatusLine().getStatusCode(),
- httpClientResponse);
+ httpResponse.getStatusLine().getStatusCode(), httpClientResponse);
}
private <T> HttpUriRequest getHttpUriRequest(OpenStackRequest<T> request, URI uri, HttpEntity entity) {
@@ -206,7 +191,7 @@ public class HttpClientConnector implements OpenStackClientConnector {
switch (request.method()) {
case POST:
HttpPost post = new HttpPost(uri);
- post.setEntity (entity);
+ post.setEntity(entity);
httpRequest = post;
break;
@@ -216,7 +201,7 @@ public class HttpClientConnector implements OpenStackClientConnector {
case PUT:
HttpPut put = new HttpPut(uri);
- put.setEntity (entity);
+ put.setEntity(entity);
httpRequest = put;
break;
@@ -231,11 +216,11 @@ public class HttpClientConnector implements OpenStackClientConnector {
}
private <T> void setUriBuilderParams(OpenStackRequest<T> request, URIBuilder uriBuilder) {
- for(Entry<String, List<Object>> entry : request.queryParams().entrySet()) {
+ 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/HttpClientException.java b/openstack-client-connectors/http-connector/src/main/java/com/woorea/openstack/connector/HttpClientException.java
index ecdc1d7..b2e08b4 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
@@ -1,39 +1,39 @@
-/*-
- * ============LICENSE_START=======================================================
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * ============LICENSE_END=========================================================
- */
-
-
-package com.woorea.openstack.connector;
-
-/*
- * Declare a RuntimeException since the Interface does not declare any
- * throwables. Any caught exception will be wrapped in HttpClientException
- */
-public class HttpClientException extends RuntimeException {
-
- 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);
- }
-} \ No newline at end of file
+/*-
+ * ============LICENSE_START=======================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+
+package com.woorea.openstack.connector;
+
+/*
+ * Declare a RuntimeException since the Interface does not declare any throwables. Any caught exception will be wrapped
+ * in HttpClientException
+ */
+public class HttpClientException extends RuntimeException {
+
+ 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);
+ }
+}
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 c611e13..4c5946d 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
@@ -1,103 +1,92 @@
-/*-
- * ============LICENSE_START=======================================================
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * ============LICENSE_END=========================================================
- */
-
-
-package com.woorea.openstack.connector;
-
-import java.net.URI;
-
-import org.apache.http.HttpRequest;
-import org.apache.http.HttpResponse;
-import org.apache.http.HttpStatus;
-import org.apache.http.ProtocolException;
-import org.apache.http.annotation.Immutable;
-import org.apache.http.client.methods.HttpGet;
-import org.apache.http.client.methods.HttpHead;
-import org.apache.http.client.methods.HttpDelete;
-import org.apache.http.client.methods.HttpUriRequest;
-import org.apache.http.client.methods.RequestBuilder;
-import org.apache.http.impl.client.DefaultRedirectStrategy;
-import org.apache.http.protocol.HttpContext;
-
-/**
- * Custom {@link org.apache.http.client.RedirectStrategy} implementation
- * that automatically redirects all HEAD, GET and DELETE requests.
- * The {@link org.apache.http.client.DefaultRedirectStrategy} only
- * redirects GET and HEAD automatically, per the HTTP specification
- * (POST and PUT typically have bodies and thus cannot be redirected).
- *
- * A custom strategy is needed for the Openstack API, which can also send
- * 302 on a DELETE (by name) request, expecting the client to follow the
- * redirect to perform the actual deletion.
- */
-@Immutable
-public class HttpClientRedirectStrategy extends DefaultRedirectStrategy {
-
- /**
- * Redirectable methods.
- */
- private static final String[] REDIRECT_METHODS = new String[] {
- HttpGet.METHOD_NAME,
- HttpDelete.METHOD_NAME,
- HttpHead.METHOD_NAME
- };
-
- /**
- * Determine if the request should be redirected.
- * This may not actually be needed, since the REDIRECT_METHODS
- * array has been updated with the DELETE.
- */
- @Override
- protected boolean isRedirectable(final String method) {
- for (final String m: REDIRECT_METHODS) {
- if (m.equalsIgnoreCase(method)) {
- return true;
- }
- }
- return false;
- }
-
- /**
- * Override the default redirect handling method. As implemented
- * in HttpClient, it does not preserve the method on 301 or 302
- * responses, always redirecting to a GET.
- */
- @Override
- public HttpUriRequest getRedirect(
- 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)) {
- return new HttpHead(uri);
- } else if (method.equalsIgnoreCase(HttpGet.METHOD_NAME)) {
- return new HttpGet(uri);
- } else {
-
- final int status = response.getStatusLine().getStatusCode();
-
- 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;
- }
- }
-} \ No newline at end of file
+/*-
+ * ============LICENSE_START=======================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+
+package com.woorea.openstack.connector;
+
+import java.net.URI;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpStatus;
+import org.apache.http.ProtocolException;
+import org.apache.http.annotation.Immutable;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpHead;
+import org.apache.http.client.methods.HttpDelete;
+import org.apache.http.client.methods.HttpUriRequest;
+import org.apache.http.client.methods.RequestBuilder;
+import org.apache.http.impl.client.DefaultRedirectStrategy;
+import org.apache.http.protocol.HttpContext;
+
+/**
+ * Custom {@link org.apache.http.client.RedirectStrategy} implementation that automatically redirects all HEAD, GET and
+ * DELETE requests. The {@link org.apache.http.client.DefaultRedirectStrategy} only redirects GET and HEAD
+ * automatically, per the HTTP specification (POST and PUT typically have bodies and thus cannot be redirected).
+ *
+ * A custom strategy is needed for the Openstack API, which can also send 302 on a DELETE (by name) request, expecting
+ * the client to follow the redirect to perform the actual deletion.
+ */
+@Immutable
+public class HttpClientRedirectStrategy extends DefaultRedirectStrategy {
+
+ /**
+ * Redirectable methods.
+ */
+ private static final String[] REDIRECT_METHODS =
+ new String[] {HttpGet.METHOD_NAME, HttpDelete.METHOD_NAME, HttpHead.METHOD_NAME};
+
+ /**
+ * Determine if the request should be redirected. This may not actually be needed, since the REDIRECT_METHODS array
+ * has been updated with the DELETE.
+ */
+ @Override
+ protected boolean isRedirectable(final String method) {
+ for (final String m : REDIRECT_METHODS) {
+ if (m.equalsIgnoreCase(method)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Override the default redirect handling method. As implemented in HttpClient, it does not preserve the method on
+ * 301 or 302 responses, always redirecting to a GET.
+ */
+ @Override
+ public HttpUriRequest getRedirect(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)) {
+ return new HttpHead(uri);
+ } else if (method.equalsIgnoreCase(HttpGet.METHOD_NAME)) {
+ return new HttpGet(uri);
+ } else {
+
+ final int status = response.getStatusLine().getStatusCode();
+
+ 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;
+ }
+ }
+}
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 f1c8c78..24541cd 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
@@ -21,10 +21,8 @@ import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-
import com.fasterxml.jackson.databind.ObjectMapper;
import com.woorea.openstack.base.client.OpenStackResponse;
-
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@@ -39,14 +37,13 @@ public class HttpClientResponse implements OpenStackResponse {
private HttpResponse response = null;
private String entityBody = null;
- public HttpClientResponse(HttpResponse response)
- {
+ public HttpClientResponse(HttpResponse response) {
this.response = response;
// 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;
}
@@ -54,23 +51,23 @@ public class HttpClientResponse implements OpenStackResponse {
try {
response.getEntity().writeTo(responseBody);
} catch (IOException e) {
- throw new HttpClientException ("Error Reading Response Body", e);
+ throw new HttpClientException("Error Reading Response Body", e);
}
entityBody = responseBody.toString();
- logger.debug (entityBody);
+ logger.debug(entityBody);
}
@Override
- public <T> T getEntity (Class<T> returnType) {
+ public <T> T getEntity(Class<T> returnType) {
// Get appropriate mapper, based on existence of a root element
- ObjectMapper mapper = HttpClientConnector.getObjectMapper (returnType);
+ 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);
+ throw new HttpClientException("Caught exception in getEntity", e);
}
return resp;
}
@@ -82,7 +79,7 @@ public class HttpClientResponse implements OpenStackResponse {
@Override
public InputStream getInputStream() {
- return new ByteArrayInputStream (entityBody.getBytes());
+ return new ByteArrayInputStream(entityBody.getBytes());
}
@Override
@@ -102,4 +99,4 @@ public class HttpClientResponse implements OpenStackResponse {
return headers;
}
-} \ No newline at end of file
+}