8d2953b695a3672b2b070e9bee3ce2e78261e17f
[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      * Build a 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 the port the server is bound to
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, 
128                     String host, int port, String contextPath, boolean swagger,
129             boolean managed) {
130
131         if (servers.containsKey(port)) {
132             return servers.get(port);
133         }
134
135         JettyJerseyServer server = new JettyJerseyServer(name, https, host, port, contextPath, swagger);
136         if (managed) {
137             servers.put(port, server);
138         }
139
140         return server;
141     }
142
143     @Override
144     public synchronized HttpServletServer build(String name, String host, int port, String contextPath,
145                                                 boolean swagger, boolean managed) {
146         return build(name, false, host, port, contextPath, swagger, managed);
147     }
148
149
150     @Override
151     public synchronized List<HttpServletServer> build(Properties properties) {
152
153         ArrayList<HttpServletServer> serviceList = new ArrayList<>();
154
155         String serviceNames = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES);
156         if (serviceNames == null || serviceNames.isEmpty()) {
157             logger.warn("No topic for HTTP Service: {}", properties);
158             return serviceList;
159         }
160
161         List<String> serviceNameList = Arrays.asList(serviceNames.split(SPACES_COMMA_SPACES));
162
163         for (String serviceName : serviceNameList) {
164             String servicePortString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES
165                     + "." + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX);
166
167             int servicePort;
168             try {
169                 if (servicePortString == null || servicePortString.isEmpty()) {
170                     if (logger.isWarnEnabled()) {
171                         logger.warn("No HTTP port for service in {}", serviceName);
172                     }
173                     continue;
174                 }
175                 servicePort = Integer.parseInt(servicePortString);
176             } catch (NumberFormatException nfe) {
177                 if (logger.isWarnEnabled()) {
178                     logger.warn("No HTTP port for service in {}", serviceName);
179                 }
180                 continue;
181             }
182
183             String hostName = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
184                     + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX);
185
186             String contextUriPath = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
187                     + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_CONTEXT_URIPATH_SUFFIX);
188
189             String userName = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
190                     + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX);
191
192             String password = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
193                     + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX);
194
195             String authUriPath = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
196                     + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_URIPATH_SUFFIX);
197
198             String restClasses = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
199                     + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX);
200
201             String restPackages = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
202                     + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_REST_PACKAGES_SUFFIX);
203             String restUriPath = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
204                     + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_REST_URIPATH_SUFFIX);
205
206             String managedString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
207                     + serviceName + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX);
208             boolean managed = true;
209             if (managedString != null && !managedString.isEmpty()) {
210                 managed = Boolean.parseBoolean(managedString);
211             }
212
213             String swaggerString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
214                     + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_SWAGGER_SUFFIX);
215             boolean swagger = false;
216             if (swaggerString != null && !swaggerString.isEmpty()) {
217                 swagger = Boolean.parseBoolean(swaggerString);
218             }
219
220             String httpsString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
221                 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX);
222             boolean https = false;
223             if (httpsString != null && !httpsString.isEmpty()) {
224                 https = Boolean.parseBoolean(httpsString);
225             }
226
227             String aafString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
228                 + serviceName + PolicyEndPointProperties.PROPERTY_AAF_SUFFIX);
229             boolean aaf = false;
230             if (aafString != null && !aafString.isEmpty()) {
231                 aaf = Boolean.parseBoolean(httpsString);
232             }
233
234             HttpServletServer service = build(serviceName, https, hostName, servicePort, contextUriPath, swagger, managed);
235             if (userName != null && !userName.isEmpty() && password != null && !password.isEmpty()) {
236                 service.setBasicAuthentication(userName, password, authUriPath);
237             }
238
239             if (restClasses != null && !restClasses.isEmpty()) {
240                 List<String> restClassesList = Arrays.asList(restClasses.split(SPACES_COMMA_SPACES));
241                 for (String restClass : restClassesList) {
242                     service.addServletClass(restUriPath, restClass);
243                 }
244             }
245
246             if (restPackages != null && !restPackages.isEmpty()) {
247                 List<String> restPackageList = Arrays.asList(restPackages.split(SPACES_COMMA_SPACES));
248                 for (String restPackage : restPackageList) {
249                     service.addServletPackage(restUriPath, restPackage);
250                 }
251             }
252
253             if (aaf) {
254                 service.addFilterClass(contextUriPath, CadiFilter.class.getCanonicalName());
255             }
256
257             serviceList.add(service);
258         }
259
260         return serviceList;
261     }
262
263     @Override
264     public synchronized HttpServletServer get(int port) {
265
266         if (servers.containsKey(port)) {
267             return servers.get(port);
268         }
269
270         throw new IllegalArgumentException("Http Server for " + port + " not found");
271     }
272
273     @Override
274     public synchronized List<HttpServletServer> inventory() {
275         return new ArrayList<>(this.servers.values());
276     }
277
278     @Override
279     public synchronized void destroy(int port) {
280
281         if (!servers.containsKey(port)) {
282             return;
283         }
284
285         HttpServletServer server = servers.remove(port);
286         server.shutdown();
287     }
288
289     @Override
290     public synchronized void destroy() {
291         List<HttpServletServer> httpServletServers = this.inventory();
292         for (HttpServletServer server : httpServletServers) {
293             server.shutdown();
294         }
295
296         synchronized (this) {
297             this.servers.clear();
298         }
299     }
300
301 }