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.HashMap;
 
  25 import java.util.List;
 
  26 import java.util.Properties;
 
  27 import org.apache.commons.lang3.StringUtils;
 
  28 import org.onap.policy.common.endpoints.http.server.internal.JettyJerseyServer;
 
  29 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
 
  30 import org.onap.policy.common.endpoints.utils.PropertyUtils;
 
  31 import org.slf4j.Logger;
 
  32 import org.slf4j.LoggerFactory;
 
  35  * Indexed factory implementation.
 
  37 class IndexedHttpServletServerFactory implements HttpServletServerFactory {
 
  39     private static final String SPACES_COMMA_SPACES = "\\s*,\\s*";
 
  44     protected static Logger logger = LoggerFactory.getLogger(IndexedHttpServletServerFactory.class);
 
  49     protected HashMap<Integer, HttpServletServer> servers = new HashMap<>();
 
  52     public synchronized HttpServletServer build(String name, boolean https, String host, int port, String contextPath,
 
  53         boolean swagger, boolean managed) {
 
  55         if (servers.containsKey(port)) {
 
  56             return servers.get(port);
 
  59         JettyJerseyServer server = new JettyJerseyServer(name, https, host, port, contextPath, swagger);
 
  61             servers.put(port, server);
 
  68     public synchronized HttpServletServer build(String name, String host, int port, String contextPath, boolean swagger,
 
  70         return build(name, false, host, port, contextPath, swagger, managed);
 
  74     public synchronized List<HttpServletServer> build(Properties properties) {
 
  76         ArrayList<HttpServletServer> serviceList = new ArrayList<>();
 
  78         String serviceNames = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES);
 
  79         if (StringUtils.isBlank(serviceNames)) {
 
  80             logger.warn("No topic for HTTP Service: {}", properties);
 
  84         for (String serviceName : serviceNames.split(SPACES_COMMA_SPACES)) {
 
  85             addService(serviceList, serviceName, properties);
 
  91     private void addService(ArrayList<HttpServletServer> serviceList, String serviceName, Properties properties) {
 
  93         String servicePrefix = PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + serviceName;
 
  95         PropertyUtils props = new PropertyUtils(properties, servicePrefix,
 
  96             (name, value, ex) -> logger
 
  97                         .warn("{}: {} {} is in invalid format for http service {} ", this, name, value, serviceName));
 
  99         int servicePort = props.getInteger(PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX, -1);
 
 100         if (servicePort < 0) {
 
 101             logger.warn("No HTTP port for service in {}", serviceName);
 
 105         final String hostName = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, null);
 
 106         final String contextUriPath =
 
 107                         props.getString(PolicyEndPointProperties.PROPERTY_HTTP_CONTEXT_URIPATH_SUFFIX, null);
 
 108         boolean managed = props.getBoolean(PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, true);
 
 109         boolean swagger = props.getBoolean(PolicyEndPointProperties.PROPERTY_HTTP_SWAGGER_SUFFIX, false);
 
 110         boolean https = props.getBoolean(PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX, false);
 
 112         // create the service
 
 113         HttpServletServer service = build(serviceName, https, hostName, servicePort, contextUriPath, swagger, managed);
 
 115         // configure the service
 
 116         setSerializationProvider(props, service);
 
 117         setAuthentication(props, service, contextUriPath);
 
 119         final String restUriPath = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_REST_URIPATH_SUFFIX, null);
 
 121         addFilterClasses(props, service, restUriPath);
 
 122         addServletClasses(props, service, restUriPath);
 
 123         addServletPackages(props, service, restUriPath);
 
 125         serviceList.add(service);
 
 128     private void setSerializationProvider(PropertyUtils props, HttpServletServer service) {
 
 130         final String classProv = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER, null);
 
 132         if (!StringUtils.isBlank(classProv)) {
 
 133             service.setSerializationProvider(classProv);
 
 137     private void setAuthentication(PropertyUtils props, HttpServletServer service, final String contextUriPath) {
 
 138         /* authentication method either AAF or HTTP Basic Auth */
 
 140         boolean aaf = props.getBoolean(PolicyEndPointProperties.PROPERTY_AAF_SUFFIX, false);
 
 141         final String userName = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX, null);
 
 142         final String password = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX, null);
 
 143         final String authUriPath = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_AUTH_URIPATH_SUFFIX, null);
 
 146             service.setAafAuthentication(contextUriPath);
 
 147         } else if (!StringUtils.isBlank(userName) && !StringUtils.isBlank(password)) {
 
 148             service.setBasicAuthentication(userName, password, authUriPath);
 
 152     private void addFilterClasses(PropertyUtils props, HttpServletServer service, final String restUriPath) {
 
 154         final String filterClasses =
 
 155                         props.getString(PolicyEndPointProperties.PROPERTY_HTTP_FILTER_CLASSES_SUFFIX, null);
 
 157         if (!StringUtils.isBlank(filterClasses)) {
 
 158             for (String filterClass : filterClasses.split(SPACES_COMMA_SPACES)) {
 
 159                 service.addFilterClass(restUriPath, filterClass);
 
 164     private void addServletClasses(PropertyUtils props, HttpServletServer service, final String restUriPath) {
 
 166         final String restClasses = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX, null);
 
 168         if (!StringUtils.isBlank(restClasses)) {
 
 169             for (String restClass : restClasses.split(SPACES_COMMA_SPACES)) {
 
 170                 service.addServletClass(restUriPath, restClass);
 
 175     private void addServletPackages(PropertyUtils props, HttpServletServer service, final String restUriPath) {
 
 177         final String restPackages = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_REST_PACKAGES_SUFFIX, null);
 
 179         if (!StringUtils.isBlank(restPackages)) {
 
 180             for (String restPackage : restPackages.split(SPACES_COMMA_SPACES)) {
 
 181                 service.addServletPackage(restUriPath, restPackage);
 
 187     public synchronized HttpServletServer get(int port) {
 
 189         if (servers.containsKey(port)) {
 
 190             return servers.get(port);
 
 193         throw new IllegalArgumentException("Http Server for " + port + " not found");
 
 197     public synchronized List<HttpServletServer> inventory() {
 
 198         return new ArrayList<>(this.servers.values());
 
 202     public synchronized void destroy(int port) {
 
 204         if (!servers.containsKey(port)) {
 
 208         HttpServletServer server = servers.remove(port);
 
 213     public synchronized void destroy() {
 
 214         List<HttpServletServer> httpServletServers = this.inventory();
 
 215         for (HttpServletServer server : httpServletServers) {
 
 219         synchronized (this) {
 
 220             this.servers.clear();