2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019-2020, 2023-2024 Nordix Foundation.
7 * Modifications Copyright (C) 2020-2021 Bell Canada. All rights reserved.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.common.endpoints.http.server.internal;
25 import io.prometheus.client.hotspot.DefaultExports;
26 import io.prometheus.client.servlet.jakarta.exporter.MetricsServlet;
27 import jakarta.servlet.Servlet;
28 import java.util.EnumSet;
29 import java.util.HashMap;
32 import lombok.NonNull;
33 import lombok.ToString;
34 import org.eclipse.jetty.security.ConstraintMapping;
35 import org.eclipse.jetty.security.ConstraintSecurityHandler;
36 import org.eclipse.jetty.security.HashLoginService;
37 import org.eclipse.jetty.security.UserStore;
38 import org.eclipse.jetty.security.authentication.BasicAuthenticator;
39 import org.eclipse.jetty.server.CustomRequestLog;
40 import org.eclipse.jetty.server.HttpConfiguration;
41 import org.eclipse.jetty.server.HttpConnectionFactory;
42 import org.eclipse.jetty.server.SecureRequestCustomizer;
43 import org.eclipse.jetty.server.Server;
44 import org.eclipse.jetty.server.ServerConnector;
45 import org.eclipse.jetty.server.Slf4jRequestLogWriter;
46 import org.eclipse.jetty.servlet.ServletContextHandler;
47 import org.eclipse.jetty.servlet.ServletHolder;
48 import org.eclipse.jetty.util.security.Constraint;
49 import org.eclipse.jetty.util.security.Credential;
50 import org.eclipse.jetty.util.ssl.SslContextFactory;
51 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
56 * Http Server implementation using Embedded Jetty.
59 public abstract class JettyServletServer implements HttpServletServer, Runnable {
62 * Keystore/Truststore system property names.
64 public static final String SYSTEM_KEYSTORE_PROPERTY_NAME = "javax.net.ssl.keyStore";
65 public static final String SYSTEM_KEYSTORE_PASSWORD_PROPERTY_NAME = "javax.net.ssl.keyStorePassword"; // NOSONAR
66 public static final String SYSTEM_TRUSTSTORE_PROPERTY_NAME = "javax.net.ssl.trustStore";
67 public static final String SYSTEM_TRUSTSTORE_PASSWORD_PROPERTY_NAME = "javax.net.ssl.trustStorePassword"; // NOSONAR
72 private static final Logger logger = LoggerFactory.getLogger(JettyServletServer.class);
74 private static final String NOT_SUPPORTED = " is not supported on this type of jetty server";
80 protected final String name;
83 * Server host address.
86 protected final String host;
89 * Server port to bind.
92 protected final int port;
95 * Should SNI host checking be done.
98 protected boolean sniHostCheck;
101 * Server auth username.
104 protected String user;
107 * Server auth password name.
110 protected String password;
113 * Server base context path.
115 protected final String contextPath;
118 * Embedded jetty server.
120 protected final Server jettyServer;
125 protected final ServletContextHandler context;
130 protected final ServerConnector connector;
135 protected Thread jettyThread;
138 * Container for default servlets.
140 protected final Map<String, ServletHolder> servlets = new HashMap<>();
146 protected final Object startCondition = new Object();
151 * @param name server name
152 * @param host server host
153 * @param port server port
154 * @param sniHostCheck SNI Host checking flag
155 * @param contextPath context path
157 * @throws IllegalArgumentException if invalid parameters are passed in
159 protected JettyServletServer(String name, boolean https, String host, int port, boolean sniHostCheck,
160 String contextPath) {
161 String srvName = name;
163 if (srvName == null || srvName.isEmpty()) {
164 srvName = "http-" + port;
167 if (port <= 0 || port >= 65535) {
168 throw new IllegalArgumentException("Invalid Port provided: " + port);
171 String srvHost = host;
172 if (srvHost == null || srvHost.isEmpty()) {
173 srvHost = "localhost";
176 String ctxtPath = contextPath;
177 if (ctxtPath == null || ctxtPath.isEmpty()) {
185 this.sniHostCheck = sniHostCheck;
187 this.contextPath = ctxtPath;
189 this.context = new ServletContextHandler(ServletContextHandler.SESSIONS);
190 this.context.setContextPath(ctxtPath);
192 this.jettyServer = new Server();
194 var requestLog = new CustomRequestLog(new Slf4jRequestLogWriter(), CustomRequestLog.EXTENDED_NCSA_FORMAT);
195 this.jettyServer.setRequestLog(requestLog);
198 this.connector = httpsConnector();
200 this.connector = httpConnector();
203 this.connector.setName(srvName);
204 this.connector.setReuseAddress(true);
205 this.connector.setPort(port);
206 this.connector.setHost(srvHost);
208 this.jettyServer.addConnector(this.connector);
209 this.jettyServer.setHandler(context);
212 protected JettyServletServer(String name, String host, int port, boolean sniHostCheck, String contextPath) {
213 this(name, false, host, port, sniHostCheck, contextPath);
217 public void addFilterClass(String filterPath, String filterClass) {
218 if (filterClass == null || filterClass.isEmpty()) {
219 throw new IllegalArgumentException("No filter class provided");
222 String tempFilterPath = filterPath;
223 if (filterPath == null || filterPath.isEmpty()) {
224 tempFilterPath = "/*";
227 context.addFilter(filterClass, tempFilterPath,
228 EnumSet.of(jakarta.servlet.DispatcherType.INCLUDE, jakarta.servlet.DispatcherType.REQUEST));
231 protected ServletHolder getServlet(@NonNull Class<? extends Servlet> servlet, @NonNull String servletPath) {
232 synchronized (servlets) {
233 return servlets.computeIfAbsent(servletPath, key -> context.addServlet(servlet, servletPath));
237 protected ServletHolder getServlet(String servletClass, String servletPath) {
238 synchronized (servlets) {
239 return servlets.computeIfAbsent(servletPath, key -> context.addServlet(servletClass, servletPath));
244 * Returns the https connector.
246 * @return the server connector
248 public ServerConnector httpsConnector() {
249 SslContextFactory.Server sslContextFactoryServer = new SslContextFactory.Server();
251 String keyStore = System.getProperty(SYSTEM_KEYSTORE_PROPERTY_NAME);
252 if (keyStore != null) {
253 sslContextFactoryServer.setKeyStorePath(keyStore);
255 String ksPassword = System.getProperty(SYSTEM_KEYSTORE_PASSWORD_PROPERTY_NAME);
256 if (ksPassword != null) {
257 sslContextFactoryServer.setKeyStorePassword(ksPassword);
261 String trustStore = System.getProperty(SYSTEM_TRUSTSTORE_PROPERTY_NAME);
262 if (trustStore != null) {
263 sslContextFactoryServer.setTrustStorePath(trustStore);
265 String tsPassword = System.getProperty(SYSTEM_TRUSTSTORE_PASSWORD_PROPERTY_NAME);
266 if (tsPassword != null) {
267 sslContextFactoryServer.setTrustStorePassword(tsPassword);
272 var httpsConfiguration = new HttpConfiguration();
273 SecureRequestCustomizer src = new SecureRequestCustomizer();
274 src.setSniHostCheck(sniHostCheck);
275 httpsConfiguration.addCustomizer(src);
277 return new ServerConnector(jettyServer, sslContextFactoryServer, new HttpConnectionFactory(httpsConfiguration));
280 public ServerConnector httpConnector() {
281 return new ServerConnector(this.jettyServer);
285 public void setBasicAuthentication(String user, String password, String servletPath) {
286 String srvltPath = servletPath;
288 if (user == null || user.isEmpty() || password == null || password.isEmpty()) {
289 throw new IllegalArgumentException("Missing user and/or password");
292 if (srvltPath == null || srvltPath.isEmpty()) {
296 final var hashLoginService = new HashLoginService();
297 final var userStore = new UserStore();
298 userStore.addUser(user, Credential.getCredential(password), new String[] {
301 hashLoginService.setUserStore(userStore);
302 hashLoginService.setName(this.connector.getName() + "-login-service");
304 var constraint = new Constraint();
305 constraint.setName(Constraint.__BASIC_AUTH);
306 constraint.setRoles(new String[] {
309 constraint.setAuthenticate(true);
311 var constraintMapping = new ConstraintMapping();
312 constraintMapping.setConstraint(constraint);
313 constraintMapping.setPathSpec(srvltPath);
315 var securityHandler = new ConstraintSecurityHandler();
316 securityHandler.setAuthenticator(new BasicAuthenticator());
317 securityHandler.setRealmName(this.connector.getName() + "-realm");
318 securityHandler.addConstraintMapping(constraintMapping);
319 securityHandler.setLoginService(hashLoginService);
321 this.context.setSecurityHandler(securityHandler);
324 this.password = password;
328 * jetty server execution.
333 logger.info("{}: STARTING", this);
335 this.jettyServer.start();
337 if (logger.isTraceEnabled()) {
338 logger.trace("{}: STARTED: {}", this, this.jettyServer.dump());
341 synchronized (this.startCondition) {
342 this.startCondition.notifyAll();
345 this.jettyServer.join();
347 } catch (InterruptedException e) {
348 logger.error("{}: error found while bringing up server", this, e);
349 Thread.currentThread().interrupt();
351 } catch (Exception e) {
352 logger.error("{}: error found while bringing up server", this, e);
357 public boolean waitedStart(long maxWaitTime) throws InterruptedException {
358 logger.info("{}: WAITED-START", this);
360 if (maxWaitTime < 0) {
361 throw new IllegalArgumentException("max-wait-time cannot be negative");
364 long pendingWaitTime = maxWaitTime;
370 synchronized (this.startCondition) {
372 while (!this.jettyServer.isRunning()) {
374 long startTs = System.currentTimeMillis();
376 this.startCondition.wait(pendingWaitTime);
378 if (maxWaitTime == 0) {
379 /* spurious notification */
383 long endTs = System.currentTimeMillis();
384 pendingWaitTime = pendingWaitTime - (endTs - startTs);
386 logger.info("{}: pending time is {} ms.", this, pendingWaitTime);
388 if (pendingWaitTime <= 0) {
392 } catch (InterruptedException e) {
393 logger.warn("{}: waited-start has been interrupted", this);
398 return this.jettyServer.isRunning();
403 public boolean start() {
404 logger.info("{}: STARTING", this);
406 synchronized (this) {
407 if (jettyThread == null || !this.jettyThread.isAlive()) {
409 this.jettyThread = new Thread(this);
410 this.jettyThread.setName(this.name + "-" + this.port);
411 this.jettyThread.start();
419 public boolean stop() {
420 logger.info("{}: STOPPING", this);
422 synchronized (this) {
423 if (jettyThread == null) {
427 if (!jettyThread.isAlive()) {
428 this.jettyThread = null;
432 this.connector.stop();
433 } catch (Exception e) {
434 logger.error("{}: error while stopping management server", this, e);
438 this.jettyServer.stop();
439 } catch (Exception e) {
440 logger.error("{}: error while stopping management server", this, e);
451 public void shutdown() {
452 logger.info("{}: SHUTTING DOWN", this);
456 Thread jettyThreadCopy;
457 synchronized (this) {
458 if ((jettyThreadCopy = this.jettyThread) == null) {
463 if (jettyThreadCopy.isAlive()) {
465 jettyThreadCopy.join(2000L);
466 } catch (InterruptedException e) {
467 logger.warn("{}: error while shutting down management server", this);
468 Thread.currentThread().interrupt();
470 if (!jettyThreadCopy.isInterrupted()) {
472 jettyThreadCopy.interrupt();
473 } catch (Exception e) {
475 logger.warn("{}: exception while shutting down (OK)", this, e);
480 this.jettyServer.destroy();
484 public boolean isAlive() {
485 if (this.jettyThread != null) {
486 return this.jettyThread.isAlive();
493 public void setSerializationProvider(String provider) {
494 throw new UnsupportedOperationException("setSerializationProvider()" + NOT_SUPPORTED);
498 public void addServletClass(String servletPath, String servletClass) {
499 throw new UnsupportedOperationException("addServletClass()" + NOT_SUPPORTED);
503 public void addStdServletClass(@NonNull String servletPath, @NonNull String plainServletClass) {
504 this.getServlet(plainServletClass, servletPath);
508 public void setPrometheus(String metricsPath) {
509 this.getServlet(MetricsServlet.class, metricsPath);
510 DefaultExports.initialize();
514 public boolean isPrometheus() {
515 for (ServletHolder servlet : context.getServletHandler().getServlets()) {
516 if (MetricsServlet.class.getName().equals(servlet.getClassName())) {
524 public void addServletPackage(String servletPath, String restPackage) {
525 throw new UnsupportedOperationException("addServletPackage()" + NOT_SUPPORTED);
529 public void addServletResource(String servletPath, String resourceBase) {
530 throw new UnsupportedOperationException("addServletResource()" + NOT_SUPPORTED);