Top Description Inners Fields Constructors Methods
org.eclipse.jetty.server.handler

public abstract Class ConditionalHandler

extends Wrapper
Class Inheritance
Known Direct Subclasses
org.eclipse.jetty.server.handler.ConditionalHandler.Abstract, org.eclipse.jetty.server.handler.ConditionalHandler.ElseNext
Imports
java.io.IOException, java.net.InetAddress, .InetSocketAddress, .SocketAddress, .UnknownHostException, java.util.AbstractSet, .ArrayList, .Iterator, .Objects, .Set, java.util.function.Predicate, org.eclipse.jetty.http.HttpStatus, org.eclipse.jetty.http.pathmap.PathSpec, .PathSpecSet, org.eclipse.jetty.server.ConnectionMetaData, .Connector, .Handler, .Request, .Response, org.eclipse.jetty.util.Callback, .IncludeExclude, .IncludeExcludeSet, .InetAddressPattern, .TypeUtil, org.eclipse.jetty.util.component.DumpableCollection, org.slf4j.Logger, .LoggerFactory

A Handler.Wrapper that conditionally handles a Request. The conditions are implemented by IncludeExcludes of:

If the conditions are met, the abstract onConditionsMet(Request, Response, Callback) method will be invoked, otherwise the onConditionsNotMet(Request, Response, Callback) method will be invoked. Implementations may call the nextHandler(Request, Response, Callback) method to call the wrapped handler.

A typical usage is to extend the Abstract sub class and provide an implementation of onConditionsMet(Request, Response, Callback) and onConditionsNotMet(Request, Response, Callback):

public class MyOptionalHandler extends ConditionalHandler.Abstract
{
    @Override
    public boolean onConditionsMet(Request request, Response response, Callback callback)
    {
        response.getHeaders().add("Test", "My Optional Handling");
        return nextHandle(request, response, callback);
    }

    @Override
    public boolean onConditionsNoMet(Request request, Response response, Callback callback)
    {
        return false;
    }
}

If the conditions added to MyOptionalHandler are met, then the onConditionsMet(Request, Response, Callback) method is called and a response header added before invoking nextHandler(Request, Response, Callback), otherwise the onConditionsNotMet(Request, Response, Callback) is called, which returns false to indicate no more handling.

Alternatively, one of the concrete subclasses may be used. These implementations conditionally provide a specific action in their onConditionsMet(Request, Response, Callback) methods:

Otherwise, if their conditions are not met, these subclasses are all extension of the abstract ElseNext subclass, that implements onConditionsNotMet(Request, Response, Callback) to call nextHandler(Request, Response, Callback). Thus their specific behaviour is not applied and the handling continues with the next handler.

These concrete handlers are ideal for retrofitting conditional behavior. For example, if an application handler was found to not correctly handle the OPTIONS method for the path "/secret/*", it could be protected as follows:

Server server = new Server();
   ApplicationHandler application = new ApplicationHandler();
   server.setHandler(application);

   ConditionalHandler reject = new ConditionalHandler.Reject(403); // or DontHandle
   reject.includeMethod("OPTIONS");
   reject.includePath("/secret/*");
   server.insertHandler(reject);

Another example, in an application comprised of several handlers, one of which is a wrapping handler whose behavior needs to be skipped for "POST" requests, then it could be achieved as follows:

Server server = new Server();
   ApplicationWrappingHandler wrappingHandler = new ApplicationWrappingHandler();
   ApplicationHandler applicationHandler = new ApplicationHandler();
   server.setHandler(wrappingHandler);
   filter.setHandler(applicationHandler);

   ConditionalHandler skipNext = new ConditionalHandler.SkipNext();
   skipNext.includeMethod("POST");
   skipNext.setHandler(wrappingHandler);
   server.setHandler(skipNext);

Note that a better solution, if possible, would be for the ApplicationFilterHandler and/or ApplicationHandler handlers to extend ConditionalHandler.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public abstract static class
public static class
public static class
ConditionalHandler.DontHandle

An implementation of ConditionalHandler that, if conditions are met, will not do any further handling by returning false from onConditionsMet(Request, Response, Callback).

public abstract static class
public static class
public static class
public static class
public static class
public static class
ConditionalHandler.Reject

