|
| 1 | +// |
| 2 | +// ======================================================================== |
| 3 | +// Copyright (c) 1995-2021 Mort Bay Consulting Pty Ltd and others. |
| 4 | +// |
| 5 | +// This program and the accompanying materials are made available under the |
| 6 | +// terms of the Eclipse Public License v. 2.0 which is available at |
| 7 | +// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 |
| 8 | +// which is available at https://www.apache.org/licenses/LICENSE-2.0. |
| 9 | +// |
| 10 | +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 |
| 11 | +// ======================================================================== |
| 12 | +// |
| 13 | + |
| 14 | +package org.eclipse.jetty.websocket.javax.tests; |
| 15 | + |
| 16 | +import java.net.URI; |
| 17 | +import java.util.concurrent.LinkedBlockingQueue; |
| 18 | +import java.util.concurrent.TimeUnit; |
| 19 | +import javax.servlet.annotation.WebServlet; |
| 20 | +import javax.servlet.http.HttpServlet; |
| 21 | +import javax.servlet.http.HttpServletRequest; |
| 22 | +import javax.servlet.http.HttpServletResponse; |
| 23 | +import javax.websocket.ClientEndpoint; |
| 24 | +import javax.websocket.ContainerProvider; |
| 25 | +import javax.websocket.OnMessage; |
| 26 | +import javax.websocket.OnOpen; |
| 27 | +import javax.websocket.Session; |
| 28 | +import javax.websocket.WebSocketContainer; |
| 29 | +import javax.websocket.server.ServerEndpoint; |
| 30 | + |
| 31 | +import org.eclipse.jetty.client.HttpClient; |
| 32 | +import org.eclipse.jetty.client.api.ContentResponse; |
| 33 | +import org.eclipse.jetty.client.api.Response; |
| 34 | +import org.eclipse.jetty.http.BadMessageException; |
| 35 | +import org.eclipse.jetty.http.HttpStatus; |
| 36 | +import org.eclipse.jetty.io.ByteBufferPool; |
| 37 | +import org.eclipse.jetty.util.component.ContainerLifeCycle; |
| 38 | +import org.eclipse.jetty.webapp.Configuration; |
| 39 | +import org.eclipse.jetty.webapp.Configurations; |
| 40 | +import org.eclipse.jetty.websocket.core.WebSocketComponents; |
| 41 | +import org.eclipse.jetty.websocket.core.client.CoreClientUpgradeRequest; |
| 42 | +import org.eclipse.jetty.websocket.javax.client.JavaxWebSocketClientContainerProvider; |
| 43 | +import org.eclipse.jetty.websocket.javax.common.JavaxWebSocketContainer; |
| 44 | +import org.eclipse.jetty.websocket.javax.server.config.JavaxWebSocketConfiguration; |
| 45 | +import org.eclipse.jetty.xml.XmlConfiguration; |
| 46 | +import org.junit.jupiter.api.AfterEach; |
| 47 | +import org.junit.jupiter.api.Test; |
| 48 | + |
| 49 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 50 | +import static org.hamcrest.Matchers.containsString; |
| 51 | +import static org.hamcrest.Matchers.is; |
| 52 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 53 | + |
| 54 | +public class JavaxClientClassLoaderTest |
| 55 | +{ |
| 56 | + private WSServer server; |
| 57 | + private HttpClient httpClient; |
| 58 | + |
| 59 | + @FunctionalInterface |
| 60 | + interface ThrowingRunnable |
| 61 | + { |
| 62 | + void run() throws Exception; |
| 63 | + } |
| 64 | + |
| 65 | + public void start(ThrowingRunnable configuration) throws Exception |
| 66 | + { |
| 67 | + server = new WSServer(); |
| 68 | + configuration.run(); |
| 69 | + server.start(); |
| 70 | + httpClient = new HttpClient(); |
| 71 | + httpClient.start(); |
| 72 | + } |
| 73 | + |
| 74 | + @AfterEach |
| 75 | + public void after() throws Exception |
| 76 | + { |
| 77 | + httpClient.stop(); |
| 78 | + server.stop(); |
| 79 | + } |
| 80 | + |
| 81 | + @ClientEndpoint() |
| 82 | + public static class ClientSocket |
| 83 | + { |
| 84 | + LinkedBlockingQueue<String> textMessages = new LinkedBlockingQueue<>(); |
| 85 | + |
| 86 | + @OnOpen |
| 87 | + public void onOpen(Session session) |
| 88 | + { |
| 89 | + session.getAsyncRemote().sendText("ContextClassLoader: " + Thread.currentThread().getContextClassLoader()); |
| 90 | + } |
| 91 | + |
| 92 | + @OnMessage |
| 93 | + public void onMessage(String message) |
| 94 | + { |
| 95 | + textMessages.add(message); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + @WebServlet("/servlet") |
| 100 | + public static class WebSocketClientServlet extends HttpServlet |
| 101 | + { |
| 102 | + private final WebSocketContainer clientContainer = ContainerProvider.getWebSocketContainer(); |
| 103 | + |
| 104 | + @Override |
| 105 | + protected void doGet(HttpServletRequest req, HttpServletResponse resp) |
| 106 | + { |
| 107 | + URI wsEchoUri = URI.create("ws://localhost:" + req.getServerPort() + "/echo/"); |
| 108 | + ClientSocket clientSocket = new ClientSocket(); |
| 109 | + |
| 110 | + try (Session ignored = clientContainer.connectToServer(clientSocket, wsEchoUri)) |
| 111 | + { |
| 112 | + String recv = clientSocket.textMessages.poll(5, TimeUnit.SECONDS); |
| 113 | + assertNotNull(recv); |
| 114 | + resp.setStatus(HttpStatus.OK_200); |
| 115 | + resp.getWriter().println(recv); |
| 116 | + resp.getWriter().println("ClientClassLoader: " + clientContainer.getClass().getClassLoader()); |
| 117 | + } |
| 118 | + catch (Exception e) |
| 119 | + { |
| 120 | + throw new RuntimeException(e); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + @ServerEndpoint("/") |
| 126 | + public static class EchoSocket |
| 127 | + { |
| 128 | + @OnMessage |
| 129 | + public void onMessage(Session session, String message) throws Exception |
| 130 | + { |
| 131 | + session.getBasicRemote().sendText(message); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + public WSServer.WebApp createWebSocketWebapp(String contextName) throws Exception |
| 136 | + { |
| 137 | + WSServer.WebApp app = server.createWebApp(contextName); |
| 138 | + |
| 139 | + // Exclude the Javax WebSocket configuration from the webapp (so we use versions from the webapp). |
| 140 | + Configuration[] configurations = Configurations.getKnown().stream() |
| 141 | + .filter(configuration -> !(configuration instanceof JavaxWebSocketConfiguration)) |
| 142 | + .toArray(Configuration[]::new); |
| 143 | + app.getWebAppContext().setConfigurations(configurations); |
| 144 | + |
| 145 | + // Copy over the individual jars required for Javax WebSocket. |
| 146 | + app.createWebInf(); |
| 147 | + app.copyLib(JavaxWebSocketClientContainerProvider.class, "websocket-javax-client.jar"); |
| 148 | + app.copyLib(JavaxWebSocketContainer.class, "websocket-javax-common.jar"); |
| 149 | + app.copyLib(ContainerLifeCycle.class, "jetty-util.jar"); |
| 150 | + app.copyLib(CoreClientUpgradeRequest.class, "websocket-core-client.jar"); |
| 151 | + app.copyLib(WebSocketComponents.class, "websocket-core-common.jar"); |
| 152 | + app.copyLib(Response.class, "jetty-client.jar"); |
| 153 | + app.copyLib(ByteBufferPool.class, "jetty-io.jar"); |
| 154 | + app.copyLib(BadMessageException.class, "jetty-http.jar"); |
| 155 | + app.copyLib(XmlConfiguration.class, "jetty-xml.jar"); |
| 156 | + |
| 157 | + return app; |
| 158 | + } |
| 159 | + |
| 160 | + @Test |
| 161 | + public void websocketProvidedByServer() throws Exception |
| 162 | + { |
| 163 | + start(() -> |
| 164 | + { |
| 165 | + WSServer.WebApp app1 = server.createWebApp("app"); |
| 166 | + app1.createWebInf(); |
| 167 | + app1.copyClass(WebSocketClientServlet.class); |
| 168 | + app1.copyClass(ClientSocket.class); |
| 169 | + app1.deploy(); |
| 170 | + |
| 171 | + WSServer.WebApp app2 = server.createWebApp("echo"); |
| 172 | + app2.createWebInf(); |
| 173 | + app2.copyClass(EchoSocket.class); |
| 174 | + app2.deploy(); |
| 175 | + }); |
| 176 | + |
| 177 | + // After hitting each WebApp we will get 200 response if test succeeds. |
| 178 | + ContentResponse response = httpClient.GET(server.getServerUri().resolve("/app/servlet")); |
| 179 | + assertThat(response.getStatus(), is(HttpStatus.OK_200)); |
| 180 | + |
| 181 | + // The ContextClassLoader in the WebSocketClients onOpen was the WebAppClassloader. |
| 182 | + assertThat(response.getContentAsString(), containsString("ContextClassLoader: WebAppClassLoader")); |
| 183 | + |
| 184 | + // Verify that we used Servers version of WebSocketClient. |
| 185 | + ClassLoader serverClassLoader = server.getServer().getClass().getClassLoader(); |
| 186 | + assertThat(response.getContentAsString(), containsString("ClientClassLoader: " + serverClassLoader)); } |
| 187 | + |
| 188 | + @Test |
| 189 | + public void websocketProvidedByWebApp() throws Exception |
| 190 | + { |
| 191 | + start(() -> |
| 192 | + { |
| 193 | + WSServer.WebApp app1 = createWebSocketWebapp("app"); |
| 194 | + app1.createWebInf(); |
| 195 | + app1.copyClass(WebSocketClientServlet.class); |
| 196 | + app1.copyClass(ClientSocket.class); |
| 197 | + app1.copyClass(EchoSocket.class); |
| 198 | + app1.deploy(); |
| 199 | + |
| 200 | + // Do not exclude JavaxWebSocketConfiguration for this webapp (we need the websocket server classes). |
| 201 | + WSServer.WebApp app2 = server.createWebApp("echo"); |
| 202 | + app2.createWebInf(); |
| 203 | + app2.copyClass(EchoSocket.class); |
| 204 | + app2.deploy(); |
| 205 | + }); |
| 206 | + |
| 207 | + // After hitting each WebApp we will get 200 response if test succeeds. |
| 208 | + ContentResponse response = httpClient.GET(server.getServerUri().resolve("/app/servlet")); |
| 209 | + assertThat(response.getStatus(), is(HttpStatus.OK_200)); |
| 210 | + |
| 211 | + // The ContextClassLoader in the WebSocketClients onOpen was the WebAppClassloader. |
| 212 | + assertThat(response.getContentAsString(), containsString("ContextClassLoader: WebAppClassLoader")); |
| 213 | + |
| 214 | + // Verify that we used WebApps version of WebSocketClient. |
| 215 | + assertThat(response.getContentAsString(), containsString("ClientClassLoader: WebAppClassLoader")); |
| 216 | + } |
| 217 | +} |
0 commit comments