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.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 server with support for servlets
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 public HttpServletServer build(String name, String host, int port, String contextPath, boolean swagger,
55 * list of http servers per properties
57 * @param properties properties based configuration
58 * @return list of http servers
59 * @throws IllegalArgumentException when invalid parameters are provided
61 public List<HttpServletServer> build(Properties properties);
64 * gets a server based on the port
69 public HttpServletServer get(int port);
72 * provides an inventory of servers
74 * @return inventory of servers
76 public List<HttpServletServer> inventory();
79 * destroys server bound to a port
83 public void destroy(int port);
86 * destroys the factory and therefore all servers
88 public void destroy();
93 * Indexed factory implementation
95 class IndexedHttpServletServerFactory implements HttpServletServerFactory {
97 private static final String SPACES_COMMA_SPACES = "\\s*,\\s*";
102 protected static Logger logger = LoggerFactory.getLogger(IndexedHttpServletServerFactory.class);
107 protected HashMap<Integer, HttpServletServer> servers = new HashMap<>();
110 public synchronized HttpServletServer build(String name, String host, int port, String contextPath, boolean swagger,
113 if (servers.containsKey(port)) {
114 return servers.get(port);
117 JettyJerseyServer server = new JettyJerseyServer(name, host, port, contextPath, swagger);
119 servers.put(port, server);
126 public synchronized List<HttpServletServer> build(Properties properties) {
128 ArrayList<HttpServletServer> serviceList = new ArrayList<>();
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);
136 List<String> serviceNameList = Arrays.asList(serviceNames.split(SPACES_COMMA_SPACES));
138 for (String serviceName : serviceNameList) {
139 String servicePortString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES
140 + "." + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX);
144 if (servicePortString == null || servicePortString.isEmpty()) {
145 if (logger.isWarnEnabled()) {
146 logger.warn("No HTTP port for service in {}", serviceName);
150 servicePort = Integer.parseInt(servicePortString);
151 } catch (NumberFormatException nfe) {
152 if (logger.isWarnEnabled()) {
153 logger.warn("No HTTP port for service in {}", serviceName);
158 String hostName = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
159 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX);
161 String contextUriPath = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
162 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_CONTEXT_URIPATH_SUFFIX);
164 String userName = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
165 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX);
167 String password = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
168 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX);
170 String authUriPath = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
171 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_URIPATH_SUFFIX);
173 String restClasses = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
174 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX);
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);
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);
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);
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);
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);
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);
214 serviceList.add(service);
221 public synchronized HttpServletServer get(int port) {
223 if (servers.containsKey(port)) {
224 return servers.get(port);
227 throw new IllegalArgumentException("Http Server for " + port + " not found");
231 public synchronized List<HttpServletServer> inventory() {
232 return new ArrayList<>(this.servers.values());
236 public synchronized void destroy(int port) {
238 if (!servers.containsKey(port)) {
242 HttpServletServer server = servers.remove(port);
247 public synchronized void destroy() {
248 List<HttpServletServer> httpServletServers = this.inventory();
249 for (HttpServletServer server : httpServletServers) {
253 synchronized (this) {
254 this.servers.clear();