diff options
Diffstat (limited to 'controlloop/common/model-impl/rest/src/main/java/org')
2 files changed, 158 insertions, 90 deletions
diff --git a/controlloop/common/model-impl/rest/src/main/java/org/onap/policy/rest/HttpDeleteWithBody.java b/controlloop/common/model-impl/rest/src/main/java/org/onap/policy/rest/HttpDeleteWithBody.java new file mode 100644 index 000000000..262872262 --- /dev/null +++ b/controlloop/common/model-impl/rest/src/main/java/org/onap/policy/rest/HttpDeleteWithBody.java @@ -0,0 +1,52 @@ +/* + * ============LICENSE_START======================================================= + * rest + * ================================================================================ + * Copyright (C) 2018 Amdocs. All rights reserved. + * ================================================================================ + * 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 org.onap.policy.rest; + +import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; + +import java.net.URI; + + +/** + * Allows for HTTP DELETE requests to contain a body, which the HttpDelete + * class does not support. + */ +public class HttpDeleteWithBody extends HttpEntityEnclosingRequestBase { + public static final String METHOD_NAME = "DELETE"; + + public String getMethod() { + return METHOD_NAME; + } + + public HttpDeleteWithBody(final String uri) { + super(); + setURI(URI.create(uri)); + } + + public HttpDeleteWithBody(final URI uri) { + super(); + setURI(uri); + } + + public HttpDeleteWithBody() { + super(); + } +} diff --git a/controlloop/common/model-impl/rest/src/main/java/org/onap/policy/rest/RESTManager.java b/controlloop/common/model-impl/rest/src/main/java/org/onap/policy/rest/RESTManager.java index 39e0d8a7f..a7e373b5f 100644 --- a/controlloop/common/model-impl/rest/src/main/java/org/onap/policy/rest/RESTManager.java +++ b/controlloop/common/model-impl/rest/src/main/java/org/onap/policy/rest/RESTManager.java @@ -7,9 +7,9 @@ * 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. @@ -20,15 +20,16 @@ package org.onap.policy.rest; -import java.io.IOException; import java.nio.charset.Charset; import java.util.Map; import java.util.Map.Entry; import javax.xml.bind.DatatypeConverter; + import org.apache.http.HttpHeaders; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.conn.ssl.NoopHostnameVerifier; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; @@ -42,126 +43,141 @@ public class RESTManager { private static final Logger logger = LoggerFactory.getLogger(RESTManager.class); public class Pair<A, B> { - public final A a; - public final B b; + public final A first; + public final B second; - public Pair(A a, B b) { - this.a = a; - this.b = b; + public Pair(A first, B second) { + this.first = first; + this.second = second; } } /** * Perform REST Post. - * - * @param url the url - * @param username the user name - * @param password the password - * @param headers any headers + * + * @param url the url + * @param username the user name + * @param password the password + * @param headers any headers * @param contentType what the content type is - * @param body body to send + * @param body body to send * @return the response status code and the body */ public Pair<Integer, String> post(String url, String username, String password, - Map<String, String> headers, String contentType, String body) { - - String authHeader = makeAuthHeader(username, password); - - logger.debug("HTTP REQUEST: {} -> {} {} -> {}", url, username, - ((password != null) ? password.length() : "-"), contentType); - if (headers != null) { - logger.debug("Headers: "); - headers.forEach((name, value) -> logger.debug("{} -> {}", name, value)); + Map<String, String> headers, String contentType, String body) { + HttpPost post = new HttpPost(url); + addHeaders(post, username, password, headers); + post.addHeader("Content-Type", contentType); + try { + StringEntity input = new StringEntity(body); + input.setContentType(contentType); + post.setEntity(input); + } catch (Exception e) { + logger.error("post threw: ", e); + return null; } - logger.debug(body); + return sendRequest(post); + } - try (CloseableHttpClient client = - HttpClientBuilder - .create() - .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE) - .build()) { - - HttpPost post = new HttpPost(url); - if (headers != null) { - for (Entry<String, String> entry : headers.entrySet()) { - post.addHeader(entry.getKey(), headers.get(entry.getKey())); - } - } - post.addHeader("Content-Type", contentType); - if (authHeader != null) { - post.setHeader(HttpHeaders.AUTHORIZATION, authHeader); + /** + * Do a REST get. + * + * @param url URL + * @param username user name + * @param password password + * @param headers any headers to add + * @return a Pair for the response status and the body + */ + public Pair<Integer, String> get(String url, String username, String password, + Map<String, String> headers) { + HttpGet get = new HttpGet(url); + addHeaders(get, username, password, headers); + return sendRequest(get); + } + + /** + * Perform REST Delete. + * + * @param url the url + * @param username the user name + * @param password the password + * @param headers any headers + * @param contentType what the content type is + * @param body body (optional) to send + * @return the response status code and the body + */ + public Pair<Integer, String> delete(String url, String username, String password, Map<String, String> headers, + String contentType, String body) { + HttpDeleteWithBody delete = new HttpDeleteWithBody(url); + addHeaders(delete, username, password, headers); + delete.addHeader("Content-Type", contentType); + if (body != null && !body.isEmpty()) { + try { + StringEntity input = new StringEntity(body); + input.setContentType(contentType); + delete.setEntity(input); + } catch (Exception e) { + logger.error("delete threw: ", e); + return null; } + } + return sendRequest(delete); + } - StringEntity input = new StringEntity(body); - input.setContentType(contentType); - post.setEntity(input); + /** + * Send REST request. + * + * @param request http request to send + * @return the response status code and the body + */ + private Pair<Integer, String> sendRequest(HttpRequestBase request) { + if (logger.isDebugEnabled()) { + logger.debug("***** sendRequest to url {}:", request.getURI()); + } - HttpResponse response = client.execute(post); + try (CloseableHttpClient client = + HttpClientBuilder + .create() + .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE) + .build()) { + HttpResponse response = client.execute(request); if (response != null) { String returnBody = EntityUtils.toString(response.getEntity(), "UTF-8"); - logger.debug("HTTP POST Response Status Code: {}", + logger.debug("HTTP Response Status Code: {}", response.getStatusLine().getStatusCode()); - logger.debug("HTTP POST Response Body:"); + logger.debug("HTTP Response Body:"); logger.debug(returnBody); return new Pair<>(response.getStatusLine().getStatusCode(), returnBody); - } - else { - logger.error("Response from {} is null", url); + } else { + logger.error("Response from {} is null", request.getURI()); return null; } - } - catch (Exception e) { - logger.error("Failed to POST to {}", url, e); + } catch (Exception e) { + logger.error("Request failed to {}", request.getURI(), e); return null; } } /** - * Do a REST get. - * - * @param url URL - * @param username user name - * @param password password - * @param headers any headers to add - * @return a Pair for the response status and the body + * Add header to the request. + * + * @param request http request to send + * @param username the user name + * @param password the password + * @param headers any headers */ - public Pair<Integer, String> get(String url, String username, String password, - Map<String, String> headers) { - + private void addHeaders(HttpRequestBase request, String username, String password, Map<String, + String> headers) { String authHeader = makeAuthHeader(username, password); - - try (CloseableHttpClient client = - HttpClientBuilder - .create() - .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE) - .build()) { - - HttpGet get = new HttpGet(url); - if (headers != null) { - for (Entry<String, String> entry : headers.entrySet()) { - get.addHeader(entry.getKey(), headers.get(entry.getKey())); - } - } - if (authHeader != null) { - get.setHeader(HttpHeaders.AUTHORIZATION, authHeader); + if (headers != null) { + for (Entry<String, String> entry : headers.entrySet()) { + request.addHeader(entry.getKey(), headers.get(entry.getKey())); } - - HttpResponse response = client.execute(get); - - String returnBody = EntityUtils.toString(response.getEntity(), "UTF-8"); - - logger.debug("HTTP GET Response Status Code: {}", - response.getStatusLine().getStatusCode()); - logger.debug("HTTP GET Response Body:"); - logger.debug(returnBody); - - return new Pair<>(response.getStatusLine().getStatusCode(), returnBody); } - catch (IOException e) { - logger.error("Failed to GET to {}", url, e); - return null; + if (authHeader != null) { + request.setHeader(HttpHeaders.AUTHORIZATION, authHeader); } } |