diff options
author | Lusheng Ji <lji@research.att.com> | 2018-04-09 16:27:53 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2018-04-09 16:27:53 +0000 |
commit | da7aa97bebc796e898b9225d45a256f946d39f82 (patch) | |
tree | 886350d5b742cfbf5773b70141dd401bc026faee /prh-aai-client/src/main | |
parent | 6bb4aa2c5ddc47ed2c8ffa03844f10b3482d1fcc (diff) | |
parent | 9725138e6882182c50cbac79ace5d66774a17619 (diff) |
Merge "Http put added"
Diffstat (limited to 'prh-aai-client/src/main')
8 files changed, 150 insertions, 52 deletions
diff --git a/prh-aai-client/src/main/java/services/config/AAIHttpClientConfiguration.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/config/AAIHttpClientConfiguration.java index d5a9281c..9f93f896 100644 --- a/prh-aai-client/src/main/java/services/config/AAIHttpClientConfiguration.java +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/config/AAIHttpClientConfiguration.java @@ -17,14 +17,15 @@ * limitations under the License. * ============LICENSE_END========================================================= */ +package org.onap.dcaegen2.services.config; -package services.config; - -import java.io.Serializable; import org.immutables.value.Value; import org.springframework.stereotype.Component; +import java.io.Serializable; + + @Component @Value.Immutable(prehash = true) @Value.Style(stagedBuilder = true) diff --git a/prh-aai-client/src/main/java/services/service/AAIExtendedHttpClient.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClient.java index 41cf87e5..24149e6d 100644 --- a/prh-aai-client/src/main/java/services/service/AAIExtendedHttpClient.java +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClient.java @@ -17,11 +17,14 @@ * limitations under the License. * ============LICENSE_END========================================================= */ +package org.onap.dcaegen2.services.service; -package services.service; +import org.onap.dcaegen2.services.utils.HttpRequestDetails; -import java.util.Map; +import java.util.Optional; + +@FunctionalInterface public interface AAIExtendedHttpClient { - String getExtendedDetails(String aaiAPIPath, Map<String, String> queryParams, Map<String, String> headers); + Optional<String> getHttpResponse(HttpRequestDetails httpRequestDetails); } diff --git a/prh-aai-client/src/main/java/services/service/AAIExtendedHttpClientImpl.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClientImpl.java index 7a5a9f8b..133a537e 100644 --- a/prh-aai-client/src/main/java/services/service/AAIExtendedHttpClientImpl.java +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClientImpl.java @@ -17,30 +17,37 @@ * limitations under the License. * ============LICENSE_END========================================================= */ -package services.service; +package org.onap.dcaegen2.services.service; -import java.util.Optional; import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpEntity; import org.apache.http.client.ResponseHandler; import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPut; +import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.client.utils.URIBuilder; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.util.EntityUtils; +import org.onap.dcaegen2.services.config.AAIHttpClientConfiguration; +import org.onap.dcaegen2.services.utils.HttpRequestDetails; +import org.onap.dcaegen2.services.utils.HttpUtils; +import org.onap.dcaegen2.services.utils.RequestVerbs; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import services.config.AAIHttpClientConfiguration; -import services.utils.HttpUtils; - import javax.annotation.Nonnull; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.util.Iterator; import java.util.Map; +import java.util.Optional; public class AAIExtendedHttpClientImpl implements AAIExtendedHttpClient { + Logger logger = LoggerFactory.getLogger(AAIExtendedHttpClientImpl.class); + private final CloseableHttpClient closeableHttpClient; private final String aaiHost; private final String aaiProtocol; @@ -56,53 +63,52 @@ public class AAIExtendedHttpClientImpl implements AAIExtendedHttpClient { } @Override - public String getExtendedDetails(final String aaiAPIPath, final Map<String, String> queryParams, - final Map<String, String> headers) { - final URI extendedURI = - createAAIExtendedURI(aaiProtocol, aaiHost, aaiHostPortNumber, aaiAPIPath, queryParams); + public Optional<String> getHttpResponse(HttpRequestDetails httpRequestDetails) { - if (extendedURI == null) { - return null; - } + Optional<String> extendedDetails = Optional.empty(); - final HttpGet getRequest = new HttpGet(extendedURI); + final URI extendedURI = createAAIExtendedURI(httpRequestDetails.aaiAPIPath(), + httpRequestDetails.queryParameters()); + final HttpRequestBase request = createHttpRequest(extendedURI, httpRequestDetails); - for (Map.Entry<String, String> headersEntry : headers.entrySet()) { - getRequest.addHeader(headersEntry.getKey(), headersEntry.getValue()); + if (request == null) { + return Optional.empty(); } - Optional<String> extendedDetails = Optional.empty(); + for (Map.Entry<String, String> headersEntry : httpRequestDetails.headers().entrySet()) { + request.addHeader(headersEntry.getKey(), headersEntry.getValue()); + } try { - extendedDetails = closeableHttpClient.execute(getRequest, aaiResponseHandler()); - } catch (IOException ex) { - //ToDo loging + extendedDetails = closeableHttpClient.execute(request, aaiResponseHandler()); + } catch (IOException e) { + logger.error("Exception while executing HTTP request: {}", e); } - // return response if (extendedDetails.isPresent()) { - return extendedDetails.get(); + return extendedDetails; } else { - return null; + return Optional.empty(); } } - private URI createAAIExtendedURI(final String protocol, final String hostName, final Integer portNumber, - final String path, Map<String, String> queryParams) { - final URIBuilder uriBuilder = new URIBuilder().setScheme(protocol).setHost(hostName).setPort(portNumber) - .setPath(path); + private URI createAAIExtendedURI(final String path, Map<String, String> queryParams) { + URI extendedURI = null; + final URIBuilder uriBuilder = new URIBuilder().setScheme(this.aaiProtocol).setHost(this.aaiHost) + .setPort(this.aaiHostPortNumber) + .setPath(path); final String customQuery = createCustomQuery(queryParams); + if (StringUtils.isNoneBlank(customQuery)) { uriBuilder.setCustomQuery(customQuery); } - URI extendedURI = null; - try { + logger.info("Building extended URI"); extendedURI = uriBuilder.build(); } catch (URISyntaxException e) { - // ToDo loging + logger.error("Exception while building extended URI: {}", e); } return extendedURI; @@ -111,15 +117,15 @@ public class AAIExtendedHttpClientImpl implements AAIExtendedHttpClient { private String createCustomQuery(@Nonnull final Map<String, String> queryParams) { final StringBuilder queryStringBuilder = new StringBuilder(""); final Iterator<Map.Entry<String, String>> queryParamIterator = queryParams.entrySet().iterator(); + while (queryParamIterator.hasNext()) { final Map.Entry<String, String> queryParamsEntry = queryParamIterator.next(); - queryStringBuilder.append(queryParamsEntry.getKey()); - queryStringBuilder.append("="); - queryStringBuilder.append(queryParamsEntry.getValue()); + queryStringBuilder.append(queryParamsEntry.getKey()).append("=").append(queryParamsEntry.getValue()); if (queryParamIterator.hasNext()) { queryStringBuilder.append("&"); } } + return queryStringBuilder.toString(); } @@ -128,15 +134,29 @@ public class AAIExtendedHttpClientImpl implements AAIExtendedHttpClient { final int responseCode = httpResponse.getStatusLine().getStatusCode(); final HttpEntity responseEntity = httpResponse.getEntity(); - if (HttpUtils.isSuccessfulResponseCode(responseCode) && null != responseEntity) { + if (HttpUtils.isSuccessfulResponseCode(responseCode) && responseEntity != null) { + logger.info("HTTP response successful."); final String aaiResponse = EntityUtils.toString(responseEntity); return Optional.of(aaiResponse); } else { String aaiResponse = responseEntity != null ? EntityUtils.toString(responseEntity) : ""; - //ToDo loging + logger.error("HTTP response not successful : {}", aaiResponse); return Optional.empty(); } }; } + private HttpRequestBase createHttpRequest(URI extendedURI, HttpRequestDetails httpRequestDetails) { + if (isExtendedURINotNull(extendedURI) && (httpRequestDetails.requestVerb().equals(RequestVerbs.GET))) { + return new HttpGet(extendedURI); + } else if (isExtendedURINotNull(extendedURI) && (httpRequestDetails.requestVerb().equals(RequestVerbs.PUT))) { + return new HttpPut(extendedURI); + } else { + return null; + } + } + + private Boolean isExtendedURINotNull(URI extendedURI) { + return extendedURI != null ? true : false; + } } diff --git a/prh-aai-client/src/main/java/services/service/AAIHttpClient.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIHttpClient.java index c1054d7b..c60027c2 100644 --- a/prh-aai-client/src/main/java/services/service/AAIHttpClient.java +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIHttpClient.java @@ -18,10 +18,11 @@ * ============LICENSE_END========================================================= */ -package services.service; +package org.onap.dcaegen2.services.service; import org.apache.http.impl.client.CloseableHttpClient; +@FunctionalInterface public interface AAIHttpClient { CloseableHttpClient getAAIHttpClient(); } diff --git a/prh-aai-client/src/main/java/services/service/AAIHttpClientImpl.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIHttpClientImpl.java index 39c91ddc..90b551db 100644 --- a/prh-aai-client/src/main/java/services/service/AAIHttpClientImpl.java +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIHttpClientImpl.java @@ -18,7 +18,7 @@ * ============LICENSE_END========================================================= */ -package services.service; +package org.onap.dcaegen2.services.service; import org.apache.http.auth.AuthScope; import org.apache.http.auth.Credentials; @@ -31,11 +31,10 @@ import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClients; import org.apache.http.ssl.SSLContextBuilder; import org.apache.http.ssl.TrustStrategy; +import org.onap.dcaegen2.services.config.AAIHttpClientConfiguration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import services.config.AAIHttpClientConfiguration; - import java.security.KeyManagementException; import java.security.KeyStoreException; @@ -62,12 +61,13 @@ public class AAIHttpClientImpl implements AAIHttpClient { if (aaiIgnoreSSLCertificateErrors) { try { - SSLContextBuilder sslContextBuilder = new SSLContextBuilder(); - sslContextBuilder.loadTrustMaterial(null, acceptingTrustStrategy); - httpClientBuilder.setSSLContext(sslContextBuilder.build()); + logger.info("Setting SSL Context for AAI HTTP Client"); + httpClientBuilder.setSSLContext(new SSLContextBuilder() + .loadTrustMaterial(null, acceptingTrustStrategy) + .build()); } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e ) { - logger.error("Exception while setting SSL Context for AAI HTTP Client."); + logger.error("Exception while setting SSL Context for AAI HTTP Client: {}", e); } httpClientBuilder.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE); diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/HttpRequestDetails.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/HttpRequestDetails.java new file mode 100644 index 00000000..896e3068 --- /dev/null +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/HttpRequestDetails.java @@ -0,0 +1,45 @@ +/*- + * ============LICENSE_START======================================================= + * PNF-REGISTRATION-HANDLER + * ================================================================================ + * Copyright (C) 2018 NOKIA Intellectual Property. 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.dcaegen2.services.utils; + + +import org.immutables.value.Value; + +import java.io.Serializable; +import java.util.Map; + +@Value.Immutable(prehash = true) +@Value.Style(stagedBuilder = true) +public abstract class HttpRequestDetails implements Serializable { + + private static final long serialVersionUID = 1L; + + @Value.Parameter + public abstract String aaiAPIPath(); + + @Value.Parameter + public abstract Map<String,String> queryParameters(); + + @Value.Parameter + public abstract Map<String,String> headers(); + + @Value.Parameter + public abstract RequestVerbs requestVerb(); +} diff --git a/prh-aai-client/src/main/java/services/utils/HttpUtils.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/HttpUtils.java index d4d5c880..eac7f83a 100644 --- a/prh-aai-client/src/main/java/services/utils/HttpUtils.java +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/HttpUtils.java @@ -17,16 +17,16 @@ * limitations under the License. * ============LICENSE_END========================================================= */ -package services.utils; +package org.onap.dcaegen2.services.utils; import org.apache.http.HttpStatus; -public class HttpUtils implements HttpStatus { - - private HttpUtils() {} +public final class HttpUtils implements HttpStatus { public static final String JSON_APPLICATION_TYPE = "application/json"; + private HttpUtils() {} + public static boolean isSuccessfulResponseCode(Integer statusCode) { return statusCode >= 200 && statusCode < 300; } diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/RequestVerbs.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/RequestVerbs.java new file mode 100644 index 00000000..f350aa48 --- /dev/null +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/RequestVerbs.java @@ -0,0 +1,28 @@ +/*- + * ============LICENSE_START======================================================= + * PNF-REGISTRATION-HANDLER + * ================================================================================ + * Copyright (C) 2018 NOKIA Intellectual Property. 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.dcaegen2.services.utils; + +public enum RequestVerbs { + GET, + PUT; + + private RequestVerbs() {}; +} |