2 * ============LICENSE_START=======================================================
3 * ONAP Policy Engine - Common Modules
4 * ================================================================================
5 * Copyright (C) 2017-2019, 2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2020 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.common.endpoints.http.server;
24 import com.google.re2j.Pattern;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Properties;
29 import org.apache.commons.lang3.StringUtils;
30 import org.onap.policy.common.endpoints.http.server.internal.JettyJerseyServer;
31 import org.onap.policy.common.endpoints.http.server.internal.JettyStaticResourceServer;
32 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
33 import org.onap.policy.common.endpoints.utils.PropertyUtils;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * Indexed factory implementation.
40 class IndexedHttpServletServerFactory implements HttpServletServerFactory {
41 private static final Pattern COMMA_SPACE_PAT = Pattern.compile("\\s*,\\s*");
46 protected static Logger logger = LoggerFactory.getLogger(IndexedHttpServletServerFactory.class);
51 protected HashMap<Integer, HttpServletServer> servers = new HashMap<>();
54 public synchronized HttpServletServer build(String name, boolean https, String host, int port, String contextPath,
55 boolean swagger, boolean managed) {
57 if (servers.containsKey(port)) {
58 return servers.get(port);
61 var server = new JettyJerseyServer(name, https, host, port, contextPath, swagger);
63 servers.put(port, server);
70 public synchronized HttpServletServer build(String name, String host, int port, String contextPath, boolean swagger,
72 return build(name, false, host, port, contextPath, swagger, managed);
76 public synchronized List<HttpServletServer> build(Properties properties) {
78 ArrayList<HttpServletServer> serviceList = new ArrayList<>();
80 String serviceNames = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES);
81 if (StringUtils.isBlank(serviceNames)) {
82 logger.warn("No topic for HTTP Service: {}", properties);
86 for (String serviceName : COMMA_SPACE_PAT.split(serviceNames)) {
87 addService(serviceList, serviceName, properties);
96 public HttpServletServer buildStaticResourceServer(String name, boolean https, String host, int port,
97 String contextPath, boolean managed) {
98 if (servers.containsKey(port)) {
99 return servers.get(port);
102 var server = new JettyStaticResourceServer(name, https, host, port, contextPath);
104 servers.put(port, server);
110 private void addService(ArrayList<HttpServletServer> serviceList, String serviceName, Properties properties) {
112 String servicePrefix = PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + serviceName;
114 var props = new PropertyUtils(properties, servicePrefix,
115 (name, value, ex) -> logger
116 .warn("{}: {} {} is in invalid format for http service {} ", this, name, value, serviceName));
118 var servicePort = props.getInteger(PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX, -1);
119 if (servicePort < 0) {
120 logger.warn("No HTTP port for service in {}", serviceName);
124 final var hostName = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, null);
125 final var contextUriPath = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_CONTEXT_URIPATH_SUFFIX, null);
126 var managed = props.getBoolean(PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, true);
127 var swagger = props.getBoolean(PolicyEndPointProperties.PROPERTY_HTTP_SWAGGER_SUFFIX, false);
128 var https = props.getBoolean(PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX, false);
130 // create the service
131 HttpServletServer service = build(serviceName, https, hostName, servicePort, contextUriPath, swagger, managed);
133 // configure the service
134 setSerializationProvider(props, service);
135 setAuthentication(props, service, contextUriPath);
137 final var restUriPath = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_REST_URIPATH_SUFFIX, null);
139 addFilterClasses(props, service, restUriPath);
140 addServletClasses(props, service, restUriPath);
141 addServletPackages(props, service, restUriPath);
143 serviceList.add(service);
146 private void setSerializationProvider(PropertyUtils props, HttpServletServer service) {
148 final var classProv = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER, null);
150 if (!StringUtils.isBlank(classProv)) {
151 service.setSerializationProvider(classProv);
155 private void setAuthentication(PropertyUtils props, HttpServletServer service, final String contextUriPath) {
156 /* authentication method either AAF or HTTP Basic Auth */
158 final var aaf = props.getBoolean(PolicyEndPointProperties.PROPERTY_AAF_SUFFIX, false);
159 final var userName = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX, null);
160 final var password = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX, null);
161 final var authUriPath = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_AUTH_URIPATH_SUFFIX, null);
164 service.setAafAuthentication(contextUriPath);
165 } else if (!StringUtils.isBlank(userName) && !StringUtils.isBlank(password)) {
166 service.setBasicAuthentication(userName, password, authUriPath);
170 private void addFilterClasses(PropertyUtils props, HttpServletServer service, final String restUriPath) {
172 final var filterClasses =
173 props.getString(PolicyEndPointProperties.PROPERTY_HTTP_FILTER_CLASSES_SUFFIX, null);
175 if (!StringUtils.isBlank(filterClasses)) {
176 for (String filterClass : COMMA_SPACE_PAT.split(filterClasses)) {
177 service.addFilterClass(restUriPath, filterClass);
182 private void addServletClasses(PropertyUtils props, HttpServletServer service, final String restUriPath) {
184 final var restClasses = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX, null);
186 if (!StringUtils.isBlank(restClasses)) {
187 for (String restClass : COMMA_SPACE_PAT.split(restClasses)) {
188 service.addServletClass(restUriPath, restClass);
193 private void addServletPackages(PropertyUtils props, HttpServletServer service, final String restUriPath) {
195 final var restPackages = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_REST_PACKAGES_SUFFIX, null);
197 if (!StringUtils.isBlank(restPackages)) {
198 for (String restPackage : COMMA_SPACE_PAT.split(restPackages)) {
199 service.addServletPackage(restUriPath, restPackage);
205 public synchronized HttpServletServer get(int port) {
207 if (servers.containsKey(port)) {
208 return servers.get(port);
211 throw new IllegalArgumentException("Http Server for " + port + " not found");
215 public synchronized List<HttpServletServer> inventory() {
216 return new ArrayList<>(this.servers.values());
220 public synchronized void destroy(int port) {
222 if (!servers.containsKey(port)) {
226 HttpServletServer server = servers.remove(port);
231 public synchronized void destroy() {
232 List<HttpServletServer> httpServletServers = this.inventory();
233 for (HttpServletServer server : httpServletServers) {
237 synchronized (this) {
238 this.servers.clear();