aboutsummaryrefslogtreecommitdiffstats
path: root/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/IndexedHttpServletServerFactory.java
blob: 7c9aca4c2fc5b248c28c3f200856f4d01e1ceaab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
 * ============LICENSE_START=======================================================
 * ONAP Policy Engine - Common Modules
 * ================================================================================
 * Copyright (C) 2017-2019, 2021 AT&T Intellectual Property. All rights reserved.
 * Modifications Copyright (C) 2020,2023-2024 Nordix Foundation.
 * Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.common.endpoints.http.server;

import com.google.re2j.Pattern;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;
import org.apache.commons.lang3.StringUtils;
import org.onap.policy.common.endpoints.http.server.internal.JettyJerseyServer;
import org.onap.policy.common.endpoints.http.server.internal.JettyStaticResourceServer;
import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
import org.onap.policy.common.endpoints.utils.PropertyUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Indexed factory implementation.
 */
class IndexedHttpServletServerFactory implements HttpServletServerFactory {
    private static final Pattern COMMA_SPACE_PAT = Pattern.compile("\\s*,\\s*");

    /**
     * logger.
     */
    protected static Logger logger = LoggerFactory.getLogger(IndexedHttpServletServerFactory.class);

    /**
     * servers index.
     */
    protected HashMap<Integer, HttpServletServer> servers = new HashMap<>();

    @Override
    public synchronized HttpServletServer build(String name, boolean https, String host, int port, boolean sniHostCheck,
        String contextPath, boolean swagger, boolean managed) {

        if (servers.containsKey(port)) {
            return servers.get(port);
        }

        var server = new JettyJerseyServer(name, https, host, port, sniHostCheck, contextPath, swagger);
        if (managed) {
            servers.put(port, server);
        }

        return server;
    }

    @Override
    public synchronized HttpServletServer build(String name, String host, int port, String contextPath, boolean swagger,
        boolean managed) {
        return build(name, false, host, port, false, contextPath, swagger, managed);
    }

    @Override
    public synchronized List<HttpServletServer> build(Properties properties) {

        ArrayList<HttpServletServer> serviceList = new ArrayList<>();

        String serviceNames = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES);
        if (StringUtils.isBlank(serviceNames)) {
            logger.warn("No topic for HTTP Service: {}", properties);
            return serviceList;
        }

        for (String serviceName : COMMA_SPACE_PAT.split(serviceNames)) {
            addService(serviceList, serviceName, properties);
        }

