Top Description Inners Fields Constructors Methods
org.eclipse.jetty.ee10.websocket.server

public abstract Class JettyWebSocketServlet

extends HttpServlet
Class Inheritance
Imports
java.io.IOException, java.lang.reflect.Constructor, java.time.Duration, java.util.Set, jakarta.servlet.ServletException, jakarta.servlet.http.HttpServlet, .HttpServletRequest, .HttpServletResponse, org.eclipse.jetty.ee10.servlet.ServletContextHandler, .ServletContextRequest, .ServletContextResponse, org.eclipse.jetty.ee10.websocket.server.internal.DelegatedServerUpgradeRequest, .DelegatedServerUpgradeResponse, .JettyServerFrameHandlerFactory, org.eclipse.jetty.ee10.websocket.servlet.WebSocketUpgradeFilter, org.eclipse.jetty.http.HttpStatus, org.eclipse.jetty.server.handler.ContextHandler, org.eclipse.jetty.util.Callback, .FutureCallback, org.eclipse.jetty.websocket.core.Configuration, .WebSocketComponents, .WebSocketConstants, org.eclipse.jetty.websocket.core.server.FrameHandlerFactory, .ServerUpgradeRequest, .ServerUpgradeResponse, .WebSocketCreator, .WebSocketMappings, .WebSocketServerComponents, org.slf4j.Logger, .LoggerFactory

Abstract Servlet used to bridge the Servlet API to the WebSocket API.

To use this servlet, you will be required to register your websockets with the WebSocketMappings so that it can create your websockets under the appropriate conditions.

The most basic implementation would be as follows:

package my.example;

import JettyWebSocketServlet;
import JettyWebSocketServletFactory;

public class MyEchoServlet extends JettyWebSocketServlet
{
    @Override
    public void configure(JettyWebSocketServletFactory factory)
    {
      factory.setDefaultMaxFrameSize(4096);
      factory.addMapping(factory.parsePathSpec("/"), (req,res)->new EchoSocket());
    }
}

Only request that conforms to a "WebSocket: Upgrade" handshake request will trigger the WebSocketMappings handling of creating WebSockets. All other requests are treated as normal servlet requests. The configuration defined by this servlet init parameters will be used as the customizer for any mappings created by JettyWebSocketServletFactory#addMapping(String, JettyWebSocketCreator) during configure(JettyWebSocketServletFactory) calls. The request upgrade may be peformed by this servlet, or is may be performed by a WebSocketUpgradeFilter instance that will share the same WebSocketMappings instance. If the filter is used, then the filter configuraton is used as the default configuration prior to this servlets configuration being applied.

Configuration / Init-Parameters:

idleTimeout
set the time in ms that a websocket may be idle before closing
maxTextMessageSize
set the size in UTF-8 bytes that a websocket may be accept as a Text Message before closing
maxBinaryMessageSize
set the size in bytes that a websocket may be accept as a Binary Message before closing
inputBufferSize
set the size in bytes of the buffer used to read raw bytes from the network layer
*
outputBufferSize
set the size in bytes of the buffer used to write bytes to the network layer
maxFrameSize
The maximum frame size sent or received.
autoFragment
If true, frames are automatically fragmented to respect the maximum frame size.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
private static record

Field Summary

Modifier and TypeField and Description
private WebSocketComponents
private final JettyWebSocketServlet.CustomizedWebSocketServletFactory
private static final Logger
private WebSocketMappings
Inherited from jakarta.servlet.http.HttpServlet:
LEGACY_DO_HEAD

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected abstract void
configure(JettyWebSocketServletFactory
the JettyWebSocketServletFactory
factory
)

Configure the JettyWebSocketServletFactory for this servlet instance by setting default configuration (which may be overriden by annotations) and mapping JettyWebSocketCreators.

public void
destroy()

Overrides jakarta.servlet.GenericServlet.destroy.

Implements jakarta.servlet.Servlet.destroy.

Called by the servlet container to indicate to a servlet that the servlet is being taken out of service.

private FrameHandlerFactory

Returns:

the instance of FrameHandlerFactory to be used to create the FrameHandler
getFactory
()

Get the instance of FrameHandlerFactory to be used to create the FrameHandler.

public void
init()

Overrides jakarta.servlet.GenericServlet.init.

A convenience method which can be overridden so that there's no need to call super.init(config).

