Top Description Fields Constructors Methods
org.eclipse.jetty.ee10.servlet

public Class ServletChannel

extends Object
Class Inheritance
Imports
java.io.IOException, java.net.InetSocketAddress, .SocketAddress, java.util.concurrent.TimeoutException, java.util.concurrent.atomic.AtomicLong, jakarta.servlet.RequestDispatcher, org.eclipse.jetty.ee10.servlet.ServletChannelState.Action, org.eclipse.jetty.http.BadMessageException, .HttpFields, .HttpStatus, .HttpURI, org.eclipse.jetty.io.Connection, .EndPoint, .QuietException, org.eclipse.jetty.server.ConnectionMetaData, .CustomRequestLog, .HttpConfiguration, .Request, .Response, .ResponseUtils, .Server, org.eclipse.jetty.server.handler.ContextHandler, .ContextRequest, org.eclipse.jetty.util.Callback, .ExceptionUtil, .HostPort, .URIUtil, org.slf4j.Logger, .LoggerFactory

The ServletChannel contains the state and behaviors associated with the Servlet API lifecycle for a single request/response cycle. Specifically it uses ServletChannelState to coordinate the states of dispatch state, input and output according to the servlet specification. The combined state so obtained is reflected in the behaviour of the contained HttpInput implementation of jakarta.servlet.ServletInputStream.

This class is reusable over multiple requests for the same ServletContextHandler and is recycled after each use before being associated with a new ServletContextRequest and then associated with possibly wrapped request, response and callback.

See Also
ServletChannelState, HttpInput

Field Summary

Modifier and TypeField and Description
private Callback
private final ConnectionMetaData
private final ServletContextHandler.ServletScopedContext
pack-priv final HttpInput
private final HttpOutput
private Request
private final AtomicLong
private Response
private final ServletContextHandler.ServletContextApi
private ServletContextRequest
private final ServletChannelState
private static final Logger

Constructor Summary

AccessConstructor and Description
public
ServletChannel(ServletContextHandler servletContextHandler, Request request)

public
ServletChannel(ServletContextHandler servletContextHandler, ConnectionMetaData connectionMetaData)

Method Summary

Modifier and TypeMethod and Description
public void
abort(Throwable
the failure that caused the abort.
failure
)

If a write or similar operation to this channel fails, then this method should be called.

public void
associate(ServletContextRequest
The servlet context request to associate
servletContextRequest
)

Associate this channel with a specific request.

public void
associate(Request
The request, which may have been wrapped after #ServletContextHandler#wrapRequest(Request, Response)
request
,
Response
The response, which may have been wrapped after #ServletContextHandler#wrapResponse(ContextRequest, Response)
response
,
Callback
The context, which may have been wrapped after ServletContextHandler#handle(Request, Response, Callback)
callback
)

Associate this channel with possibly wrapped values for getRequest(), getResponse() and getCallback().

private void
public void
protected void
protected String

Returns:

Default implementation returns HostPort#normalizeHost(String)
formatAddrOrHost
(String
The address or host
addr
)

Format the address or host returned from Request methods

private long
public Callback
public Connection
public ConnectionMetaData
public ServletContextHandler.ServletScopedContext
public EndPoint
public HttpConfiguration
public HttpInput
public HttpOutput
public long

Returns:

the idle timeout (in milliseconds)
getIdleTimeout
()

Get the idle timeout.

public InetSocketAddress
public String

Returns:

the local name, or null
getLocalName
()

Return the local name of the connected channel.

public int

Returns:

the local port, or 0 if unspecified
getLocalPort
()

Return the Local Port of the connected channel.

public InetSocketAddress
public Request

Returns:

The core Request associated with the request. This may differ from getServletContextRequest() if the request was wrapped by another handler after the ServletContextHandler and passed to ServletChannel#associate(Request, Response, Callback).
getRequest
()

public Response

Returns:

The core Response associated with the API response. This may differ from getServletContextResponse() if the response was wrapped by another handler after the ServletContextHandler and passed to ServletChannel#associate(Request, Response, Callback).
getResponse
()

public Server
public HostPort

Returns:

return the HttpConfiguration server authority override
getServerAuthority
()

Get return the HttpConfiguration server authority override.

public ServletContextHandler.ServletContextApi
public ServletContextHandler
public ServletContextRequest
public ServletContextResponse

Returns:

The ServetContextResponse as wrapped by the ServletContextHandler.
getServletContextResponse
()

public ServletChannelState
public void
handle()

Handle the servlet request.

protected void
handleException(Throwable
the Throwable that caused the problem
failure
)

