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.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)
51 throws KeyManagementException, NoSuchAlgorithmException, ClassNotFoundException {
52 if (clients.containsKey(busTopicParams.getClientName())) {
53 return clients.get(busTopicParams.getClientName());
57 new JerseyClient(busTopicParams);
59 if (busTopicParams.isManaged()) {
60 clients.put(busTopicParams.getClientName(), client);
67 public synchronized List<HttpClient> build(Properties properties)
68 throws KeyManagementException, NoSuchAlgorithmException {
69 ArrayList<HttpClient> clientList = new ArrayList<>();
71 String clientNames = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES);
72 if (StringUtils.isBlank(clientNames)) {
76 for (String clientName : clientNames.split("\\s*,\\s*")) {
77 addClient(clientList, clientName, properties);
83 private void addClient(ArrayList<HttpClient> clientList, String clientName, Properties properties) {
84 String clientPrefix = PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + clientName;
86 PropertyUtils props = new PropertyUtils(properties, clientPrefix,
88 logger.warn("{}: {} {} is in invalid format for http client {} ", this, name, value, clientName));
90 int port = props.getInteger(PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX, -1);
92 logger.warn("No HTTP port for client in {}", clientName);
96 boolean https = props.getBoolean(PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX, false);
99 HttpClient client = this.build(BusTopicParams.builder()
100 .clientName(clientName)
102 .allowSelfSignedCerts(https)
103 .hostname(props.getString(PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, null))
105 .basePath(props.getString(PolicyEndPointProperties.PROPERTY_HTTP_URL_SUFFIX, null))
106 .userName(props.getString(PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX,
108 .password(props.getString(PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX,
110 .managed(props.getBoolean(PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, true))
111 .serializationProvider(props.getString(
112 PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER, null))
114 clientList.add(client);
115 } catch (Exception e) {
116 logger.error("http-client-factory: cannot build client {}", clientName, e);
121 public synchronized HttpClient get(String name) {
122 if (clients.containsKey(name)) {
123 return clients.get(name);
126 throw new IllegalArgumentException("Http Client " + name + " not found");
130 public synchronized List<HttpClient> inventory() {
131 return new ArrayList<>(this.clients.values());
135 public synchronized void destroy(String name) {
136 if (!clients.containsKey(name)) {
140 HttpClient client = clients.remove(name);
143 } catch (IllegalStateException e) {
144 logger.error("http-client-factory: cannot shutdown client {}", client, e);
149 public void destroy() {
150 List<HttpClient> clientsInventory = this.inventory();
151 for (HttpClient client : clientsInventory) {
155 synchronized (this) {
156 this.clients.clear();