protected void
service(HttpServletRequest
the HttpServletRequest object that contains the request the client made of the servlet
req
,
HttpServletResponse
the HttpServletResponse object that contains the response the servlet returns to the client
resp
)

Overrides jakarta.servlet.http.HttpServlet.service.

Receives standard HTTP requests from the public service method and dispatches them to the doXXX methods defined in this class.

Inherited from jakarta.servlet.http.HttpServlet:
doDeletedoGetdoHeaddoOptionsdoPostdoPutdoTracegetLastModifiedinitservice

Field Detail

componentsback to summary
private WebSocketComponents components
customizerback to summary
private final JettyWebSocketServlet.CustomizedWebSocketServletFactory customizer
LOGback to summary
private static final Logger LOG
mappingsback to summary
private WebSocketMappings mappings

Constructor Detail

JettyWebSocketServletback to summary
public JettyWebSocketServlet()

Method Detail

configureback to summary
protected abstract void configure(JettyWebSocketServletFactory factory)

Configure the JettyWebSocketServletFactory for this servlet instance by setting default configuration (which may be overriden by annotations) and mapping JettyWebSocketCreators. This method assumes a single FrameHandlerFactory will be available as a bean on the ContextHandler, which in practise will mostly the the Jetty WebSocket API factory.

Parameters
factory:JettyWebSocketServletFactory

the JettyWebSocketServletFactory

destroyback to summary
public void destroy()

Overrides jakarta.servlet.GenericServlet.destroy.

Implements jakarta.servlet.Servlet.destroy.

Doc from jakarta.servlet.GenericServlet.destroy.

Called by the servlet container to indicate to a servlet that the servlet is being taken out of service. See Servlet#destroy.

Annotations
@Override
getFactoryback to summary
private FrameHandlerFactory getFactory()

Get the instance of FrameHandlerFactory to be used to create the FrameHandler.

Returns:FrameHandlerFactory

the instance of FrameHandlerFactory to be used to create the FrameHandler

initback to summary
public void init() throws ServletException

Overrides jakarta.servlet.GenericServlet.init.

Doc from jakarta.servlet.GenericServlet.init.

A convenience method which can be overridden so that there's no need to call super.init(config).

Instead of overriding init(ServletConfig), simply override this method and it will be called by GenericServlet.init(ServletConfig config). The ServletConfig object can still be retrieved via getServletConfig.

Annotations
@Override
Exceptions
ServletException:
if an exception occurs that interrupts the servlet's normal operation
serviceback to summary
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException

Overrides jakarta.servlet.http.HttpServlet.service.

Doc from jakarta.servlet.http.HttpServlet.service.

Receives standard HTTP requests from the public service method and dispatches them to the doXXX methods defined in this class. This method is an HTTP-specific version of the jakarta.servlet.Servlet#service method. There's no need to override this method.

Parameters
req:HttpServletRequest

the HttpServletRequest object that contains the request the client made of the servlet

resp:HttpServletResponse

the HttpServletResponse object that contains the response the servlet returns to the client

Annotations
@Override
Exceptions
ServletException:
if the HTTP request cannot be handled
IOException:
if an input or output error occurs while the servlet is handling the HTTP request
org.eclipse.jetty.ee10.websocket.server back to summary

private Class JettyWebSocketServlet.CustomizedWebSocketServletFactory

extends ConfigurationCustomizer
implements JettyWebSocketServletFactory
Class Inheritance
All Implemented Interfaces
org.eclipse.jetty.ee10.websocket.server.JettyWebSocketServletFactory, org.eclipse.jetty.websocket.api.Configurable

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public void
addMapping(String
the pathspec to respond on
pathSpec
,
JettyWebSocketCreator
the WebSocketCreator to use
creator
)

Implements org.eclipse.jetty.ee10.websocket.server.JettyWebSocketServletFactory.addMapping.

Add a WebSocket mapping to a provided JettyWebSocketCreator.

public Set<String>
public JettyWebSocketCreator
getMapping(String
the pathspec to respond on
pathSpec
)

Implements org.eclipse.jetty.ee10.websocket.server.JettyWebSocketServletFactory.getMapping.

Returns the creator for the given path spec.

public void
register(Class<?>
the WebSocket class to use
endpointClass
)

