2e911c9aaf6723773cdf02ac01898bd0a169f825
[policy/common.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * policy-endpoints
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
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.impl;
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.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;
37
38 /**
39  * http client factory implementation indexed by name
40  */
41 public class IndexedHttpClientFactory implements HttpClientFactory {
42
43     private static final HttpClientFactory instance = new IndexedHttpClientFactory();
44
45     /**
46      * Logger
47      */
48     private static Logger logger = LoggerFactory.getLogger(IndexedHttpClientFactory.class);
49
50     protected HashMap<String, HttpClient> clients = new HashMap<>();
51
52     /**
53      * Get the singleton instance.
54      * 
55      * @return the instance
56      */
57     public static HttpClientFactory getInstance() {
58         return instance;
59     }
60
61     private IndexedHttpClientFactory() {}
62
63     @Override
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);
69         }
70
71         JerseyClient client =
72                 new JerseyClient(name, https, selfSignedCerts, hostname, port, baseUrl, userName, password);
73
74         if (managed) {
75             clients.put(name, client);
76         }
77
78         return client;
79     }
80
81     @Override
82     public synchronized List<HttpClient> build(Properties properties)
83             throws KeyManagementException, NoSuchAlgorithmException {
84         ArrayList<HttpClient> clientList = new ArrayList<>();
85
86         String clientNames = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES);
87         if (clientNames == null || clientNames.isEmpty()) {
88             return clientList;
89         }
90
91         List<String> clientNameList = new ArrayList<>(Arrays.asList(clientNames.split("\\s*,\\s*")));
92
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);
99             }
100
101             String hostName = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "."
102                     + clientName + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX);
103
104             String servicePortString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES
105                     + "." + clientName + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX);
106             int port;
107             try {
108                 if (servicePortString == null || servicePortString.isEmpty()) {
109                     continue;
110                 }
111                 port = Integer.parseInt(servicePortString);
112             } catch (NumberFormatException nfe) {
113                 logger.error("http-client-factory: cannot parse port {}", servicePortString, nfe);
114                 continue;
115             }
116
117             String baseUrl = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "."
118                     + clientName + PolicyEndPointProperties.PROPERTY_HTTP_URL_SUFFIX);
119
120             String userName = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "."
121                     + clientName + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX);
122
123             String password = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "."
124                     + clientName + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX);
125
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);
131             }
132
133             try {
134                 HttpClient client =
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);
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 }