diff --git a/jetty-core/jetty-maven/src/main/java/org/eclipse/jetty/maven/ServerSupport.java b/jetty-core/jetty-maven/src/main/java/org/eclipse/jetty/maven/ServerSupport.java index cc766b67706..99178482737 100644 --- a/jetty-core/jetty-maven/src/main/java/org/eclipse/jetty/maven/ServerSupport.java +++ b/jetty-core/jetty-maven/src/main/java/org/eclipse/jetty/maven/ServerSupport.java @@ -19,8 +19,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import org.eclipse.jetty.maven.MavenServerConnector; -import org.eclipse.jetty.maven.PluginLog; import org.eclipse.jetty.security.LoginService; import org.eclipse.jetty.server.Connector; import org.eclipse.jetty.server.Handler; @@ -65,7 +63,15 @@ public class ServerSupport if (contexts == null) { contexts = new ContextHandlerCollection(); - server.setHandler(contexts); + if (server.getHandler() != null) + { + Handler.Sequence handlers = new Handler.Sequence(); + handlers.addHandler(server.getHandler()); + handlers.addHandler(contexts); + server.setHandler(handlers); + } + else + server.setHandler(contexts); } if (contextHandlers != null) diff --git a/jetty-core/jetty-maven/src/test/java/org/eclipse/jetty/maven/ServerSupportTest.java b/jetty-core/jetty-maven/src/test/java/org/eclipse/jetty/maven/ServerSupportTest.java new file mode 100644 index 00000000000..cf7fa181ed9 --- /dev/null +++ b/jetty-core/jetty-maven/src/test/java/org/eclipse/jetty/maven/ServerSupportTest.java @@ -0,0 +1,109 @@ +// +// ======================================================================== +// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others. +// +// This program and the accompanying materials are made available under the +// terms of the Eclipse Public License v. 2.0 which is available at +// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 +// which is available at https://www.apache.org/licenses/LICENSE-2.0. +// +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 +// ======================================================================== +// + +package org.eclipse.jetty.maven; + +import java.util.Arrays; +import java.util.List; +import java.util.Set; + +import org.eclipse.jetty.server.Handler; +import org.eclipse.jetty.server.Request; +import org.eclipse.jetty.server.Response; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.handler.ContextHandler; +import org.eclipse.jetty.server.handler.ContextHandlerCollection; +import org.eclipse.jetty.util.Callback; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class ServerSupportTest +{ + @Test + public void testNoServerHandlers() throws Exception + { + //Test that a server will always create a ContextHandlerCollection and DefaultHandler + Server server = new Server(); + assertNull(server.getHandler()); + ServerSupport.configureHandlers(server, null, null); + assertNotNull(server.getDefaultHandler()); + assertNotNull(server.getHandler()); + } + + @Test + public void testExistingServerHandler() throws Exception + { + //Test that if a Server already has a handler, we replace it with a + //sequence containing the original handler plus a ContextHandlerCollection + Server server = new Server(); + Handler.Abstract testHandler = new Handler.Abstract() + { + @Override + public boolean handle(Request request, Response response, Callback callback) throws Exception + { + return false; + } + }; + + server.setHandler(testHandler); + ServerSupport.configureHandlers(server, null, null); + assertNotNull(server.getDefaultHandler()); + assertInstanceOf(Handler.Sequence.class, server.getHandler()); + Handler.Sequence handlers = (Handler.Sequence)server.getHandler(); + assertTrue(handlers.contains(testHandler)); + assertNotNull(handlers.getDescendant(ContextHandlerCollection.class)); + } + + @Test + public void testExistingServerHandlerWithContextHandlers() throws Exception + { + //Test that if a Server already has a handler, we replace it with + //a sequence containing the original handler plus a ContextHandlerCollection + //into which we add any supplied ContextHandlers + Server server = new Server(); + Handler.Abstract testHandlerA = new Handler.Abstract() + { + @Override + public boolean handle(Request request, Response response, Callback callback) throws Exception + { + return false; + } + }; + + ContextHandler contextHandlerA = new ContextHandler(); + contextHandlerA.setContextPath("/A"); + ContextHandler contextHandlerB = new ContextHandler(); + contextHandlerB.setContextPath("/B"); + List contextHandlerList = Arrays.asList(contextHandlerA, contextHandlerB); + + server.setHandler(testHandlerA); + ServerSupport.configureHandlers(server, contextHandlerList, null); + assertNotNull(server.getDefaultHandler()); + assertInstanceOf(Handler.Sequence.class, server.getHandler()); + Handler.Sequence handlers = (Handler.Sequence)server.getHandler(); + List handlerList = handlers.getHandlers(); + assertEquals(testHandlerA, handlerList.get(0)); + Handler second = handlerList.get(1); + assertInstanceOf(ContextHandlerCollection.class, second); + ContextHandlerCollection contextHandlers = (ContextHandlerCollection)second; + Set contextPaths = contextHandlers.getContextPaths(); + assertNotNull(contextPaths); + assertTrue(contextPaths.contains("/A")); + assertTrue(contextPaths.contains("/B")); + } +} diff --git a/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/SessionHandler.java b/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/SessionHandler.java index 788f24f68c3..1cf55219044 100644 --- a/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/SessionHandler.java +++ b/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/SessionHandler.java @@ -253,6 +253,14 @@ public class SessionHandler extends AbstractSessionManager implements Handler.Si SessionHandler.this.setSecureCookies(secure); } + @Override + public String toString() + { + return String.format("%s@%x[name=%s,domain=%s,path=%s,max-age=%d,secure=%b,http-only=%b,comment=%s,attributes=%s]", + this.getClass().getName(), this.hashCode(), getName(), getDomain(), getPath(), + getMaxAge(), isSecure(), isHttpOnly(), getComment(), getSessionCookieAttributes().toString()); + } + private void checkState() { //It is allowable to call the CookieConfig.setXX methods after the SessionHandler has started, @@ -383,6 +391,10 @@ public class SessionHandler extends AbstractSessionManager implements Handler.Si public SessionHandler() { setSessionTrackingModes(DEFAULT_SESSION_TRACKING_MODES); + installBean(_cookieConfig); + installBean(_sessionListeners); + installBean(_sessionIdListeners); + installBean(_sessionAttributeListeners); } @Override diff --git a/jetty-ee11/jetty-ee11-servlet/src/main/java/org/eclipse/jetty/ee11/servlet/SessionHandler.java b/jetty-ee11/jetty-ee11-servlet/src/main/java/org/eclipse/jetty/ee11/servlet/SessionHandler.java index 9c887e97bc7..1507b18376f 100644 --- a/jetty-ee11/jetty-ee11-servlet/src/main/java/org/eclipse/jetty/ee11/servlet/SessionHandler.java +++ b/jetty-ee11/jetty-ee11-servlet/src/main/java/org/eclipse/jetty/ee11/servlet/SessionHandler.java @@ -254,6 +254,14 @@ public class SessionHandler extends AbstractSessionManager implements Handler.Si SessionHandler.this.setSecureCookies(secure); } + @Override + public String toString() + { + return String.format("%s@%x[name=%s,domain=%s,path=%s,max-age=%d,secure=%b,http-only=%b,comment=%s,attributes=%s]", + this.getClass().getName(), this.hashCode(), getName(), getDomain(), getPath(), + getMaxAge(), isSecure(), isHttpOnly(), getComment(), getSessionCookieAttributes().toString()); + } + private void checkState() { //It is allowable to call the CookieConfig.setXX methods after the SessionHandler has started, @@ -427,6 +435,10 @@ public class SessionHandler extends AbstractSessionManager implements Handler.Si public SessionHandler() { setSessionTrackingModes(DEFAULT_SESSION_TRACKING_MODES); + installBean(_cookieConfig); + installBean(_sessionListeners); + installBean(_sessionIdListeners); + installBean(_sessionAttributeListeners); } @Override diff --git a/jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/SessionHandler.java b/jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/SessionHandler.java index 2ea75100d9a..d77c172dc02 100644 --- a/jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/SessionHandler.java +++ b/jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/SessionHandler.java @@ -729,6 +729,14 @@ public class SessionHandler extends ScopedHandler implements SessionConfig.Mutab checkAvailable(); _sessionManager.setSecureCookies(secure); } + + @Override + public String toString() + { + return String.format("%s@%x[name=%s,domain=%s,path=%s,max-age=%d,secure=%b,http-only=%b,same-site=%s,comment=%s]", + this.getClass().getName(), this.hashCode(), _sessionManager.getSessionCookie(), _sessionManager.getSessionDomain(), _sessionManager.getSessionPath(), + _sessionManager.getMaxCookieAge(), _sessionManager.isSecureCookies(), _sessionManager.isHttpOnly(), _sessionManager.getSameSite(), _sessionManager.getSessionComment()); + } } private class CoreSessionManager extends AbstractSessionManager