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;
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;
36 * Factory of HTTP Servlet-Enabled Servlets.
38 public interface HttpServletServerFactory {
41 * Builds an http or https server with support for servlets.
44 * @param https use secured http over tls connection
45 * @param host binding host
47 * @param contextPath server base path
48 * @param swagger enable swagger documentation
49 * @param managed is it managed by infrastructure
51 * @throws IllegalArgumentException when invalid parameters are provided
53 HttpServletServer build(String name, boolean https, String host, int port, String contextPath, boolean swagger,
57 * Builds an http server with support for servlets.
60 * @param host binding host
62 * @param contextPath server base path
63 * @param swagger enable swagger documentation
64 * @param managed is it managed by infrastructure
66 * @throws IllegalArgumentException when invalid parameters are provided
68 HttpServletServer build(String name, String host, int port, String contextPath, boolean swagger,
72 * Build a list of http servers per properties.
74 * @param properties properties based configuration
75 * @return list of http servers
76 * @throws IllegalArgumentException when invalid parameters are provided
78 List<HttpServletServer> build(Properties properties);
81 * Gets a server based on the port.
86 HttpServletServer get(int port);
89 * Provides an inventory of servers.
91 * @return inventory of servers
93 List<HttpServletServer> inventory();
96 * Destroys server bound to a port.
98 * @param port the port the server is bound to
100 void destroy(int port);
103 * Destroys the factory and therefore all servers.
110 * Indexed factory implementation.
112 class IndexedHttpServletServerFactory implements HttpServletServerFactory {
114 private static final String SPACES_COMMA_SPACES = "\\s*,\\s*";
119 protected static Logger logger = LoggerFactory.getLogger(IndexedHttpServletServerFactory.class);
124 protected HashMap<Integer, HttpServletServer> servers = new HashMap<>();
127 public synchronized HttpServletServer build(String name, boolean https,
128 String host, int port, String contextPath, boolean swagger,
131 if (servers.containsKey(port)) {
132 return servers.get(port);
135 JettyJerseyServer server = new JettyJerseyServer(name, https, host, port, contextPath, swagger);
137 servers.put(port, server);
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);
151 public synchronized List<HttpServletServer> build(Properties properties) {
153 ArrayList<HttpServletServer> serviceList = new ArrayList<>();
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);
161 List<String> serviceNameList = Arrays.asList(serviceNames.split(SPACES_COMMA_SPACES));
163 for (String serviceName : serviceNameList) {
164 String servicePortString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES
165 + "." + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX);
169 if (servicePortString == null || servicePortString.isEmpty()) {
170 if (logger.isWarnEnabled()) {
171 logger.warn("No HTTP port for service in {}", serviceName);
175 servicePort = Integer.parseInt(servicePortString);
176 } catch (NumberFormatException nfe) {
177 if (logger.isWarnEnabled()) {
178 logger.warn("No HTTP port for service in {}", serviceName);
183 String hostName = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
184 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX);
186 String contextUriPath = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
187 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_CONTEXT_URIPATH_SUFFIX);
189 String userName = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
190 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX);
192 String password = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
193 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX);
195 String authUriPath = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
196 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_URIPATH_SUFFIX);
198 String restClasses = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
199 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX);
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);
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);
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);
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);
227 String aafString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
228 + serviceName + PolicyEndPointProperties.PROPERTY_AAF_SUFFIX);
230 if (aafString != null && !aafString.isEmpty()) {
231 aaf = Boolean.parseBoolean(httpsString);
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);
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);
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);
254 service.addFilterClass(contextUriPath, CadiFilter.class.getCanonicalName());
257 serviceList.add(service);
264 public synchronized HttpServletServer get(int port) {
266 if (servers.containsKey(port)) {
267 return servers.get(port);
270 throw new IllegalArgumentException("Http Server for " + port + " not found");
274 public synchronized List<HttpServletServer> inventory() {
275 return new ArrayList<>(this.servers.values());
279 public synchronized void destroy(int port) {
281 if (!servers.containsKey(port)) {
285 HttpServletServer server = servers.remove(port);
290 public synchronized void destroy() {
291 List<HttpServletServer> httpServletServers = this.inventory();
292 for (HttpServletServer server : httpServletServers) {
296 synchronized (this) {
297 this.servers.clear();