9aef09e4bbe093774c39a200b4967d01e3b1736c
[policy/common.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.policy.common.endpoints.http.client;
22
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;
30
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;
37
38 /**
39  * HTTP client factory implementation indexed by name.
40  */
41 class IndexedHttpClientFactory implements HttpClientFactory {
42
43     /**
44      * Logger.
45      */
46     private static Logger logger = LoggerFactory.getLogger(IndexedHttpClientFactory.class);
47
48     protected HashMap<String, HttpClient> clients = new HashMap<>();
49
50     @Override
51     public synchronized HttpClient build(BusTopicParams busTopicParams)
52             throws KeyManagementException, NoSuchAlgorithmException {
53         if (clients.containsKey(busTopicParams.getClientName())) {
54             return clients.get(busTopicParams.getClientName());
55         }
56
57         JerseyClient client =
58                 new JerseyClient(busTopicParams);
59
60         if (busTopicParams.isManaged()) {
61             clients.put(busTopicParams.getClientName(), client);
62         }
63
64         return client;
65     }
66
67     @Override
68     public synchronized List<HttpClient> build(Properties properties)
69             throws KeyManagementException, NoSuchAlgorithmException {
70         ArrayList<HttpClient> clientList = new ArrayList<>();
71
72         String clientNames = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES);
73         if (clientNames == null || clientNames.isEmpty()) {
74             return clientList;
75         }
76
77         List<String> clientNameList = new ArrayList<>(Arrays.asList(clientNames.split("\\s*,\\s*")));
78
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);
85             }
86
87             String hostName = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "."
88                     + clientName + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX);
89
90             String servicePortString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES
91                     + "." + clientName + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX);
92             int port;
93             try {
94                 if (servicePortString == null || servicePortString.isEmpty()) {
95                     continue;
96                 }
97                 port = Integer.parseInt(servicePortString);
98             } catch (NumberFormatException nfe) {
99                 logger.error("http-client-factory: cannot parse port {}", servicePortString, nfe);
100                 continue;
101             }
102
103             String baseUrl = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "."
104                     + clientName + PolicyEndPointProperties.PROPERTY_HTTP_URL_SUFFIX);
105
106             String userName = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "."
107                     + clientName + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX);
108
109             String password = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "."
110                     + clientName + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX);
111
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);
117             }
118
119             try {
120                 HttpClient client =
121                         this.build(BusTopicParams.builder()
122                                 .clientName(clientName)
123                                 .useHttps(https)
124                                 .allowSelfSignedCerts(https)
125                                 .hostname(hostName)
126                                 .port(port)
127                                 .basePath(baseUrl)
128                                 .userName(userName)
129                                 .password(password)
130                                 .managed(managed)
131                                 .build());
132                 clientList.add(client);
133             } catch (Exception e) {
134                 logger.error("http-client-factory: cannot build client {}", clientName, e);
135             }
136         }
137
138         return clientList;
139     }
140
141     @Override
142     public synchronized HttpClient get(String name) {
143         if (clients.containsKey(name)) {
144             return clients.get(name);
145         }
146
147         throw new IllegalArgumentException("Http Client " + name + " not found");
148     }
149
150     @Override
151     public synchronized List<HttpClient> inventory() {
152         return new ArrayList<>(this.clients.values());
153     }
154
155     @Override
156     public synchronized void destroy(String name) {
157         if (!clients.containsKey(name)) {
158             return;
159         }
160
161         HttpClient client = clients.remove(name);
162         try {
163             client.shutdown();
164         } catch (IllegalStateException e) {
165             logger.error("http-client-factory: cannot shutdown client {}", client, e);
166         }
167     }
168
169     @Override
170     public void destroy() {
171         List<HttpClient> clientsInventory = this.inventory();
172         for (HttpClient client : clientsInventory) {
173             client.shutdown();
174         }
175
176         synchronized (this) {
177             this.clients.clear();
178         }
179     }
180
181 }