445a35156a6b8fcd0b5ea61c8420a5c124de49c0
[policy/common.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine - Common Modules
4  * ================================================================================
5  * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2020 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.common.endpoints.http.server;
23
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Properties;
28 import org.apache.commons.lang3.StringUtils;
29 import org.onap.policy.common.endpoints.http.server.internal.JettyJerseyServer;
30 import org.onap.policy.common.endpoints.http.server.internal.JettyStaticResourceServer;
31 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
32 import org.onap.policy.common.endpoints.utils.PropertyUtils;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * Indexed factory implementation.
38  */
39 class IndexedHttpServletServerFactory implements HttpServletServerFactory {
40
41     private static final String SPACES_COMMA_SPACES = "\\s*,\\s*";
42
43     /**
44      * logger.
45      */
46     protected static Logger logger = LoggerFactory.getLogger(IndexedHttpServletServerFactory.class);
47
48     /**
49      * servers index.
50      */
51     protected HashMap<Integer, HttpServletServer> servers = new HashMap<>();
52
53     @Override
54     public synchronized HttpServletServer build(String name, boolean https, String host, int port, String contextPath,
55         boolean swagger, boolean managed) {
56
57         if (servers.containsKey(port)) {
58             return servers.get(port);
59         }
60
61         JettyJerseyServer server = new JettyJerseyServer(name, https, host, port, contextPath, swagger);
62         if (managed) {
63             servers.put(port, server);
64         }
65
66         return server;
67     }
68
69     @Override
70     public synchronized HttpServletServer build(String name, String host, int port, String contextPath, boolean swagger,
71         boolean managed) {
72         return build(name, false, host, port, contextPath, swagger, managed);
73     }
74
75     @Override
76     public synchronized List<HttpServletServer> build(Properties properties) {
77
78         ArrayList<HttpServletServer> serviceList = new ArrayList<>();
79
80         String serviceNames = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES);
81         if (StringUtils.isBlank(serviceNames)) {
82             logger.warn("No topic for HTTP Service: {}", properties);
83             return serviceList;
84         }
85
86         for (String serviceName : serviceNames.split(SPACES_COMMA_SPACES)) {
87             addService(serviceList, serviceName, properties);
88         }
89
90         return serviceList;
91     }
92
93
94
95     @Override
96     public HttpServletServer buildStaticResourceServer(String name, boolean https, String host, int port,
97             String contextPath, boolean managed) {
98         if (servers.containsKey(port)) {
99             return servers.get(port);
100         }
101
102         JettyStaticResourceServer server = new JettyStaticResourceServer(name, https, host, port, contextPath);
103         if (managed) {
104             servers.put(port, server);
105         }
106
107         return server;
108     }
109
110     private void addService(ArrayList<HttpServletServer> serviceList, String serviceName, Properties properties) {
111
112         String servicePrefix = PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + serviceName;
113
114         PropertyUtils props = new PropertyUtils(properties, servicePrefix,
115             (name, value, ex) -> logger
116                         .warn("{}: {} {} is in invalid format for http service {} ", this, name, value, serviceName));
117
118         int servicePort = props.getInteger(PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX, -1);
119         if (servicePort < 0) {
120             logger.warn("No HTTP port for service in {}", serviceName);
121             return;
122         }
123
124         final String hostName = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, null);
125         final String contextUriPath =
126                         props.getString(PolicyEndPointProperties.PROPERTY_HTTP_CONTEXT_URIPATH_SUFFIX, null);
127         boolean managed = props.getBoolean(PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, true);
128         boolean swagger = props.getBoolean(PolicyEndPointProperties.PROPERTY_HTTP_SWAGGER_SUFFIX, false);
129         boolean https = props.getBoolean(PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX, false);
130
131         // create the service
132         HttpServletServer service = build(serviceName, https, hostName, servicePort, contextUriPath, swagger, managed);
133
134         // configure the service
135         setSerializationProvider(props, service);
136         setAuthentication(props, service, contextUriPath);
137
138         final String restUriPath = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_REST_URIPATH_SUFFIX, null);
139
140         addFilterClasses(props, service, restUriPath);
141         addServletClasses(props, service, restUriPath);
142         addServletPackages(props, service, restUriPath);
143
144         serviceList.add(service);
145     }
146
147     private void setSerializationProvider(PropertyUtils props, HttpServletServer service) {
148
149         final String classProv = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER, null);
150
151         if (!StringUtils.isBlank(classProv)) {
152             service.setSerializationProvider(classProv);
153         }
154     }
155
156     private void setAuthentication(PropertyUtils props, HttpServletServer service, final String contextUriPath) {
157         /* authentication method either AAF or HTTP Basic Auth */
158
159         boolean aaf = props.getBoolean(PolicyEndPointProperties.PROPERTY_AAF_SUFFIX, false);
160         final String userName = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX, null);
161         final String password = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX, null);
162         final String authUriPath = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_AUTH_URIPATH_SUFFIX, null);
163
164         if (aaf) {
165             service.setAafAuthentication(contextUriPath);
166         } else if (!StringUtils.isBlank(userName) && !StringUtils.isBlank(password)) {
167             service.setBasicAuthentication(userName, password, authUriPath);
168         }
169     }
170
171     private void addFilterClasses(PropertyUtils props, HttpServletServer service, final String restUriPath) {
172
173         final String filterClasses =
174                         props.getString(PolicyEndPointProperties.PROPERTY_HTTP_FILTER_CLASSES_SUFFIX, null);
175
176         if (!StringUtils.isBlank(filterClasses)) {
177             for (String filterClass : filterClasses.split(SPACES_COMMA_SPACES)) {
178                 service.addFilterClass(restUriPath, filterClass);
179             }
180         }
181     }
182
183     private void addServletClasses(PropertyUtils props, HttpServletServer service, final String restUriPath) {
184
185         final String restClasses = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX, null);
186
187         if (!StringUtils.isBlank(restClasses)) {
188             for (String restClass : restClasses.split(SPACES_COMMA_SPACES)) {
189                 service.addServletClass(restUriPath, restClass);
190             }
191         }
192     }
193
194     private void addServletPackages(PropertyUtils props, HttpServletServer service, final String restUriPath) {
195
196         final String restPackages = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_REST_PACKAGES_SUFFIX, null);
197
198         if (!StringUtils.isBlank(restPackages)) {
199             for (String restPackage : restPackages.split(SPACES_COMMA_SPACES)) {
200                 service.addServletPackage(restUriPath, restPackage);
201             }
202         }
203     }
204
205     @Override
206     public synchronized HttpServletServer get(int port) {
207
208         if (servers.containsKey(port)) {
209             return servers.get(port);
210         }
211
212         throw new IllegalArgumentException("Http Server for " + port + " not found");
213     }
214
215     @Override
216     public synchronized List<HttpServletServer> inventory() {
217         return new ArrayList<>(this.servers.values());
218     }
219
220     @Override
221     public synchronized void destroy(int port) {
222
223         if (!servers.containsKey(port)) {
224             return;
225         }
226
227         HttpServletServer server = servers.remove(port);
228         server.shutdown();
229     }
230
231     @Override
232     public synchronized void destroy() {
233         List<HttpServletServer> httpServletServers = this.inventory();
234         for (HttpServletServer server : httpServletServers) {
235             server.shutdown();
236         }
237
238         synchronized (this) {
239             this.servers.clear();
240         }
241     }
242
243 }