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.impl;
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.HttpServletServer;
30 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactory;
31 import org.onap.policy.common.endpoints.http.server.internal.JettyJerseyServer;
32 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * Indexed factory implementation
39 public class IndexedHttpServletServerFactory implements HttpServletServerFactory {
41 private static final HttpServletServerFactory instance = new IndexedHttpServletServerFactory();
44 * Get the singleton instance.
46 * @return the instance
48 public static HttpServletServerFactory getInstance() {
52 private IndexedHttpServletServerFactory() {}
54 private static final String SPACES_COMMA_SPACES = "\\s*,\\s*";
59 protected static Logger logger = LoggerFactory.getLogger(IndexedHttpServletServerFactory.class);
64 protected HashMap<Integer, HttpServletServer> servers = new HashMap<>();
67 public synchronized HttpServletServer build(String name, String host, int port, String contextPath, boolean swagger,
70 if (servers.containsKey(port)) {
71 return servers.get(port);
74 JettyJerseyServer server = new JettyJerseyServer(name, host, port, contextPath, swagger);
76 servers.put(port, server);
83 public synchronized List<HttpServletServer> build(Properties properties) {
85 ArrayList<HttpServletServer> serviceList = new ArrayList<>();
87 String serviceNames = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES);
88 if (serviceNames == null || serviceNames.isEmpty()) {
89 logger.warn("No topic for HTTP Service: {}", properties);
93 List<String> serviceNameList = Arrays.asList(serviceNames.split(SPACES_COMMA_SPACES));
95 for (String serviceName : serviceNameList) {
96 String servicePortString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
97 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX);
101 if (servicePortString == null || servicePortString.isEmpty()) {
102 if (logger.isWarnEnabled()) {
103 logger.warn("No HTTP port for service in {}", serviceName);
107 servicePort = Integer.parseInt(servicePortString);
108 } catch (NumberFormatException nfe) {
109 if (logger.isWarnEnabled()) {
110 logger.warn("No HTTP port for service in {}", serviceName);
115 String hostName = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + serviceName
116 + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX);
118 String contextUriPath = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
119 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_CONTEXT_URIPATH_SUFFIX);
121 String userName = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + serviceName
122 + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX);
124 String password = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + serviceName
125 + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX);
127 String authUriPath = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
128 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_URIPATH_SUFFIX);
130 String restClasses = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
131 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX);
133 String restPackages = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
134 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_REST_PACKAGES_SUFFIX);
135 String restUriPath = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
136 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_REST_URIPATH_SUFFIX);
138 String managedString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
139 + serviceName + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX);
140 boolean managed = true;
141 if (managedString != null && !managedString.isEmpty()) {
142 managed = Boolean.parseBoolean(managedString);
145 String swaggerString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
146 + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_SWAGGER_SUFFIX);
147 boolean swagger = false;
148 if (swaggerString != null && !swaggerString.isEmpty()) {
149 swagger = Boolean.parseBoolean(swaggerString);
152 HttpServletServer service = build(serviceName, hostName, servicePort, contextUriPath, swagger, managed);
153 if (userName != null && !userName.isEmpty() && password != null && !password.isEmpty()) {
154 service.setBasicAuthentication(userName, password, authUriPath);
157 if (restClasses != null && !restClasses.isEmpty()) {
158 List<String> restClassesList = Arrays.asList(restClasses.split(SPACES_COMMA_SPACES));
159 for (String restClass : restClassesList) {
160 service.addServletClass(restUriPath, restClass);
164 if (restPackages != null && !restPackages.isEmpty()) {
165 List<String> restPackageList = Arrays.asList(restPackages.split(SPACES_COMMA_SPACES));
166 for (String restPackage : restPackageList) {
167 service.addServletPackage(restUriPath, restPackage);
171 serviceList.add(service);
178 public synchronized HttpServletServer get(int port) {
180 if (servers.containsKey(port)) {
181 return servers.get(port);
184 throw new IllegalArgumentException("Http Server for " + port + " not found");
188 public synchronized List<HttpServletServer> inventory() {
189 return new ArrayList<>(this.servers.values());
193 public synchronized void destroy(int port) {
195 if (!servers.containsKey(port)) {
199 HttpServletServer server = servers.remove(port);
204 public synchronized void destroy() {
205 List<HttpServletServer> httpServletServers = this.inventory();
206 for (HttpServletServer server : httpServletServers) {
210 synchronized (this) {
211 this.servers.clear();