Sends an error 500, performing a special logic to detect whether the request is suspended, to avoid concurrent writes from the application.

public boolean
public boolean
public boolean

Returns:

True if the request lifecycle is completed
isRequestCompleted
()

public boolean

Returns:

True if the response is completely written.
isResponseCompleted
()

public boolean
public void
pack-priv void
pack-priv void
recycle(Throwable
Any completion exception, or null for successful completion.
x
)

Prepare to be reused.

private void
private boolean

Returns:

true if we have sent an error, false if we have aborted.
sendErrorOrAbort
(String
the error message.
message
)

public void
public void
setIdleTimeout(long
the idle timeout in milliseconds
timeoutMs
)

Set the idle timeout.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
protected Throwable

Returns:

A target throwable or null
unwrap
(Throwable
The throwable to have its causes unwrapped
failure
,
Class<?>...
Exception classes that we should not unwrap
targets
)

Unwrap failure causes to find target class

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

_callbackback to summary
private Callback _callback
_connectionMetaDataback to summary
private final ConnectionMetaData _connectionMetaData
_contextback to summary
private final ServletContextHandler.ServletScopedContext _context
_httpInputback to summary
pack-priv final HttpInput _httpInput
_httpOutputback to summary
private final HttpOutput _httpOutput
_requestback to summary
private Request _request
_requestsback to summary
private final AtomicLong _requests
_responseback to summary
private Response _response
_servletContextApiback to summary
private final ServletContextHandler.ServletContextApi _servletContextApi
_servletContextRequestback to summary
private ServletContextRequest _servletContextRequest
_stateback to summary
private final ServletChannelState _state
LOGback to summary
private static final Logger LOG

Constructor Detail

ServletChannelback to summary
public ServletChannel(ServletContextHandler servletContextHandler, Request request)
ServletChannelback to summary
public ServletChannel(ServletContextHandler servletContextHandler, ConnectionMetaData connectionMetaData)

Method Detail

abortback to summary
public void abort(Throwable failure)

If a write or similar operation to this channel fails, then this method should be called.

Parameters
failure:Throwable

the failure that caused the abort.

See Also
onCompleted()
associateback to summary
public void associate(ServletContextRequest servletContextRequest)

Associate this channel with a specific request. This method is called by the ServletContextHandler when a core Request is accepted and associated with a servlet mapping. The association remains functional until recycle(Throwable) is called, and it remains readable until a call to recycle(Throwable) or a subsequent call to associate.

Parameters
servletContextRequest:ServletContextRequest

The servlet context request to associate

See Also
recycle(Throwable)
associateback to summary
public void associate(Request request, Response response, Callback callback)

Associate this channel with possibly wrapped values for getRequest(), getResponse() and getCallback(). This is called by the ServletHandler immediately before calling handle() on the initial dispatch. This allows for handlers between the ServletContextHandler and the ServletHandler to wrap the instances.

Parameters
request:Request

The request, which may have been wrapped after #ServletContextHandler#wrapRequest(Request, Response)

response:Response

The response, which may have been wrapped after #ServletContextHandler#wrapResponse(ContextRequest, Response)

callback:Callback

The context, which may have been wrapped after ServletContextHandler#handle(Request, Response, Callback)

dispatchback to summary
private void dispatch() throws Exception
dispatchAsyncback to summary
public void dispatchAsync() throws Exception
executeback to summary
protected void execute(Runnable task)
formatAddrOrHostback to summary
protected String formatAddrOrHost(String addr)

Format the address or host returned from Request methods

Parameters
addr:String

The address or host

Returns:String

Default implementation returns HostPort#normalizeHost(String)

getBytesWrittenback to summary
private long getBytesWritten()
getCallbackback to summary
public Callback getCallback()
getConnectionback to summary
public Connection getConnection()
getConnectionMetaDataback to summary
public ConnectionMetaData getConnectionMetaData()
getContextback to summary
public ServletContextHandler.ServletScopedContext getContext()
getEndPointback to summary
public EndPoint getEndPoint()
getHttpConfigurationback to summary
public HttpConfiguration getHttpConfiguration()
getHttpInputback to summary
public HttpInput getHttpInput()
getHttpOutputback to summary
public HttpOutput getHttpOutput()
getIdleTimeoutback to summary
public long getIdleTimeout()

Get the idle timeout.

This is implemented as a call to EndPoint#getIdleTimeout(), but may be overridden by channels that have timeouts different from their connections.

Returns:long

the idle timeout (in milliseconds)

getLocalAddressback to summary
public InetSocketAddress getLocalAddress()
getLocalNameback to summary
public String getLocalName()

Return the local name of the connected channel.