Implements org.eclipse.jetty.ee10.websocket.server.JettyWebSocketServletFactory.register.

Add a WebSocket mapping at PathSpec "/" for a creator which creates the endpointClass

public boolean
removeMapping(String
the pathspec to respond on
pathSpec
)

Implements org.eclipse.jetty.ee10.websocket.server.JettyWebSocketServletFactory.removeMapping.

Removes the mapping based on the given path spec.

public void
setCreator(JettyWebSocketCreator
the WebSocketCreator to use
creator
)

Implements org.eclipse.jetty.ee10.websocket.server.JettyWebSocketServletFactory.setCreator.

Add a WebSocket mapping at PathSpec "/" for a creator

Inherited from org.eclipse.jetty.websocket.core.Configuration.ConfigurationCustomizer:
customizegetIdleTimeoutgetInputBufferSizegetMaxBinaryMessageSizegetMaxFrameSizegetMaxOutgoingFramesgetMaxTextMessageSizegetOutputBufferSizegetWriteTimeoutisAutoFragmentsetAutoFragmentsetIdleTimeoutsetInputBufferSizesetMaxBinaryMessageSizesetMaxFrameSizesetMaxOutgoingFramessetMaxTextMessageSizesetOutputBufferSizesetWriteTimeouttoString

Constructor Detail

CustomizedWebSocketServletFactoryback to summary
private CustomizedWebSocketServletFactory()

Method Detail

addMappingback to summary
public void addMapping(String pathSpec, JettyWebSocketCreator creator)

Implements org.eclipse.jetty.ee10.websocket.server.JettyWebSocketServletFactory.addMapping.

Doc from org.eclipse.jetty.ee10.websocket.server.JettyWebSocketServletFactory.addMapping.

Add a WebSocket mapping to a provided JettyWebSocketCreator.

If mapping is added before this configuration is started, then it is persisted through stop/start of this configuration's lifecycle. Otherwise it will be removed when this configuration is stopped.

Parameters
pathSpec:String

the pathspec to respond on

creator:JettyWebSocketCreator

the WebSocketCreator to use

Annotations
@Override
getAvailableExtensionNamesback to summary
public Set<String> getAvailableExtensionNames()

Implements org.eclipse.jetty.ee10.websocket.server.JettyWebSocketServletFactory.getAvailableExtensionNames.

Doc from org.eclipse.jetty.ee10.websocket.server.JettyWebSocketServletFactory.getAvailableExtensionNames.

Get the names of all available WebSocket Extensions.

Returns:Set<String>

a set the available extension names.

Annotations
@Override
getMappingback to summary
public JettyWebSocketCreator getMapping(String pathSpec)

Implements org.eclipse.jetty.ee10.websocket.server.JettyWebSocketServletFactory.getMapping.

Doc from org.eclipse.jetty.ee10.websocket.server.JettyWebSocketServletFactory.getMapping.

Returns the creator for the given path spec.

Parameters
pathSpec:String

the pathspec to respond on

Returns:JettyWebSocketCreator

the websocket creator if path spec exists, or null

Annotations
@Override
registerback to summary
public void register(Class<?> endpointClass)

Implements org.eclipse.jetty.ee10.websocket.server.JettyWebSocketServletFactory.register.

Doc from org.eclipse.jetty.ee10.websocket.server.JettyWebSocketServletFactory.register.

Add a WebSocket mapping at PathSpec "/" for a creator which creates the endpointClass

Parameters
endpointClass:Class<?>

the WebSocket class to use

Annotations
@Override
removeMappingback to summary
public boolean removeMapping(String pathSpec)

Implements org.eclipse.jetty.ee10.websocket.server.JettyWebSocketServletFactory.removeMapping.

Doc from org.eclipse.jetty.ee10.websocket.server.JettyWebSocketServletFactory.removeMapping.

Removes the mapping based on the given path spec.

Parameters
pathSpec:String

the pathspec to respond on

Returns:boolean

true if underlying mapping were altered, false otherwise

Annotations
@Override
setCreatorback to summary
public void setCreator(JettyWebSocketCreator creator)

Implements org.eclipse.jetty.ee10.websocket.server.JettyWebSocketServletFactory.setCreator.

Doc from org.eclipse.jetty.ee10.websocket.server.JettyWebSocketServletFactory.setCreator.

Add a WebSocket mapping at PathSpec "/" for a creator

