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