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