Parameters
creator:JettyWebSocketCreator

the WebSocketCreator to use

Annotations
@Override
org.eclipse.jetty.ee10.websocket.server back to summary

private final Record JettyWebSocketServlet.WrappedJettyCreator

extends Record
implements WebSocketCreator
Class Inheritance
All Implemented Interfaces
org.eclipse.jetty.websocket.core.server.WebSocketCreator
Record Components
creator:JettyWebSocketCreator 

Field Summary

Modifier and TypeField and Description
private final JettyWebSocketCreator
creator

Record Component accessed by creator().

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public Object
createWebSocket(ServerUpgradeRequest
the request details
upgradeRequest
,
ServerUpgradeResponse
the response details
upgradeResponse
,
Callback
the callback, should only be completed by the creator if a null WebSocket object is returned.
callback
)

Implements org.eclipse.jetty.websocket.core.server.WebSocketCreator.createWebSocket.

Create a websocket from the incoming request.

public JettyWebSocketCreator
creator()

Record Component getter of creator.

public final boolean
equals(Object
the reference object with which to compare.
o
)

Implements abstract java.lang.Record.equals.

Indicates whether some other object is "equal to" this one.

private JettyWebSocketCreator
public final int
hashCode()

Implements abstract java.lang.Record.hashCode.

Returns a hash code value for the record.

public final String
toString()

Implements abstract java.lang.Record.toString.

Returns a string representation of the record.

Field Detail

creatorback to summary
private final JettyWebSocketCreator creator

Record Component accessed by creator().

Constructor Detail

WrappedJettyCreatorback to summary
private WrappedJettyCreator(JettyWebSocketCreator creator)

Method Detail

createWebSocketback to summary
public Object createWebSocket(ServerUpgradeRequest upgradeRequest, ServerUpgradeResponse upgradeResponse, Callback callback)

Implements org.eclipse.jetty.websocket.core.server.WebSocketCreator.createWebSocket.

Doc from org.eclipse.jetty.websocket.core.server.WebSocketCreator.createWebSocket.

Create a websocket from the incoming request.

If the creator returns null it is responsible for completing the Callback and sending a response. But if the creator intends to return non-null WebSocket object, it MUST NOT write content to the response or complete the Callback, but it may modify the response headers.

Parameters
upgradeRequest:ServerUpgradeRequest

the request details

upgradeResponse:ServerUpgradeResponse

the response details

callback:Callback

the callback, should only be completed by the creator if a null WebSocket object is returned.

Returns:Object

the WebSocket object, or null to take responsibility to send error response if no WebSocket is to be created.

Annotations
@Override
creatorback to summary
public JettyWebSocketCreator creator()

Record Component getter of creator.

equalsback to summary
public final boolean equals(Object o)

Implements abstract java.lang.Record.equals.

Doc from java.lang.Record.equals.

Indicates whether some other object is "equal to" this one. In addition to the general contract of Object.equals, record classes must further obey the invariant that when a record instance is "copied" by passing the result of the record component accessor methods to the canonical constructor, as follows:

    R copy = new R(r.c1(), r.c2(), ..., r.cn());
then it must be the case that r.equals(copy).
Parameters
o:Object

the reference object with which to compare.

Returns:boolean

true if this record is equal to the argument; false otherwise.

getJettyWebSocketCreatorback to summary
private JettyWebSocketCreator getJettyWebSocketCreator()
hashCodeback to summary
public final int hashCode()

Implements abstract java.lang.Record.hashCode.

Doc from java.lang.Record.hashCode.

Returns a hash code value for the record. Obeys the general contract of Object.hashCode. For records, hashing behavior is constrained by the refined contract of Record.equals, so that any two records created from the same components must have the same hash code.

Returns:int

a hash code value for this record.

toStringback to summary
public final String toString()

Implements abstract java.lang.Record.toString.

Doc from java.lang.Record.toString.

Returns a string representation of the record. In accordance with the general contract of Object#toString(), the toString method returns a string that "textually represents" this record. The result should be a concise but informative representation that is easy for a person to read.

In addition to this general contract, record classes must further participate in the invariant that any two records which are equal must produce equal strings. This invariant is necessarily relaxed in the rare case where corresponding equal component values might fail to produce equal strings for themselves.

Returns:String

a string representation of the object.