Merge remote-tracking branch 'origin/jetty-10.0.x' into jetty-11.0.x

This commit is contained in:
Lachlan Roberts 2021-03-01 16:26:18 +11:00
commit 7418657d33
34 changed files with 616 additions and 76 deletions

View File

@ -34,6 +34,11 @@
<artifactId>jetty-client</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.toolchain</groupId>
<artifactId>jetty-jakarta-servlet-api</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-xml</artifactId>

View File

@ -19,6 +19,8 @@ module org.eclipse.jetty.websocket.jakarta.client
exports org.eclipse.jetty.websocket.jakarta.client;
exports org.eclipse.jetty.websocket.jakarta.client.internal to org.eclipse.jetty.websocket.jakarta.server;
requires static jetty.servlet.api;
requires org.slf4j;
requires org.eclipse.jetty.websocket.core.client;
requires org.eclipse.jetty.websocket.jakarta.common;
requires transitive org.eclipse.jetty.client;

View File

@ -17,7 +17,7 @@ import jakarta.websocket.ContainerProvider;
import jakarta.websocket.WebSocketContainer;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.util.component.LifeCycle;
import org.eclipse.jetty.util.thread.ShutdownThread;
import org.eclipse.jetty.websocket.jakarta.client.internal.JakartaWebSocketClientContainer;
/**
* Client {@link ContainerProvider} implementation.
@ -62,15 +62,8 @@ public class JakartaWebSocketClientContainerProvider extends ContainerProvider
public WebSocketContainer getContainer(HttpClient httpClient)
{
JakartaWebSocketClientContainer clientContainer = new JakartaWebSocketClientContainer(httpClient);
registerShutdown(clientContainer);
// See: https://github.com/eclipse-ee4j/websocket-api/issues/212
LifeCycle.start(clientContainer);
return clientContainer;
}
// See: https://github.com/eclipse-ee4j/websocket-api/issues/212
private void registerShutdown(JakartaWebSocketClientContainer container)
{
// Register as JVM runtime shutdown hook.
ShutdownThread.register(container);
LifeCycle.start(container);
}
}

View File

@ -0,0 +1,73 @@
//
// ========================================================================
// Copyright (c) 1995-2021 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.websocket.jakarta.client;
import java.util.Set;
import jakarta.servlet.ServletContainerInitializer;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletContextEvent;
import jakarta.servlet.ServletContextListener;
import jakarta.servlet.ServletException;
import org.eclipse.jetty.util.component.ContainerLifeCycle;
import org.eclipse.jetty.util.component.LifeCycle;
import org.eclipse.jetty.websocket.jakarta.client.internal.JakartaWebSocketClientContainer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* <p>This manages the LifeCycle of {@link jakarta.websocket.WebSocketContainer} instances that are created with
* {@link jakarta.websocket.ContainerProvider}, if this code is being run from another ServletContainer, or if run inside a
* Jetty Server with the WebSocket client classes provided by the webapp.</p>
*
* <p>This mechanism will not work if run with embedded Jetty or if the WebSocket client classes are provided by the server.
* In this case then the client {@link jakarta.websocket.WebSocketContainer} will register itself to be automatically shutdown
* with the Jetty {@code ContextHandler}.</p>
*/
public class JakartaWebSocketShutdownContainer extends ContainerLifeCycle implements ServletContainerInitializer, ServletContextListener
{
private static final Logger LOG = LoggerFactory.getLogger(JakartaWebSocketShutdownContainer.class);
@Override
public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletException
{
JakartaWebSocketClientContainer.setShutdownContainer(this);
ctx.addListener(this);
}
@Override
public void contextInitialized(ServletContextEvent sce)
{
if (LOG.isDebugEnabled())
LOG.debug("contextInitialized({}) {}", sce, this);
LifeCycle.start(this);
}
@Override
public void contextDestroyed(ServletContextEvent sce)
{
if (LOG.isDebugEnabled())
LOG.debug("contextDestroyed({}) {}", sce, this);
LifeCycle.stop(this);
removeBeans();
JakartaWebSocketClientContainer.setShutdownContainer(null);
}
@Override
public String toString()
{
return String.format("%s@%x{%s, size=%s}", getClass().getSimpleName(), hashCode(), getState(), getBeans().size());
}
}

View File

@ -11,7 +11,7 @@
// ========================================================================
//
package org.eclipse.jetty.websocket.jakarta.client;
package org.eclipse.jetty.websocket.jakarta.client.internal;
import java.util.Collections;
import java.util.List;

View File

@ -11,7 +11,7 @@
// ========================================================================
//
package org.eclipse.jetty.websocket.jakarta.client;
package org.eclipse.jetty.websocket.jakarta.client.internal;
import org.eclipse.jetty.websocket.jakarta.common.ClientEndpointConfigWrapper;

