Class ServletChannel

java.lang.Object
org.eclipse.jetty.ee10.servlet.ServletChannel

public class ServletChannel extends Object
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 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:
  • Constructor Details

    • ServletChannel

      public ServletChannel(ServletContextHandler servletContextHandler, org.eclipse.jetty.server.Request request)
    • ServletChannel

      public ServletChannel(ServletContextHandler servletContextHandler, org.eclipse.jetty.server.ConnectionMetaData connectionMetaData)
  • Method Details

    • getConnectionMetaData

      public org.eclipse.jetty.server.ConnectionMetaData getConnectionMetaData()
    • getCallback

      public org.eclipse.jetty.util.Callback getCallback()
    • associate

      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 - The servlet context request to associate
      See Also:
      • recycle(Throwable)
    • associate

      public void associate(org.eclipse.jetty.server.Request request, org.eclipse.jetty.server.Response response, org.eclipse.jetty.util.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 - The request, which may have been wrapped after #ServletContextHandler.wrapRequest(Request, Response)
      response - The response, which may have been wrapped after #ServletContextHandler.wrapResponse(ContextRequest, Response)
      callback - The context, which may have been wrapped after ContextHandler.handle(Request, Response, Callback)
    • getContext

    • getServletContextHandler

      public ServletContextHandler getServletContextHandler()
    • getServletContextApi

      public ServletContextHandler.ServletContextApi getServletContextApi()
    • getHttpOutput

      public HttpOutput getHttpOutput()
    • getHttpInput

      public HttpInput getHttpInput()
    • isAborted

      public boolean isAborted()
    • isSendError

      public boolean isSendError()
    • formatAddrOrHost

      protected String formatAddrOrHost(String addr)
      Format the address or host returned from Request methods
      Parameters:
      addr - The address or host
      Returns:
      Default implementation returns HostPort.normalizeHost(String)
    • getServletRequestState

      public ServletChannelState getServletRequestState()
    • getIdleTimeout

      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:
      the idle timeout (in milliseconds)
    • setIdleTimeout

      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 - the idle timeout in milliseconds
    • getHttpConfiguration

      public org.eclipse.jetty.server.HttpConfiguration getHttpConfiguration()
    • getServer

      public org.eclipse.jetty.server.Server getServer()
    • getServletContextRequest

      public ServletContextRequest getServletContextRequest()
      Returns:
      The ServletContextRequest as wrapped by the ServletContextHandler.
      See Also:
    • getRequest

      public org.eclipse.jetty.server.Request getRequest()
      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 associate(Request, Response, Callback).
      See Also:
    • getServletContextResponse

      public ServletContextResponse getServletContextResponse()
      Returns:
      The ServetContextResponse as wrapped by the ServletContextHandler.
      See Also:
    • getResponse

      public org.eclipse.jetty.server.Response getResponse()
      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 associate(Request, Response, Callback).
      See Also:
    • getConnection

      public org.eclipse.jetty.io.Connection getConnection()
    • getEndPoint

      public org.eclipse.jetty.io.EndPoint getEndPoint()
    • getLocalName

      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:
      the local name, or null
    • getLocalPort

      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:
      the local port, or 0 if unspecified
    • getLocalAddress

      public InetSocketAddress getLocalAddress()
    • getRemoteAddress

      public InetSocketAddress getRemoteAddress()
    • getServerAuthority

      public org.eclipse.jetty.util.HostPort getServerAuthority()
      Get return the HttpConfiguration server authority override.
      Returns:
      return the HttpConfiguration server authority override
    • handle

      public void handle()
      Handle the servlet request. This is called on the initial dispatch and then again on any asynchronous events.
    • handleException

      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 - the Throwable that caused the problem
    • unwrap

      protected Throwable unwrap(Throwable failure, Class<?>... targets)
      Unwrap failure causes to find target class
      Parameters:
      failure - The throwable to have its causes unwrapped
      targets - Exception classes that we should not unwrap
      Returns:
      A target throwable or null
    • sendErrorResponseAndComplete

      public void sendErrorResponseAndComplete()
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • onCompleted

      public void onCompleted()
      See Also:
    • isCommitted

      public boolean isCommitted()
    • isRequestCompleted

      public boolean isRequestCompleted()
      Returns:
      True if the request lifecycle is completed
    • isResponseCompleted

      public boolean isResponseCompleted()
      Returns:
      True if the response is completely written.
    • execute

      protected void execute(Runnable task)
    • execute

      protected void execute(Runnable task, org.eclipse.jetty.server.Request request)
    • abort

      public void abort(Throwable failure)
      If a write or similar operation to this channel fails, then this method should be called.
      Parameters:
      failure - the failure that caused the abort.
      See Also:
    • dispatchAsync

      public void dispatchAsync() throws Exception
      An AsyncContext dispatch method was called. We may need to dispatch to a different context.
      Throws:
      Exception