Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jetty 7 embedded configuration for websockets, servlets AND resource handlers

I am trying to configure embedded Jetty (7.6) to handle the WebSocketHandler, ServletContextHandler and ResourceHandler classes at the same time.

I have tried using both HandlerCollection and HandlerList classes but I can't get all 3 parts to work.

  Server server = new Server(8081);

  // static files handler
  ResourceHandler resourceHandler = new ResourceHandler();
  resourceHandler.setDirectoriesListed(true);
  resourceHandler.setResourceBase("./src/main/webapp/");

  // servlet handler
  ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
  servletContextHandler.setContextPath("/");
  servletContextHandler.addServlet(new ServletHolder(new myServlet()), "/myServlet");

  // websocket handler
  myWebSocketHandler myWebSocketHandler = new myWebSocketHandler();

  // putting it together
  HandlerCollection handlerList = new HandlerCollection();
  handlerList.setHandlers(new Handler[]{resourceHandler,myWebSocketHandler,servletContextHandler});
  server.setHandler(handlerList);

In this state, the static files and the servlet are handled fine, but when I send a websocket request, I get:

2012-02-22 10:16:44.703:WARN:oejs.Response:Committed before 503 null
2012-02-22 10:16:44.705:WARN:oejs.AbstractHttpConnection:/
java.lang.IllegalStateException: Committed
    at org.eclipse.jetty.server.Response.resetBuffer(Response.java:1080)
...

and if I check the state of the base requests "handled" property inside my websockets handler, it's already set to true. So this means that my request is being handled and committing before it gets to myWebSocketHandler?

Any comments are appreciated, thanks.

like image 331
Taran Avatar asked Jan 18 '23 10:01

Taran


1 Answers

Have you tried simply switching the order of the handlers?

 handlerList.setHandlers(new Handler[]{myWebSocketHandler,servletContextHandler,resourceHandler});

Jetty runs the handlers in the order you add them, and the resource handler handles every request that comes its way (potentially by serving a 404 error).

like image 84
Tim Avatar answered Jan 31 '23 06:01

Tim