Class WebSocketUpgradeFilter
- All Implemented Interfaces:
javax.servlet.Filter
,org.eclipse.jetty.util.component.Dumpable
,MappedWebSocketCreator
Inline Servlet Filter to capture WebSocket upgrade requests and perform path mappings to WebSocketCreator
objects.
Embedded Jetty Users should initialize this filter using the configure(ServletContextHandler)
method.
If you also want to establish some mappings of PathSpec
to WebSocketCreator
against this WebSocketUpgradeFilter
then these actions must occur during the Servlet Initialization Phase.
A convenience method is provided with NativeWebSocketServletContainerInitializer.configure(ServletContextHandler, NativeWebSocketServletContainerInitializer.Configurator)
to create a lambda that will execute during the appropriate Servlet Initialization Phase.
ServletContextHandler contextHandler = new ServletContextHandler(...); WebSocketUpgradeFilter.configure(contextHandler); NativeWebSocketServletContainerInitializer.configure(contextHandler, (context, container) -> { container.getPolicy().setMaxTextMessageBufferSize(65535); container.getPolicy().setInputBufferSize(16384); container.addMapping("/echo", ServerEchoSocket.class); container.addMapping(new ServletPathSpec("/api"), (req, resp) -> new ServerApiSocket()); });
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.eclipse.jetty.util.component.Dumpable
org.eclipse.jetty.util.component.Dumpable.DumpableContainer
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate boolean
static final String
static final String
private NativeWebSocketConfiguration
static final String
private boolean
private static final org.eclipse.jetty.util.log.Logger
Fields inherited from interface org.eclipse.jetty.util.component.Dumpable
KEY
-
Constructor Summary
ConstructorsConstructorDescriptionWebSocketUpgradeFilter
(NativeWebSocketConfiguration configuration) Deprecated.Deprecated. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addMapping
(String spec, WebSocketCreator creator) Add Mapping to underlyingNativeWebSocketConfiguration
void
addMapping
(org.eclipse.jetty.http.pathmap.PathSpec spec, WebSocketCreator creator) Add Mapping to underlyingNativeWebSocketConfiguration
void
addMapping
(PathSpec spec, WebSocketCreator creator) Deprecated.static WebSocketUpgradeFilter
configure
(org.eclipse.jetty.servlet.ServletContextHandler context) Configure the default WebSocketUpgradeFilter.static WebSocketUpgradeFilter
configureContext
(javax.servlet.ServletContext context) Deprecated.useconfigureContext(ServletContextHandler)
insteadstatic WebSocketUpgradeFilter
configureContext
(org.eclipse.jetty.servlet.ServletContextHandler context) Deprecated.useconfigure(ServletContextHandler)
insteadvoid
destroy()
void
doFilter
(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain chain) dump()
void
dump
(Appendable out, String indent) getMapping
(String target) /** Returns the creator for the given path spec.void
init
(javax.servlet.FilterConfig config) boolean
removeMapping
(String spec) Remove Mapping to underlyingNativeWebSocketConfiguration
void
setToAttribute
(javax.servlet.ServletContext context, String key) private void
setToAttribute
(org.eclipse.jetty.servlet.ServletContextHandler context, String key) toString()
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.eclipse.jetty.util.component.Dumpable
dumpSelf
-
Field Details
-
LOG
private static final org.eclipse.jetty.util.log.Logger LOG -
CONTEXT_ATTRIBUTE_KEY
- See Also:
-
CONFIG_ATTRIBUTE_KEY
- See Also:
-
ATTR_KEY
-
configuration
-
localConfiguration
private boolean localConfiguration -
alreadySetToAttribute
private boolean alreadySetToAttribute
-
-
Constructor Details
-
WebSocketUpgradeFilter
public WebSocketUpgradeFilter() -
WebSocketUpgradeFilter
Deprecated. -
WebSocketUpgradeFilter
Deprecated.
-
-
Method Details
-
configure
public static WebSocketUpgradeFilter configure(org.eclipse.jetty.servlet.ServletContextHandler context) throws javax.servlet.ServletException Configure the default WebSocketUpgradeFilter.This will return the default
WebSocketUpgradeFilter
on the providedServletContextHandler
, creating the filter if necessary.The default
WebSocketUpgradeFilter
is also available via theServletContext
attribute namedorg.eclipse.jetty.websocket.server.WebSocketUpgradeFilter
- Parameters:
context
- theServletContextHandler
to use- Returns:
- the configured default
WebSocketUpgradeFilter
instance, use this reference later in your Servlet Initialization Phase to establish mappings of websockets. - Throws:
javax.servlet.ServletException
- if the filer cannot be configured
-
configureContext
@Deprecated public static WebSocketUpgradeFilter configureContext(org.eclipse.jetty.servlet.ServletContextHandler context) throws javax.servlet.ServletException Deprecated.useconfigure(ServletContextHandler)
instead- Parameters:
context
- theServletContextHandler
to use- Returns:
- a configured
WebSocketUpgradeFilter
instance - Throws:
javax.servlet.ServletException
- if the filer cannot be configured
-
configureContext
@Deprecated public static WebSocketUpgradeFilter configureContext(javax.servlet.ServletContext context) throws javax.servlet.ServletException Deprecated.useconfigureContext(ServletContextHandler)
instead- Parameters:
context
- the ServletContext to use- Returns:
- a configured
WebSocketUpgradeFilter
instance - Throws:
javax.servlet.ServletException
- if the filer cannot be configured
-
addMapping
Add Mapping to underlying
NativeWebSocketConfiguration
IMPORTANT: Can only be used during Servlet Initialization phase.
Embedded Jetty users, consider using
NativeWebSocketServletContainerInitializer.configure(ServletContextHandler, NativeWebSocketServletContainerInitializer.Configurator)
instead to define mappings early that happen during the Servlet Initialization phase.- Specified by:
addMapping
in interfaceMappedWebSocketCreator
- Parameters:
spec
- the path spec to usecreator
- the creator for the mapping- See Also:
-
addMapping
Deprecated.Description copied from interface:MappedWebSocketCreator
Add a mapping.- Specified by:
addMapping
in interfaceMappedWebSocketCreator
- Parameters:
spec
- the path spec to usecreator
- the creator for the mapping
-
addMapping
Add Mapping to underlying
NativeWebSocketConfiguration
IMPORTANT: Can only be used during Servlet Initialization phase.
Embedded Jetty users, consider using
NativeWebSocketServletContainerInitializer.configure(ServletContextHandler, NativeWebSocketServletContainerInitializer.Configurator)
instead to define mappings early that happen during the Servlet Initialization phase.- Specified by:
addMapping
in interfaceMappedWebSocketCreator
- Parameters:
spec
- the path spec to use.creator
- the websocket creator for this specific mapping- See Also:
-
removeMapping
Remove Mapping to underlying
NativeWebSocketConfiguration
IMPORTANT: Can only be used during Servlet Initialization phase.
- Specified by:
removeMapping
in interfaceMappedWebSocketCreator
- Parameters:
spec
- the path spec to remove (using the same spec syntax as seen inMappedWebSocketCreator.addMapping(String, WebSocketCreator)
)- Returns:
- true if underlying mapping were altered, false otherwise
- See Also:
-
destroy
public void destroy()- Specified by:
destroy
in interfacejavax.servlet.Filter
-
doFilter
public void doFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain chain) throws IOException, javax.servlet.ServletException - Specified by:
doFilter
in interfacejavax.servlet.Filter
- Throws:
IOException
javax.servlet.ServletException
-
dump
- Specified by:
dump
in interfaceorg.eclipse.jetty.util.component.Dumpable
-
dump
- Specified by:
dump
in interfaceorg.eclipse.jetty.util.component.Dumpable
- Throws:
IOException
-
getFactory
-
getConfiguration
@ManagedAttribute(value="configuration", readonly=true) public NativeWebSocketConfiguration getConfiguration() -
getMapping
Description copied from interface:MappedWebSocketCreator
/** Returns the creator for the given path spec.- Specified by:
getMapping
in interfaceMappedWebSocketCreator
- Parameters:
target
- the spec to test for (using the same spec syntax as seen inMappedWebSocketCreator.addMapping(String, WebSocketCreator)
)- Returns:
- the websocket creator if path spec exists, or null
-
init
public void init(javax.servlet.FilterConfig config) throws javax.servlet.ServletException - Specified by:
init
in interfacejavax.servlet.Filter
- Throws:
javax.servlet.ServletException
-
setToAttribute
private void setToAttribute(org.eclipse.jetty.servlet.ServletContextHandler context, String key) throws javax.servlet.ServletException - Throws:
javax.servlet.ServletException
-
setToAttribute
public void setToAttribute(javax.servlet.ServletContext context, String key) throws javax.servlet.ServletException - Throws:
javax.servlet.ServletException
-
toString
-
addMapping(org.eclipse.jetty.http.pathmap.PathSpec, WebSocketCreator)
instead