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 * Jetty Server Factory
36 public interface HttpServletServerFactory {
38 public HttpServletServer build(String name, String host, int port, String contextPath, boolean managed)
39 throws IllegalArgumentException;
41 public ArrayList<HttpServletServer> build(Properties properties) throws IllegalArgumentException;
43 public HttpServletServer get(int port);
44 public List<HttpServletServer> inventory();
45 public void destroy(int port);
46 public void destroy();
49 class IndexedHttpServletServerFactory implements HttpServletServerFactory {
51 protected static Logger logger = FlexLogger.getLogger(IndexedHttpServletServerFactory.class);
53 protected HashMap<Integer, JettyJerseyServer> servers = new HashMap<Integer, JettyJerseyServer>();
56 public synchronized HttpServletServer build(String name, String host, int port,
57 String contextPath, boolean managed)
58 throws IllegalArgumentException {
60 if (servers.containsKey(port))
61 return servers.get(port);
63 JettyJerseyServer server = new JettyJerseyServer(name, host, port, contextPath);
65 servers.put(port, server);
71 public synchronized ArrayList<HttpServletServer> build(Properties properties)
72 throws IllegalArgumentException {
74 ArrayList<HttpServletServer> serviceList = new ArrayList<HttpServletServer>();
76 String serviceNames = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES);
77 if (serviceNames == null || serviceNames.isEmpty()) {
78 logger.warn("No topic for HTTP Service " + properties);
82 List<String> serviceNameList =
83 new ArrayList<String>(Arrays.asList(serviceNames.split("\\s*,\\s*")));
85 for (String serviceName : serviceNameList) {
86 String servicePortString = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." +
88 PolicyProperties.PROPERTY_HTTP_PORT_SUFFIX);
92 if (servicePortString == null || servicePortString.isEmpty()) {
93 if (logger.isWarnEnabled())
94 logger.warn("No HTTP port for service in " + serviceName);
97 servicePort = Integer.parseInt(servicePortString);
98 } catch (NumberFormatException nfe) {
99 if (logger.isWarnEnabled())
100 logger.warn("No HTTP port for service in " + serviceName);
104 String hostName = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." +
106 PolicyProperties.PROPERTY_HTTP_HOST_SUFFIX);
108 String contextUriPath = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." +
110 PolicyProperties.PROPERTY_HTTP_CONTEXT_URIPATH_SUFFIX);
112 String userName = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." +
114 PolicyProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX);
116 String password = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." +
118 PolicyProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX);
120 String authUriPath = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." +
122 PolicyProperties.PROPERTY_HTTP_AUTH_URIPATH_SUFFIX);
124 String restClasses = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." +
126 PolicyProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX);
128 String restPackages = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." +
130 PolicyProperties.PROPERTY_HTTP_REST_PACKAGES_SUFFIX);
131 String restUriPath = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." +
133 PolicyProperties.PROPERTY_HTTP_REST_URIPATH_SUFFIX);
135 String managedString = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." +
137 PolicyProperties.PROPERTY_MANAGED_SUFFIX);
138 boolean managed = true;
139 if (managedString != null && !managedString.isEmpty()) {
140 managed = Boolean.parseBoolean(managedString);
143 HttpServletServer service = build(serviceName, hostName, servicePort, contextUriPath, managed);
144 if (userName != null && !userName.isEmpty() && password != null && !password.isEmpty()) {
145 service.setBasicAuthentication(userName, password, authUriPath);
148 if (restClasses != null && !restClasses.isEmpty()) {
149 List<String> restClassesList =
150 new ArrayList<String>(Arrays.asList(restClasses.split("\\s*,\\s*")));
151 for (String restClass : restClassesList)
152 service.addServletClass(restUriPath, restClass);
155 if (restPackages != null && !restPackages.isEmpty()) {
156 List<String> restPackageList =
157 new ArrayList<String>(Arrays.asList(restPackages.split("\\s*,\\s*")));
158 for (String restPackage : restPackageList)
159 service.addServletPackage(restUriPath, restPackage);
162 serviceList.add(service);
169 public synchronized HttpServletServer get(int port) throws IllegalArgumentException {
171 if (servers.containsKey(port)) {
172 return servers.get(port);
175 throw new IllegalArgumentException("Http Server for " + port + " not found");
179 public synchronized List<HttpServletServer> inventory() {
180 return new ArrayList<HttpServletServer>(this.servers.values());
184 public synchronized void destroy(int port) throws IllegalArgumentException, IllegalStateException {
186 if (!servers.containsKey(port)) {
190 HttpServletServer server = servers.remove(port);
195 public synchronized void destroy() throws IllegalArgumentException, IllegalStateException {
196 List<HttpServletServer> servers = this.inventory();
197 for (HttpServletServer server: servers) {
202 this.servers.clear();