Handler.Wrapper that conditionally handles a Request.
The conditions are implemented by IncludeExcludes of:
PathSpec or string representation, which can be efficiently matched.Predicate taking the Request, which is matched in a linear test of all predicates.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:
DontHandle - If the conditions are met, terminate further handling by returning falseReject - If the conditions are met, reject the request with a HttpStatus#FORBIDDEN_403 (or other status code) response.SkipNext - If the conditions are met, then the next handler is skipped and the
following hander invoked instead.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.
| Modifier and Type | Class and Description |
|---|---|
| public abstract static class | ConditionalHandler.Abstract
An Abstract |
| public static class | ConditionalHandler.ConnectorPredicate
A |
| public static class | ConditionalHandler.DontHandle
An implementation of |
| public abstract static class | ConditionalHandler.ElseNext
An abstract implementation of |
| public static class | ConditionalHandler.InetAddressPatternPredicate
A |
| public static class | |
| public static class | ConditionalHandler.PathSpecPredicate
A |
| public static class | ConditionalHandler.PredicateSet
A Set of |
| public static class | ConditionalHandler.Reject
An implementation of |
| public static class | ConditionalHandler.SkipNext
An implementation of |
| Modifier and Type | Field and Description |
|---|---|
| private Predicate | |
| private final IncludeExclude | |
| private final IncludeExclude | |
| private final IncludeExcludeSet | |
| private static final Logger |
| Access | Constructor and Description |
|---|---|
| private | |
| private | |
| private |
| Modifier and Type | Method and Description |
|---|---|
| public void | |
| protected void | doStart()
Overrides org. |
| public void | dump(Appendable
The appendable to dump to out, String The indent to apply after any new lines. indent)Overrides org. Implements org. |
| public void | exclude(PathSpec...
The paths)PathSpecs that are tested against the pathInContext.Exclude |
| public void | exclude(InetAddressPattern...
InetAddressPatterns that are
tested against the getRemoteSocketAddress() of
Request#getConnectionMetaData().Exclude |
| 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. |
| public void | excludeInetAddressPattern(String...
String representations of patterns)InetAddressPatterns that are
tested against the getRemoteSocketAddress() of
Request#getConnectionMetaData().Exclude |
| public void | excludeMethod(String...
The exact case-sensitive method name methods)Exclude |
| public void | excludePath(String...
String representations of paths)PathSpecs that are
tested against the pathInContext.Exclude |
| public static Predicate | |
| public static Predicate | from(String
The connector name or connectorName, InetAddressPattern nullAn inetAddressPattern, String InetAddressPattern or nullA method, PathSpec org. name or nullA pathSpec)PathSpec or nullCreate a |
| pack-priv IncludeExclude | |
| pack-priv IncludeExclude | |
| pack-priv IncludeExcludeSet | |
| 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. Implements org. Invoked to handle the passed HTTP request and response. |
| public void | include(PathSpec...
The paths)PathSpecs that are tested against the pathInContext.Include |
| public void | include(InetAddressPattern...
InetAddressPatterns that are
tested against the getRemoteSocketAddress() of
Request#getConnectionMetaData().Include |
| 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. |
| public void | includeInetAddressPattern(String...
String representations of patterns)InetAddressPatterns that are
tested against the getRemoteSocketAddress() of
Request#getConnectionMetaData().Include |
| public void | includeMethod(String...
The exact case-sensitive method name methods)Include |
| public void | includePath(String...
String representations of paths)PathSpecs that are
tested against the pathInContext.Include |
| protected boolean | Returns: True if this handler will complete the callbackThe request to handle request, Response The response to generate response, Callback The callback for completion callback)Handle a request by invoking the |
| protected abstract boolean | Returns: True if this handler will complete the callbackThe 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 callbackThe 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 |