View File

@ -11,7 +11,7 @@
// ========================================================================
//
package org.eclipse.jetty.websocket.jakarta.client;
package org.eclipse.jetty.websocket.jakarta.client.internal;
import java.util.List;
import java.util.Map;

View File

@ -11,7 +11,7 @@
// ========================================================================
//
package org.eclipse.jetty.websocket.jakarta.client;
package org.eclipse.jetty.websocket.jakarta.client.internal;
import java.net.URI;
import java.security.Principal;

View File

@ -11,7 +11,7 @@
// ========================================================================
//
package org.eclipse.jetty.websocket.jakarta.client;
package org.eclipse.jetty.websocket.jakarta.client.internal;
import java.io.IOException;
import java.net.URI;
@ -22,6 +22,7 @@ import java.util.concurrent.Executor;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import jakarta.websocket.ClientEndpoint;
@ -33,17 +34,21 @@ import jakarta.websocket.Extension;
import jakarta.websocket.Session;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.util.annotation.ManagedObject;
import org.eclipse.jetty.util.component.ContainerLifeCycle;
import org.eclipse.jetty.util.component.LifeCycle;
import org.eclipse.jetty.util.thread.ShutdownThread;
import org.eclipse.jetty.websocket.core.WebSocketComponents;
import org.eclipse.jetty.websocket.core.client.WebSocketCoreClient;
import org.eclipse.jetty.websocket.core.exception.InvalidWebSocketException;
import org.eclipse.jetty.websocket.core.exception.UpgradeException;
import org.eclipse.jetty.websocket.core.exception.WebSocketTimeoutException;
import org.eclipse.jetty.websocket.jakarta.client.internal.JsrUpgradeListener;
import org.eclipse.jetty.websocket.jakarta.common.ConfiguredEndpoint;
import org.eclipse.jetty.websocket.jakarta.common.JakartaWebSocketContainer;
import org.eclipse.jetty.websocket.jakarta.common.JakartaWebSocketExtensionConfig;
import org.eclipse.jetty.websocket.jakarta.common.JakartaWebSocketFrameHandler;
import org.eclipse.jetty.websocket.jakarta.common.JakartaWebSocketFrameHandlerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Container for Client use of the jakarta.websocket API.
@ -53,6 +58,16 @@ import org.eclipse.jetty.websocket.jakarta.common.JakartaWebSocketFrameHandlerFa
@ManagedObject("JSR356 Client Container")
public class JakartaWebSocketClientContainer extends JakartaWebSocketContainer implements jakarta.websocket.WebSocketContainer
{
private static final Logger LOG = LoggerFactory.getLogger(JakartaWebSocketClientContainer.class);
private static final AtomicReference<ContainerLifeCycle> SHUTDOWN_CONTAINER = new AtomicReference<>();
public static void setShutdownContainer(ContainerLifeCycle container)
{
SHUTDOWN_CONTAINER.set(container);
if (LOG.isDebugEnabled())
LOG.debug("initialized {} to {}", String.format("%s@%x", SHUTDOWN_CONTAINER.getClass().getSimpleName(), SHUTDOWN_CONTAINER.hashCode()), container);
}
protected WebSocketCoreClient coreClient;
protected Function<WebSocketComponents, WebSocketCoreClient> coreClientFactory;
private final JakartaWebSocketClientFrameHandlerFactory frameHandlerFactory;
@ -262,4 +277,123 @@ public class JakartaWebSocketClientContainer extends JakartaWebSocketContainer i
return new AnnotatedClientEndpointConfig(anno);
}
@Override
protected void doStart() throws Exception
{
doClientStart();
super.doStart();
}
@Override
protected void doStop() throws Exception
{
super.doStop();
doClientStop();
}
protected void doClientStart()
{
if (LOG.isDebugEnabled())
LOG.debug("doClientStart() {}", this);
// If we are running in Jetty register shutdown with the ContextHandler.
if (addToContextHandler())
{
if (LOG.isDebugEnabled())
LOG.debug("Shutdown registered with ContextHandler");
return;
}
// If we are running inside a different ServletContainer we can register with the SHUTDOWN_CONTAINER static.
ContainerLifeCycle shutdownContainer = SHUTDOWN_CONTAINER.get();
if (shutdownContainer != null)
{
shutdownContainer.addManaged(this);
if (LOG.isDebugEnabled())
LOG.debug("Shutdown registered with ShutdownContainer {}", shutdownContainer);
return;
}
ShutdownThread.register(this);
if (LOG.isDebugEnabled())
LOG.debug("Shutdown registered with ShutdownThread");
}
protected void doClientStop()
{
if (LOG.isDebugEnabled())
LOG.debug("doClientStop() {}", this);
// Remove from context handler if running in Jetty server.
removeFromContextHandler();
// Remove from the Shutdown Container.
ContainerLifeCycle shutdownContainer = SHUTDOWN_CONTAINER.get();
if (shutdownContainer != null && shutdownContainer.contains(this))
{
// Un-manage first as we don't want to call stop again while in STOPPING state.
shutdownContainer.unmanage(this);
shutdownContainer.removeBean(this);
}
// If not running in a server we need to de-register with the shutdown thread.
ShutdownThread.deregister(this);
}
private boolean addToContextHandler()
{
try
{
Object context = getClass().getClassLoader()
.loadClass("org.eclipse.jetty.server.handler.ContextHandler")
.getMethod("getCurrentContext")
.invoke(null);
Object contextHandler = context.getClass()
.getMethod("getContextHandler")
.invoke(context);
contextHandler.getClass()
.getMethod("addManaged", LifeCycle.class)
.invoke(contextHandler, this);
return true;
}
catch (Throwable throwable)
{
if (LOG.isDebugEnabled())
LOG.debug("error from addToContextHandler() for {}", this, throwable);
return false;
}
}
private void removeFromContextHandler()
{
try
{
Object context = getClass().getClassLoader()
.loadClass("org.eclipse.jetty.server.handler.ContextHandler")
.getMethod("getCurrentContext")
.invoke(null);
Object contextHandler = context.getClass()
.getMethod("getContextHandler")
.invoke(context);
// Un-manage first as we don't want to call stop again while in STOPPING state.
contextHandler.getClass()
.getMethod("unmanage", Object.class)
.invoke(contextHandler, this);
contextHandler.getClass()
.getMethod("removeBean", Object.class)
.invoke(contextHandler, this);
}
catch (Throwable throwable)
{
if (LOG.isDebugEnabled())
LOG.debug("error from removeFromContextHandler() for {}", this, throwable);
}
}
}

