2 * ============LICENSE_START=======================================================
3 * ONAP Policy Engine - Common Modules
4 * ================================================================================
5 * Copyright (C) 2017-2019 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 * Indexed factory implementation.
36 class IndexedHttpServletServerFactory implements HttpServletServerFactory {
38 private static final String SPACES_COMMA_SPACES = "\\s*,\\s*";
43 protected static Logger logger = LoggerFactory.getLogger(IndexedHttpServletServerFactory.class);
48 protected HashMap<Integer, HttpServletServer> servers = new HashMap<>();
51 public synchronized HttpServletServer build(String name, boolean https, String host, int port, String contextPath,
52 boolean swagger, boolean managed) {
54 if (servers.containsKey(port)) {
55 return servers.get(port);
58 JettyJerseyServer server = new JettyJerseyServer(name, https, host, port, contextPath, swagger);
60 servers.put(port, server);
67 public synchronized HttpServletServer build(String name, String host, int port, String contextPath, boolean swagger,
69 return build(name, false, host, port, contextPath, swagger, managed);
73 public synchronized List<HttpServletServer> build(Properties properties) {
75 ArrayList<HttpServletServer> serviceList = new ArrayList<>();
77 String serviceNames = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES);
78 if (serviceNames == null || serviceNames.isEmpty()) {
79 logger.warn("No topic for HTTP Service: {}", properties);
83 List<String> serviceNameList = Arrays.asList(serviceNames.split(SPACES_COMMA_SPACES));
85 for (String serviceName : serviceNameList) {
86 String servicePortString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES
87 + "." + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX);
91 if (servicePortString == null || servicePortString.isEmpty()) {
92 if (logger.isWarnEnabled()) {
93 logger.warn("No HTTP port for service in {}", serviceName);
97 servicePort = Integer.parseInt(servicePortString);
98 } catch (NumberFormatException nfe) {
99 if (logger.isWarnEnabled()) {
100 logger.warn("No HTTP port for service in {}", serviceName);
105 final String hostName = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
106 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX);
108 final String contextUriPath = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES
109 + "." + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_CONTEXT_URIPATH_SUFFIX);
111 final String userName = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
112 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX);
114 final String password = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
115 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX);
117 final String authUriPath = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES
118 + "." + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_URIPATH_SUFFIX);
120 final String restClasses = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES
121 + "." + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX);
123 final String filterClasses = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES
124 + "." + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_FILTER_CLASSES_SUFFIX);
126 final String restPackages = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES
127 + "." + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_REST_PACKAGES_SUFFIX);
129 final String restUriPath = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES
130 + "." + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_REST_URIPATH_SUFFIX);
132 final String managedString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES
133 + "." + serviceName + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX);
134 boolean managed = true;
135 if (managedString != null && !managedString.isEmpty()) {
136 managed = Boolean.parseBoolean(managedString);
139 String swaggerString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
140 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_SWAGGER_SUFFIX);
141 boolean swagger = false;
142 if (swaggerString != null && !swaggerString.isEmpty()) {
143 swagger = Boolean.parseBoolean(swaggerString);
146 String httpsString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
147 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX);
148 boolean https = false;
149 if (httpsString != null && !httpsString.isEmpty()) {
150 https = Boolean.parseBoolean(httpsString);
153 String aafString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
154 + serviceName + PolicyEndPointProperties.PROPERTY_AAF_SUFFIX);
156 if (aafString != null && !aafString.isEmpty()) {
157 aaf = Boolean.parseBoolean(aafString);
160 HttpServletServer service = build(serviceName, https, hostName, servicePort, contextUriPath, swagger,
163 /* authentication method either AAF or HTTP Basic Auth */
166 service.setAafAuthentication(contextUriPath);
167 } else if (userName != null && !userName.isEmpty() && password != null && !password.isEmpty()) {
168 service.setBasicAuthentication(userName, password, authUriPath);
171 if (filterClasses != null && !filterClasses.isEmpty()) {
172 List<String> filterClassesList = Arrays.asList(filterClasses.split(SPACES_COMMA_SPACES));
173 for (String filterClass : filterClassesList) {
174 service.addFilterClass(restUriPath, filterClass);
178 if (restClasses != null && !restClasses.isEmpty()) {
179 List<String> restClassesList = Arrays.asList(restClasses.split(SPACES_COMMA_SPACES));
180 for (String restClass : restClassesList) {
181 service.addServletClass(restUriPath, restClass);
185 if (restPackages != null && !restPackages.isEmpty()) {
186 List<String> restPackageList = Arrays.asList(restPackages.split(SPACES_COMMA_SPACES));
187 for (String restPackage : restPackageList) {
188 service.addServletPackage(restUriPath, restPackage);
192 serviceList.add(service);
199 public synchronized HttpServletServer get(int port) {
201 if (servers.containsKey(port)) {
202 return servers.get(port);
205 throw new IllegalArgumentException("Http Server for " + port + " not found");
209 public synchronized List<HttpServletServer> inventory() {
210 return new ArrayList<>(this.servers.values());
214 public synchronized void destroy(int port) {
216 if (!servers.containsKey(port)) {
220 HttpServletServer server = servers.remove(port);
225 public synchronized void destroy() {
226 List<HttpServletServer> httpServletServers = this.inventory();
227 for (HttpServletServer server : httpServletServers) {
231 synchronized (this) {
232 this.servers.clear();