e977f02c43fc5615afa0b8f65832515fd5f085da
[policy/common.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine - Common Modules
4  * ================================================================================
5  * Copyright (C) 2017-2019, 2021 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 com.google.re2j.Pattern;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Properties;
29 import org.apache.commons.lang3.StringUtils;
30 import org.onap.policy.common.endpoints.http.server.internal.JettyJerseyServer;
31 import org.onap.policy.common.endpoints.http.server.internal.JettyStaticResourceServer;
32 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
33 import org.onap.policy.common.endpoints.utils.PropertyUtils;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * Indexed factory implementation.
39  */
40 class IndexedHttpServletServerFactory implements HttpServletServerFactory {
41     private static final Pattern COMMA_SPACE_PAT = Pattern.compile("\\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         var 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 : COMMA_SPACE_PAT.split(serviceNames)) {
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         var 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         var 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         var 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 var hostName = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, null);
125         final var contextUriPath = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_CONTEXT_URIPATH_SUFFIX, null);
126         var managed = props.getBoolean(PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, true);
127         var swagger = props.getBoolean(PolicyEndPointProperties.PROPERTY_HTTP_SWAGGER_SUFFIX, false);
128         var https = props.getBoolean(PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX, false);
129
130         // create the service
131         HttpServletServer service = build(serviceName, https, hostName, servicePort, contextUriPath, swagger, managed);
132
133         // configure the service
134         setSerializationProvider(props, service);
135         setAuthentication(props, service, contextUriPath);
136
137         final var restUriPath = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_REST_URIPATH_SUFFIX, null);
138
139         addFilterClasses(props, service, restUriPath);
140         addServletClasses(props, service, restUriPath);
141         addServletPackages(props, service, restUriPath);
142
143         serviceList.add(service);
144     }
145
146     private void setSerializationProvider(PropertyUtils props, HttpServletServer service) {
147
148         final var classProv = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER, null);
149
150         if (!StringUtils.isBlank(classProv)) {
151             service.setSerializationProvider(classProv);
152         }
153     }
154
155     private void setAuthentication(PropertyUtils props, HttpServletServer service, final String contextUriPath) {
156         /* authentication method either AAF or HTTP Basic Auth */
157
158         final var aaf = props.getBoolean(PolicyEndPointProperties.PROPERTY_AAF_SUFFIX, false);
159         final var userName = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX, null);
160         final var password = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX, null);
161         final var authUriPath = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_AUTH_URIPATH_SUFFIX, null);
162
163         if (aaf) {
164             service.setAafAuthentication(contextUriPath);
165         } else if (!StringUtils.isBlank(userName) && !StringUtils.isBlank(password)) {
166             service.setBasicAuthentication(userName, password, authUriPath);
167         }
168     }
169
170     private void addFilterClasses(PropertyUtils props, HttpServletServer service, final String restUriPath) {
171
172         final var filterClasses =
173                         props.getString(PolicyEndPointProperties.PROPERTY_HTTP_FILTER_CLASSES_SUFFIX, null);
174
175         if (!StringUtils.isBlank(filterClasses)) {
176             for (String filterClass : COMMA_SPACE_PAT.split(filterClasses)) {
177                 service.addFilterClass(restUriPath, filterClass);
178             }
179         }
180     }
181
182     private void addServletClasses(PropertyUtils props, HttpServletServer service, final String restUriPath) {
183
184         final var restClasses = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX, null);
185
186         if (!StringUtils.isBlank(restClasses)) {
187             for (String restClass : COMMA_SPACE_PAT.split(restClasses)) {
188                 service.addServletClass(restUriPath, restClass);
189             }
190         }
191     }
192
193     private void addServletPackages(PropertyUtils props, HttpServletServer service, final String restUriPath) {
194
195         final var restPackages = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_REST_PACKAGES_SUFFIX, null);
196
197         if (!StringUtils.isBlank(restPackages)) {
198             for (String restPackage : COMMA_SPACE_PAT.split(restPackages)) {
199                 service.addServletPackage(restUriPath, restPackage);
200             }
201         }
202     }
203
204     @Override
205     public synchronized HttpServletServer get(int port) {
206
207         if (servers.containsKey(port)) {
208             return servers.get(port);
209         }
210
211         throw new IllegalArgumentException("Http Server for " + port + " not found");
212     }
213
214     @Override
215     public synchronized List<HttpServletServer> inventory() {
216         return new ArrayList<>(this.servers.values());
217     }
218
219     @Override
220     public synchronized void destroy(int port) {
221
222         if (!servers.containsKey(port)) {
223             return;
224         }
225
226         HttpServletServer server = servers.remove(port);
227         server.shutdown();
228     }
229
230     @Override
231     public synchronized void destroy() {
232         List<HttpServletServer> httpServletServers = this.inventory();
233         for (HttpServletServer server : httpServletServers) {
234             server.shutdown();
235         }
236
237         synchronized (this) {
238             this.servers.clear();
239         }
240     }
241
242 }