2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 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.policy.common.endpoints.http.client.impl;
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.onap.policy.common.endpoints.http.client.HttpClient;
32 import org.onap.policy.common.endpoints.http.client.HttpClientFactory;
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 public class IndexedHttpClientFactory implements HttpClientFactory {
43 private static final HttpClientFactory instance = new IndexedHttpClientFactory();
48 private static Logger logger = LoggerFactory.getLogger(IndexedHttpClientFactory.class);
50 protected HashMap<String, HttpClient> clients = new HashMap<>();
53 * Get the singleton instance.
55 * @return the instance
57 public static HttpClientFactory getInstance() {
61 private IndexedHttpClientFactory() {}
64 public synchronized HttpClient build(String name, boolean https, boolean selfSignedCerts, String hostname, int port,
65 String baseUrl, String userName, String password, boolean managed)
66 throws KeyManagementException, NoSuchAlgorithmException {
67 if (clients.containsKey(name)) {
68 return clients.get(name);
72 new JerseyClient(name, https, selfSignedCerts, hostname, port, baseUrl, userName, password);
75 clients.put(name, client);
82 public synchronized List<HttpClient> build(Properties properties)
83 throws KeyManagementException, NoSuchAlgorithmException {
84 ArrayList<HttpClient> clientList = new ArrayList<>();
86 String clientNames = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES);
87 if (clientNames == null || clientNames.isEmpty()) {
91 List<String> clientNameList = new ArrayList<>(Arrays.asList(clientNames.split("\\s*,\\s*")));
93 for (String clientName : clientNameList) {
94 String httpsString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "."
95 + clientName + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX);
96 boolean https = false;
97 if (httpsString != null && !httpsString.isEmpty()) {
98 https = Boolean.parseBoolean(httpsString);
101 String hostName = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "."
102 + clientName + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX);
104 String servicePortString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES
105 + "." + clientName + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX);
108 if (servicePortString == null || servicePortString.isEmpty()) {
111 port = Integer.parseInt(servicePortString);
112 } catch (NumberFormatException nfe) {
113 logger.error("http-client-factory: cannot parse port {}", servicePortString, nfe);
117 String baseUrl = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "."
118 + clientName + PolicyEndPointProperties.PROPERTY_HTTP_URL_SUFFIX);
120 String userName = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "."
121 + clientName + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX);
123 String password = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "."
124 + clientName + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX);
126 String managedString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "."
127 + clientName + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX);
128 boolean managed = true;
129 if (managedString != null && !managedString.isEmpty()) {
130 managed = Boolean.parseBoolean(managedString);
135 this.build(clientName, https, https, hostName, port, baseUrl, userName, password, managed);
136 clientList.add(client);
137 } catch (Exception e) {
138 logger.error("http-client-factory: cannot build client {}", clientName, e);
146 public synchronized HttpClient get(String name) {
147 if (clients.containsKey(name)) {
148 return clients.get(name);
151 throw new IllegalArgumentException("Http Client " + name + " not found");
155 public synchronized List<HttpClient> inventory() {
156 return new ArrayList<>(this.clients.values());
160 public synchronized void destroy(String name) {
161 if (!clients.containsKey(name)) {
165 HttpClient client = clients.remove(name);
168 } catch (IllegalStateException e) {
169 logger.error("http-client-factory: cannot shutdown client {}", client, e);
174 public void destroy() {
175 List<HttpClient> clientsInventory = this.inventory();
176 for (HttpClient client : clientsInventory) {
180 synchronized (this) {
181 this.clients.clear();