2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019, 2021 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.HashMap;
27 import java.util.List;
28 import java.util.Properties;
29 import org.apache.commons.lang3.StringUtils;
30 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
31 import org.onap.policy.common.endpoints.http.client.internal.JerseyClient;
32 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
33 import org.onap.policy.common.endpoints.utils.PropertyUtils;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * HTTP client factory implementation indexed by name.
40 class IndexedHttpClientFactory implements HttpClientFactory {
45 private static Logger logger = LoggerFactory.getLogger(IndexedHttpClientFactory.class);
47 protected HashMap<String, HttpClient> clients = new HashMap<>();
50 public synchronized HttpClient build(BusTopicParams busTopicParams) throws HttpClientConfigException {
51 if (clients.containsKey(busTopicParams.getClientName())) {
52 return clients.get(busTopicParams.getClientName());
57 client = new JerseyClient(busTopicParams);
58 } catch (KeyManagementException | NoSuchAlgorithmException | ClassNotFoundException e) {
59 throw new HttpClientConfigException(e);
62 if (busTopicParams.isManaged()) {
63 clients.put(busTopicParams.getClientName(), client);
70 public synchronized List<HttpClient> build(Properties properties) throws HttpClientConfigException {
71 ArrayList<HttpClient> clientList = new ArrayList<>();
73 String clientNames = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES);
74 if (StringUtils.isBlank(clientNames)) {
78 for (String clientName : clientNames.split("\\s*,\\s*")) {
79 addClient(clientList, clientName, properties);
85 private void addClient(ArrayList<HttpClient> clientList, String clientName, Properties properties) {
86 String clientPrefix = PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + clientName;
88 PropertyUtils props = new PropertyUtils(properties, clientPrefix,
90 logger.warn("{}: {} {} is in invalid format for http client {} ", this, name, value, clientName));
92 int port = props.getInteger(PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX, -1);
94 logger.warn("No HTTP port for client in {}", clientName);
99 HttpClient client = this.build(BusTopicParams.builder()
100 .clientName(clientName)
101 .useHttps(props.getBoolean(PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX, false))
102 .allowSelfSignedCerts(
103 props.getBoolean(PolicyEndPointProperties.PROPERTY_ALLOW_SELF_SIGNED_CERTIFICATES_SUFFIX, false))
104 .hostname(props.getString(PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, null))
106 .basePath(props.getString(PolicyEndPointProperties.PROPERTY_HTTP_URL_SUFFIX, null))
107 .userName(props.getString(PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX,
109 .password(props.getString(PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX,
111 .managed(props.getBoolean(PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, true))
112 .serializationProvider(props.getString(
113 PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER, null))
115 clientList.add(client);
116 } catch (Exception e) {
117 logger.error("http-client-factory: cannot build client {}", clientName, e);
122 public synchronized HttpClient get(String name) {
123 if (clients.containsKey(name)) {
124 return clients.get(name);
127 throw new IllegalArgumentException("Http Client " + name + " not found");
131 public synchronized List<HttpClient> inventory() {
132 return new ArrayList<>(this.clients.values());
136 public synchronized void destroy(String name) {
137 if (!clients.containsKey(name)) {
141 HttpClient client = clients.remove(name);
144 } catch (IllegalStateException e) {
145 logger.error("http-client-factory: cannot shutdown client {}", client, e);
150 public void destroy() {
151 List<HttpClient> clientsInventory = this.inventory();
152 for (HttpClient client : clientsInventory) {
156 synchronized (this) {
157 this.clients.clear();