9723d808914c91e6e7293936d20e8b470765791b
[policy/common.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * policy-endpoints
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.policy.common.endpoints.http.server.impl;
22
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;
28
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;
35
36 /**
37  * Indexed factory implementation
38  */
39 public class IndexedHttpServletServerFactory implements HttpServletServerFactory {
40
41     private static final HttpServletServerFactory instance = new IndexedHttpServletServerFactory();
42
43     /**
44      * Get the singleton instance.
45      * 
46      * @return the instance
47      */
48     public static HttpServletServerFactory getInstance() {
49         return instance;
50     }
51
52     private IndexedHttpServletServerFactory() {}
53
54     private static final String SPACES_COMMA_SPACES = "\\s*,\\s*";
55
56     /**
57      * logger
58      */
59     protected static Logger logger = LoggerFactory.getLogger(IndexedHttpServletServerFactory.class);
60
61     /**
62      * servers index
63      */
64     protected HashMap<Integer, HttpServletServer> servers = new HashMap<>();
65
66     @Override
67     public synchronized HttpServletServer build(String name, String host, int port, String contextPath, boolean swagger,
68             boolean managed) {
69
70         if (servers.containsKey(port)) {
71             return servers.get(port);
72         }
73
74         JettyJerseyServer server = new JettyJerseyServer(name, host, port, contextPath, swagger);
75         if (managed) {
76             servers.put(port, server);
77         }
78
79         return server;
80     }
81
82     @Override
83     public synchronized List<HttpServletServer> build(Properties properties) {
84
85         ArrayList<HttpServletServer> serviceList = new ArrayList<>();
86
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);
90             return serviceList;
91         }
92
93         List<String> serviceNameList = Arrays.asList(serviceNames.split(SPACES_COMMA_SPACES));
94
95         for (String serviceName : serviceNameList) {
96             String servicePortString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
97                     + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX);
98
99             int servicePort;
100             try {
101                 if (servicePortString == null || servicePortString.isEmpty()) {
102                     if (logger.isWarnEnabled()) {
103                         logger.warn("No HTTP port for service in {}", serviceName);
104                     }
105                     continue;
106                 }
107                 servicePort = Integer.parseInt(servicePortString);
108             } catch (NumberFormatException nfe) {
109                 if (logger.isWarnEnabled()) {
110                     logger.warn("No HTTP port for service in {}", serviceName);
111                 }
112                 continue;
113             }
114
115             String hostName = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + serviceName
116                     + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX);
117
118             String contextUriPath = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
119                     + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_CONTEXT_URIPATH_SUFFIX);
120
121             String userName = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + serviceName
122                     + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX);
123
124             String password = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + serviceName
125                     + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX);
126
127             String authUriPath = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
128                     + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_URIPATH_SUFFIX);
129
130             String restClasses = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
131                     + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX);
132
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);
137
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);
143             }
144
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);
150             }
151
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);
155             }
156
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);
161                 }
162             }
163
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);
168                 }
169             }
170
171             serviceList.add(service);
172         }
173
174         return serviceList;
175     }
176
177     @Override
178     public synchronized HttpServletServer get(int port) {
179
180         if (servers.containsKey(port)) {
181             return servers.get(port);
182         }
183
184         throw new IllegalArgumentException("Http Server for " + port + " not found");
185     }
186
187     @Override
188     public synchronized List<HttpServletServer> inventory() {
189         return new ArrayList<>(this.servers.values());
190     }
191
192     @Override
193     public synchronized void destroy(int port) {
194
195         if (!servers.containsKey(port)) {
196             return;
197         }
198
199         HttpServletServer server = servers.remove(port);
200         server.shutdown();
201     }
202
203     @Override
204     public synchronized void destroy() {
205         List<HttpServletServer> httpServletServers = this.inventory();
206         for (HttpServletServer server : httpServletServers) {
207             server.shutdown();
208         }
209
210         synchronized (this) {
211             this.servers.clear();
212         }
213     }
214
215 }