4a430b20f9e3f5fa2a7cccdb0cd8ea2da188781a
[policy/common.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * policy-endpoints
4  * ================================================================================
5  * Copyright (C) 2017-2018 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.server;
22
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Properties;
28
29 import org.onap.aaf.cadi.filter.CadiFilter;
30 import org.onap.policy.common.endpoints.http.server.internal.JettyJerseyServer;
31 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * Factory of HTTP Servlet-Enabled Servlets
37  */
38 public interface HttpServletServerFactory {
39
40     /**
41      * builds an http or https server with support for servlets
42      * 
43      * @param name name
44      * @param https use secured http over tls connection
45      * @param host binding host
46      * @param port port
47      * @param contextPath server base path
48      * @param swagger enable swagger documentation
49      * @param managed is it managed by infrastructure
50      * @return http server
51      * @throws IllegalArgumentException when invalid parameters are provided
52      */
53     HttpServletServer build(String name, boolean https, String host, int port, String contextPath, boolean swagger,
54             boolean managed);
55
56     /**
57      * builds an http server with support for servlets
58      *
59      * @param name name
60      * @param host binding host
61      * @param port port
62      * @param contextPath server base path
63      * @param swagger enable swagger documentation
64      * @param managed is it managed by infrastructure
65      * @return http server
66      * @throws IllegalArgumentException when invalid parameters are provided
67      */
68     HttpServletServer build(String name, String host, int port, String contextPath, boolean swagger,
69         boolean managed);
70
71     /**
72      * list of http servers per properties
73      * 
74      * @param properties properties based configuration
75      * @return list of http servers
76      * @throws IllegalArgumentException when invalid parameters are provided
77      */
78     List<HttpServletServer> build(Properties properties);
79
80     /**
81      * gets a server based on the port
82      * 
83      * @param port port
84      * @return http server
85      */
86     HttpServletServer get(int port);
87
88     /**
89      * provides an inventory of servers
90      * 
91      * @return inventory of servers
92      */
93     List<HttpServletServer> inventory();
94
95     /**
96      * destroys server bound to a port
97      * 
98      * @param port
99      */
100     void destroy(int port);
101
102     /**
103      * destroys the factory and therefore all servers
104      */
105     void destroy();
106 }
107
108
109 /**
110  * Indexed factory implementation
111  */
112 class IndexedHttpServletServerFactory implements HttpServletServerFactory {
113
114     private static final String SPACES_COMMA_SPACES = "\\s*,\\s*";
115
116     /**
117      * logger
118      */
119     protected static Logger logger = LoggerFactory.getLogger(IndexedHttpServletServerFactory.class);
120
121     /**
122      * servers index
123      */
124     protected HashMap<Integer, HttpServletServer> servers = new HashMap<>();
125
126     @Override
127     public synchronized HttpServletServer build(String name, boolean https, String host, int port, String contextPath, boolean swagger,
128             boolean managed) {
129
130         if (servers.containsKey(port)) {
131             return servers.get(port);
132         }
133
134         JettyJerseyServer server = new JettyJerseyServer(name, https, host, port, contextPath, swagger);
135         if (managed) {
136             servers.put(port, server);
137         }
138
139         return server;
140     }
141
142     @Override
143     public synchronized HttpServletServer build(String name, String host, int port, String contextPath,
144                                                 boolean swagger, boolean managed) {
145         return build(name, false, host, port, contextPath, swagger, managed);
146     }
147
148
149     @Override
150     public synchronized List<HttpServletServer> build(Properties properties) {
151
152         ArrayList<HttpServletServer> serviceList = new ArrayList<>();
153
154         String serviceNames = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES);
155         if (serviceNames == null || serviceNames.isEmpty()) {
156             logger.warn("No topic for HTTP Service: {}", properties);
157             return serviceList;
158         }
159
160         List<String> serviceNameList = Arrays.asList(serviceNames.split(SPACES_COMMA_SPACES));
161
162         for (String serviceName : serviceNameList) {
163             String servicePortString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES
164                     + "." + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX);
165
166             int servicePort;
167             try {
168                 if (servicePortString == null || servicePortString.isEmpty()) {
169                     if (logger.isWarnEnabled()) {
170                         logger.warn("No HTTP port for service in {}", serviceName);
171                     }
172                     continue;
173                 }
174                 servicePort = Integer.parseInt(servicePortString);
175             } catch (NumberFormatException nfe) {
176                 if (logger.isWarnEnabled()) {
177                     logger.warn("No HTTP port for service in {}", serviceName);
178                 }
179                 continue;
180             }
181
182             String hostName = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
183                     + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX);
184
185             String contextUriPath = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
186                     + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_CONTEXT_URIPATH_SUFFIX);
187
188             String userName = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
189                     + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX);
190
191             String password = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
192                     + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX);
193
194             String authUriPath = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
195                     + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_URIPATH_SUFFIX);
196
197             String restClasses = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
198                     + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX);
199
200             String restPackages = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
201                     + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_REST_PACKAGES_SUFFIX);
202             String restUriPath = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
203                     + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_REST_URIPATH_SUFFIX);
204
205             String managedString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
206                     + serviceName + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX);
207             boolean managed = true;
208             if (managedString != null && !managedString.isEmpty()) {
209                 managed = Boolean.parseBoolean(managedString);
210             }
211
212             String swaggerString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
213                     + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_SWAGGER_SUFFIX);
214             boolean swagger = false;
215             if (swaggerString != null && !swaggerString.isEmpty()) {
216                 swagger = Boolean.parseBoolean(swaggerString);
217             }
218
219             String httpsString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
220                 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX);
221             boolean https = false;
222             if (httpsString != null && !httpsString.isEmpty()) {
223                 https = Boolean.parseBoolean(httpsString);
224             }
225
226             String aafString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
227                 + serviceName + PolicyEndPointProperties.PROPERTY_AAF_SUFFIX);
228             boolean aaf = false;
229             if (aafString != null && !aafString.isEmpty()) {
230                 aaf = Boolean.parseBoolean(httpsString);
231             }
232
233             HttpServletServer service = build(serviceName, https, hostName, servicePort, contextUriPath, swagger, managed);
234             if (userName != null && !userName.isEmpty() && password != null && !password.isEmpty()) {
235                 service.setBasicAuthentication(userName, password, authUriPath);
236             }
237
238             if (restClasses != null && !restClasses.isEmpty()) {
239                 List<String> restClassesList = Arrays.asList(restClasses.split(SPACES_COMMA_SPACES));
240                 for (String restClass : restClassesList) {
241                     service.addServletClass(restUriPath, restClass);
242                 }
243             }
244
245             if (restPackages != null && !restPackages.isEmpty()) {
246                 List<String> restPackageList = Arrays.asList(restPackages.split(SPACES_COMMA_SPACES));
247                 for (String restPackage : restPackageList) {
248                     service.addServletPackage(restUriPath, restPackage);
249                 }
250             }
251
252             if (aaf) {
253                 service.addFilterClass(contextUriPath, CadiFilter.class.getCanonicalName());
254             }
255
256             serviceList.add(service);
257         }
258
259         return serviceList;
260     }
261
262     @Override
263     public synchronized HttpServletServer get(int port) {
264
265         if (servers.containsKey(port)) {
266             return servers.get(port);
267         }
268
269         throw new IllegalArgumentException("Http Server for " + port + " not found");
270     }
271
272     @Override
273     public synchronized List<HttpServletServer> inventory() {
274         return new ArrayList<>(this.servers.values());
275     }
276
277     @Override
278     public synchronized void destroy(int port) {
279
280         if (!servers.containsKey(port)) {
281             return;
282         }
283
284         HttpServletServer server = servers.remove(port);
285         server.shutdown();
286     }
287
288     @Override
289     public synchronized void destroy() {
290         List<HttpServletServer> httpServletServers = this.inventory();
291         for (HttpServletServer server : httpServletServers) {
292             server.shutdown();
293         }
294
295         synchronized (this) {
296             this.servers.clear();
297         }
298     }
299
300 }