View File

@ -11,7 +11,7 @@
// ========================================================================
//
package org.eclipse.jetty.websocket.jakarta.client;
package org.eclipse.jetty.websocket.jakarta.client.internal;
import jakarta.websocket.ClientEndpoint;
import jakarta.websocket.EndpointConfig;

View File

@ -0,0 +1 @@
org.eclipse.jetty.websocket.jakarta.client.JakartaWebSocketShutdownContainer

View File

@ -23,7 +23,7 @@ import org.eclipse.jetty.client.http.HttpClientTransportOverHTTP;
import org.eclipse.jetty.io.ClientConnector;
import org.eclipse.jetty.util.component.LifeCycle;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.websocket.jakarta.client.JakartaWebSocketClientContainer;
import org.eclipse.jetty.websocket.jakarta.client.internal.JakartaWebSocketClientContainer;
public class SecureClientContainerExample
{

View File

@ -28,7 +28,6 @@ import jakarta.websocket.server.ServerEndpoint;
import jakarta.websocket.server.ServerEndpointConfig;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.listener.ContainerInitializer;
import org.eclipse.jetty.util.TypeUtil;
import org.eclipse.jetty.util.thread.ThreadClassLoaderScope;
import org.eclipse.jetty.websocket.core.WebSocketComponents;
@ -52,6 +51,18 @@ public class JakartaWebSocketServletContainerInitializer implements ServletConta
public static final String HTTPCLIENT_ATTRIBUTE = "org.eclipse.jetty.websocket.jakarta.HttpClient";
private static final Logger LOG = LoggerFactory.getLogger(JakartaWebSocketServletContainerInitializer.class);
private final Configurator configurator;
public JakartaWebSocketServletContainerInitializer()
{
this(null);
}
public JakartaWebSocketServletContainerInitializer(Configurator configurator)
{
this.configurator = configurator;
}
/**
* Test a ServletContext for {@code init-param} or {@code attribute} at {@code keyName} for
* true or false setting that determines if the specified feature is enabled (or not).
@ -96,28 +107,7 @@ public class JakartaWebSocketServletContainerInitializer implements ServletConta
{
if (!context.isStopped())
throw new IllegalStateException("configure should be called before starting");
// In this embedded-jetty usage, allow ServletContext.addListener() to
// add other ServletContextListeners (such as the ContextDestroyListener) after
// the initialization phase is over. (important for this SCI to function)
context.getServletContext().setExtendedListenerTypes(true);
context.addEventListener(ContainerInitializer.asContextListener(new JakartaWebSocketServletContainerInitializer())
.afterStartup((servletContext) ->
{
JakartaWebSocketServerContainer serverContainer = JakartaWebSocketServerContainer.getContainer(servletContext);
if (configurator != null)
{
try
{
configurator.accept(servletContext, serverContainer);
}
catch (DeploymentException e)
{
throw new RuntimeException("Failed to deploy WebSocket Endpoint", e);
}
}
}));
context.addServletContainerInitializer(new JakartaWebSocketServletContainerInitializer(configurator));
}
/**
@ -270,6 +260,19 @@ public class JakartaWebSocketServletContainerInitializer implements ServletConta
}
}
}
// Call the configurator after startup.
if (configurator != null)
{
try
{
configurator.accept(context, container);
}
catch (DeploymentException e)
{
throw new RuntimeException("Failed to deploy WebSocket Endpoint", e);
}
}
}
@SuppressWarnings("unchecked")

View File

@ -36,7 +36,7 @@ import org.eclipse.jetty.websocket.core.exception.InvalidSignatureException;
import org.eclipse.jetty.websocket.core.internal.util.ReflectUtils;
import org.eclipse.jetty.websocket.core.server.WebSocketMappings;
import org.eclipse.jetty.websocket.core.server.WebSocketServerComponents;
import org.eclipse.jetty.websocket.jakarta.client.JakartaWebSocketClientContainer;
import org.eclipse.jetty.websocket.jakarta.client.internal.JakartaWebSocketClientContainer;
import org.eclipse.jetty.websocket.jakarta.server.config.ContainerDefaultConfigurator;
import org.eclipse.jetty.websocket.jakarta.server.config.JakartaWebSocketServletContainerInitializer;
import org.slf4j.Logger;
@ -297,4 +297,16 @@ public class JakartaWebSocketServerContainer extends JakartaWebSocketClientConta
deferredEndpointConfigs.clear();
}
}
@Override
protected void doClientStart()
{
// Do nothing.
}
@Override
protected void doClientStop()
{
// Do nothing.
}
}

View File

@ -20,7 +20,7 @@ import org.eclipse.jetty.websocket.core.FrameHandler;
import org.eclipse.jetty.websocket.core.server.FrameHandlerFactory;
import org.eclipse.jetty.websocket.core.server.ServerUpgradeRequest;
import org.eclipse.jetty.websocket.core.server.ServerUpgradeResponse;
import org.eclipse.jetty.websocket.jakarta.client.JakartaWebSocketClientFrameHandlerFactory;
import org.eclipse.jetty.websocket.jakarta.client.internal.JakartaWebSocketClientFrameHandlerFactory;
import org.eclipse.jetty.websocket.jakarta.common.JakartaWebSocketContainer;
import org.eclipse.jetty.websocket.jakarta.common.JakartaWebSocketFrameHandlerMetadata;

View File

@ -17,7 +17,9 @@ import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Random;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
@ -46,7 +48,16 @@ public class WSServer extends LocalServer implements LocalFuzzer.Provider
{
private static final Logger LOG = LoggerFactory.getLogger(WSServer.class);
private final Path testDir;
private ContextHandlerCollection contexts = new ContextHandlerCollection();
private final ContextHandlerCollection contexts = new ContextHandlerCollection();
public WSServer()
{
String baseDirName = Long.toString(Math.abs(new Random().nextLong()));
this.testDir = MavenTestingUtils.getTargetTestingPath(baseDirName);
if (Files.exists(testDir))
throw new IllegalStateException("TestDir already exists.");
FS.ensureDirExists(testDir);
}
public WSServer(Path testDir)
{

View File

@ -26,7 +26,7 @@ import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.util.BlockingArrayQueue;
import org.eclipse.jetty.util.component.Graceful;
import org.eclipse.jetty.websocket.jakarta.client.JakartaWebSocketClientContainer;
import org.eclipse.jetty.websocket.jakarta.client.internal.JakartaWebSocketClientContainer;
import org.eclipse.jetty.websocket.jakarta.server.config.JakartaWebSocketServletContainerInitializer;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;

View File

@ -0,0 +1,111 @@
//
// ========================================================================
// Copyright (c) 1995-2021 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.websocket.jakarta.tests;
import java.net.URI;
import java.util.Collection;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.websocket.ContainerProvider;
import jakarta.websocket.WebSocketContainer;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.websocket.jakarta.client.JakartaWebSocketShutdownContainer;
import org.eclipse.jetty.websocket.jakarta.client.internal.JakartaWebSocketClientContainer;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertNotNull;
public class JakartaClientShutdownWithServerEmbeddedTest
{
private Server server;
private ServletContextHandler contextHandler;
private URI serverUri;
private HttpClient httpClient;
private volatile WebSocketContainer container;
public class ContextHandlerShutdownServlet extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
{
container = ContainerProvider.getWebSocketContainer();
}
}
@BeforeEach
public void before() throws Exception
{
server = new Server();
ServerConnector connector = new ServerConnector(server);
server.addConnector(connector);
contextHandler = new ServletContextHandler();
contextHandler.setContextPath("/");
contextHandler.addServlet(new ServletHolder(new ContextHandlerShutdownServlet()), "/");
server.setHandler(contextHandler);
// Because we are using embedded we must manually add the Jakarta WS Client Shutdown SCI.
JakartaWebSocketShutdownContainer jakartaWebSocketClientShutdown = new JakartaWebSocketShutdownContainer();
contextHandler.addServletContainerInitializer(jakartaWebSocketClientShutdown);
server.start();
serverUri = WSURI.toWebsocket(server.getURI());
httpClient = new HttpClient();
httpClient.start();
}
@AfterEach
public void after() throws Exception
{
httpClient.stop();
server.stop();
}
@Test
public void testShutdownWithContextHandler() throws Exception
{
ContentResponse response = httpClient.GET(serverUri);
assertThat(response.getStatus(), is(HttpStatus.OK_200));
assertNotNull(container);
assertThat(container, instanceOf(JakartaWebSocketClientContainer.class));
JakartaWebSocketClientContainer clientContainer = (JakartaWebSocketClientContainer)container;
assertThat(clientContainer.isRunning(), is(true));
// The container should be a bean on the ContextHandler.
Collection<WebSocketContainer> containedBeans = contextHandler.getBeans(WebSocketContainer.class);
assertThat(containedBeans.size(), is(1));
assertThat(containedBeans.toArray()[0], is(container));
// The client should be attached to the servers LifeCycle and should stop with it.
server.stop();
assertThat(clientContainer.isRunning(), is(false));
assertThat(server.getContainedBeans(WebSocketContainer.class), empty());
}
}

View File

@ -0,0 +1,191 @@
//
// ========================================================================
// Copyright (c) 1995-2021 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.websocket.jakarta.tests;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.websocket.ContainerProvider;
import jakarta.websocket.WebSocketContainer;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.client.api.Response;
import org.eclipse.jetty.http.BadMessageException;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.io.ByteBufferPool;
import org.eclipse.jetty.util.component.ContainerLifeCycle;
import org.eclipse.jetty.webapp.Configuration;
import org.eclipse.jetty.webapp.Configurations;
import org.eclipse.jetty.websocket.core.WebSocketComponents;
import org.eclipse.jetty.websocket.core.client.CoreClientUpgradeRequest;
import org.eclipse.jetty.websocket.jakarta.client.JakartaWebSocketClientContainerProvider;
import org.eclipse.jetty.websocket.jakarta.client.JakartaWebSocketShutdownContainer;
import org.eclipse.jetty.websocket.jakarta.common.JakartaWebSocketContainer;
import org.eclipse.jetty.websocket.jakarta.server.config.JakartaWebSocketConfiguration;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
public class JakartaClientShutdownWithServerWebAppTest
{
private WSServer server;
private HttpClient httpClient;
@FunctionalInterface
interface ThrowingRunnable
{
void run() throws Exception;
}
public void start(ThrowingRunnable configuration) throws Exception
{
server = new WSServer();
configuration.run();
server.start();
httpClient = new HttpClient();
httpClient.start();
}
@AfterEach
public void after() throws Exception
{
httpClient.stop();
server.stop();
}
@WebServlet("/")
public static class ContextHandlerShutdownServlet extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
{
ContainerProvider.getWebSocketContainer();
}
}
public WSServer.WebApp createWebSocketWebapp(String contextName) throws Exception
{
WSServer.WebApp app = server.createWebApp(contextName);
// Exclude the Jakarta WebSocket configuration from the webapp.
Configuration[] configurations = Configurations.getKnown().stream()
.filter(configuration -> !(configuration instanceof JakartaWebSocketConfiguration))
.toArray(Configuration[]::new);
app.getWebAppContext().setConfigurations(configurations);
// Copy over the individual jars required for Jakarta WebSocket.
app.createWebInf();
app.copyLib(JakartaWebSocketClientContainerProvider.class, "websocket-jakarta-client.jar");
app.copyLib(JakartaWebSocketContainer.class, "websocket-jakarta-common.jar");
app.copyLib(ContainerLifeCycle.class, "jetty-util.jar");
app.copyLib(CoreClientUpgradeRequest.class, "websocket-core-client.jar");
app.copyLib(WebSocketComponents.class, "websocket-core-common.jar");
app.copyLib(Response.class, "jetty-client.jar");
app.copyLib(ByteBufferPool.class, "jetty-io.jar");
app.copyLib(BadMessageException.class, "jetty-http.jar");
return app;
}
@Test
public void websocketProvidedByServer() throws Exception
{
start(() ->
{
WSServer.WebApp app1 = server.createWebApp("app1");
app1.createWebInf();
app1.copyClass(ContextHandlerShutdownServlet.class);
app1.deploy();
WSServer.WebApp app2 = server.createWebApp("app2");
app2.createWebInf();
app2.copyClass(ContextHandlerShutdownServlet.class);
app2.deploy();
WSServer.WebApp app3 = server.createWebApp("app3");
app3.createWebInf();
app3.copyClass(ContextHandlerShutdownServlet.class);
app3.deploy();
});
// Before connecting to the server there is only the containers created for the server component of each WebApp.
assertThat(server.isRunning(), is(true));
assertThat(server.getContainedBeans(WebSocketContainer.class).size(), is(3));
// After hitting each WebApp with a request we now have an additional 3 client containers.
ContentResponse response = httpClient.GET(server.getServerUri().resolve("/app1"));
assertThat(response.getStatus(), is(HttpStatus.OK_200));
response = httpClient.GET(server.getServerUri().resolve("/app2"));
assertThat(response.getStatus(), is(HttpStatus.OK_200));
response = httpClient.GET(server.getServerUri().resolve("/app3"));
assertThat(response.getStatus(), is(HttpStatus.OK_200));
assertThat(server.getContainedBeans(WebSocketContainer.class).size(), is(6));
// All the websocket containers are removed on stopping of the server.
server.stop();
assertThat(server.isRunning(), is(false));
assertThat(server.getContainedBeans(WebSocketContainer.class).size(), is(0));
}
@Test
public void websocketProvidedByWebApp() throws Exception
{
start(() ->
{
WSServer.WebApp app1 = createWebSocketWebapp("app1");
app1.copyClass(ContextHandlerShutdownServlet.class);
app1.deploy();
WSServer.WebApp app2 = createWebSocketWebapp("app2");
app2.copyClass(ContextHandlerShutdownServlet.class);
app2.deploy();
WSServer.WebApp app3 = createWebSocketWebapp("app3");
app3.copyClass(ContextHandlerShutdownServlet.class);
app3.deploy();
});
// Before connecting to the server there is only the containers created for the server component of each WebApp.
assertThat(server.isRunning(), is(true));
assertThat(server.getContainedBeans(WebSocketContainer.class).size(), is(0));
// After hitting each WebApp with a request we now have an additional 3 client containers.
ContentResponse response = httpClient.GET(server.getServerUri().resolve("/app1"));
assertThat(response.getStatus(), is(HttpStatus.OK_200));
response = httpClient.GET(server.getServerUri().resolve("/app2"));
assertThat(response.getStatus(), is(HttpStatus.OK_200));
response = httpClient.GET(server.getServerUri().resolve("/app3"));
assertThat(response.getStatus(), is(HttpStatus.OK_200));
// Collect the toString result of the ShutdownContainers from the dump.
List<String> results = Arrays.stream(server.getServer().dump().split("\n"))
.filter(line -> line.contains("+> " + JakartaWebSocketShutdownContainer.class.getSimpleName()))
.collect(Collectors.toList());
// We only have 3 Shutdown Containers and they all contain only 1 item to be shutdown.
assertThat(results.size(), is(3));
for (String result : results)
{
assertThat(result, containsString("size=1"));
}
}
}

View File

@ -29,7 +29,7 @@ import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.util.BlockingArrayQueue;
import org.eclipse.jetty.websocket.jakarta.client.JakartaWebSocketClientContainer;
import org.eclipse.jetty.websocket.jakarta.client.internal.JakartaWebSocketClientContainer;
import org.eclipse.jetty.websocket.jakarta.server.config.JakartaWebSocketServletContainerInitializer;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;

View File

@ -28,7 +28,7 @@ import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.websocket.jakarta.client.JakartaWebSocketClientContainer;
import org.eclipse.jetty.websocket.jakarta.client.internal.JakartaWebSocketClientContainer;
import org.eclipse.jetty.websocket.jakarta.server.config.JakartaWebSocketServletContainerInitializer;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

View File

@ -20,7 +20,7 @@ import jakarta.websocket.CloseReason;
import org.eclipse.jetty.util.Jetty;
import org.eclipse.jetty.util.UrlEncoded;
import org.eclipse.jetty.util.component.LifeCycle;
import org.eclipse.jetty.websocket.jakarta.client.JakartaWebSocketClientContainer;
import org.eclipse.jetty.websocket.jakarta.client.internal.JakartaWebSocketClientContainer;
import org.eclipse.jetty.websocket.jakarta.tests.EventSocket;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

View File

@ -14,8 +14,8 @@
package org.eclipse.jetty.websocket.jakarta.tests.client;
import org.eclipse.jetty.websocket.core.CoreSession;
import org.eclipse.jetty.websocket.jakarta.client.BasicClientEndpointConfig;
import org.eclipse.jetty.websocket.jakarta.client.JakartaWebSocketClientContainer;
import org.eclipse.jetty.websocket.jakarta.client.internal.BasicClientEndpointConfig;
import org.eclipse.jetty.websocket.jakarta.client.internal.JakartaWebSocketClientContainer;
import org.eclipse.jetty.websocket.jakarta.common.JakartaWebSocketContainer;
import org.eclipse.jetty.websocket.jakarta.common.JakartaWebSocketFrameHandler;
import org.eclipse.jetty.websocket.jakarta.common.JakartaWebSocketSession;

View File

@ -22,8 +22,8 @@ import jakarta.websocket.ClientEndpointConfig;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.websocket.core.CloseStatus;
import org.eclipse.jetty.websocket.core.CoreSession;
import org.eclipse.jetty.websocket.jakarta.client.BasicClientEndpointConfig;
import org.eclipse.jetty.websocket.jakarta.client.JakartaWebSocketClientContainer;
import org.eclipse.jetty.websocket.jakarta.client.internal.BasicClientEndpointConfig;
import org.eclipse.jetty.websocket.jakarta.client.internal.JakartaWebSocketClientContainer;
import org.eclipse.jetty.websocket.jakarta.common.JakartaWebSocketFrameHandler;
import org.eclipse.jetty.websocket.jakarta.common.UpgradeRequest;
import org.eclipse.jetty.websocket.jakarta.common.UpgradeRequestAdapter;

View File

@ -25,9 +25,9 @@ import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.websocket.core.CoreSession;
import org.eclipse.jetty.websocket.core.Frame;
import org.eclipse.jetty.websocket.core.OpCode;
import org.eclipse.jetty.websocket.jakarta.client.BasicClientEndpointConfig;
import org.eclipse.jetty.websocket.jakarta.client.JakartaWebSocketClientContainer;
import org.eclipse.jetty.websocket.jakarta.client.JakartaWebSocketClientFrameHandlerFactory;
import org.eclipse.jetty.websocket.jakarta.client.internal.BasicClientEndpointConfig;
import org.eclipse.jetty.websocket.jakarta.client.internal.JakartaWebSocketClientContainer;
import org.eclipse.jetty.websocket.jakarta.client.internal.JakartaWebSocketClientFrameHandlerFactory;
import org.eclipse.jetty.websocket.jakarta.common.ConfiguredEndpoint;
import org.eclipse.jetty.websocket.jakarta.common.JakartaWebSocketFrameHandler;
import org.eclipse.jetty.websocket.jakarta.common.JakartaWebSocketFrameHandlerFactory;

View File

@ -19,7 +19,7 @@ import jakarta.websocket.EndpointConfig;
import jakarta.websocket.Session;
import jakarta.websocket.server.ServerEndpoint;
import org.eclipse.jetty.websocket.core.exception.WebSocketWriteTimeoutException;
import org.eclipse.jetty.websocket.jakarta.client.JakartaWebSocketClientContainer;
import org.eclipse.jetty.websocket.jakarta.client.internal.JakartaWebSocketClientContainer;
import org.eclipse.jetty.websocket.jakarta.common.JakartaWebSocketContainer;
import org.eclipse.jetty.websocket.jakarta.tests.EventSocket;
import org.eclipse.jetty.websocket.jakarta.tests.LocalServer;

View File

@ -26,7 +26,7 @@ import jakarta.websocket.Encoder;
import jakarta.websocket.EndpointConfig;
import org.eclipse.jetty.toolchain.test.Hex;
import org.eclipse.jetty.websocket.core.exception.InvalidWebSocketException;
import org.eclipse.jetty.websocket.jakarta.client.BasicClientEndpointConfig;
import org.eclipse.jetty.websocket.jakarta.client.internal.BasicClientEndpointConfig;
import org.eclipse.jetty.websocket.jakarta.common.encoders.AvailableEncoders;
import org.eclipse.jetty.websocket.jakarta.common.encoders.IntegerEncoder;
import org.junit.jupiter.api.BeforeAll;

View File

@ -35,7 +35,7 @@ import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.websocket.core.exception.InvalidWebSocketException;
import org.eclipse.jetty.websocket.jakarta.client.JakartaWebSocketClientContainer;
import org.eclipse.jetty.websocket.jakarta.client.internal.JakartaWebSocketClientContainer;
import org.eclipse.jetty.websocket.jakarta.common.decoders.AbstractDecoder;
import org.eclipse.jetty.websocket.jakarta.server.config.JakartaWebSocketServletContainerInitializer;
import org.eclipse.jetty.websocket.jakarta.tests.EventSocket;

View File

@ -19,7 +19,7 @@ import java.util.Map;
import jakarta.websocket.EndpointConfig;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.websocket.jakarta.client.BasicClientEndpointConfig;
import org.eclipse.jetty.websocket.jakarta.client.internal.BasicClientEndpointConfig;
import org.eclipse.jetty.websocket.jakarta.common.decoders.AvailableDecoders;
import org.eclipse.jetty.websocket.jakarta.common.encoders.AvailableEncoders;
import org.eclipse.jetty.websocket.jakarta.server.config.JakartaWebSocketServletContainerInitializer;

View File

@ -37,7 +37,7 @@ import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.websocket.core.CloseStatus;
import org.eclipse.jetty.websocket.core.Frame;
import org.eclipse.jetty.websocket.core.OpCode;
import org.eclipse.jetty.websocket.jakarta.client.JakartaWebSocketClientContainer;
import org.eclipse.jetty.websocket.jakarta.client.internal.JakartaWebSocketClientContainer;
import org.eclipse.jetty.websocket.jakarta.tests.DataUtils;
import org.eclipse.jetty.websocket.jakarta.tests.Fuzzer;
import org.eclipse.jetty.websocket.jakarta.tests.LocalServer;

View File

@ -27,7 +27,7 @@ import jakarta.websocket.Session;
import jakarta.websocket.server.ServerEndpoint;
import org.eclipse.jetty.toolchain.test.Hex;
import org.eclipse.jetty.websocket.core.CloseStatus;
import org.eclipse.jetty.websocket.jakarta.client.JakartaWebSocketClientContainer;
import org.eclipse.jetty.websocket.jakarta.client.internal.JakartaWebSocketClientContainer;
import org.eclipse.jetty.websocket.jakarta.tests.EventSocket;
import org.eclipse.jetty.websocket.jakarta.tests.LocalServer;
import org.junit.jupiter.api.AfterAll;

View File

@ -26,7 +26,7 @@ import jakarta.websocket.Session;
import jakarta.websocket.server.ServerContainer;
import jakarta.websocket.server.ServerEndpoint;
import org.eclipse.jetty.websocket.core.CloseStatus;
import org.eclipse.jetty.websocket.jakarta.client.JakartaWebSocketClientContainer;
import org.eclipse.jetty.websocket.jakarta.client.internal.JakartaWebSocketClientContainer;
import org.eclipse.jetty.websocket.jakarta.tests.EventSocket;
import org.eclipse.jetty.websocket.jakarta.tests.LocalServer;
import org.junit.jupiter.api.AfterAll;

View File

@ -22,7 +22,7 @@ import jakarta.websocket.OnOpen;
import jakarta.websocket.Session;
import jakarta.websocket.server.ServerEndpoint;
import org.eclipse.jetty.util.BlockingArrayQueue;
import org.eclipse.jetty.websocket.jakarta.client.JakartaWebSocketClientContainer;
import org.eclipse.jetty.websocket.jakarta.client.internal.JakartaWebSocketClientContainer;
import org.eclipse.jetty.websocket.jakarta.tests.EventSocket;
import org.eclipse.jetty.websocket.jakarta.tests.LocalServer;
import org.junit.jupiter.api.AfterAll;

View File

@ -18,7 +18,6 @@ import java.util.Set;
import jakarta.servlet.ServletContainerInitializer;
import jakarta.servlet.ServletContext;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.listener.ContainerInitializer;
import org.eclipse.jetty.websocket.core.WebSocketComponents;
import org.eclipse.jetty.websocket.core.server.WebSocketMappings;
import org.eclipse.jetty.websocket.core.server.WebSocketServerComponents;
@ -32,6 +31,17 @@ import org.slf4j.LoggerFactory;
public class JettyWebSocketServletContainerInitializer implements ServletContainerInitializer
{
private static final Logger LOG = LoggerFactory.getLogger(JettyWebSocketServletContainerInitializer.class);
private final Configurator configurator;
public JettyWebSocketServletContainerInitializer()
{
this(null);
}
public JettyWebSocketServletContainerInitializer(Configurator configurator)
{
this.configurator = configurator;
}
public interface Configurator
{
@ -50,18 +60,7 @@ public class JettyWebSocketServletContainerInitializer implements ServletContain
{
if (!context.isStopped())
throw new IllegalStateException("configure should be called before starting");
context.addEventListener(
ContainerInitializer
.asContextListener(new JettyWebSocketServletContainerInitializer())
.afterStartup((servletContext) ->
{
if (configurator != null)
{
JettyWebSocketServerContainer container = JettyWebSocketServerContainer.getContainer(servletContext);
configurator.accept(servletContext, container);
}
}));
context.addServletContainerInitializer(new JettyWebSocketServletContainerInitializer(configurator));
}
/**
@ -101,5 +100,10 @@ public class JettyWebSocketServletContainerInitializer implements ServletContain
JettyWebSocketServerContainer container = JettyWebSocketServletContainerInitializer.initialize(contextHandler);
if (LOG.isDebugEnabled())
LOG.debug("onStartup {}", container);
if (configurator != null)
{
configurator.accept(context, container);
}
}
}