Top Description Inners Methods
org.eclipse.jetty.server

public Interface Handler

extends LifeCycle, Destroyable, Handler
Known Direct Subinterfaces
org.eclipse.jetty.server.Handler.Container
Known Direct Implementers
org.eclipse.jetty.server.Handler.Abstract
Annotations
@ManagedObject
Imports
java.util.ArrayList, .Collections, .List, java.util.function.Supplier, org.eclipse.jetty.util.Callback, org.eclipse.jetty.util.annotation.ManagedAttribute, .ManagedObject, org.eclipse.jetty.util.component.ContainerLifeCycle, .Destroyable, .LifeCycle, org.eclipse.jetty.util.thread.Invocable, org.slf4j.Logger, .LoggerFactory

A Jetty component that handles HTTP requests, of any version (HTTP/1.1, HTTP/2 or HTTP/3). A Handler is a Request.Handler with the addition of LifeCycle behaviours, plus variants that allow organizing Handlers as a tree structure.

Handlers may wrap the Request, Response and/or Callback and then forward the wrapped instances to their children, so that they see a modified request; and/or to intercept the read of the request content; and/or intercept the generation of the response; and/or to intercept the completion of the callback.

A Handler is an Invocable and implementations must respect the InvocationType they declare within calls to handle(Request, Response, Callback).

A minimal tree structure could be:

Server
`- YourCustomHandler

A more sophisticated tree structure:

Server
`- GzipHandler
   `- ContextHandlerCollection
      +- ContextHandler (contextPath="/user")
      |  `- YourUserHandler
      |- ContextHandler (contextPath="/admin")
      |  `- YourAdminHandler
      `- DefaultHandler

A simple Handler implementation could be:

class SimpleHandler extends Handler.Abstract.NonBlocking
{
    @Override
    public boolean handle(Request request, Response response, Callback callback)
    {
        // Implicitly sends a 200 OK response with no content.
        callback.succeeded();
        return true;
    }
}

A more sophisticated example of a Handler that decides whether to handle requests based on their URI path:

class YourHelloHandler extends Handler.Abstract.NonBlocking
{
    @Override
    public boolean handle(Request request, Response response, Callback callback)
    {
        if (request.getHttpURI().getPath().startsWith("/yourPath"))
        {
            // The request is for this Handler
            response.setStatus(200);
            // The callback is completed when the write is completed.
            response.write(true, UTF_8.encode("hello"), callback);
            return true;
        }
        return false;
    }
}

An example of a Handler that decides whether to pass the request to a child:

class ConditionalHandler extends Handler.Wrapper
{
    @Override
    public boolean handle(Request request, Response response, Callback callback)
    {
        if (request.getHttpURI().getPath().startsWith("/yourPath")
            return super.handle(request, response, callback);
        if (request.getHttpURI().getPath().startsWith("/wrong"))
        {
            Response.writeError(request, response, callback, HttpStatus.BAD_REQUEST_400);
            return true;
        }
        return false;
    }
}
See Also
Request.Handler

Nested and Inner Type Summary

Modifier and TypeClass and Description
public abstract static class
Handler.Abstract

An abstract implementation of Handler that is a ContainerLifeCycle.

public abstract static class
public static interface
Handler.Collection

A Handler.Container that can contain multiple other Handlers.

public static interface
Handler.Container

A Handler that contains one or more other Handlers.

public static class
Handler.Sequence

A Handler.Container that contains an ordered list of children Handlers whose Handler#handle(Request, Response, Callback) method is invoked in sequence on each child until a child returns true.

public static interface
Handler.Singleton

A Handler.Container that can contain one single other Handler.

public static class
Handler.Wrapper

An implementation of Singleton, which is a Container that wraps one single other Handler.

Method Summary

Modifier and TypeMethod and Description
public Server

Returns:

the Server associated with this Handler
getServer
()

public void
setServer(Server
the Server to associate to this Handler
server
)

Set the Server to associate to this Handler.

Inherited from org.eclipse.jetty.server.Request.Handler:
getInvocationTypehandle
Inherited from org.eclipse.jetty.util.component.Destroyable:
destroy
Inherited from org.eclipse.jetty.util.component.LifeCycle:
addEventListenerisFailedisRunningisStartedisStartingisStoppedisStoppingremoveEventListenerstartstartstopstop