diff options
author | pwielebs <piotr.wielebski@nokia.com> | 2018-03-27 19:49:29 +0200 |
---|---|---|
committer | pwielebs <piotr.wielebski@nokia.com> | 2018-03-29 15:01:09 +0200 |
commit | f355da4d8a2f3fd3b46652d7562469af505f7f27 (patch) | |
tree | 80d3cc3549f67a8c8dd7e161a4a6a2d117fa51d7 /prh-aai-client | |
parent | 112044f9d9999f7e1e1871e49aba9ba73ce3959e (diff) |
AAI HTTP simple and extended clients added
Change-Id: I759aeeb64085dbe32883a942941bffc8e03515f7
Issue-ID: DCAEGEN2-407
Signed-off-by: pwielebs <piotr.wielebski@nokia.com>
Diffstat (limited to 'prh-aai-client')
-rw-r--r-- | prh-aai-client/pom.xml | 25 | ||||
-rw-r--r-- | prh-aai-client/src/main/java/services/config/AAIHttpClientConfiguration.java (renamed from prh-aai-client/src/main/java/services/config/AAIDmaapProducerConfiguration.java) | 13 | ||||
-rw-r--r-- | prh-aai-client/src/main/java/services/service/AAIExtendedHttpClient.java | 27 | ||||
-rw-r--r-- | prh-aai-client/src/main/java/services/service/AAIExtendedHttpClientImpl.java | 142 | ||||
-rw-r--r-- | prh-aai-client/src/main/java/services/service/AAIHttpClient.java | 28 | ||||
-rw-r--r-- | prh-aai-client/src/main/java/services/service/AAIHttpClientImpl.java | 93 | ||||
-rw-r--r-- | prh-aai-client/src/main/java/services/utils/HttpUtils.java | 37 |
7 files changed, 356 insertions, 9 deletions
diff --git a/prh-aai-client/pom.xml b/prh-aai-client/pom.xml index 6ac6484a..6a40d32c 100644 --- a/prh-aai-client/pom.xml +++ b/prh-aai-client/pom.xml @@ -46,6 +46,29 @@ <groupId>org.immutables</groupId> <artifactId>value</artifactId> </dependency> + <dependency> + <groupId>org.apache.httpcomponents</groupId> + <artifactId>httpclient</artifactId> + </dependency> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-beans</artifactId> + </dependency> + + <!-- VERSION SET DUE TO THE FAILING BUILD--> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-context</artifactId> + <version>3.0.2.RELEASE</version> + </dependency> + <dependency> + <groupId>com.google.guava</groupId> + <artifactId>guava</artifactId> + </dependency> + <dependency> + <groupId>org.apache.commons</groupId> + <artifactId>commons-lang3</artifactId> + </dependency> <!-- LOGGING DEPENDENCIES--> <dependency> @@ -84,4 +107,4 @@ </dependency> </dependencies> -</project>
\ No newline at end of file +</project> diff --git a/prh-aai-client/src/main/java/services/config/AAIDmaapProducerConfiguration.java b/prh-aai-client/src/main/java/services/config/AAIHttpClientConfiguration.java index dfeafddd..4c50966b 100644 --- a/prh-aai-client/src/main/java/services/config/AAIDmaapProducerConfiguration.java +++ b/prh-aai-client/src/main/java/services/config/AAIHttpClientConfiguration.java @@ -17,17 +17,17 @@ * limitations under the License. * ============LICENSE_END========================================================= */ + package services.config; -import java.net.URL; + import org.immutables.value.Value; +import org.springframework.stereotype.Component; -/** - * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 3/23/18 - */ +@Component @Value.Immutable(prehash = true) @Value.Style(stagedBuilder = true) -public abstract class AAIDmaapProducerConfiguration implements AAIConfig { +public abstract class AAIHttpClientConfiguration implements AAIConfig { private static final long serialVersionUID = 1L; @@ -47,9 +47,6 @@ public abstract class AAIDmaapProducerConfiguration implements AAIConfig { public abstract String aaiUserPassword(); @Value.Parameter - public abstract URL aaiProxyURL(); - - @Value.Parameter public abstract boolean aaiIgnoreSSLCertificateErrors(); } diff --git a/prh-aai-client/src/main/java/services/service/AAIExtendedHttpClient.java b/prh-aai-client/src/main/java/services/service/AAIExtendedHttpClient.java new file mode 100644 index 00000000..8b7db1ba --- /dev/null +++ b/prh-aai-client/src/main/java/services/service/AAIExtendedHttpClient.java @@ -0,0 +1,27 @@ +/*- + * ============LICENSE_START======================================================= + * PROJECT + * ================================================================================ + * 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 services.service; + +import java.util.Map; + +public interface AAIExtendedHttpClient { + String getExtendedDetails(String aaiAPIPath, Map<String, String> queryParams, Map<String, String> headers); +} diff --git a/prh-aai-client/src/main/java/services/service/AAIExtendedHttpClientImpl.java b/prh-aai-client/src/main/java/services/service/AAIExtendedHttpClientImpl.java new file mode 100644 index 00000000..06d1a55f --- /dev/null +++ b/prh-aai-client/src/main/java/services/service/AAIExtendedHttpClientImpl.java @@ -0,0 +1,142 @@ +/*- + * ============LICENSE_START======================================================= + * PROJECT + * ================================================================================ + * 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 services.service; + +import com.google.common.base.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.utils.URIBuilder; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.util.EntityUtils; +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; + +public class AAIExtendedHttpClientImpl implements AAIExtendedHttpClient { + + 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 String getExtendedDetails(final String aaiAPIPath, final Map<String, String> queryParams, + final Map<String, String> headers) { + final URI extendedURI = + createAAIExtendedURI(aaiProtocol, aaiHost, aaiHostPortNumber, aaiAPIPath, queryParams); + + if (extendedURI == null) { + return null; + } + + final HttpGet getRequest = new HttpGet(extendedURI); + + for (Map.Entry<String, String> headersEntry : headers.entrySet()) { + getRequest.addHeader(headersEntry.getKey(), headersEntry.getValue()); + } + + Optional<String> extendedDetails = Optional.absent(); + + try { + extendedDetails = closeableHttpClient.execute(getRequest, aaiResponseHandler()); + } catch (IOException ex) { + //ToDo loging + } + + // return response + if (extendedDetails.isPresent()) { + return extendedDetails.get(); + } else { + return null; + } + } + + 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); + + final String customQuery = createCustomQuery(queryParams); + if (StringUtils.isNoneBlank(customQuery)) { + uriBuilder.setCustomQuery(customQuery); + } + + URI extendedURI = null; + + try { + extendedURI = uriBuilder.build(); + } catch (URISyntaxException e) { + // ToDo loging + } + + return extendedURI; + } + + private static 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()); + if (queryParamIterator.hasNext()) { + queryStringBuilder.append("&"); + } + } + return queryStringBuilder.toString(); + } + + public static ResponseHandler<Optional<String>> aaiResponseHandler() { + return httpResponse -> { + final int responseCode = httpResponse.getStatusLine().getStatusCode(); + final HttpEntity responseEntity = httpResponse.getEntity(); + + if (HttpUtils.isSuccessfulResponseCode(responseCode) && null != responseEntity) { + final String aaiResponse = EntityUtils.toString(responseEntity); + return Optional.of(aaiResponse); + } else { + String aaiResponse = responseEntity != null ? EntityUtils.toString(responseEntity) : ""; + //ToDo loging + return Optional.absent(); + } + }; + } + +} diff --git a/prh-aai-client/src/main/java/services/service/AAIHttpClient.java b/prh-aai-client/src/main/java/services/service/AAIHttpClient.java new file mode 100644 index 00000000..3dd17fc2 --- /dev/null +++ b/prh-aai-client/src/main/java/services/service/AAIHttpClient.java @@ -0,0 +1,28 @@ +/*- + * ============LICENSE_START======================================================= + * PROJECT + * ================================================================================ + * 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 services.service; + +import org.apache.http.impl.client.CloseableHttpClient; + +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/services/service/AAIHttpClientImpl.java new file mode 100644 index 00000000..76d090d6 --- /dev/null +++ b/prh-aai-client/src/main/java/services/service/AAIHttpClientImpl.java @@ -0,0 +1,93 @@ +/*- + * ============LICENSE_START======================================================= + * PROJECT + * ================================================================================ + * 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 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.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; +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 { + SSLContextBuilder sslContextBuilder = new SSLContextBuilder(); + sslContextBuilder.loadTrustMaterial(null, acceptingTrustStrategy); + httpClientBuilder.setSSLContext(sslContextBuilder.build()); + + } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e ) { + logger.error("Exception while setting SSL Context for AAI HTTP Client."); + } + + 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/services/utils/HttpUtils.java b/prh-aai-client/src/main/java/services/utils/HttpUtils.java new file mode 100644 index 00000000..f752d145 --- /dev/null +++ b/prh-aai-client/src/main/java/services/utils/HttpUtils.java @@ -0,0 +1,37 @@ +/*- + * ============LICENSE_START======================================================= + * PROJECT + * ================================================================================ + * 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 services.utils; + +public class HttpUtils { + + private HttpUtils() {} + + public static final Integer HTTP_OK_RESPONSE_CODE = 200; + public static final Integer HTTP_ACCEPTED_RESPONSE_CODE = 200; + public static final Integer HTTP_NONAUTHORATIVE_INFORMATION_RESPONSE_CODE = 203; + public static final Integer HTTP_NO_CONTENT_RESPONSE_CODE = 204; + public static final Integer HTTP_RESET_CONTENT_RESPONSE_CODE = 205; + public static final Integer HTTP_PARTIAL_CONTENT_RESPONSE_CODE = 206; + public static final String JSON_APPLICATION_TYPE = "application/json"; + + public static boolean isSuccessfulResponseCode(Integer statusCode) { + return statusCode >= 200 && statusCode < 300; + } +} |