5cc0071e8bf8c1704c1e963e98e215e4ad5f044c
[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, ClassNotFoundException {
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             final String classProv = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES
113                 + "." + clientName + PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER);
114
115             String managedString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "."
116                     + clientName + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX);
117             boolean managed = true;
118             if (managedString != null && !managedString.isEmpty()) {
119                 managed = Boolean.parseBoolean(managedString);
120             }
121
122             try {
123                 HttpClient client =
124                         this.build(BusTopicParams.builder()
125                                 .clientName(clientName)
126                                 .useHttps(https)
127                                 .allowSelfSignedCerts(https)
128                                 .hostname(hostName)
129                                 .port(port)
130                                 .basePath(baseUrl)
131                                 .userName(userName)
132                                 .password(password)
133                                 .managed(managed)
134                                 .serializationProvider(classProv)
135                                 .build());
136                 clientList.add(client);
137             } catch (Exception e) {
138                 logger.error("http-client-factory: cannot build client {}", clientName, e);
139             }
140         }
141
142         return clientList;
143     }
144
145     @Override
146     public synchronized HttpClient get(String name) {
147         if (clients.containsKey(name)) {
148             return clients.get(name);
149         }
150
151         throw new IllegalArgumentException("Http Client " + name + " not found");
152     }
153
154     @Override
155     public synchronized List<HttpClient> inventory() {
156         return new ArrayList<>(this.clients.values());
157     }
158
159     @Override
160     public synchronized void destroy(String name) {
161         if (!clients.containsKey(name)) {
162             return;
163         }
164
165         HttpClient client = clients.remove(name);
166         try {
167             client.shutdown();
168         } catch (IllegalStateException e) {
169             logger.error("http-client-factory: cannot shutdown client {}", client, e);
170         }
171     }
172
173     @Override
174     public void destroy() {
175         List<HttpClient> clientsInventory = this.inventory();
176         for (HttpClient client : clientsInventory) {
177             client.shutdown();
178         }
179
180         synchronized (this) {
181             this.clients.clear();
182         }
183     }
184
185 }