2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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=========================================================
20 package org.openecomp.policy.drools.http.server;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Properties;
28 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
29 import org.openecomp.policy.common.logging.flexlogger.Logger;
30 import org.openecomp.policy.drools.http.server.internal.JettyJerseyServer;
31 import org.openecomp.policy.drools.properties.PolicyProperties;
34 * Factory of HTTP Servlet-Enabled Servlets
36 public interface HttpServletServerFactory {
39 * builds an http server with support for servlets
42 * @param host binding host
44 * @param contextPath server base path
45 * @param swagger enable swagger documentation
46 * @param managed is it managed by infrastructure
48 * @throws IllegalArgumentException when invalid parameters are provided
50 public HttpServletServer build(String name, String host, int port, String contextPath,
51 boolean swagger, boolean managed)
52 throws IllegalArgumentException;
55 * list of http servers per properties
57 * @param properties properties based configuration
58 * @return list of http servers
59 * @throws IllegalArgumentException when invalid parameters are provided
61 public ArrayList<HttpServletServer> build(Properties properties) throws IllegalArgumentException;
64 * gets a server based on the port
69 public HttpServletServer get(int port);
72 * provides an inventory of servers
74 * @return inventory of servers
76 public List<HttpServletServer> inventory();
79 * destroys server bound to a port
82 public void destroy(int port);
85 * destroys the factory and therefore all servers
87 public void destroy();
91 * Indexed factory implementation
93 class IndexedHttpServletServerFactory implements HttpServletServerFactory {
98 protected static Logger logger = FlexLogger.getLogger(IndexedHttpServletServerFactory.class);
103 protected HashMap<Integer, HttpServletServer> servers = new HashMap<Integer, HttpServletServer>();
106 public synchronized HttpServletServer build(String name, String host, int port,
107 String contextPath, boolean swagger,
109 throws IllegalArgumentException {
111 if (servers.containsKey(port))
112 return servers.get(port);
114 JettyJerseyServer server = new JettyJerseyServer(name, host, port, contextPath, swagger);
116 servers.put(port, server);
122 public synchronized ArrayList<HttpServletServer> build(Properties properties)
123 throws IllegalArgumentException {
125 ArrayList<HttpServletServer> serviceList = new ArrayList<HttpServletServer>();
127 String serviceNames = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES);
128 if (serviceNames == null || serviceNames.isEmpty()) {
129 logger.warn("No topic for HTTP Service " + properties);
133 List<String> serviceNameList =
134 new ArrayList<String>(Arrays.asList(serviceNames.split("\\s*,\\s*")));
136 for (String serviceName : serviceNameList) {
137 String servicePortString = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." +
139 PolicyProperties.PROPERTY_HTTP_PORT_SUFFIX);
143 if (servicePortString == null || servicePortString.isEmpty()) {
144 if (logger.isWarnEnabled())
145 logger.warn("No HTTP port for service in " + serviceName);
148 servicePort = Integer.parseInt(servicePortString);
149 } catch (NumberFormatException nfe) {
150 if (logger.isWarnEnabled())
151 logger.warn("No HTTP port for service in " + serviceName);
155 String hostName = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." +
157 PolicyProperties.PROPERTY_HTTP_HOST_SUFFIX);
159 String contextUriPath = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." +
161 PolicyProperties.PROPERTY_HTTP_CONTEXT_URIPATH_SUFFIX);
163 String userName = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." +
165 PolicyProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX);
167 String password = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." +
169 PolicyProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX);
171 String authUriPath = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." +
173 PolicyProperties.PROPERTY_HTTP_AUTH_URIPATH_SUFFIX);
175 String restClasses = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." +
177 PolicyProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX);
179 String restPackages = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." +
181 PolicyProperties.PROPERTY_HTTP_REST_PACKAGES_SUFFIX);
182 String restUriPath = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." +
184 PolicyProperties.PROPERTY_HTTP_REST_URIPATH_SUFFIX);
186 String managedString = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." +
188 PolicyProperties.PROPERTY_MANAGED_SUFFIX);
189 boolean managed = true;
190 if (managedString != null && !managedString.isEmpty()) {
191 managed = Boolean.parseBoolean(managedString);
194 String swaggerString = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." +
196 PolicyProperties.PROPERTY_HTTP_SWAGGER_SUFFIX);
197 boolean swagger = false;
198 if (swaggerString != null && !swaggerString.isEmpty()) {
199 swagger = Boolean.parseBoolean(swaggerString);
202 HttpServletServer service = build(serviceName, hostName, servicePort, contextUriPath, swagger, managed);
203 if (userName != null && !userName.isEmpty() && password != null && !password.isEmpty()) {
204 service.setBasicAuthentication(userName, password, authUriPath);
207 if (restClasses != null && !restClasses.isEmpty()) {
208 List<String> restClassesList =
209 new ArrayList<String>(Arrays.asList(restClasses.split("\\s*,\\s*")));
210 for (String restClass : restClassesList)
211 service.addServletClass(restUriPath, restClass);
214 if (restPackages != null && !restPackages.isEmpty()) {
215 List<String> restPackageList =
216 new ArrayList<String>(Arrays.asList(restPackages.split("\\s*,\\s*")));
217 for (String restPackage : restPackageList)
218 service.addServletPackage(restUriPath, restPackage);
221 serviceList.add(service);
228 public synchronized HttpServletServer get(int port) throws IllegalArgumentException {
230 if (servers.containsKey(port)) {
231 return servers.get(port);
234 throw new IllegalArgumentException("Http Server for " + port + " not found");
238 public synchronized List<HttpServletServer> inventory() {
239 return new ArrayList<HttpServletServer>(this.servers.values());
243 public synchronized void destroy(int port) throws IllegalArgumentException, IllegalStateException {
245 if (!servers.containsKey(port)) {
249 HttpServletServer server = servers.remove(port);
254 public synchronized void destroy() throws IllegalArgumentException, IllegalStateException {
255 List<HttpServletServer> servers = this.inventory();
256 for (HttpServletServer server: servers) {
261 this.servers.clear();