        return serviceList;
    }

    @Override
    public HttpServletServer buildStaticResourceServer(String name, boolean https, String host, int port,
        boolean sniHostCheck, String contextPath, boolean managed) {
        if (servers.containsKey(port)) {
            return servers.get(port);
        }

        var server = new JettyStaticResourceServer(name, https, host, port, sniHostCheck, contextPath);
        if (managed) {
            servers.put(port, server);
        }

        return server;
    }

    private void addService(ArrayList<HttpServletServer> serviceList, String serviceName, Properties properties) {

        String servicePrefix = PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + serviceName;

        var props = new PropertyUtils(properties, servicePrefix,
            (name, value, ex) -> logger
                .warn("{}: {} {} is in invalid format for http service {} ", this, name, value, serviceName));

        var servicePort = props.getInteger(PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX, -1);
        if (servicePort < 0) {
            logger.warn("No HTTP port for service in {}", serviceName);
            return;
        }

        final var hostName = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, null);
        final var contextUriPath = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_CONTEXT_URIPATH_SUFFIX, null);
        var managed = props.getBoolean(PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, true);
        var swagger = props.getBoolean(PolicyEndPointProperties.PROPERTY_HTTP_SWAGGER_SUFFIX, false);
        var https = props.getBoolean(PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX, false);
        var sniHostCheck = props.getBoolean(PolicyEndPointProperties.PROPERTY_HTTP_SNI_HOST_CHECK_SUFFIX, false);

        // create the service
        HttpServletServer service =
            build(serviceName, https, hostName, servicePort, sniHostCheck, contextUriPath, swagger, managed);

        // configure the service
        setSerializationProvider(props, service);
        setAuthentication(props, service);

        final var restUriPath = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_REST_URIPATH_SUFFIX, null);

        addFilterClasses(props, service, restUriPath);
        addRestServletClasses(props, service, restUriPath);
        addServletPackages(props, service, restUriPath);

        addServletClass(props, service);
        setPrometheus(props, service);

        serviceList.add(service);
    }

    private void setSerializationProvider(PropertyUtils props, HttpServletServer service) {

        final var classProv = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER, null);

        if (!StringUtils.isBlank(classProv)) {
            service.setSerializationProvider(classProv);
        }
    }

    private void setAuthentication(PropertyUtils props, HttpServletServer service) {
        /* authentication method HTTP Basic Auth */
        final var userName = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX, null);
        final var password = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX, null);
        final var authUriPath = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_AUTH_URIPATH_SUFFIX, null);

        if (!StringUtils.isBlank(userName) && !StringUtils.isBlank(password)) {
            service.setBasicAuthentication(userName, password, authUriPath);
        }
    }

    private void setPrometheus(PropertyUtils props, HttpServletServer service) {
        if (props.getBoolean(PolicyEndPointProperties.PROPERTY_HTTP_PROMETHEUS_SUFFIX, false)) {
            service.setPrometheus("/metrics");
        }
    }

    private void addFilterClasses(PropertyUtils props, HttpServletServer service, final String restUriPath) {

        final var filterClasses =
            props.getString(PolicyEndPointProperties.PROPERTY_HTTP_FILTER_CLASSES_SUFFIX, null);

        if (!StringUtils.isBlank(filterClasses)) {
            for (String filterClass : COMMA_SPACE_PAT.split(filterClasses)) {
                service.addFilterClass(restUriPath, filterClass);
            }
        }
    }

    private void addRestServletClasses(PropertyUtils props, HttpServletServer service, final String restUriPath) {
        final var restClasses = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX, null);

        if (!StringUtils.isBlank(restClasses)) {
            for (String restClass : COMMA_SPACE_PAT.split(restClasses)) {
                service.addServletClass(restUriPath, restClass);
            }
        }
    }

    private void addServletClass(PropertyUtils props, HttpServletServer service) {
        var servletClass = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_SERVLET_CLASS_SUFFIX, null);
        var servletUriPath = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_SERVLET_URIPATH_SUFFIX, null);

        if (!StringUtils.isBlank(servletClass) && !StringUtils.isBlank(servletUriPath)) {
            service.addStdServletClass(servletUriPath, servletClass);
        }
    }

    private void addServletPackages(PropertyUtils props, HttpServletServer service, final String restUriPath) {

        final var restPackages = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_REST_PACKAGES_SUFFIX, null);

        if (!StringUtils.isBlank(restPackages)) {
            for (String restPackage : COMMA_SPACE_PAT.split(restPackages)) {
                service.addServletPackage(restUriPath, restPackage);
            }
        }
    }

    @Override
    public synchronized HttpServletServer get(int port) {

        if (servers.containsKey(port)) {
            return servers.get(port);
        }

        throw new IllegalArgumentException("Http Server for " + port + " not found");
    }

    @Override
    public synchronized List<HttpServletServer> inventory() {
        return new ArrayList<>(this.servers.values());
    }

    @Override
    public synchronized void destroy(int port) {

        if (!servers.containsKey(port)) {
            return;
        }

        HttpServletServer server = servers.remove(port);
        server.shutdown();
    }

    @Override
    public synchronized void destroy() {
        List<HttpServletServer> httpServletServers = this.inventory();
        for (HttpServletServer server : httpServletServers) {
            server.shutdown();
        }

        synchronized (this) {
            this.servers.clear();
        }
    }

}