2 * ===============================LICENSE_START======================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============================LICENSE_END===========================================
21 package org.onap.dcae.apod.analytics.aai.service;
23 import com.google.inject.Inject;
24 import com.google.inject.assistedinject.Assisted;
25 import org.apache.commons.lang3.StringUtils;
26 import org.apache.http.HttpHost;
27 import org.apache.http.auth.AuthScope;
28 import org.apache.http.auth.Credentials;
29 import org.apache.http.auth.UsernamePasswordCredentials;
30 import org.apache.http.client.CredentialsProvider;
31 import org.apache.http.conn.ssl.NoopHostnameVerifier;
32 import org.apache.http.impl.client.BasicCredentialsProvider;
33 import org.apache.http.impl.client.CloseableHttpClient;
34 import org.apache.http.impl.client.HttpClientBuilder;
35 import org.apache.http.impl.client.HttpClients;
36 import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
37 import org.apache.http.ssl.SSLContextBuilder;
38 import org.onap.dcae.apod.analytics.aai.domain.config.AAIHttpClientConfig;
39 import org.onap.dcae.apod.analytics.aai.utils.ssl.AlwaysTrustingTrustStrategy;
40 import org.onap.dcae.apod.analytics.common.exception.DCAEAnalyticsRuntimeException;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 import java.security.KeyManagementException;
46 import java.security.KeyStoreException;
47 import java.security.NoSuchAlgorithmException;
51 * A concrete implementation of {@link AAIHttpClient} which provides Apache {@link CloseableHttpClient} for
52 * making rest calls to A&AI Enrichment API.
55 * @author Rajiv Singla . Creation Date: 9/19/2017.
57 public class AAIHttpClientImpl implements AAIHttpClient {
59 private static final Logger LOG = LoggerFactory.getLogger(AAIHttpClientImpl.class);
61 private final AAIHttpClientConfig aaiHttpClientConfig;
64 public AAIHttpClientImpl(@Assisted final AAIHttpClientConfig aaiHttpClientConfig) {
65 this.aaiHttpClientConfig = aaiHttpClientConfig;
69 * Provides an instance of {@link CloseableHttpClient} used to make REST calls to A&AI Enrichment API
71 * @return An instance of Closeable HTTP Client used to make A&AI API Rest calls
74 public CloseableHttpClient getAAIHttpClient() {
76 final HttpClientBuilder httpClientBuilder = HttpClients.custom().useSystemProperties();
77 final boolean aaiIgnoreSSLCertificateErrors = aaiHttpClientConfig.isAaiIgnoreSSLCertificateErrors();
79 // Setup SSL Context to ignore SSL certificate issues if ignoreSSLCertificateErrors is true
80 LOG.info("SSL Certificate Errors attributed is set to : {}", aaiIgnoreSSLCertificateErrors);
82 if (aaiIgnoreSSLCertificateErrors) {
83 LOG.warn("SSL Certificate Errors will be ignored for this A&AI Http Client Instance");
85 SSLContextBuilder sslContextBuilder = new SSLContextBuilder();
86 sslContextBuilder.loadTrustMaterial(null, new AlwaysTrustingTrustStrategy());
87 httpClientBuilder.setSSLContext(sslContextBuilder.build());
88 } catch (NoSuchAlgorithmException e) {
89 final String errorMessage = "NoSuchAlgorithmException while setting SSL Context for AAI HTTP Client.";
90 throw new DCAEAnalyticsRuntimeException(errorMessage, LOG, e);
91 } catch (KeyStoreException e) {
92 final String errorMessage = "KeyStoreException while setting SSL Context for AAI HTTP Client.";
93 throw new DCAEAnalyticsRuntimeException(errorMessage, LOG, e);
94 } catch (KeyManagementException e) {
95 final String errorMessage = "KeyManagementException while setting SSL Context for AAI HTTP Client.";
96 throw new DCAEAnalyticsRuntimeException(errorMessage, LOG, e);
99 httpClientBuilder.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE);
102 LOG.info("SSL Certification Errors will be enforced for A&AI Http Client instance");
105 // Setup credentials and proxy
106 final String aaiUserName = aaiHttpClientConfig.getAaiUserName();
108 final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
110 if (aaiUserName != null) {
111 final String aaiHost = aaiHttpClientConfig.getAaiHost();
112 final Integer aaiHostPortNumber = aaiHttpClientConfig.getAaiHostPortNumber();
113 final String aaiUserPassword = aaiHttpClientConfig.getAaiUserPassword();
114 LOG.info("Setting A&AI host credentials for AAI Host: {}", aaiHost);
115 final AuthScope aaiHostPortAuthScope = new AuthScope(aaiHost, aaiHostPortNumber);
116 final Credentials aaiCredentials = new UsernamePasswordCredentials(aaiUserName, aaiUserPassword);
117 credentialsProvider.setCredentials(aaiHostPortAuthScope, aaiCredentials);
119 LOG.warn("A&AI userName not present. No credentials set for A&AI authentication");
122 final URL aaiProxyURL = aaiHttpClientConfig.getAaiProxyURL();
124 if (aaiProxyURL != null) {
125 final String aaiProxyHost = aaiProxyURL.getHost();
126 final Integer aaiProxyPortNumber = aaiProxyURL.getPort();
127 final String aaiProxyProtocol = aaiProxyURL.getProtocol();
128 final HttpHost proxy = new HttpHost(aaiProxyHost, aaiProxyPortNumber, aaiProxyProtocol);
129 LOG.info("Setting A&AI Http Client default proxy as: {}", proxy);
130 final DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
131 httpClientBuilder.setRoutePlanner(routePlanner);
133 final String userInfo = aaiProxyURL.getUserInfo();
134 if (StringUtils.isNotBlank(userInfo)) {
135 final String[] userInfoArray = userInfo.split(":");
136 final String aaiProxyUsername = userInfoArray[0];
137 String aaiProxyPassword = null;
138 if (userInfoArray.length > 1) {
139 aaiProxyPassword = userInfoArray[1];
141 LOG.info("Setting A&AI Http Client proxy credentials with username: {}", aaiProxyUsername);
142 final AuthScope aaiProxyAuthScope = new AuthScope(aaiProxyHost, aaiProxyPortNumber);
143 final Credentials aaiProxyCredentials = new UsernamePasswordCredentials(aaiProxyUsername,
145 credentialsProvider.setCredentials(aaiProxyAuthScope, aaiProxyCredentials);
147 LOG.debug("NO A&AI Proxy Username present.Bypassing setting up A&AI Proxy authentication credentials");
150 LOG.debug("A&AI proxy not Enabled - bypassing setting A&AI Proxy settings");
153 // setup credentials provider
154 httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
156 return httpClientBuilder.build();