An implementation of ConditionalHandler that, if conditions are met, will reject the request by sending a response (by default a HttpStatus#FORBIDDEN_403).

public static class
ConditionalHandler.SkipNext

An implementation of ConditionalHandler that, if conditions are met, will skip the next Handler by invoking its next Handler.

Field Summary

Modifier and TypeField and Description
private Predicate<Request>
private final IncludeExclude<String>
private final IncludeExclude<String>
private final IncludeExcludeSet<Predicate<Request>, Request>
private static final Logger

Constructor Summary

AccessConstructor and Description
private
private
private
ConditionalHandler(boolean dynamic, Handler nextHandler)

Method Summary

Modifier and TypeMethod and Description
public void
clear()

Clear all inclusions and exclusions.

protected void
doStart()

Overrides org.eclipse.jetty.server.Handler.Abstract.doStart.

Starts the managed lifecycle beans in the order they were added.
public void
dump(Appendable
The appendable to dump to
out
,
String
The indent to apply after any new lines.
indent
)

Overrides org.eclipse.jetty.util.component.ContainerLifeCycle.dump.

Implements org.eclipse.jetty.util.component.Dumpable.dump.

Dump this object (and children) into an Appendable using the provided indent after any new lines.
public void
exclude(PathSpec...
The PathSpecs that are tested against the pathInContext.
paths
)

Exclude PathSpecs in the conditions to be met

public void
exclude(InetAddressPattern... patterns)

Exclude InetAddressPatterns in the conditions to be met

public final void
exclude(Predicate<Request>...
Predicates that are tested against the Request. This method is optimized so that a passed MethodPredicate or PathSpecPredicate is converted to a more efficient excludeMethod(String...) or exclude(PathSpec...) respectively.
predicates
)

Exclude arbitrary Predicates in the conditions.

public void
excludeInetAddressPattern(String...
String representations of InetAddressPatterns that are tested against the getRemoteSocketAddress() of Request#getConnectionMetaData().
patterns
)

Exclude InetAddressPattern in the conditions to be met

public void
excludeMethod(String...
The exact case-sensitive method name
methods
)

Exclude methods in the conditions to be met

public void
excludePath(String...
String representations of PathSpecs that are tested against the pathInContext.
paths
)

Exclude PathSpec in the conditions to be met

public static Predicate<Request>

Returns:

the combined Predicate over Request
from
(String
The connector name or null
connectorName
,
String
An InetAddressPattern string or null
inetAddressPattern
,
String method, String
A PathSpec string or null
pathSpec
)

Create a Predicate over Request built from the and of one or more of:

public static Predicate<Request>

Returns:

the combined Predicate over Request
from
(String
The connector name or null
connectorName
,
InetAddressPattern inetAddressPattern, String method, PathSpec
A PathSpec or null
pathSpec
)

Create a Predicate over Request built from the and of one or more of:

pack-priv IncludeExclude<String>
pack-priv IncludeExclude<String>
pack-priv IncludeExcludeSet<Predicate<Request>, Request>
public final boolean
handle(Request
the HTTP request to handle
request
,
Response
the HTTP response to handle
response
,
Callback
the callback to complete when the handling is complete
callback
)

Overrides org.eclipse.jetty.server.Handler.Wrapper.handle.

Implements org.eclipse.jetty.server.Request.Handler.handle.

Invoked to handle the passed HTTP request and response.

public void
include(PathSpec...
The PathSpecs that are tested against the pathInContext.
paths
)

Include PathSpecs in the conditions to be met

public void
include(InetAddressPattern... patterns)

Include InetAddressPatterns in the conditions to be met

public final void
include(Predicate<Request>...
Predicates that are tested against the Request. This method is optimized so that a passed MethodPredicate or PathSpecPredicate is converted to a more efficient includeMethod(String...) or include(PathSpec...) respectively.
predicates
)

Include arbitrary Predicates in the conditions.

public void
includeInetAddressPattern(String...
String representations of InetAddressPatterns that are tested against the getRemoteSocketAddress() of Request#getConnectionMetaData().
patterns
)

Include InetAddressPatterns in the conditions to be met

public void
includeMethod(String...
The exact case-sensitive method name
methods
)

Include methods in the conditions to be met

public void
includePath(String...
String representations of PathSpecs that are tested against the pathInContext.
paths
)

Include PathSpecs in the conditions to be met

protected boolean

Returns:

True if this handler will complete the callback
nextHandler
(Request
The request to handle
request
,
Response
The response to generate
response
,
Callback
The callback for completion
callback
)

Handle a request by invoking the handle(Request, Response, Callback) method of the next Handler.

protected abstract boolean

Returns:

True if this handler will complete the callback
onConditionsMet
(Request
The request to handle
request
,
Response
The response to generate
response
,
Callback
The callback for completion
callback
)

Handle a request that has met the conditions.

protected abstract boolean

Returns:

True if this handler will complete the callback
onConditionsNotMet
(Request
The request to handle
request
,
Response
The response to generate
response
,
Callback
The callback for completion
callback
)

This method is called when the request has not met the conditions and is not to be handled by this handler.

private boolean
private boolean
private boolean
Inherited from org.eclipse.jetty.server.Handler.Wrapper:
getHandlergetInvocationTypesetHandler