Class GuiceServerEndpointConfigurator
- java.lang.Object
-
- javax.websocket.server.ServerEndpointConfig.Configurator
-
- pl.morgwai.base.servlet.guice.scopes.GuiceServerEndpointConfigurator
-
- Direct Known Subclasses:
PingingEndpointConfigurator
public class GuiceServerEndpointConfigurator extends ServerEndpointConfig.Configurator
ObtainsEndpoint
instances fromGuice
and ensures their methodsrun within websocket Contexts
. This way, all dependencies are injected andScope
s fromServletModule
(ServletModule.containerCallScope
,ServletModule.websocketConnectionScope
andServletModule.httpSessionScope
) work properly.To use this
Configurator
for programmatically addedEndpoints
, create an instance usingGuiceServerEndpointConfigurator(ServletContext)
and pass it to theconfig
:final var configurator = new GuiceServerEndpointConfigurator(servletContext); websocketContainer.addEndpoint( ServerEndpointConfig.Builder .create(MyProgrammaticEndpoint.class, "/websocket/programmatic") .configurator(configurator) .build() );
To use this
Configurator
for @ServerEndpoint
annotatedEndpoints
, first the app-wideInjector
must bestored as a deployment attribute
under thefully-qualified name
ofInjector
class incontextInitialized(event)
method of app'sServletContextListener
.
Secondly,registerDeployment(ServletContext)
andderegisterDeployment(ServletContext)
static methods must be called respectively incontextInitialized(event)
andcontextDestroyed(event)
methods of app'sServletContextListener
.
This way container-created instances (withthe param-less constructor
) of thisConfigurator
can obtain a reference to theInjector
. Note that if app'sListener
extendsGuiceServletContextListener
, the whole above setup is automatically taken care of.
Finally,Endpoint
methods annotated with @OnOpen
must have aSession
param.
After the above conditions are met, simply pass this class asconfigurator
param of the annotation:@ServerEndpoint( value = "/websocket/annotated", configurator = GuiceServerEndpointConfigurator.class) public class MyAnnotatedEndpoint { @OnOpen public void onOpen( Session connection // other optional params here... ) { // ... } // other methods here... }
NOTE: due to the way most debuggers work, it is strongly recommended for
toString()
methods ofEndpoints
to work properly even when called outside of anyContext
.- See Also:
GuiceServletContextListener
-
-
Field Summary
Fields Modifier and Type Field Description protected ServletContext
appDeployment
protected ContextTracker<ContainerCallContext>
ctxTracker
protected Injector
injector
-
Constructor Summary
Constructors Constructor Description GuiceServerEndpointConfigurator()
Necessary forServerEndpoint
annotatedEndpoints
.GuiceServerEndpointConfigurator(ServletContext appDeployment)
ForGuiceServletContextListener
managed instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static void
deregisterDeployment(ServletContext appDeployment)
Removes a reference toappDeployment
.protected InvocationHandler
getAdditionalDecorator(Object endpoint)
Subclasses may override this method to further customizeEndpoints
.<EndpointT>
EndpointTgetEndpointInstance(Class<EndpointT> endpointClass)
Obtains an instance ofendpointClass
fromGuice
and creates a context-aware proxy for it, so thatEndpoint
lifecycle methods are executed withinContainerCallContext
andWebsocketConnectionContext
.protected HashSet<Class<? extends Annotation>>
getRequiredEndpointMethodAnnotationTypes()
Returns a set of annotations ofEndpoint
lifecycle methods that are required to be present inEndpoint
classes using this configurator.protected void
initialize(ServletContext appDeployment)
Initializes this instance member fields with references fromappDeployment
attributes
.void
modifyHandshake(ServerEndpointConfig config, HandshakeRequest request, HandshakeResponse response)
static void
registerDeployment(ServletContext appDeployment)
RegistersappDeployment
to be used by container-createdGuiceServerEndpointConfigurator
instances.-
Methods inherited from class javax.websocket.server.ServerEndpointConfig.Configurator
checkOrigin, getNegotiatedExtensions, getNegotiatedSubprotocol
-
-
-
-
Field Detail
-
appDeployment
protected volatile ServletContext appDeployment
-
injector
protected Injector injector
-
ctxTracker
protected ContextTracker<ContainerCallContext> ctxTracker
-
-
Constructor Detail
-
GuiceServerEndpointConfigurator
public GuiceServerEndpointConfigurator()
Necessary forServerEndpoint
annotatedEndpoints
.
-
GuiceServerEndpointConfigurator
public GuiceServerEndpointConfigurator(ServletContext appDeployment)
ForGuiceServletContextListener
managed instance.
-
-
Method Detail
-
registerDeployment
public static void registerDeployment(ServletContext appDeployment)
RegistersappDeployment
to be used by container-createdGuiceServerEndpointConfigurator
instances.This method is called automatically by
GuiceServletContextListener.contextInitialized(javax.servlet.ServletContextEvent)
, it must be called manually in apps that don't use it.
-
deregisterDeployment
public static void deregisterDeployment(ServletContext appDeployment)
Removes a reference toappDeployment
.This method is called automatically by
GuiceServletContextListener.contextDestroyed(javax.servlet.ServletContextEvent)
, it must be called manually in apps that don't use it.
-
initialize
protected void initialize(ServletContext appDeployment)
Initializes this instance member fields with references fromappDeployment
attributes
. Called either byGuiceServerEndpointConfigurator(ServletContext)
or bymodifyHandshake(ServerEndpointConfig, HandshakeRequest, HandshakeResponse)
in case of container-created instances forEndpoints
annotated withServerEndpoint
.
-
getEndpointInstance
public <EndpointT> EndpointT getEndpointInstance(Class<EndpointT> endpointClass) throws InstantiationException
Obtains an instance ofendpointClass
fromGuice
and creates a context-aware proxy for it, so thatEndpoint
lifecycle methods are executed withinContainerCallContext
andWebsocketConnectionContext
.- Overrides:
getEndpointInstance
in classServerEndpointConfig.Configurator
- Returns:
- a proxy for the newly created
endpointClass
instance. - Throws:
InstantiationException
-
getRequiredEndpointMethodAnnotationTypes
protected HashSet<Class<? extends Annotation>> getRequiredEndpointMethodAnnotationTypes()
Returns a set of annotations ofEndpoint
lifecycle methods that are required to be present inEndpoint
classes using this configurator. By default a singleton ofOnOpen
. Subclasses may override this method if needed by callingsuper
and adding their required annotations to the obtained set before returning it.
-
modifyHandshake
public void modifyHandshake(ServerEndpointConfig config, HandshakeRequest request, HandshakeResponse response)
Stores intouser properties
theHttpSession
associated withrequest
.For
Configurator
instances created by the container (as a result of providing this class as aServerEndpoint.configurator()
argument of some annotatedEndpoint
class), this method will also callinitialize(ServletContext)
on its first invocation.- Overrides:
modifyHandshake
in classServerEndpointConfig.Configurator
-
getAdditionalDecorator
protected InvocationHandler getAdditionalDecorator(Object endpoint)
Subclasses may override this method to further customizeEndpoints
.InvocationHandler.invoke(Object, Method, Object[])
method of the returned handler will be executed withinContainerCallContext
andWebsocketConnectionContext
. By default it returns a handler that simply invokes the given method onendpoint
.
-
-