TomCools / dropwizard-websocket-jsr356-bundle

A Dropwizard bundle that enables the usage of the Java API for WebSocket Protocol (JSR 356).
Apache License 2.0
22 stars 14 forks source link

setting SessionHandler in run-method breaks WebSocketUpgradeFilter #18

Open GuyIncognito-123 opened 2 months ago

GuyIncognito-123 commented 2 months ago

Hi, I am trying to implement a websocket in our existing dropwizard-server. It bundles a Webapp via dropizward-guicey. We use the SessionHandler from jetty in an AuthFilter for Authentication. It gets set in the run method like this:

   public void run(final TestConfig configuration, final Environment environment) {
       websocket.addEndpoint(PingPongServerEndpoint.class);

        try {
            environment.servlets().setSessionHandler(getInjector().getInstance(SessionHandler.class));
        } catch (Exception e) {
            log.error("konnte session Handler nicht setzen", e);
            System.exit(-1);
        }
    }

This seems to break the WebSocketUpgradeFilter and I get the following error message:

ERROR [2024-08-21 13:13:44,942] io.dropwizard.core.cli.ServerCommand: Unable to start server, shutting down ! java.lang.IllegalStateException: WebSocketComponents has not been created ! at org.eclipse.jetty.websocket.core.server.WebSocketServerComponents.getWebSocketComponents(WebSocketServerComponents.java:131) ! at org.eclipse.jetty.websocket.core.server.WebSocketMappings.ensureMappings(WebSocketMappings.java:68) ! at org.eclipse.jetty.websocket.servlet.WebSocketUpgradeFilter.init(WebSocketUpgradeFilter.java:182) ! at org.eclipse.jetty.servlet.FilterHolder.initialize(FilterHolder.java:133) ! at org.eclipse.jetty.servlet.ServletHandler.initializeHolders(ServletHandler.java:774) ! ... 50 common frames omitted ! Causing: java.lang.RuntimeException: java.lang.IllegalStateException: WebSocketComponents has not been created ! at org.eclipse.jetty.servlet.ServletHandler.initializeHolders(ServletHandler.java:779) ! at org.eclipse.jetty.servlet.ServletHandler.setFilters(ServletHandler.java:1472) ! at org.eclipse.jetty.servlet.ServletHandler.prependFilter(ServletHandler.java:1127) ! at org.eclipse.jetty.websocket.servlet.WebSocketUpgradeFilter.ensureFilter(WebSocketUpgradeFilter.java:124) ! at org.eclipse.jetty.websocket.jakarta.server.config.JakartaWebSocketServletContainerInitializer.initialize(JakartaWebSocketServletContainerInitializer.java:140) ! at org.eclipse.jetty.websocket.jakarta.server.config.JakartaWebSocketServletContainerInitializer.onStartup(JakartaWebSocketServletContainerInitializer.java:166) ! at org.eclipse.jetty.servlet.ServletContainerInitializerHolder.doStart(ServletContainerInitializerHolder.java:148) ! at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:93) ! at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:171) ! at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:114) ! at org.eclipse.jetty.servlet.ServletContextHandler$ServletContainerInitializerStarter.doStart(ServletContextHandler.java:1658) ! at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:93) ! at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:369) ! at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:902) ! at org.eclipse.jetty.servlet.ServletContextHandler.doStart(ServletContextHandler.java:306) ! at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:93) ! at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:171) ! at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:114) ! at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:89) ! at io.dropwizard.metrics.jetty11.InstrumentedHandler.doStart(InstrumentedHandler.java:157) ! at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:93) ! at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:171) ! at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:121) ! at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:89) ! at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:93) ! at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:171) ! at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:114) ! at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:89) ! at org.eclipse.jetty.server.handler.gzip.GzipHandler.doStart(GzipHandler.java:221) ! at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:93) ! at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:171) ! at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:114) ! at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:89) ! at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:93) ! at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:171) ! at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:114) ! at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:89) ! at org.eclipse.jetty.server.handler.StatisticsHandler.doStart(StatisticsHandler.java:253) ! at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:93) ! at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:171) ! at org.eclipse.jetty.server.Server.start(Server.java:470) ! at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:114) ! at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:89) ! at org.eclipse.jetty.server.Server.doStart(Server.java:415) ! at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:93) ! at io.dropwizard.core.cli.ServerCommand.run(ServerCommand.java:52) ! at io.dropwizard.core.cli.EnvironmentCommand.run(EnvironmentCommand.java:67) ! at io.dropwizard.core.cli.ConfiguredCommand.run(ConfiguredCommand.java:98) ! at io.dropwizard.core.cli.Cli.run(Cli.java:78) ! at io.dropwizard.core.Application.run(Application.java:94) ! at org.example.App.main(App.java:25)

The initialize-step looks like this:

public void initialize(Bootstrap<TestConfig> bootstrap) {
    bootstrap.getObjectMapper().registerModule(new JavaTimeModule());
    bootstrap.addBundle(new MultiPartBundle());
    bootstrap.addBundle(websocket);

    bootstrap.addBundle(GuiceBundle.builder()
            .enableAutoConfig(getClass().getPackage().getName())
            .modules(new TestServerModule())
            .bundles(SpaBundle.app("webapp", "/" + "assets/", "/").build())
            .build());
}

Removing the GuiceBundle and the SessionHandler fixes the issue. I am honestly not sure if this is more of a general problem with how Dropwizard starts jetty or something else but I am banging my head against this problem for almost 2 days.

Any help would be greatly appreciated.

TomCools commented 1 month ago

Have you tried running the application with all your GuiceBundle and SessionHandler but without this plugin?

I have been looking at this, but I also can't really figure this out that quickly. Seems like the GuiceBundle is messing with the basic setup.