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 * 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
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, 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 String hostName = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
183 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX);
185 String contextUriPath = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
186 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_CONTEXT_URIPATH_SUFFIX);
188 String userName = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
189 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX);
191 String password = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
192 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX);
194 String authUriPath = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
195 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_URIPATH_SUFFIX);
197 String restClasses = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
198 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX);
200 String restPackages = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
201 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_REST_PACKAGES_SUFFIX);
202 String restUriPath = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
203 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_REST_URIPATH_SUFFIX);
205 String managedString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
206 + serviceName + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX);
207 boolean managed = true;
208 if (managedString != null && !managedString.isEmpty()) {
209 managed = Boolean.parseBoolean(managedString);
212 String swaggerString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
213 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_SWAGGER_SUFFIX);
214 boolean swagger = false;
215 if (swaggerString != null && !swaggerString.isEmpty()) {
216 swagger = Boolean.parseBoolean(swaggerString);
219 String httpsString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
220 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX);
221 boolean https = false;
222 if (httpsString != null && !httpsString.isEmpty()) {
223 https = Boolean.parseBoolean(httpsString);
226 String aafString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
227 + serviceName + PolicyEndPointProperties.PROPERTY_AAF_SUFFIX);
229 if (aafString != null && !aafString.isEmpty()) {
230 aaf = Boolean.parseBoolean(httpsString);
233 HttpServletServer service = build(serviceName, https, hostName, servicePort, contextUriPath, swagger, managed);
234 if (userName != null && !userName.isEmpty() && password != null && !password.isEmpty()) {
235 service.setBasicAuthentication(userName, password, authUriPath);
238 if (restClasses != null && !restClasses.isEmpty()) {
239 List<String> restClassesList = Arrays.asList(restClasses.split(SPACES_COMMA_SPACES));
240 for (String restClass : restClassesList) {
241 service.addServletClass(restUriPath, restClass);
245 if (restPackages != null && !restPackages.isEmpty()) {
246 List<String> restPackageList = Arrays.asList(restPackages.split(SPACES_COMMA_SPACES));
247 for (String restPackage : restPackageList) {
248 service.addServletPackage(restUriPath, restPackage);
253 service.addFilterClass(contextUriPath, CadiFilter.class.getCanonicalName());
256 serviceList.add(service);
263 public synchronized HttpServletServer get(int port) {
265 if (servers.containsKey(port)) {
266 return servers.get(port);
269 throw new IllegalArgumentException("Http Server for " + port + " not found");
273 public synchronized List<HttpServletServer> inventory() {
274 return new ArrayList<>(this.servers.values());
278 public synchronized void destroy(int port) {
280 if (!servers.containsKey(port)) {
284 HttpServletServer server = servers.remove(port);
289 public synchronized void destroy() {
290 List<HttpServletServer> httpServletServers = this.inventory();
291 for (HttpServletServer server : httpServletServers) {
295 synchronized (this) {
296 this.servers.clear();