Merge pull request #6293 from eclipse/jetty-10.0.x-6287-WebSocketClientClassLoading
Issue #6287 - fix classloading for WebSocketClient in webapp
This commit is contained in:
commit
455e798906
|
@ -442,6 +442,7 @@ public abstract class CoreClientUpgradeRequest extends HttpRequest implements Re
|
|||
WebSocketConstants.SPEC_VERSION_STRING);
|
||||
|
||||
WebSocketCoreSession coreSession = new WebSocketCoreSession(frameHandler, Behavior.CLIENT, negotiated, wsClient.getWebSocketComponents());
|
||||
coreSession.setClassLoader(wsClient.getClassLoader());
|
||||
customizer.customize(coreSession);
|
||||
|
||||
HttpClient httpClient = wsClient.getHttpClient();
|
||||
|
|
|
@ -38,6 +38,7 @@ public class WebSocketCoreClient extends ContainerLifeCycle
|
|||
private static final Logger LOG = LoggerFactory.getLogger(WebSocketCoreClient.class);
|
||||
private final HttpClient httpClient;
|
||||
private final WebSocketComponents components;
|
||||
private ClassLoader classLoader;
|
||||
|
||||
// TODO: Things to consider for inclusion in this class (or removal if they can be set elsewhere, like HttpClient)
|
||||
// - AsyncWrite Idle Timeout
|
||||
|
@ -61,12 +62,23 @@ public class WebSocketCoreClient extends ContainerLifeCycle
|
|||
if (httpClient == null)
|
||||
httpClient = Objects.requireNonNull(HttpClientProvider.get());
|
||||
|
||||
this.classLoader = Thread.currentThread().getContextClassLoader();
|
||||
this.httpClient = httpClient;
|
||||
this.components = webSocketComponents;
|
||||
addBean(httpClient);
|
||||
addBean(webSocketComponents);
|
||||
}
|
||||
|
||||
public ClassLoader getClassLoader()
|
||||
{
|
||||
return classLoader;
|
||||
}
|
||||
|
||||
public void setClassLoader(ClassLoader classLoader)
|
||||
{
|
||||
this.classLoader = Objects.requireNonNull(classLoader);
|
||||
}
|
||||
|
||||
public CompletableFuture<CoreSession> connect(FrameHandler frameHandler, URI wsUri) throws IOException
|
||||
{
|
||||
CoreClientUpgradeRequest request = CoreClientUpgradeRequest.from(this, wsUri, frameHandler);
|
||||
|
|
|
@ -80,9 +80,11 @@ public class WebSocketCoreSession implements IncomingFrames, CoreSession, Dumpab
|
|||
private long maxTextMessageSize = WebSocketConstants.DEFAULT_MAX_TEXT_MESSAGE_SIZE;
|
||||
private Duration idleTimeout = WebSocketConstants.DEFAULT_IDLE_TIMEOUT;
|
||||
private Duration writeTimeout = WebSocketConstants.DEFAULT_WRITE_TIMEOUT;
|
||||
private ClassLoader classLoader;
|
||||
|
||||
public WebSocketCoreSession(FrameHandler handler, Behavior behavior, Negotiated negotiated, WebSocketComponents components)
|
||||
{
|
||||
this.classLoader = Thread.currentThread().getContextClassLoader();
|
||||
this.components = components;
|
||||
this.handler = handler;
|
||||
this.behavior = behavior;
|
||||
|
@ -91,13 +93,32 @@ public class WebSocketCoreSession implements IncomingFrames, CoreSession, Dumpab
|
|||
negotiated.getExtensions().initialize(new IncomingAdaptor(), new OutgoingAdaptor(), this);
|
||||
}
|
||||
|
||||
public ClassLoader getClassLoader()
|
||||
{
|
||||
return classLoader;
|
||||
}
|
||||
|
||||
public void setClassLoader(ClassLoader classLoader)
|
||||
{
|
||||
this.classLoader = classLoader;
|
||||
}
|
||||
|
||||
/**
|
||||
* Can be overridden to scope into the correct classloader before calling application code.
|
||||
* @param runnable the runnable to execute.
|
||||
*/
|
||||
protected void handle(Runnable runnable)
|
||||
{
|
||||
runnable.run();
|
||||
ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
|
||||
try
|
||||
{
|
||||
Thread.currentThread().setContextClassLoader(classLoader);
|
||||
runnable.run();
|
||||
}
|
||||
finally
|
||||
{
|
||||
Thread.currentThread().setContextClassLoader(oldClassLoader);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -209,7 +209,7 @@ public abstract class AbstractHandshaker implements Handshaker
|
|||
if (contextHandler != null)
|
||||
contextHandler.handle(runnable);
|
||||
else
|
||||
runnable.run();
|
||||
super.handle(runnable);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
@ -100,6 +100,7 @@ public class WSServer extends LocalServer implements LocalFuzzer.Provider
|
|||
// Configure the WebAppContext.
|
||||
context = new WebAppContext();
|
||||
context.setContextPath("/" + contextName);
|
||||
context.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "false");
|
||||
context.setBaseResource(new PathResource(contextDir));
|
||||
context.setAttribute("org.eclipse.jetty.websocket.javax", Boolean.TRUE);
|
||||
context.addConfiguration(new JavaxWebSocketConfiguration());
|
||||
|
|
|
@ -0,0 +1,217 @@
|
|||
//
|
||||
// ========================================================================
|
||||
// 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.javax.tests;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.websocket.ClientEndpoint;
|
||||
import javax.websocket.ContainerProvider;
|
||||
import javax.websocket.OnMessage;
|
||||
import javax.websocket.OnOpen;
|
||||
import javax.websocket.Session;
|
||||
import javax.websocket.WebSocketContainer;
|
||||
import javax.websocket.server.ServerEndpoint;
|
||||
|
||||
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.javax.client.JavaxWebSocketClientContainerProvider;
|
||||
import org.eclipse.jetty.websocket.javax.common.JavaxWebSocketContainer;
|
||||
import org.eclipse.jetty.websocket.javax.server.config.JavaxWebSocketConfiguration;
|
||||
import org.eclipse.jetty.xml.XmlConfiguration;
|
||||
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;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
public class JavaxClientClassLoaderTest
|
||||
{
|
||||
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();
|
||||
}
|
||||
|
||||
@ClientEndpoint()
|
||||
public static class ClientSocket
|
||||
{
|
||||
LinkedBlockingQueue<String> textMessages = new LinkedBlockingQueue<>();
|
||||
|
||||
@OnOpen
|
||||
public void onOpen(Session session)
|
||||
{
|
||||
session.getAsyncRemote().sendText("ContextClassLoader: " + Thread.currentThread().getContextClassLoader());
|
||||
}
|
||||
|
||||
@OnMessage
|
||||
public void onMessage(String message)
|
||||
{
|
||||
textMessages.add(message);
|
||||
}
|
||||
}
|
||||
|
||||
@WebServlet("/servlet")
|
||||
public static class WebSocketClientServlet extends HttpServlet
|
||||
{
|
||||
private final WebSocketContainer clientContainer = ContainerProvider.getWebSocketContainer();
|
||||
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
|
||||
{
|
||||
URI wsEchoUri = URI.create("ws://localhost:" + req.getServerPort() + "/echo/");
|
||||
ClientSocket clientSocket = new ClientSocket();
|
||||
|
||||
try (Session ignored = clientContainer.connectToServer(clientSocket, wsEchoUri))
|
||||
{
|
||||
String recv = clientSocket.textMessages.poll(5, TimeUnit.SECONDS);
|
||||
assertNotNull(recv);
|
||||
resp.setStatus(HttpStatus.OK_200);
|
||||
resp.getWriter().println(recv);
|
||||
resp.getWriter().println("ClientClassLoader: " + clientContainer.getClass().getClassLoader());
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ServerEndpoint("/")
|
||||
public static class EchoSocket
|
||||
{
|
||||
@OnMessage
|
||||
public void onMessage(Session session, String message) throws Exception
|
||||
{
|
||||
session.getBasicRemote().sendText(message);
|
||||
}
|
||||
}
|
||||
|
||||
public WSServer.WebApp createWebSocketWebapp(String contextName) throws Exception
|
||||
{
|
||||
WSServer.WebApp app = server.createWebApp(contextName);
|
||||
|
||||
// Exclude the Javax WebSocket configuration from the webapp (so we use versions from the webapp).
|
||||
Configuration[] configurations = Configurations.getKnown().stream()
|
||||
.filter(configuration -> !(configuration instanceof JavaxWebSocketConfiguration))
|
||||
.toArray(Configuration[]::new);
|
||||
app.getWebAppContext().setConfigurations(configurations);
|
||||
|
||||
// Copy over the individual jars required for Javax WebSocket.
|
||||
app.createWebInf();
|
||||
app.copyLib(JavaxWebSocketClientContainerProvider.class, "websocket-javax-client.jar");
|
||||
app.copyLib(JavaxWebSocketContainer.class, "websocket-javax-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");
|
||||
app.copyLib(XmlConfiguration.class, "jetty-xml.jar");
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void websocketProvidedByServer() throws Exception
|
||||
{
|
||||
start(() ->
|
||||
{
|
||||
WSServer.WebApp app1 = server.createWebApp("app");
|
||||
app1.createWebInf();
|
||||
app1.copyClass(WebSocketClientServlet.class);
|
||||
app1.copyClass(ClientSocket.class);
|
||||
app1.deploy();
|
||||
|
||||
WSServer.WebApp app2 = server.createWebApp("echo");
|
||||
app2.createWebInf();
|
||||
app2.copyClass(EchoSocket.class);
|
||||
app2.deploy();
|
||||
});
|
||||
|
||||
// After hitting each WebApp we will get 200 response if test succeeds.
|
||||
ContentResponse response = httpClient.GET(server.getServerUri().resolve("/app/servlet"));
|
||||
assertThat(response.getStatus(), is(HttpStatus.OK_200));
|
||||
|
||||
// The ContextClassLoader in the WebSocketClients onOpen was the WebAppClassloader.
|
||||
assertThat(response.getContentAsString(), containsString("ContextClassLoader: WebAppClassLoader"));
|
||||
|
||||
// Verify that we used Servers version of WebSocketClient.
|
||||
ClassLoader serverClassLoader = server.getServer().getClass().getClassLoader();
|
||||
assertThat(response.getContentAsString(), containsString("ClientClassLoader: " + serverClassLoader)); }
|
||||
|
||||
@Test
|
||||
public void websocketProvidedByWebApp() throws Exception
|
||||
{
|
||||
start(() ->
|
||||
{
|
||||
WSServer.WebApp app1 = createWebSocketWebapp("app");
|
||||
app1.createWebInf();
|
||||
app1.copyClass(WebSocketClientServlet.class);
|
||||
app1.copyClass(ClientSocket.class);
|
||||
app1.copyClass(EchoSocket.class);
|
||||
app1.deploy();
|
||||
|
||||
// Do not exclude JavaxWebSocketConfiguration for this webapp (we need the websocket server classes).
|
||||
WSServer.WebApp app2 = server.createWebApp("echo");
|
||||
app2.createWebInf();
|
||||
app2.copyClass(EchoSocket.class);
|
||||
app2.deploy();
|
||||
});
|
||||
|
||||
// After hitting each WebApp we will get 200 response if test succeeds.
|
||||
ContentResponse response = httpClient.GET(server.getServerUri().resolve("/app/servlet"));
|
||||
assertThat(response.getStatus(), is(HttpStatus.OK_200));
|
||||
|
||||
// The ContextClassLoader in the WebSocketClients onOpen was the WebAppClassloader.
|
||||
assertThat(response.getContentAsString(), containsString("ContextClassLoader: WebAppClassLoader"));
|
||||
|
||||
// Verify that we used WebApps version of WebSocketClient.
|
||||
assertThat(response.getContentAsString(), containsString("ClientClassLoader: WebAppClassLoader"));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,245 @@
|
|||
//
|
||||
// ========================================================================
|
||||
// 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.tests;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import javax.servlet.ServletConfig;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
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.websocket.api.Session;
|
||||
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
|
||||
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
|
||||
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
|
||||
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
|
||||
import org.eclipse.jetty.websocket.client.WebSocketClient;
|
||||
import org.eclipse.jetty.websocket.client.config.JettyWebSocketClientConfiguration;
|
||||
import org.eclipse.jetty.websocket.common.WebSocketSession;
|
||||
import org.eclipse.jetty.websocket.core.WebSocketComponents;
|
||||
import org.eclipse.jetty.websocket.core.client.CoreClientUpgradeRequest;
|
||||
import org.eclipse.jetty.websocket.server.JettyWebSocketServlet;
|
||||
import org.eclipse.jetty.websocket.server.JettyWebSocketServletFactory;
|
||||
import org.eclipse.jetty.websocket.server.config.JettyWebSocketConfiguration;
|
||||
import org.eclipse.jetty.xml.XmlConfiguration;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.condition.DisabledOnJre;
|
||||
import org.junit.jupiter.api.condition.JRE;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
public class JettyClientClassLoaderTest
|
||||
{
|
||||
private final WebAppTester webAppTester = new WebAppTester();
|
||||
private final HttpClient httpClient = new HttpClient();
|
||||
|
||||
@FunctionalInterface
|
||||
interface ThrowingRunnable
|
||||
{
|
||||
void run() throws Exception;
|
||||
}
|
||||
|
||||
public void start(ThrowingRunnable configuration) throws Exception
|
||||
{
|
||||
configuration.run();
|
||||
webAppTester.start();
|
||||
httpClient.start();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void after() throws Exception
|
||||
{
|
||||
httpClient.stop();
|
||||
webAppTester.stop();
|
||||
}
|
||||
|
||||
@WebSocket
|
||||
public static class ClientSocket
|
||||
{
|
||||
LinkedBlockingQueue<String> textMessages = new LinkedBlockingQueue<>();
|
||||
|
||||
@OnWebSocketConnect
|
||||
public void onOpen(Session session) throws Exception
|
||||
{
|
||||
session.getRemote().sendString("ContextClassLoader: " + Thread.currentThread().getContextClassLoader());
|
||||
}
|
||||
|
||||
@OnWebSocketMessage
|
||||
public void onMessage(String message)
|
||||
{
|
||||
textMessages.add(message);
|
||||
}
|
||||
}
|
||||
|
||||
@WebServlet("/servlet")
|
||||
public static class WebSocketClientServlet extends HttpServlet
|
||||
{
|
||||
private final WebSocketClient clientContainer = new WebSocketClient();
|
||||
|
||||
@Override
|
||||
public void init(ServletConfig config) throws ServletException
|
||||
{
|
||||
try
|
||||
{
|
||||
clientContainer.start();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new ServletException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
|
||||
{
|
||||
URI wsEchoUri = URI.create("ws://localhost:" + req.getServerPort() + "/echo");
|
||||
ClientSocket clientSocket = new ClientSocket();
|
||||
|
||||
try (Session ignored = clientContainer.connect(clientSocket, wsEchoUri).get(5, TimeUnit.SECONDS))
|
||||
{
|
||||
String recv = clientSocket.textMessages.poll(5, TimeUnit.SECONDS);
|
||||
assertNotNull(recv);
|
||||
resp.setStatus(HttpStatus.OK_200);
|
||||
resp.getWriter().println(recv);
|
||||
resp.getWriter().println("ClientClassLoader: " + clientContainer.getClass().getClassLoader());
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@WebServlet("/")
|
||||
public static class EchoServlet extends JettyWebSocketServlet
|
||||
{
|
||||
@Override
|
||||
protected void configure(JettyWebSocketServletFactory factory)
|
||||
{
|
||||
factory.register(EchoSocket.class);
|
||||
}
|
||||
}
|
||||
|
||||
@WebSocket
|
||||
public static class EchoSocket
|
||||
{
|
||||
@OnWebSocketMessage
|
||||
public void onMessage(Session session, String message) throws Exception
|
||||
{
|
||||
session.getRemote().sendString(message);
|
||||
}
|
||||
}
|
||||
|
||||
public WebAppTester.WebApp createWebSocketWebapp(String contextName) throws Exception
|
||||
{
|
||||
WebAppTester.WebApp app = webAppTester.createWebApp(contextName);
|
||||
|
||||
// Copy over the individual jars required for Javax WebSocket.
|
||||
app.createWebInf();
|
||||
app.copyLib(WebSocketPolicy.class, "websocket-jetty-api.jar");
|
||||
app.copyLib(WebSocketClient.class, "websocket-jetty-client.jar");
|
||||
app.copyLib(WebSocketSession.class, "websocket-jetty-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");
|
||||
app.copyLib(XmlConfiguration.class, "jetty-xml.jar");
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void websocketProvidedByServer() throws Exception
|
||||
{
|
||||
start(() ->
|
||||
{
|
||||
WebAppTester.WebApp app1 = webAppTester.createWebApp("/app");
|
||||
app1.addConfiguration(new JettyWebSocketClientConfiguration());
|
||||
app1.createWebInf();
|
||||
app1.copyClass(WebSocketClientServlet.class);
|
||||
app1.copyClass(ClientSocket.class);
|
||||
app1.deploy();
|
||||
|
||||
WebAppTester.WebApp app2 = webAppTester.createWebApp("/echo");
|
||||
app2.addConfiguration(new JettyWebSocketConfiguration());
|
||||
app2.createWebInf();
|
||||
app2.copyClass(EchoServlet.class);
|
||||
app2.copyClass(EchoSocket.class);
|
||||
app2.deploy();
|
||||
});
|
||||
|
||||
// After hitting each WebApp we will get 200 response if test succeeds.
|
||||
ContentResponse response = httpClient.GET(webAppTester.getServerUri().resolve("/app/servlet"));
|
||||
assertThat(response.getStatus(), is(HttpStatus.OK_200));
|
||||
|
||||
// The ContextClassLoader in the WebSocketClients onOpen was the WebAppClassloader.
|
||||
assertThat(response.getContentAsString(), containsString("ContextClassLoader: WebAppClassLoader"));
|
||||
|
||||
// Verify that we used Servers version of WebSocketClient.
|
||||
ClassLoader serverClassLoader = webAppTester.getServer().getClass().getClassLoader();
|
||||
assertThat(response.getContentAsString(), containsString("ClientClassLoader: " + serverClassLoader));
|
||||
}
|
||||
|
||||
/**
|
||||
* This reproduces some classloading issue with MethodHandles in JDK14-110, This has been fixed in JDK16.
|
||||
* @see <a href="https://bugs.openjdk.java.net/browse/JDK-8244090">JDK-8244090</a>
|
||||
*/
|
||||
@DisabledOnJre({JRE.JAVA_14, JRE.JAVA_15})
|
||||
@Test
|
||||
public void websocketProvidedByWebApp() throws Exception
|
||||
{
|
||||
start(() ->
|
||||
{
|
||||
WebAppTester.WebApp app1 = createWebSocketWebapp("/app");
|
||||
app1.createWebInf();
|
||||
app1.copyClass(WebSocketClientServlet.class);
|
||||
app1.copyClass(ClientSocket.class);
|
||||
app1.deploy();
|
||||
|
||||
WebAppTester.WebApp app2 = webAppTester.createWebApp("/echo");
|
||||
app2.addConfiguration(new JettyWebSocketConfiguration());
|
||||
app2.createWebInf();
|
||||
app2.copyClass(EchoServlet.class);
|
||||
app2.copyClass(EchoSocket.class);
|
||||
app2.deploy();
|
||||
});
|
||||
|
||||
// After hitting each WebApp we will get 200 response if test succeeds.
|
||||
ContentResponse response = httpClient.GET(webAppTester.getServerUri().resolve("/app/servlet"));
|
||||
assertThat(response.getStatus(), is(HttpStatus.OK_200));
|
||||
|
||||
// The ContextClassLoader in the WebSocketClients onOpen was the WebAppClassloader.
|
||||
assertThat(response.getContentAsString(), containsString("ContextClassLoader: WebAppClassLoader"));
|
||||
|
||||
// Verify that we used WebApps version of WebSocketClient.
|
||||
assertThat(response.getContentAsString(), containsString("ClientClassLoader: WebAppClassLoader"));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,261 @@
|
|||
//
|
||||
// ========================================================================
|
||||
// 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.tests;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import org.eclipse.jetty.annotations.AnnotationConfiguration;
|
||||
import org.eclipse.jetty.plus.webapp.EnvConfiguration;
|
||||
import org.eclipse.jetty.plus.webapp.PlusConfiguration;
|
||||
import org.eclipse.jetty.server.Server;
|
||||
import org.eclipse.jetty.server.ServerConnector;
|
||||
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
|
||||
import org.eclipse.jetty.toolchain.test.FS;
|
||||
import org.eclipse.jetty.toolchain.test.IO;
|
||||
import org.eclipse.jetty.toolchain.test.JAR;
|
||||
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
|
||||
import org.eclipse.jetty.util.TypeUtil;
|
||||
import org.eclipse.jetty.util.component.ContainerLifeCycle;
|
||||
import org.eclipse.jetty.util.resource.PathResource;
|
||||
import org.eclipse.jetty.webapp.Configuration;
|
||||
import org.eclipse.jetty.webapp.FragmentConfiguration;
|
||||
import org.eclipse.jetty.webapp.JettyWebXmlConfiguration;
|
||||
import org.eclipse.jetty.webapp.JmxConfiguration;
|
||||
import org.eclipse.jetty.webapp.JndiConfiguration;
|
||||
import org.eclipse.jetty.webapp.MetaInfConfiguration;
|
||||
import org.eclipse.jetty.webapp.WebAppConfiguration;
|
||||
import org.eclipse.jetty.webapp.WebAppContext;
|
||||
import org.eclipse.jetty.webapp.WebInfConfiguration;
|
||||
import org.eclipse.jetty.webapp.WebXmlConfiguration;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
|
||||
/**
|
||||
* Utility to build out exploded directory WebApps.
|
||||
*/
|
||||
public class WebAppTester extends ContainerLifeCycle
|
||||
{
|
||||
private static final Logger LOG = LoggerFactory.getLogger(WebAppTester.class);
|
||||
private final Path _testDir;
|
||||
private final Server _server;
|
||||
private final ServerConnector _serverConnector;
|
||||
private final ContextHandlerCollection _contexts;
|
||||
|
||||
public WebAppTester()
|
||||
{
|
||||
this(null);
|
||||
}
|
||||
|
||||
public WebAppTester(Path testDir)
|
||||
{
|
||||
if (testDir == null)
|
||||
{
|
||||
try
|
||||
{
|
||||
Path targetTestingPath = MavenTestingUtils.getTargetTestingPath();
|
||||
FS.ensureDirExists(targetTestingPath);
|
||||
_testDir = Files.createTempDirectory(targetTestingPath, "contexts");
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_testDir = testDir;
|
||||
FS.ensureDirExists(_testDir);
|
||||
}
|
||||
|
||||
_server = new Server();
|
||||
_serverConnector = new ServerConnector(_server);
|
||||
_server.addConnector(_serverConnector);
|
||||
_contexts = new ContextHandlerCollection();
|
||||
_server.setHandler(_contexts);
|
||||
addBean(_server);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doStop() throws Exception
|
||||
{
|
||||
// Recursively delete testDir when stopping.
|
||||
org.eclipse.jetty.util.IO.delete(_testDir.toFile());
|
||||
}
|
||||
|
||||
public Server getServer()
|
||||
{
|
||||
return _server;
|
||||
}
|
||||
|
||||
public URI getServerUri()
|
||||
{
|
||||
if (!isStarted())
|
||||
throw new IllegalStateException("Not Started");
|
||||
return URI.create("http://localhost:" + getPort());
|
||||
}
|
||||
|
||||
public int getPort()
|
||||
{
|
||||
return _serverConnector.getLocalPort();
|
||||
}
|
||||
|
||||
public WebApp createWebApp(String contextPath)
|
||||
{
|
||||
return new WebApp(contextPath);
|
||||
}
|
||||
|
||||
public class WebApp
|
||||
{
|
||||
private final WebAppContext _context;
|
||||
private final Path _contextDir;
|
||||
private final Path _webInf;
|
||||
private final Path _classesDir;
|
||||
private final Path _libDir;
|
||||
|
||||
private WebApp(String contextPath)
|
||||
{
|
||||
// Ensure context directory.
|
||||
String contextDirName = contextPath.replace("/", "");
|
||||
if (contextDirName.length() == 0)
|
||||
contextDirName = "ROOT";
|
||||
_contextDir = _testDir.resolve(contextDirName);
|
||||
FS.ensureEmpty(_contextDir);
|
||||
|
||||
// Ensure WEB-INF directories.
|
||||
_webInf = _contextDir.resolve("WEB-INF");
|
||||
FS.ensureDirExists(_webInf);
|
||||
_classesDir = _webInf.resolve("classes");
|
||||
FS.ensureDirExists(_classesDir);
|
||||
_libDir = _webInf.resolve("lib");
|
||||
FS.ensureDirExists(_libDir);
|
||||
|
||||
// Configure the WebAppContext.
|
||||
_context = new WebAppContext();
|
||||
_context.setContextPath(contextPath);
|
||||
_context.setBaseResource(new PathResource(_contextDir));
|
||||
|
||||
_context.setConfigurations(new Configuration[]
|
||||
{
|
||||
new JmxConfiguration(),
|
||||
new WebInfConfiguration(),
|
||||
new WebXmlConfiguration(),
|
||||
new MetaInfConfiguration(),
|
||||
new FragmentConfiguration(),
|
||||
new EnvConfiguration(),
|
||||
new PlusConfiguration(),
|
||||
new AnnotationConfiguration(),
|
||||
new JndiConfiguration(),
|
||||
new WebAppConfiguration(),
|
||||
new JettyWebXmlConfiguration()
|
||||
});
|
||||
}
|
||||
|
||||
public WebAppContext getWebAppContext()
|
||||
{
|
||||
return _context;
|
||||
}
|
||||
|
||||
public String getContextPath()
|
||||
{
|
||||
return _context.getContextPath();
|
||||
}
|
||||
|
||||
public Path getContextDir()
|
||||
{
|
||||
return _contextDir;
|
||||
}
|
||||
|
||||
public void addConfiguration(Configuration... configurations)
|
||||
{
|
||||
_context.addConfiguration(configurations);
|
||||
}
|
||||
|
||||
public void createWebInf() throws IOException
|
||||
{
|
||||
String emptyWebXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
|
||||
"<web-app\n" +
|
||||
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
|
||||
" xmlns=\"http://java.sun.com/xml/ns/javaee\"\n" +
|
||||
" xsi:schemaLocation=\"http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd\"\n" +
|
||||
" metadata-complete=\"false\"\n" +
|
||||
" version=\"3.0\">\n" +
|
||||
"</web-app>";
|
||||
|
||||
File webXml = _webInf.resolve("web.xml").toFile();
|
||||
try (FileWriter out = new FileWriter(webXml))
|
||||
{
|
||||
out.write(emptyWebXml);
|
||||
}
|
||||
}
|
||||
|
||||
public void copyWebInf(String testResourceName) throws IOException
|
||||
{
|
||||
File testWebXml = MavenTestingUtils.getTestResourceFile(testResourceName);
|
||||
Path webXml = _webInf.resolve("web.xml");
|
||||
IO.copy(testWebXml, webXml.toFile());
|
||||
}
|
||||
|
||||
public void copyClass(Class<?> clazz) throws Exception
|
||||
{
|
||||
ClassLoader cl = Thread.currentThread().getContextClassLoader();
|
||||
String endpointPath = TypeUtil.toClassReference(clazz);
|
||||
URL classUrl = cl.getResource(endpointPath);
|
||||
assertThat("Class URL for: " + clazz, classUrl, notNullValue());
|
||||
Path destFile = _classesDir.resolve(endpointPath);
|
||||
FS.ensureDirExists(destFile.getParent());
|
||||
File srcFile = new File(classUrl.toURI());
|
||||
IO.copy(srcFile, destFile.toFile());
|
||||
}
|
||||
|
||||
public void copyLib(Class<?> clazz, String jarFileName) throws URISyntaxException, IOException
|
||||
{
|
||||
Path jarFile = _libDir.resolve(jarFileName);
|
||||
|
||||
URL codeSourceURL = clazz.getProtectionDomain().getCodeSource().getLocation();
|
||||
assertThat("Class CodeSource URL is file scheme", codeSourceURL.getProtocol(), is("file"));
|
||||
|
||||
File sourceCodeSourceFile = new File(codeSourceURL.toURI());
|
||||
if (sourceCodeSourceFile.isDirectory())
|
||||
{
|
||||
LOG.info("Creating " + jarFile + " from " + sourceCodeSourceFile);
|
||||
JAR.create(sourceCodeSourceFile, jarFile.toFile());
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG.info("Copying " + sourceCodeSourceFile + " to " + jarFile);
|
||||
IO.copy(sourceCodeSourceFile, jarFile.toFile());
|
||||
}
|
||||
}
|
||||
|
||||
public void deploy()
|
||||
{
|
||||
_contexts.addHandler(_context);
|
||||
_contexts.manage(_context);
|
||||
_context.setThrowUnavailableOnStartupException(true);
|
||||
if (LOG.isDebugEnabled())
|
||||
LOG.debug("{}", _context.dump());
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue