2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 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.policy.common.endpoints.http.client;
23 import java.security.KeyManagementException;
24 import java.security.NoSuchAlgorithmException;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Properties;
31 import org.apache.commons.lang3.StringUtils;
32 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
33 import org.onap.policy.common.endpoints.http.client.internal.JerseyClient;
34 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * HTTP client factory implementation indexed by name.
41 class IndexedHttpClientFactory implements HttpClientFactory {
46 private static Logger logger = LoggerFactory.getLogger(IndexedHttpClientFactory.class);
48 protected HashMap<String, HttpClient> clients = new HashMap<>();
51 public synchronized HttpClient build(BusTopicParams busTopicParams)
52 throws KeyManagementException, NoSuchAlgorithmException {
53 if (clients.containsKey(busTopicParams.getClientName())) {
54 return clients.get(busTopicParams.getClientName());
58 new JerseyClient(busTopicParams);
60 if (busTopicParams.isManaged()) {
61 clients.put(busTopicParams.getClientName(), client);
68 public synchronized List<HttpClient> build(Properties properties)
69 throws KeyManagementException, NoSuchAlgorithmException {
70 ArrayList<HttpClient> clientList = new ArrayList<>();
72 String clientNames = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES);
73 if (clientNames == null || clientNames.isEmpty()) {
77 List<String> clientNameList = new ArrayList<>(Arrays.asList(clientNames.split("\\s*,\\s*")));
79 for (String clientName : clientNameList) {
80 String httpsString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "."
81 + clientName + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX);
82 boolean https = false;
83 if (StringUtils.isNotBlank(httpsString)) {
84 https = Boolean.parseBoolean(httpsString);
87 String hostName = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "."
88 + clientName + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX);
90 String servicePortString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES
91 + "." + clientName + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX);
94 if (servicePortString == null || servicePortString.isEmpty()) {
97 port = Integer.parseInt(servicePortString);
98 } catch (NumberFormatException nfe) {
99 logger.error("http-client-factory: cannot parse port {}", servicePortString, nfe);
103 String baseUrl = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "."
104 + clientName + PolicyEndPointProperties.PROPERTY_HTTP_URL_SUFFIX);
106 String userName = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "."
107 + clientName + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX);
109 String password = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "."
110 + clientName + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX);
112 String managedString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "."
113 + clientName + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX);
114 boolean managed = true;
115 if (managedString != null && !managedString.isEmpty()) {
116 managed = Boolean.parseBoolean(managedString);
121 this.build(BusTopicParams.builder()
122 .clientName(clientName)
124 .allowSelfSignedCerts(https)
132 clientList.add(client);
133 } catch (Exception e) {
134 logger.error("http-client-factory: cannot build client {}", clientName, e);
142 public synchronized HttpClient get(String name) {
143 if (clients.containsKey(name)) {
144 return clients.get(name);
147 throw new IllegalArgumentException("Http Client " + name + " not found");
151 public synchronized List<HttpClient> inventory() {
152 return new ArrayList<>(this.clients.values());
156 public synchronized void destroy(String name) {
157 if (!clients.containsKey(name)) {
161 HttpClient client = clients.remove(name);
164 } catch (IllegalStateException e) {
165 logger.error("http-client-factory: cannot shutdown client {}", client, e);
170 public void destroy() {
171 List<HttpClient> clientsInventory = this.inventory();
172 for (HttpClient client : clientsInventory) {
176 synchronized (this) {
177 this.clients.clear();