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/java/org | |
parent | 6bb4aa2c5ddc47ed2c8ffa03844f10b3482d1fcc (diff) | |
parent | 9725138e6882182c50cbac79ace5d66774a17619 (diff) |
Merge "Http put added"
Diffstat (limited to 'prh-aai-client/src/main/java/org')
8 files changed, 474 insertions, 0 deletions
diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/config/AAIHttpClientConfiguration.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/config/AAIHttpClientConfiguration.java new file mode 100644 index 00000000..9f93f896 --- /dev/null +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/config/AAIHttpClientConfiguration.java @@ -0,0 +1,54 @@ +/*- + * ============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.config; + + +import org.immutables.value.Value; +import org.springframework.stereotype.Component; + +import java.io.Serializable; + + +@Component +@Value.Immutable(prehash = true) +@Value.Style(stagedBuilder = true) +public abstract class AAIHttpClientConfiguration implements Serializable { + + private static final long serialVersionUID = 1L; + + @Value.Parameter + public abstract String aaiHost(); + + @Value.Parameter + public abstract Integer aaiHostPortNumber(); + + @Value.Parameter + public abstract String aaiProtocol(); + + @Value.Parameter + public abstract String aaiUserName(); + + @Value.Parameter + public abstract String aaiUserPassword(); + + @Value.Parameter + public abstract Boolean aaiIgnoreSSLCertificateErrors(); + +} diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClient.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClient.java new file mode 100644 index 00000000..24149e6d --- /dev/null +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClient.java @@ -0,0 +1,30 @@ +/*- + * ============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.service; + +import org.onap.dcaegen2.services.utils.HttpRequestDetails; + +import java.util.Optional; + + +@FunctionalInterface +public interface AAIExtendedHttpClient { + Optional<String> getHttpResponse(HttpRequestDetails httpRequestDetails); +} diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClientImpl.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClientImpl.java new file mode 100644 index 00000000..133a537e --- /dev/null +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClientImpl.java @@ -0,0 +1,162 @@ +/*- + * ============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.service; + +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 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; + private final Integer aaiHostPortNumber; + + @Autowired + public AAIExtendedHttpClientImpl (AAIHttpClientConfiguration aaiHttpClientConfiguration) { + final AAIHttpClient aaiHttpClient = new AAIHttpClientImpl(aaiHttpClientConfiguration); + closeableHttpClient = aaiHttpClient.getAAIHttpClient(); + aaiHost = aaiHttpClientConfiguration.aaiHost(); + aaiProtocol = aaiHttpClientConfiguration.aaiProtocol(); + aaiHostPortNumber = aaiHttpClientConfiguration.aaiHostPortNumber(); + } + + @Override + public Optional<String> getHttpResponse(HttpRequestDetails httpRequestDetails) { + + Optional<String> extendedDetails = Optional.empty(); + + final URI extendedURI = createAAIExtendedURI(httpRequestDetails.aaiAPIPath(), + httpRequestDetails.queryParameters()); + final HttpRequestBase request = createHttpRequest(extendedURI, httpRequestDetails); + + if (request == null) { + return Optional.empty(); + } + + for (Map.Entry<String, String> headersEntry : httpRequestDetails.headers().entrySet()) { + request.addHeader(headersEntry.getKey(), headersEntry.getValue()); + } + + try { + extendedDetails = closeableHttpClient.execute(request, aaiResponseHandler()); + } catch (IOException e) { + logger.error("Exception while executing HTTP request: {}", e); + } + + if (extendedDetails.isPresent()) { + return extendedDetails; + } else { + return Optional.empty(); + } + } + + 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); + } + + try { + logger.info("Building extended URI"); + extendedURI = uriBuilder.build(); + } catch (URISyntaxException e) { + logger.error("Exception while building extended URI: {}", e); + } + + return extendedURI; + } + + 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()).append("=").append(queryParamsEntry.getValue()); + if (queryParamIterator.hasNext()) { + queryStringBuilder.append("&"); + } + } + + return queryStringBuilder.toString(); + } + + private ResponseHandler<Optional<String>> aaiResponseHandler() { + return httpResponse -> { + final int responseCode = httpResponse.getStatusLine().getStatusCode(); + final HttpEntity responseEntity = httpResponse.getEntity(); + + 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) : ""; + 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/org/onap/dcaegen2/services/service/AAIHttpClient.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIHttpClient.java new file mode 100644 index 00000000..c60027c2 --- /dev/null +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIHttpClient.java @@ -0,0 +1,29 @@ +/*- + * ============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.service; + +import org.apache.http.impl.client.CloseableHttpClient; + +@FunctionalInterface +public interface AAIHttpClient { + CloseableHttpClient getAAIHttpClient(); +} + diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIHttpClientImpl.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIHttpClientImpl.java new file mode 100644 index 00000000..90b551db --- /dev/null +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIHttpClientImpl.java @@ -0,0 +1,93 @@ +/*- + * ============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.service; + +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.Credentials; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.CredentialsProvider; +import org.apache.http.conn.ssl.NoopHostnameVerifier; +import org.apache.http.impl.client.BasicCredentialsProvider; +import org.apache.http.impl.client.CloseableHttpClient; +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 java.security.KeyManagementException; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; + +public class AAIHttpClientImpl implements AAIHttpClient { + + Logger logger = LoggerFactory.getLogger(AAIHttpClientImpl.class); + + private AAIHttpClientConfiguration aaiHttpClientConfig; + + @Autowired + public AAIHttpClientImpl(AAIHttpClientConfiguration aaiHttpClientConfiguration) { + this.aaiHttpClientConfig = aaiHttpClientConfiguration; + } + + @Override + public CloseableHttpClient getAAIHttpClient() { + + final HttpClientBuilder httpClientBuilder = HttpClients.custom().useSystemProperties(); + final boolean aaiIgnoreSSLCertificateErrors = aaiHttpClientConfig.aaiIgnoreSSLCertificateErrors(); + + TrustStrategy acceptingTrustStrategy = (cert, authType) -> true; + + if (aaiIgnoreSSLCertificateErrors) { + try { + 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: {}", e); + } + + httpClientBuilder.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE); + } + + final String aaiUserName = aaiHttpClientConfig.aaiUserName(); + + final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); + + if (aaiUserName != null) { + final String aaiHost = aaiHttpClientConfig.aaiHost(); + final Integer aaiHostPortNumber = aaiHttpClientConfig.aaiHostPortNumber(); + final String aaiUserPassword = aaiHttpClientConfig.aaiUserPassword(); + final AuthScope aaiHostPortAuthScope = new AuthScope(aaiHost, aaiHostPortNumber); + final Credentials aaiCredentials = new UsernamePasswordCredentials(aaiUserName, aaiUserPassword); + credentialsProvider.setCredentials(aaiHostPortAuthScope, aaiCredentials); + } + + httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); + + return httpClientBuilder.build(); + } +} 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/org/onap/dcaegen2/services/utils/HttpUtils.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/HttpUtils.java new file mode 100644 index 00000000..eac7f83a --- /dev/null +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/HttpUtils.java @@ -0,0 +1,33 @@ +/*- + * ============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.apache.http.HttpStatus; + +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() {}; +} |