#9412 ee10: Fixed missing callback failure when ws endpoint cannot be instantiated

Signed-off-by: Ludovic Orban <lorban@bitronix.be>
This commit is contained in:
Ludovic Orban 2023-03-02 17:48:01 +01:00
parent def35351f3
commit e266db1ab4
2 changed files with 47 additions and 2 deletions

View File

@ -166,9 +166,10 @@ public class JakartaWebSocketCreator implements WebSocketCreator
Object endpoint = config.getConfigurator().getEndpointInstance(endpointClass);
return new ConfiguredEndpoint(endpoint, config);
}
catch (InstantiationException e)
catch (Throwable x)
{
LOG.warn("Unable to create websocket: {}", config.getEndpointClass().getName(), e);
LOG.warn("Unable to create websocket: {}", config.getEndpointClass().getName(), x);
callback.failed(x);
return null;
}
}

View File

@ -35,16 +35,21 @@ import org.eclipse.jetty.ee10.servlet.ServletHolder;
import org.eclipse.jetty.ee10.websocket.jakarta.client.JakartaWebSocketClientContainer;
import org.eclipse.jetty.ee10.websocket.jakarta.server.JakartaWebSocketServerContainer;
import org.eclipse.jetty.ee10.websocket.jakarta.server.config.JakartaWebSocketServletContainerInitializer;
import org.eclipse.jetty.ee10.websocket.jakarta.server.internal.JakartaWebSocketCreator;
import org.eclipse.jetty.logging.StacklessLogging;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.util.ajax.JSON;
import org.eclipse.jetty.websocket.core.exception.UpgradeException;
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.is;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
public class ProgrammaticWebSocketUpgradeTest
{
@ -97,6 +102,20 @@ public class ProgrammaticWebSocketUpgradeTest
}
}
public static class BrokenEndpoint extends Endpoint
{
public BrokenEndpoint()
{
throw new RuntimeException("Broken");
}
@Override
public void onOpen(Session session, EndpointConfig config)
{
throw new UnsupportedOperationException();
}
}
public static class CustomUpgradeServlet extends HttpServlet
{
private JakartaWebSocketServerContainer container;
@ -128,6 +147,12 @@ public class ProgrammaticWebSocketUpgradeTest
container.upgradeHttpToWebSocket(request, response, sec, PATH_PARAMS);
break;
}
case "/brokenEndpoint":
{
ServerEndpointConfig sec = ServerEndpointConfig.Builder.create(BrokenEndpoint.class, "/").build();
container.upgradeHttpToWebSocket(request, response, sec, PATH_PARAMS);
break;
}
default:
throw new IllegalStateException();
}
@ -155,6 +180,25 @@ public class ProgrammaticWebSocketUpgradeTest
assertThat(socket.closeReason.getCloseCode(), is(CloseReason.CloseCodes.NORMAL_CLOSURE));
}
@Test
public void testWebSocketUpgradeFailure() throws Exception
{
try (StacklessLogging ignore = new StacklessLogging(JakartaWebSocketCreator.class);)
{
URI uri = URI.create("ws://localhost:" + connector.getLocalPort() + "/brokenEndpoint");
EventSocket socket = new EventSocket();
try
{
client.connectToServer(socket, uri);
fail("expected IOException");
}
catch (IOException ioe)
{
assertInstanceOf(UpgradeException.class, ioe.getCause());
}
}
}
@Test
public void testPathParameters() throws Exception
{