This is the host name after the connector is bound and the connection is accepted.

Value can be overridden by HttpConfiguration#setLocalAddress(SocketAddress).

Note

some connectors are not based on IP networking, and default behavior here will result in a null return. Use HttpConfiguration#setLocalAddress(SocketAddress) to set the value to an acceptable host name.

Returns:String

the local name, or null

getLocalPortback to summary
public int getLocalPort()

Return the Local Port of the connected channel.

This is the port the connector is bound to and is accepting connections on.

Value can be overridden by HttpConfiguration#setLocalAddress(SocketAddress).

Note

some connectors are not based on IP networking, and default behavior here will result in a value of 0 returned. Use HttpConfiguration#setLocalAddress(SocketAddress) to set the value to an acceptable port.

Returns:int

the local port, or 0 if unspecified

getRemoteAddressback to summary
public InetSocketAddress getRemoteAddress()
getRequestback to summary
public Request getRequest()
Returns:Request

The core Request associated with the request. This may differ from getServletContextRequest() if the request was wrapped by another handler after the ServletContextHandler and passed to ServletChannel#associate(Request, Response, Callback).

See Also
getServletContextRequest(), associate(Request, Response, Callback)
getResponseback to summary
public Response getResponse()
Returns:Response

The core Response associated with the API response. This may differ from getServletContextResponse() if the response was wrapped by another handler after the ServletContextHandler and passed to ServletChannel#associate(Request, Response, Callback).

See Also
getServletContextResponse(), associate(Request, Response, Callback)
getServerback to summary
public Server getServer()
getServerAuthorityback to summary
public HostPort getServerAuthority()

Get return the HttpConfiguration server authority override.

Returns:HostPort

return the HttpConfiguration server authority override

getServletContextApiback to summary
public ServletContextHandler.ServletContextApi getServletContextApi()
getServletContextHandlerback to summary
public ServletContextHandler getServletContextHandler()
getServletContextRequestback to summary
public ServletContextRequest getServletContextRequest()
Returns:ServletContextRequest

The ServletContextRequest as wrapped by the ServletContextHandler.

See Also
getRequest()
getServletContextResponseback to summary
public ServletContextResponse getServletContextResponse()
Returns:ServletContextResponse

The ServetContextResponse as wrapped by the ServletContextHandler.

See Also
getResponse()
getServletRequestStateback to summary
public ServletChannelState getServletRequestState()
handleback to summary
public void handle()

Handle the servlet request. This is called on the initial dispatch and then again on any asynchronous events.

handleExceptionback to summary
protected void handleException(Throwable failure)

Sends an error 500, performing a special logic to detect whether the request is suspended, to avoid concurrent writes from the application.

It may happen that the application suspends, and then throws an exception, while an application spawned thread writes the response content; in such case, we attempt to commit the error directly bypassing the ErrorHandler mechanisms and the response OutputStream.

Parameters
failure:Throwable

the Throwable that caused the problem

isAbortedback to summary
public boolean isAborted()
isCommittedback to summary
public boolean isCommitted()
isRequestCompletedback to summary
public boolean isRequestCompleted()
Returns:boolean

True if the request lifecycle is completed

isResponseCompletedback to summary
public boolean isResponseCompleted()
Returns:boolean

True if the response is completely written.

isSendErrorback to summary
public boolean isSendError()
onCompletedback to summary
public void onCompleted()
See Also
abort(Throwable)
onTrailersback to summary
pack-priv void onTrailers(HttpFields trailers)
recycleback to summary
pack-priv void recycle(Throwable x)

Prepare to be reused.

Parameters
x:Throwable

Any completion exception, or null for successful completion.

See Also
associate(ServletContextRequest)
reopenback to summary
private void reopen()
sendErrorOrAbortback to summary
private boolean sendErrorOrAbort(String message)
Parameters
message:String

the error message.

Returns:boolean

true if we have sent an error, false if we have aborted.

sendErrorResponseAndCompleteback to summary
public void sendErrorResponseAndComplete()
setIdleTimeoutback to summary
public void setIdleTimeout(long timeoutMs)

Set the idle timeout.

This is implemented as a call to EndPoint#setIdleTimeout(long), but may be overridden by channels that have timeouts different from their connections.

Parameters
timeoutMs:long

the idle timeout in milliseconds

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override
unwrapback to summary
protected Throwable unwrap(Throwable failure, Class<?>... targets)

Unwrap failure causes to find target class

Parameters
failure:Throwable

The throwable to have its causes unwrapped

targets:Class<?>[]

Exception classes that we should not unwrap

Returns:Throwable

A target throwable or null