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.aaf.cadi.filter.CadiFilter;
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;
35 * Factory of HTTP Servlet-Enabled Servlets.
37 public interface HttpServletServerFactory {
40 * Builds an http or https server with support for servlets.
43 * @param https use secured http over tls connection
44 * @param host binding host
46 * @param contextPath server base path
47 * @param swagger enable swagger documentation
48 * @param managed is it managed by infrastructure
50 * @throws IllegalArgumentException when invalid parameters are provided
52 HttpServletServer build(String name, boolean https, String host, int port, String contextPath, boolean swagger,
56 * Builds an http server with support for servlets.
59 * @param host binding host
61 * @param contextPath server base path
62 * @param swagger enable swagger documentation
63 * @param managed is it managed by infrastructure
65 * @throws IllegalArgumentException when invalid parameters are provided
67 HttpServletServer build(String name, String host, int port, String contextPath, boolean swagger,
71 * Build a list of http servers per properties.
73 * @param properties properties based configuration
74 * @return list of http servers
75 * @throws IllegalArgumentException when invalid parameters are provided
77 List<HttpServletServer> build(Properties properties);
80 * Gets a server based on the port.
85 HttpServletServer get(int port);
88 * Provides an inventory of servers.
90 * @return inventory of servers
92 List<HttpServletServer> inventory();
95 * Destroys server bound to a port.
97 * @param port the port the server is bound to
99 void destroy(int port);
102 * Destroys the factory and therefore all servers.
109 * Indexed factory implementation.
111 class IndexedHttpServletServerFactory implements HttpServletServerFactory {
113 private static final String SPACES_COMMA_SPACES = "\\s*,\\s*";
118 protected static Logger logger = LoggerFactory.getLogger(IndexedHttpServletServerFactory.class);
123 protected HashMap<Integer, HttpServletServer> servers = new HashMap<>();
126 public synchronized HttpServletServer build(String name, boolean https,
127 String host, int port, String contextPath, boolean swagger,
130 if (servers.containsKey(port)) {
131 return servers.get(port);
134 JettyJerseyServer server = new JettyJerseyServer(name, https, host, port, contextPath, swagger);
136 servers.put(port, server);
143 public synchronized HttpServletServer build(String name, String host, int port, String contextPath,
144 boolean swagger, boolean managed) {
145 return build(name, false, host, port, contextPath, swagger, managed);
150 public synchronized List<HttpServletServer> build(Properties properties) {
152 ArrayList<HttpServletServer> serviceList = new ArrayList<>();
154 String serviceNames = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES);
155 if (serviceNames == null || serviceNames.isEmpty()) {
156 logger.warn("No topic for HTTP Service: {}", properties);
160 List<String> serviceNameList = Arrays.asList(serviceNames.split(SPACES_COMMA_SPACES));
162 for (String serviceName : serviceNameList) {
163 String servicePortString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES
164 + "." + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX);
168 if (servicePortString == null || servicePortString.isEmpty()) {
169 if (logger.isWarnEnabled()) {
170 logger.warn("No HTTP port for service in {}", serviceName);
174 servicePort = Integer.parseInt(servicePortString);
175 } catch (NumberFormatException nfe) {
176 if (logger.isWarnEnabled()) {
177 logger.warn("No HTTP port for service in {}", serviceName);
182 final String hostName = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
183 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX);
185 final String contextUriPath = properties.getProperty(
186 PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
187 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_CONTEXT_URIPATH_SUFFIX);
189 final String userName = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
190 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX);
192 final String password = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
193 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX);
195 final String authUriPath = properties.getProperty(
196 PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
197 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_URIPATH_SUFFIX);
199 final String restClasses = properties.getProperty(
200 PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
201 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX);
203 final String filterClasses = properties.getProperty(
204 PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
205 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_FILTER_CLASSES_SUFFIX);
207 final String restPackages = properties.getProperty(
208 PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
209 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_REST_PACKAGES_SUFFIX);
211 final String restUriPath = properties.getProperty(
212 PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
213 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_REST_URIPATH_SUFFIX);
215 final String managedString = properties.getProperty(
216 PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
217 + serviceName + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX);
218 boolean managed = true;
219 if (managedString != null && !managedString.isEmpty()) {
220 managed = Boolean.parseBoolean(managedString);
223 String swaggerString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
224 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_SWAGGER_SUFFIX);
225 boolean swagger = false;
226 if (swaggerString != null && !swaggerString.isEmpty()) {
227 swagger = Boolean.parseBoolean(swaggerString);
230 String httpsString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
231 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX);
232 boolean https = false;
233 if (httpsString != null && !httpsString.isEmpty()) {
234 https = Boolean.parseBoolean(httpsString);
237 String aafString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
238 + serviceName + PolicyEndPointProperties.PROPERTY_AAF_SUFFIX);
240 if (aafString != null && !aafString.isEmpty()) {
241 aaf = Boolean.parseBoolean(aafString);
244 HttpServletServer service = build(serviceName, https, hostName, servicePort,
245 contextUriPath, swagger, managed);
247 /* authentication method either AAF or HTTP Basic Auth */
250 service.setAafAuthentication(contextUriPath);
251 } else if (userName != null && !userName.isEmpty() && password != null && !password.isEmpty()) {
252 service.setBasicAuthentication(userName, password, authUriPath);
255 if (filterClasses != null && !filterClasses.isEmpty()) {
256 List<String> filterClassesList = Arrays.asList(filterClasses.split(SPACES_COMMA_SPACES));
257 for (String filterClass : filterClassesList) {
258 service.addFilterClass(restUriPath, filterClass);
262 if (restClasses != null && !restClasses.isEmpty()) {
263 List<String> restClassesList = Arrays.asList(restClasses.split(SPACES_COMMA_SPACES));
264 for (String restClass : restClassesList) {
265 service.addServletClass(restUriPath, restClass);
269 if (restPackages != null && !restPackages.isEmpty()) {
270 List<String> restPackageList = Arrays.asList(restPackages.split(SPACES_COMMA_SPACES));
271 for (String restPackage : restPackageList) {
272 service.addServletPackage(restUriPath, restPackage);
276 serviceList.add(service);
283 public synchronized HttpServletServer get(int port) {
285 if (servers.containsKey(port)) {
286 return servers.get(port);
289 throw new IllegalArgumentException("Http Server for " + port + " not found");
293 public synchronized List<HttpServletServer> inventory() {
294 return new ArrayList<>(this.servers.values());
298 public synchronized void destroy(int port) {
300 if (!servers.containsKey(port)) {
304 HttpServletServer server = servers.remove(port);
309 public synchronized void destroy() {
310 List<HttpServletServer> httpServletServers = this.inventory();
311 for (HttpServletServer server : httpServletServers) {
315 synchronized (this) {
316 this.servers.clear();