2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.common.endpoints.http.server;
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;
34 * Factory of HTTP Servlet-Enabled Servlets.
36 public interface HttpServletServerFactory {
39 * Builds an http or https server with support for servlets.
42 * @param https use secured http over tls connection
43 * @param host binding host
45 * @param contextPath server base path
46 * @param swagger enable swagger documentation
47 * @param managed is it managed by infrastructure
49 * @throws IllegalArgumentException when invalid parameters are provided
51 HttpServletServer build(String name, boolean https, String host, int port, String contextPath, boolean swagger,
55 * Builds an http server with support for servlets.
58 * @param host binding host
60 * @param contextPath server base path
61 * @param swagger enable swagger documentation
62 * @param managed is it managed by infrastructure
64 * @throws IllegalArgumentException when invalid parameters are provided
66 HttpServletServer build(String name, String host, int port, String contextPath, boolean swagger, boolean managed);
69 * Build a list of http servers per properties.
71 * @param properties properties based configuration
72 * @return list of http servers
73 * @throws IllegalArgumentException when invalid parameters are provided
75 List<HttpServletServer> build(Properties properties);
78 * Gets a server based on the port.
83 HttpServletServer get(int port);
86 * Provides an inventory of servers.
88 * @return inventory of servers
90 List<HttpServletServer> inventory();
93 * Destroys server bound to a port.
95 * @param port the port the server is bound to
97 void destroy(int port);
100 * Destroys the factory and therefore all servers.
106 * Indexed factory implementation.
108 class IndexedHttpServletServerFactory implements HttpServletServerFactory {
110 private static final String SPACES_COMMA_SPACES = "\\s*,\\s*";
115 protected static Logger logger = LoggerFactory.getLogger(IndexedHttpServletServerFactory.class);
120 protected HashMap<Integer, HttpServletServer> servers = new HashMap<>();
123 public synchronized HttpServletServer build(String name, boolean https, String host, int port, String contextPath,
124 boolean swagger, boolean managed) {
126 if (servers.containsKey(port)) {
127 return servers.get(port);
130 JettyJerseyServer server = new JettyJerseyServer(name, https, host, port, contextPath, swagger);
132 servers.put(port, server);
139 public synchronized HttpServletServer build(String name, String host, int port, String contextPath, boolean swagger,
141 return build(name, false, host, port, contextPath, swagger, managed);
145 public synchronized List<HttpServletServer> build(Properties properties) {
147 ArrayList<HttpServletServer> serviceList = new ArrayList<>();
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);
155 List<String> serviceNameList = Arrays.asList(serviceNames.split(SPACES_COMMA_SPACES));
157 for (String serviceName : serviceNameList) {
158 String servicePortString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES
159 + "." + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX);
163 if (servicePortString == null || servicePortString.isEmpty()) {
164 if (logger.isWarnEnabled()) {
165 logger.warn("No HTTP port for service in {}", serviceName);
169 servicePort = Integer.parseInt(servicePortString);
170 } catch (NumberFormatException nfe) {
171 if (logger.isWarnEnabled()) {
172 logger.warn("No HTTP port for service in {}", serviceName);
177 final String hostName = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
178 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX);
180 final String contextUriPath = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES
181 + "." + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_CONTEXT_URIPATH_SUFFIX);
183 final String userName = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
184 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX);
186 final String password = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
187 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX);
189 final String authUriPath = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES
190 + "." + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_URIPATH_SUFFIX);
192 final String restClasses = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES
193 + "." + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX);
195 final String filterClasses = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES
196 + "." + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_FILTER_CLASSES_SUFFIX);
198 final String restPackages = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES
199 + "." + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_REST_PACKAGES_SUFFIX);
201 final String restUriPath = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES
202 + "." + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_REST_URIPATH_SUFFIX);
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);
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);
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);
225 String aafString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
226 + serviceName + PolicyEndPointProperties.PROPERTY_AAF_SUFFIX);
228 if (aafString != null && !aafString.isEmpty()) {
229 aaf = Boolean.parseBoolean(aafString);
232 HttpServletServer service = build(serviceName, https, hostName, servicePort, contextUriPath, swagger,
235 /* authentication method either AAF or HTTP Basic Auth */
238 service.setAafAuthentication(contextUriPath);
239 } else if (userName != null && !userName.isEmpty() && password != null && !password.isEmpty()) {
240 service.setBasicAuthentication(userName, password, authUriPath);
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);
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);
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);
264 serviceList.add(service);
271 public synchronized HttpServletServer get(int port) {
273 if (servers.containsKey(port)) {
274 return servers.get(port);
277 throw new IllegalArgumentException("Http Server for " + port + " not found");
281 public synchronized List<HttpServletServer> inventory() {
282 return new ArrayList<>(this.servers.values());
286 public synchronized void destroy(int port) {
288 if (!servers.containsKey(port)) {
292 HttpServletServer server = servers.remove(port);
297 public synchronized void destroy() {
298 List<HttpServletServer> httpServletServers = this.inventory();
299 for (HttpServletServer server : httpServletServers) {
303 synchronized (this) {
304 this.servers.clear();