diff --git a/jetty-websocket/javax-websocket-client/src/main/java/org/eclipse/jetty/websocket/javax/client/JavaxWebSocketClientContainer.java b/jetty-websocket/javax-websocket-client/src/main/java/org/eclipse/jetty/websocket/javax/client/JavaxWebSocketClientContainer.java index ae5c8465411..e051d723a28 100644 --- a/jetty-websocket/javax-websocket-client/src/main/java/org/eclipse/jetty/websocket/javax/client/JavaxWebSocketClientContainer.java +++ b/jetty-websocket/javax-websocket-client/src/main/java/org/eclipse/jetty/websocket/javax/client/JavaxWebSocketClientContainer.java @@ -22,6 +22,7 @@ import java.io.IOException; import java.net.URI; import java.util.Objects; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; import java.util.concurrent.Executor; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; @@ -126,7 +127,7 @@ public class JavaxWebSocketClientContainer extends JavaxWebSocketContainer imple JavaxClientUpgradeRequest upgradeRequest = new JavaxClientUpgradeRequest(this, getWebSocketCoreClient(), destURI, configuredEndpoint); EndpointConfig config = configuredEndpoint.getConfig(); - if (config != null && config instanceof ClientEndpointConfig) + if (config instanceof ClientEndpointConfig) { ClientEndpointConfig clientEndpointConfig = (ClientEndpointConfig)config; @@ -140,26 +141,35 @@ public class JavaxWebSocketClientContainer extends JavaxWebSocketContainer imple upgradeRequest.setSubProtocols(clientEndpointConfig.getPreferredSubprotocols()); } + long timeout = coreClient.getHttpClient().getConnectTimeout(); try { Future sessionFuture = connect(upgradeRequest); - long timeout = coreClient.getHttpClient().getConnectTimeout(); if (timeout>0) return sessionFuture.get(timeout+1000, TimeUnit.MILLISECONDS); return sessionFuture.get(); } + catch (ExecutionException e) + { + var cause = e.getCause(); + if (cause instanceof RuntimeException) + throw (RuntimeException)cause; + if (cause instanceof IOException) + throw (IOException)cause; + throw new IOException(cause); + } catch (TimeoutException e) { - throw new IOException("Connection future not completed " + destURI, e); + throw new IOException("Connection future timeout " + timeout + " ms for " + destURI, e); } - catch (Exception e) + catch (Throwable e) { throw new IOException("Unable to connect to " + destURI, e); } } @Override - public Session connectToServer(final Class endpointClass, final ClientEndpointConfig config, URI path) throws DeploymentException, IOException + public Session connectToServer(final Class endpointClass, final ClientEndpointConfig config, URI path) throws IOException { ClientEndpointConfig clientEndpointConfig = config; if (clientEndpointConfig == null) @@ -171,7 +181,7 @@ public class JavaxWebSocketClientContainer extends JavaxWebSocketContainer imple } @Override - public Session connectToServer(final Class annotatedEndpointClass, final URI path) throws DeploymentException, IOException + public Session connectToServer(final Class annotatedEndpointClass, final URI path) throws IOException { ConfiguredEndpoint instance = newConfiguredEndpoint(annotatedEndpointClass, new EmptyClientEndpointConfig()); return connect(instance, path); @@ -212,11 +222,11 @@ public class JavaxWebSocketClientContainer extends JavaxWebSocketContainer imple { try { - return newConfiguredEndpoint(endpointClass.newInstance(), config); + return newConfiguredEndpoint(endpointClass.getConstructor().newInstance(), config); } - catch (DeploymentException | InstantiationException | IllegalAccessException e) + catch (Throwable e) { - throw new InvalidWebSocketException("Unable to instantiate websocket: " + endpointClass.getClass()); + throw new InvalidWebSocketException("Unable to instantiate websocket: " + endpointClass.getName()); } } diff --git a/jetty-websocket/javax-websocket-tests/src/test/java/org/eclipse/jetty/websocket/javax/tests/client/misbehaving/MisbehavingClassTest.java b/jetty-websocket/javax-websocket-tests/src/test/java/org/eclipse/jetty/websocket/javax/tests/client/misbehaving/MisbehavingClassTest.java index 5a577c2c884..acbc3d27145 100644 --- a/jetty-websocket/javax-websocket-tests/src/test/java/org/eclipse/jetty/websocket/javax/tests/client/misbehaving/MisbehavingClassTest.java +++ b/jetty-websocket/javax-websocket-tests/src/test/java/org/eclipse/jetty/websocket/javax/tests/client/misbehaving/MisbehavingClassTest.java @@ -18,18 +18,17 @@ package org.eclipse.jetty.websocket.javax.tests.client.misbehaving; -import java.io.IOException; -import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import javax.websocket.ContainerProvider; import javax.websocket.WebSocketContainer; import org.eclipse.jetty.util.log.StacklessLogging; +import org.eclipse.jetty.websocket.core.CloseException; import org.eclipse.jetty.websocket.core.internal.WebSocketCoreSession; import org.eclipse.jetty.websocket.javax.tests.CoreServer; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.hamcrest.MatcherAssert.assertThat; @@ -39,32 +38,21 @@ import static org.junit.jupiter.api.Assertions.assertThrows; public class MisbehavingClassTest { + private CoreServer server; - private static CoreServer server; - - @SuppressWarnings("Duplicates") - @BeforeAll - public static void startServer() throws Exception + @BeforeEach + public void startServer() throws Exception { server = new CoreServer(new CoreServer.EchoNegotiator()); - // Start Server server.start(); } - @AfterAll - public static void stopServer() + @AfterEach + public void stopServer() throws Exception { - try - { - server.stop(); - } - catch (Exception e) - { - e.printStackTrace(System.err); - } + server.stop(); } - @SuppressWarnings("Duplicates") @Test public void testEndpointRuntimeOnOpen() throws Exception { @@ -74,9 +62,8 @@ public class MisbehavingClassTest try (StacklessLogging ignored = new StacklessLogging(WebSocketCoreSession.class)) { - // expecting IOException during onOpen - Exception e = assertThrows(IOException.class, () -> container.connectToServer(socket, server.getWsUri()), "Should have failed .connectToServer()"); - assertThat(e.getCause(), instanceOf(ExecutionException.class)); + // Expecting CloseException during onOpen(). + assertThrows(CloseException.class, () -> container.connectToServer(socket, server.getWsUri()), "Should have failed .connectToServer()"); assertThat("Close should have occurred", socket.closeLatch.await(1, TimeUnit.SECONDS), is(true)); @@ -85,7 +72,6 @@ public class MisbehavingClassTest } } - @SuppressWarnings("Duplicates") @Test public void testAnnotatedRuntimeOnOpen() throws Exception { @@ -95,9 +81,8 @@ public class MisbehavingClassTest try (StacklessLogging ignored = new StacklessLogging(WebSocketCoreSession.class)) { - // expecting IOException during onOpen - Exception e = assertThrows(IOException.class, () -> container.connectToServer(socket, server.getWsUri()), "Should have failed .connectToServer()"); - assertThat(e.getCause(), instanceOf(ExecutionException.class)); + // Expecting CloseException during onOpen(). + assertThrows(CloseException.class, () -> container.connectToServer(socket, server.getWsUri()), "Should have failed .connectToServer()"); assertThat("Close should have occurred", socket.closeLatch.await(5, TimeUnit.SECONDS), is(true));