server: HandshakeRequest: add getServletContext()#416
server: HandshakeRequest: add getServletContext()#416morgwai wants to merge 1 commit intojakartaee:mainfrom
Conversation
|
An alternative to this PR could be adding a method like |
For EE 12 (IFF anyone has the time for it), we talked about a generic representation of the http request. A bare minimum interface that can be used / implemented by Servlet, Portlet, REST and potentially others that have their own request type. This could perhaps be used here instead of Object. |
That kind of generic request object would be better defined by a new Jakarta Spec. Note that Jakarta WebSocket could benefit from a generic HTTP Server Request (for websocket server handshake) and a generic HTTP Client Request object (for websocket client handshake). |
|
For the record, getting access to the ServletContext can be done now with any existing Jakarta WebSocket implementation, without forks of the various implementations. You'll just want to make a ServerEndpointConfig.Configurator instance that has the ServletContext passed into it. Many third party libraries that use Jakarta WebSocket do that now. |
That's exactly the proposal put on the table of EE 12 ;) |
|
Reopening. |
|
Try again. |
Currently there is no easy universal way to obtain reference to the
ServletContextfromServerEndpointConfig.Configurator's level if the websocket container is running as a part of a Java Servlet app. This may be useful to access app init params or app wide attributes. As of 2.1.1 the standard practice is to obtainServletContextref viaHttpSessioninmodifyHandshake(...):This however requires to enforce creating of session for each handshake request to
Endpointsusing suchConfigurator(withFiltersorListeners). This in turn, may not always be acceptable due to various reasons (cookies disabled, user explicitly refusing any data storage etc) and may not serve any other purpose than this server-internal workaround.Other workarounds include really ugly hacks such as storing a ref to
ServletContexton a static var...Adding
getServletContext()removes necessity for any such hacks. This should also be very easy to implement by existing websocket server containers, for example here is an example 6 line patch for Jetty.Please forgive me, as I'm not sure if just posting a pull-request here is the right way to get something included in this API: if I receive positive initial feedback regarding this, I will get myself familiar with the process and will perform other necessary steps.
Thanks!