Class GuiceServerEndpointConfigurator
- java.lang.Object
-
- jakarta.websocket.server.ServerEndpointConfig.Configurator
-
- pl.morgwai.base.servlet.guice.scopes.GuiceServerEndpointConfigurator
-
- Direct Known Subclasses:
PingingEndpointConfigurator
public class GuiceServerEndpointConfigurator extends ServerEndpointConfig.Configurator
ObtainsEndpoint
instances fromGuice
(so that all dependencies are injected) and decorates them, so that lifecycle methods are executed withinWebsocketConnectionContext
andContainerCallContext
.In case of
Endpoints
annotated with @ServerEndpoint
, this class should be used asconfigurator
param of the annotation:@ServerEndpoint( value = "/websocket/mySocket", configurator = GuiceServerEndpointConfigurator.class) public class MyEndpoint {...}
NOTE: methods annotated with @
OnOpen
must have aSession
param.In case of
Endpoints
added programmatically, an instance of this configurator should be supplied as an argument toServerEndpointConfig.Builder.configurator(Configurator)
method:websocketContainer.addEndpoint(ServerEndpointConfig.Builder .create(MyEndpoint.class, "/websocket/mySocket") .configurator(new GuiceServerEndpointConfigurator()) .build());
-
-
Constructor Summary
Constructors Constructor Description GuiceServerEndpointConfigurator()
Necessary forServerEndpoint
annotatedEndpoints
.GuiceServerEndpointConfigurator(Injector injector, ContextTracker<ContainerCallContext> containerCallContextTracker)
ForGuiceServletContextListener
managed instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static void
deregisterInjector(ServletContext servletContext)
Removes the reference to theInjector
associated withservletContext
.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.void
modifyHandshake(ServerEndpointConfig config, HandshakeRequest request, HandshakeResponse response)
static void
registerInjector(Injector injector, ServletContext servletContext)
Registersinjector
to be used by container-createdGuiceServerEndpointConfigurator
instances forEndpoints
annotated withServerEndpoint
deployed in theservletContext
.-
Methods inherited from class jakarta.websocket.server.ServerEndpointConfig.Configurator
checkOrigin, getNegotiatedExtensions, getNegotiatedSubprotocol
-
-
-
-
Constructor Detail
-
GuiceServerEndpointConfigurator
public GuiceServerEndpointConfigurator()
Necessary forServerEndpoint
annotatedEndpoints
.
-
GuiceServerEndpointConfigurator
public GuiceServerEndpointConfigurator(Injector injector, ContextTracker<ContainerCallContext> containerCallContextTracker)
ForGuiceServletContextListener
managed instance.
-
-
Method Detail
-
registerInjector
public static void registerInjector(Injector injector, ServletContext servletContext)
Registersinjector
to be used by container-createdGuiceServerEndpointConfigurator
instances forEndpoints
annotated withServerEndpoint
deployed in theservletContext
.This method is called automatically by
GuiceServletContextListener.contextInitialized(ServletContextEvent)
, it must be called manually in apps that don't use it.
-
deregisterInjector
public static void deregisterInjector(ServletContext servletContext)
Removes the reference to theInjector
associated withservletContext
.This method is called automatically by
GuiceServletContextListener.contextDestroyed(ServletContextEvent)
, it must be called manually in apps that don't use it.
-
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 annotatedEndpoint
classes), this method will also initializeinjector
reference 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
.
-
-