2 * ============LICENSE_START=======================================================
3 * ONAP Policy Engine - Common Modules
4 * ================================================================================
5 * Copyright (C) 2017-2019 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 java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Properties;
28 import org.apache.commons.lang3.StringUtils;
29 import org.onap.policy.common.endpoints.http.server.internal.JettyJerseyServer;
30 import org.onap.policy.common.endpoints.http.server.internal.JettyStaticResourceServer;
31 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
32 import org.onap.policy.common.endpoints.utils.PropertyUtils;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * Indexed factory implementation.
39 class IndexedHttpServletServerFactory implements HttpServletServerFactory {
41 private static final String SPACES_COMMA_SPACES = "\\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 JettyJerseyServer 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 : serviceNames.split(SPACES_COMMA_SPACES)) {
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 JettyStaticResourceServer 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 PropertyUtils props = new PropertyUtils(properties, servicePrefix,
115 (name, value, ex) -> logger
116 .warn("{}: {} {} is in invalid format for http service {} ", this, name, value, serviceName));
118 int 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 String hostName = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, null);
125 final String contextUriPath =
126 props.getString(PolicyEndPointProperties.PROPERTY_HTTP_CONTEXT_URIPATH_SUFFIX, null);
127 boolean managed = props.getBoolean(PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, true);
128 boolean swagger = props.getBoolean(PolicyEndPointProperties.PROPERTY_HTTP_SWAGGER_SUFFIX, false);
129 boolean https = props.getBoolean(PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX, false);
131 // create the service
132 HttpServletServer service = build(serviceName, https, hostName, servicePort, contextUriPath, swagger, managed);
134 // configure the service
135 setSerializationProvider(props, service);
136 setAuthentication(props, service, contextUriPath);
138 final String restUriPath = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_REST_URIPATH_SUFFIX, null);
140 addFilterClasses(props, service, restUriPath);
141 addServletClasses(props, service, restUriPath);
142 addServletPackages(props, service, restUriPath);
144 serviceList.add(service);
147 private void setSerializationProvider(PropertyUtils props, HttpServletServer service) {
149 final String classProv = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER, null);
151 if (!StringUtils.isBlank(classProv)) {
152 service.setSerializationProvider(classProv);
156 private void setAuthentication(PropertyUtils props, HttpServletServer service, final String contextUriPath) {
157 /* authentication method either AAF or HTTP Basic Auth */
159 boolean aaf = props.getBoolean(PolicyEndPointProperties.PROPERTY_AAF_SUFFIX, false);
160 final String userName = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX, null);
161 final String password = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX, null);
162 final String authUriPath = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_AUTH_URIPATH_SUFFIX, null);
165 service.setAafAuthentication(contextUriPath);
166 } else if (!StringUtils.isBlank(userName) && !StringUtils.isBlank(password)) {
167 service.setBasicAuthentication(userName, password, authUriPath);
171 private void addFilterClasses(PropertyUtils props, HttpServletServer service, final String restUriPath) {
173 final String filterClasses =
174 props.getString(PolicyEndPointProperties.PROPERTY_HTTP_FILTER_CLASSES_SUFFIX, null);
176 if (!StringUtils.isBlank(filterClasses)) {
177 for (String filterClass : filterClasses.split(SPACES_COMMA_SPACES)) {
178 service.addFilterClass(restUriPath, filterClass);
183 private void addServletClasses(PropertyUtils props, HttpServletServer service, final String restUriPath) {
185 final String restClasses = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX, null);
187 if (!StringUtils.isBlank(restClasses)) {
188 for (String restClass : restClasses.split(SPACES_COMMA_SPACES)) {
189 service.addServletClass(restUriPath, restClass);
194 private void addServletPackages(PropertyUtils props, HttpServletServer service, final String restUriPath) {
196 final String restPackages = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_REST_PACKAGES_SUFFIX, null);
198 if (!StringUtils.isBlank(restPackages)) {
199 for (String restPackage : restPackages.split(SPACES_COMMA_SPACES)) {
200 service.addServletPackage(restUriPath, restPackage);
206 public synchronized HttpServletServer get(int port) {
208 if (servers.containsKey(port)) {
209 return servers.get(port);
212 throw new IllegalArgumentException("Http Server for " + port + " not found");
216 public synchronized List<HttpServletServer> inventory() {
217 return new ArrayList<>(this.servers.values());
221 public synchronized void destroy(int port) {
223 if (!servers.containsKey(port)) {
227 HttpServletServer server = servers.remove(port);
232 public synchronized void destroy() {
233 List<HttpServletServer> httpServletServers = this.inventory();
234 for (HttpServletServer server : httpServletServers) {
238 synchronized (this) {
239 this.servers.clear();