2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019-2020,2023 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.exporter.MetricsServlet;
26 import io.prometheus.client.hotspot.DefaultExports;
27 import java.util.EnumSet;
28 import java.util.HashMap;
30 import javax.servlet.DispatcherType;
31 import javax.servlet.Servlet;
33 import lombok.NonNull;
34 import lombok.ToString;
35 import org.eclipse.jetty.security.ConstraintMapping;
36 import org.eclipse.jetty.security.ConstraintSecurityHandler;
37 import org.eclipse.jetty.security.HashLoginService;
38 import org.eclipse.jetty.security.UserStore;
39 import org.eclipse.jetty.security.authentication.BasicAuthenticator;
40 import org.eclipse.jetty.server.CustomRequestLog;
41 import org.eclipse.jetty.server.HttpConfiguration;
42 import org.eclipse.jetty.server.HttpConnectionFactory;
43 import org.eclipse.jetty.server.SecureRequestCustomizer;
44 import org.eclipse.jetty.server.Server;
45 import org.eclipse.jetty.server.ServerConnector;
46 import org.eclipse.jetty.server.Slf4jRequestLogWriter;
47 import org.eclipse.jetty.servlet.FilterHolder;
48 import org.eclipse.jetty.servlet.ServletContextHandler;
49 import org.eclipse.jetty.servlet.ServletHolder;
50 import org.eclipse.jetty.util.security.Constraint;
51 import org.eclipse.jetty.util.security.Credential;
52 import org.eclipse.jetty.util.ssl.SslContextFactory;
53 import org.onap.aaf.cadi.filter.CadiFilter;
54 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
59 * Http Server implementation using Embedded Jetty.
62 public abstract class JettyServletServer implements HttpServletServer, Runnable {
65 * Keystore/Truststore system property names.
67 public static final String SYSTEM_KEYSTORE_PROPERTY_NAME = "javax.net.ssl.keyStore";
68 public static final String SYSTEM_KEYSTORE_PASSWORD_PROPERTY_NAME = "javax.net.ssl.keyStorePassword"; // NOSONAR
69 public static final String SYSTEM_TRUSTSTORE_PROPERTY_NAME = "javax.net.ssl.trustStore";
70 public static final String SYSTEM_TRUSTSTORE_PASSWORD_PROPERTY_NAME = "javax.net.ssl.trustStorePassword"; // NOSONAR
75 private static Logger logger = LoggerFactory.getLogger(JettyServletServer.class);
77 private static final String NOT_SUPPORTED = " is not supported on this type of jetty server";
83 protected final String name;
86 * Server host address.
89 protected final String host;
92 * Server port to bind.
95 protected final int port;
98 * Should SNI host checking be done.
101 protected boolean sniHostCheck;
104 * Server auth user name.
107 protected String user;
110 * Server auth password name.
113 protected String password;
116 * Server base context path.
118 protected final String contextPath;
121 * Embedded jetty server.
123 protected final Server jettyServer;
128 protected final ServletContextHandler context;
133 protected final ServerConnector connector;
138 protected Thread jettyThread;
141 * Container for default servlets.
143 protected final Map<String, ServletHolder> servlets = new HashMap<>();
149 protected Object startCondition = new Object();
154 * @param name server name
155 * @param host server host
156 * @param port server port
157 * @param sniHostCheck SNI Host checking flag
158 * @param contextPath context path
160 * @throws IllegalArgumentException if invalid parameters are passed in
162 protected JettyServletServer(String name, boolean https, String host, int port, boolean sniHostCheck,
163 String contextPath) {
164 String srvName = name;
166 if (srvName == null || srvName.isEmpty()) {
167 srvName = "http-" + port;
170 if (port <= 0 || port >= 65535) {
171 throw new IllegalArgumentException("Invalid Port provided: " + port);
174 String srvHost = host;
175 if (srvHost == null || srvHost.isEmpty()) {
176 srvHost = "localhost";
179 String ctxtPath = contextPath;
180 if (ctxtPath == null || ctxtPath.isEmpty()) {
188 this.sniHostCheck = sniHostCheck;
190 this.contextPath = ctxtPath;
192 this.context = new ServletContextHandler(ServletContextHandler.SESSIONS);
193 this.context.setContextPath(ctxtPath);
195 this.jettyServer = new Server();
197 var requestLog = new CustomRequestLog(new Slf4jRequestLogWriter(), CustomRequestLog.EXTENDED_NCSA_FORMAT);
198 this.jettyServer.setRequestLog(requestLog);
201 this.connector = httpsConnector();
203 this.connector = httpConnector();
206 this.connector.setName(srvName);
207 this.connector.setReuseAddress(true);
208 this.connector.setPort(port);
209 this.connector.setHost(srvHost);
211 this.jettyServer.addConnector(this.connector);
212 this.jettyServer.setHandler(context);
215 protected JettyServletServer(String name, String host, int port, boolean sniHostCheck, String contextPath) {
216 this(name, false, host, port, sniHostCheck, contextPath);
220 public void addFilterClass(String filterPath, String filterClass) {
221 if (filterClass == null || filterClass.isEmpty()) {
222 throw new IllegalArgumentException("No filter class provided");
225 String tempFilterPath = filterPath;
226 if (filterPath == null || filterPath.isEmpty()) {
227 tempFilterPath = "/*";
230 context.addFilter(filterClass, tempFilterPath, EnumSet.of(DispatcherType.INCLUDE, DispatcherType.REQUEST));
233 protected ServletHolder getServlet(@NonNull Class<? extends Servlet> servlet, @NonNull String servletPath) {
234 synchronized (servlets) {
235 return servlets.computeIfAbsent(servletPath, key -> context.addServlet(servlet, servletPath));
239 protected ServletHolder getServlet(String servletClass, String servletPath) {
240 synchronized (servlets) {
241 return servlets.computeIfAbsent(servletPath, key -> context.addServlet(servletClass, servletPath));
246 * Returns the https connector.
248 * @return the server connector
250 public ServerConnector httpsConnector() {
251 SslContextFactory.Server sslContextFactoryServer = new SslContextFactory.Server();
253 String keyStore = System.getProperty(SYSTEM_KEYSTORE_PROPERTY_NAME);
254 if (keyStore != null) {
255 sslContextFactoryServer.setKeyStorePath(keyStore);
257 String ksPassword = System.getProperty(SYSTEM_KEYSTORE_PASSWORD_PROPERTY_NAME);
258 if (ksPassword != null) {
259 sslContextFactoryServer.setKeyStorePassword(ksPassword);
263 String trustStore = System.getProperty(SYSTEM_TRUSTSTORE_PROPERTY_NAME);
264 if (trustStore != null) {
265 sslContextFactoryServer.setTrustStorePath(trustStore);
267 String tsPassword = System.getProperty(SYSTEM_TRUSTSTORE_PASSWORD_PROPERTY_NAME);
268 if (tsPassword != null) {
269 sslContextFactoryServer.setTrustStorePassword(tsPassword);
274 var httpsConfiguration = new HttpConfiguration();
275 SecureRequestCustomizer src = new SecureRequestCustomizer();
276 src.setSniHostCheck(sniHostCheck);
277 httpsConfiguration.addCustomizer(src);
279 return new ServerConnector(jettyServer, sslContextFactoryServer, new HttpConnectionFactory(httpsConfiguration));
282 public ServerConnector httpConnector() {
283 return new ServerConnector(this.jettyServer);
287 public void setAafAuthentication(String filterPath) {
288 this.addFilterClass(filterPath, CadiFilter.class.getName());
292 public boolean isAaf() {
293 for (FilterHolder filter : context.getServletHandler().getFilters()) {
294 if (CadiFilter.class.getName().equals(filter.getClassName())) {
302 public void setBasicAuthentication(String user, String password, String servletPath) {
303 String srvltPath = servletPath;
305 if (user == null || user.isEmpty() || password == null || password.isEmpty()) {
306 throw new IllegalArgumentException("Missing user and/or password");
309 if (srvltPath == null || srvltPath.isEmpty()) {
313 final var hashLoginService = new HashLoginService();
314 final var userStore = new UserStore();
315 userStore.addUser(user, Credential.getCredential(password), new String[] {
318 hashLoginService.setUserStore(userStore);
319 hashLoginService.setName(this.connector.getName() + "-login-service");
321 var constraint = new Constraint();
322 constraint.setName(Constraint.__BASIC_AUTH);
323 constraint.setRoles(new String[] {
326 constraint.setAuthenticate(true);
328 var constraintMapping = new ConstraintMapping();
329 constraintMapping.setConstraint(constraint);
330 constraintMapping.setPathSpec(srvltPath);
332 var securityHandler = new ConstraintSecurityHandler();
333 securityHandler.setAuthenticator(new BasicAuthenticator());
334 securityHandler.setRealmName(this.connector.getName() + "-realm");
335 securityHandler.addConstraintMapping(constraintMapping);
336 securityHandler.setLoginService(hashLoginService);
338 this.context.setSecurityHandler(securityHandler);
341 this.password = password;
345 * jetty server execution.
350 logger.info("{}: STARTING", this);
352 this.jettyServer.start();
354 if (logger.isTraceEnabled()) {
355 logger.trace("{}: STARTED: {}", this, this.jettyServer.dump());
358 synchronized (this.startCondition) {
359 this.startCondition.notifyAll();
362 this.jettyServer.join();
364 } catch (InterruptedException e) {
365 logger.error("{}: error found while bringing up server", this, e);
366 Thread.currentThread().interrupt();
368 } catch (Exception e) {
369 logger.error("{}: error found while bringing up server", this, e);
374 public boolean waitedStart(long maxWaitTime) throws InterruptedException {
375 logger.info("{}: WAITED-START", this);
377 if (maxWaitTime < 0) {
378 throw new IllegalArgumentException("max-wait-time cannot be negative");
381 long pendingWaitTime = maxWaitTime;
387 synchronized (this.startCondition) {
389 while (!this.jettyServer.isRunning()) {
391 long startTs = System.currentTimeMillis();
393 this.startCondition.wait(pendingWaitTime);
395 if (maxWaitTime == 0) {
396 /* spurious notification */
400 long endTs = System.currentTimeMillis();
401 pendingWaitTime = pendingWaitTime - (endTs - startTs);
403 logger.info("{}: pending time is {} ms.", this, pendingWaitTime);
405 if (pendingWaitTime <= 0) {
409 } catch (InterruptedException e) {
410 logger.warn("{}: waited-start has been interrupted", this);
415 return this.jettyServer.isRunning();
420 public boolean start() {
421 logger.info("{}: STARTING", this);
423 synchronized (this) {
424 if (jettyThread == null || !this.jettyThread.isAlive()) {
426 this.jettyThread = new Thread(this);
427 this.jettyThread.setName(this.name + "-" + this.port);
428 this.jettyThread.start();
436 public boolean stop() {
437 logger.info("{}: STOPPING", this);
439 synchronized (this) {
440 if (jettyThread == null) {
444 if (!jettyThread.isAlive()) {
445 this.jettyThread = null;
449 this.connector.stop();
450 } catch (Exception e) {
451 logger.error("{}: error while stopping management server", this, e);
455 this.jettyServer.stop();
456 } catch (Exception e) {
457 logger.error("{}: error while stopping management server", this, e);
468 public void shutdown() {
469 logger.info("{}: SHUTTING DOWN", this);
473 Thread jettyThreadCopy;
474 synchronized (this) {
475 if ((jettyThreadCopy = this.jettyThread) == null) {
480 if (jettyThreadCopy.isAlive()) {
482 jettyThreadCopy.join(2000L);
483 } catch (InterruptedException e) {
484 logger.warn("{}: error while shutting down management server", this);
485 Thread.currentThread().interrupt();
487 if (!jettyThreadCopy.isInterrupted()) {
489 jettyThreadCopy.interrupt();
490 } catch (Exception e) {
492 logger.warn("{}: exception while shutting down (OK)", this, e);
497 this.jettyServer.destroy();
501 public boolean isAlive() {
502 if (this.jettyThread != null) {
503 return this.jettyThread.isAlive();
510 public void setSerializationProvider(String provider) {
511 throw new UnsupportedOperationException("setSerializationProvider()" + NOT_SUPPORTED);
515 public void addServletClass(String servletPath, String servletClass) {
516 throw new UnsupportedOperationException("addServletClass()" + NOT_SUPPORTED);
520 public void addStdServletClass(@NonNull String servletPath, @NonNull String plainServletClass) {
521 this.getServlet(plainServletClass, servletPath);
525 public void setPrometheus(String metricsPath) {
526 this.getServlet(MetricsServlet.class, metricsPath);
527 DefaultExports.initialize();
531 public boolean isPrometheus() {
532 for (ServletHolder servlet : context.getServletHandler().getServlets()) {
533 if (MetricsServlet.class.getName().equals(servlet.getClassName())) {
541 public void addServletPackage(String servletPath, String restPackage) {
542 throw new UnsupportedOperationException("addServletPackage()" + NOT_SUPPORTED);
546 public void addServletResource(String servletPath, String resourceBase) {
547 throw new UnsupportedOperationException("addServletResource()" + NOT_SUPPORTED);