server: HandshakeRequest: add getServletContext() #416
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently there is no easy universal way to obtain reference to the
ServletContext
fromServerEndpointConfig.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 obtainServletContext
ref viaHttpSession
inmodifyHandshake(...)
:This however requires to enforce creating of session for each handshake request to
Endpoints
using suchConfigurator
(withFilters
orListeners
). 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
ServletContext
on 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!