Class ConditionalHandler
- All Implemented Interfaces:
Handler
,Handler.Container
,Handler.Singleton
,Request.Handler
,Container
,Destroyable
,Dumpable
,Dumpable.DumpableContainer
,LifeCycle
,Invocable
- Direct Known Subclasses:
ConditionalHandler.Abstract
,ConditionalHandler.ElseNext
Handler.Wrapper
that conditionally handles a Request
.
The conditions are implemented by IncludeExclude
s of:
- A HTTP method name, which can be efficiently matched
- A
PathSpec
or string representation, which can be efficiently matched. - An arbitrary
Predicate
taking theRequest
, 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 ConditionalHandler.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:
ConditionalHandler.DontHandle
- If the conditions are met, terminate further handling by returningfalse
ConditionalHandler.Reject
- If the conditions are met, reject the request with aHttpStatus.FORBIDDEN_403
(or other status code) response.ConditionalHandler.SkipNext
- If the conditions are met, then thenext handler
is skipped and thefollowing hander
invoked instead.
Otherwise, if their conditions are not met, these subclasses are all extension of the abstract ConditionalHandler.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 Class Summary
Modifier and TypeClassDescriptionstatic class
An AbstractConditionalHandler
.static class
APredicate
overRequest
that tests thename
of theconnector
obtained fromRequest.getConnectionMetaData()
static class
An implementation ofConditionalHandler
that, if conditions are met, will not do any further handling by returningfalse
fromConditionalHandler.DontHandle.onConditionsMet(Request, Response, Callback)
.static class
An abstract implementation ofConditionalHandler
that, if conditions are not met, will call thenextHandler(Request, Response, Callback)
fromConditionalHandler.ElseNext.onConditionsNotMet(Request, Response, Callback)
.static class
APredicate
overRequest
that tests anInetAddressPattern
against thegetRemoteSocketAddress()
ofRequest.getConnectionMetaData()
.static class
static class
static class
static class
An implementation ofConditionalHandler
that, if conditions are met, will reject the request by sending a response (by default aHttpStatus.FORBIDDEN_403
).static class
An implementation ofConditionalHandler
that, if conditions are met, will skip the nextHandler
by invoking itsnext Handler
.Nested classes/interfaces inherited from class org.eclipse.jetty.server.Handler.Abstract
Handler.Abstract.NonBlocking
Nested classes/interfaces inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
AbstractLifeCycle.AbstractLifeCycleListener, AbstractLifeCycle.StopException
Nested classes/interfaces inherited from interface org.eclipse.jetty.util.component.Container
Container.InheritedListener, Container.Listener
Nested classes/interfaces inherited from interface org.eclipse.jetty.util.component.Dumpable
Dumpable.DumpableContainer
Nested classes/interfaces inherited from interface org.eclipse.jetty.server.Handler
Handler.AbstractContainer, Handler.Collection, Handler.Container, Handler.Sequence, Handler.Singleton, Handler.Wrapper
Nested classes/interfaces inherited from interface org.eclipse.jetty.util.thread.Invocable
Invocable.Callable, Invocable.InvocationType, Invocable.ReadyTask, Invocable.Task
Nested classes/interfaces inherited from interface org.eclipse.jetty.util.component.LifeCycle
LifeCycle.Listener
Nested classes/interfaces inherited from interface org.eclipse.jetty.server.Request.Handler
Request.Handler.AbortException
-
Field Summary
Fields inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
FAILED, STARTED, STARTING, STOPPED, STOPPING
Fields inherited from interface org.eclipse.jetty.util.thread.Invocable
__nonBlocking, NOOP
-
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Clear all inclusions and exclusions.protected void
doStart()
Starts the managed lifecycle beans in the order they were added.void
dump
(Appendable out, String indent) Dump this object (and children) into an Appendable using the provided indent after any new lines.final void
void
ExcludePathSpec
s in the conditions to be metvoid
exclude
(InetAddressPattern... patterns) ExcludeInetAddressPattern
s in the conditions to be metvoid
excludeInetAddressPattern
(String... patterns) ExcludeInetAddressPattern
in the conditions to be metvoid
excludeMethod
(String... methods) Excludemethod
s in the conditions to be metvoid
excludePath
(String... paths) ExcludePathSpec
in the conditions to be metCreate aPredicate
overRequest
built from theand
of one or more of:ConditionalHandler.ConnectorPredicate
ConditionalHandler.InetAddressPatternPredicate
ConditionalHandler.MethodPredicate
ConditionalHandler.PathSpecPredicate
from
(String connectorName, InetAddressPattern inetAddressPattern, String method, PathSpec pathSpec) Create aPredicate
overRequest
built from theand
of one or more of:TypeUtil.truePredicate()
ConditionalHandler.ConnectorPredicate
ConditionalHandler.InetAddressPatternPredicate
ConditionalHandler.MethodPredicate
ConditionalHandler.PathSpecPredicate
final boolean
Invoked to handle the passed HTTP request and response.final void
void
IncludePathSpec
s in the conditions to be metvoid
include
(InetAddressPattern... patterns) IncludeInetAddressPattern
s in the conditions to be metvoid
includeInetAddressPattern
(String... patterns) IncludeInetAddressPattern
s in the conditions to be metvoid
includeMethod
(String... methods) Includemethod
s in the conditions to be metvoid
includePath
(String... paths) IncludePathSpec
s in the conditions to be metprotected boolean
nextHandler
(Request request, Response response, Callback callback) Handle a request by invoking thehandle(Request, Response, Callback)
method of thenext Handler
.protected abstract boolean
onConditionsMet
(Request request, Response response, Callback callback) Handle a request that has met the conditions.protected abstract boolean
onConditionsNotMet
(Request request, Response response, Callback callback) This method is called when the request has not met the conditions and is not to be handled by this handler.Methods inherited from class org.eclipse.jetty.server.Handler.Wrapper
getHandler, getInvocationType, setHandler
Methods inherited from class org.eclipse.jetty.server.Handler.AbstractContainer
findContainerOf, getDescendant, getDescendants, isDynamic, setDynamic, setServer
Methods inherited from class org.eclipse.jetty.server.Handler.Abstract
destroy, doStop, getServer
Methods inherited from class org.eclipse.jetty.util.component.ContainerLifeCycle
addBean, addBean, addEventListener, addManaged, contains, dump, dump, dumpObjects, dumpStdErr, getBean, getBeans, getBeans, getContainedBeans, getContainedBeans, installBean, installBean, isAuto, isManaged, isUnmanaged, manage, removeBean, removeBeans, removeEventListener, setBeans, start, stop, unmanage, updateBean, updateBean, updateBeans, updateBeans
Methods inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
getEventListeners, getState, getState, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, setEventListeners, start, stop, toString
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.eclipse.jetty.util.component.Container
getCachedBeans, getEventListeners
Methods inherited from interface org.eclipse.jetty.util.component.Destroyable
destroy
Methods inherited from interface org.eclipse.jetty.util.component.Dumpable.DumpableContainer
isDumpable
Methods inherited from interface org.eclipse.jetty.server.Handler.Container
getContainer, getDescendant, getDescendants, getDescendants
Methods inherited from interface org.eclipse.jetty.server.Handler.Singleton
getHandlers, getTail, insertHandler, setHandler
Methods inherited from interface org.eclipse.jetty.util.component.LifeCycle
addEventListener, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeEventListener, start, stop
-
Method Details
-
clear
public void clear()Clear all inclusions and exclusions. -
includeMethod
Includemethod
s in the conditions to be met- Parameters:
methods
- The exact case-sensitive method name
-
excludeMethod
Excludemethod
s in the conditions to be met- Parameters:
methods
- The exact case-sensitive method name
-
include
IncludePathSpec
s in the conditions to be met- Parameters:
paths
- ThePathSpec
s that are tested against thepathInContext
.
-
exclude
ExcludePathSpec
s in the conditions to be met- Parameters:
paths
- ThePathSpec
s that are tested against thepathInContext
.
-
includePath
IncludePathSpec
s in the conditions to be met- Parameters:
paths
- String representations ofPathSpec
s that are tested against thepathInContext
.
-
excludePath
ExcludePathSpec
in the conditions to be met- Parameters:
paths
- String representations ofPathSpec
s that are tested against thepathInContext
.
-
include
IncludeInetAddressPattern
s in the conditions to be met- Parameters:
patterns
-InetAddressPattern
s that are tested against thegetRemoteSocketAddress()
ofRequest.getConnectionMetaData()
.
-
includeInetAddressPattern
IncludeInetAddressPattern
s in the conditions to be met- Parameters:
patterns
- String representations ofInetAddressPattern
s that are tested against thegetRemoteSocketAddress()
ofRequest.getConnectionMetaData()
.
-
exclude
ExcludeInetAddressPattern
s in the conditions to be met- Parameters:
patterns
-InetAddressPattern
s that are tested against thegetRemoteSocketAddress()
ofRequest.getConnectionMetaData()
.
-
excludeInetAddressPattern
ExcludeInetAddressPattern
in the conditions to be met- Parameters:
patterns
- String representations ofInetAddressPattern
s that are tested against thegetRemoteSocketAddress()
ofRequest.getConnectionMetaData()
.
-
include
- Parameters:
predicates
-Predicate
s that are tested against theRequest
. This method is optimized so that a passedConditionalHandler.MethodPredicate
orConditionalHandler.PathSpecPredicate
is converted to a more efficientincludeMethod(String...)
orinclude(PathSpec...)
respectively.
-
exclude
- Parameters:
predicates
-Predicate
s that are tested against theRequest
. This method is optimized so that a passedConditionalHandler.MethodPredicate
orConditionalHandler.PathSpecPredicate
is converted to a more efficientexcludeMethod(String...)
orexclude(PathSpec...)
respectively.
-
doStart
Description copied from class:ContainerLifeCycle
Starts the managed lifecycle beans in the order they were added.- Overrides:
doStart
in classHandler.Abstract
- Throws:
AbstractLifeCycle.StopException
- If thrown, the lifecycle will immediately be stopped.Exception
- If there was a problem starting. Will cause a transition to FAILED state
-
handle
Description copied from interface:Request.Handler
Invoked to handle the passed HTTP request and response.
The request is accepted by returning true, then handling must be concluded by completing the passed callback. The handling may be asynchronous, i.e. this method may return true and complete the given callback later, possibly from a different thread. If this method returns false, then the callback must not be invoked and any mutation on the response reversed.
Exceptions thrown by this method may be subsequently handled by an error
Request.Handler
, if present, otherwise a default HTTP 500 error is generated and the callback completed while writing the error response.The simplest implementation is:
public boolean handle(Request request, Response response, Callback callback) { callback.succeeded(); return true; }
A HelloWorld implementation is:
public boolean handle(Request request, Response response, Callback callback) { response.write(true, ByteBuffer.wrap("Hello World\n".getBytes(StandardCharsets.UTF_8)), callback); return true; }
- Specified by:
handle
in interfaceRequest.Handler
- Overrides:
handle
in classHandler.Wrapper
- Parameters:
request
- the HTTP request to handleresponse
- the HTTP response to handlecallback
- the callback to complete when the handling is complete- Returns:
- True if and only if the request will be handled, a response generated and the callback eventually called. This may occur within the scope of the call to this method, or asynchronously some time later. If false is returned, then this method must not generate a response, nor complete the callback.
- Throws:
Exception
- if there is a failure during the handling. Catchers cannot assume that the callback will be called and thus should attempt to complete the request as if a false had been returned.- See Also:
-
onConditionsMet
protected abstract boolean onConditionsMet(Request request, Response response, Callback callback) throws Exception Handle a request that has met the conditions. Typically, the implementation will provide optional handling and then call thenextHandler(Request, Response, Callback)
method to continue handling.- Parameters:
request
- The request to handleresponse
- The response to generatecallback
- The callback for completion- Returns:
- True if this handler will complete the callback
- Throws:
Exception
- If there is a problem handling- See Also:
-
onConditionsNotMet
protected abstract boolean onConditionsNotMet(Request request, Response response, Callback callback) throws Exception This method is called when the request has not met the conditions and is not to be handled by this handler. Implementations may return false; send an error response; or handle the request differently.- Parameters:
request
- The request to handleresponse
- The response to generatecallback
- The callback for completion- Returns:
- True if this handler will complete the callback
- Throws:
Exception
- If there is a problem handling- See Also:
-
nextHandler
protected boolean nextHandler(Request request, Response response, Callback callback) throws Exception Handle a request by invoking thehandle(Request, Response, Callback)
method of thenext Handler
.- Parameters:
request
- The request to handleresponse
- The response to generatecallback
- The callback for completion- Returns:
- True if this handler will complete the callback
- Throws:
Exception
- If there is a problem handling- See Also:
-
dump
Description copied from interface:Dumpable
Dump this object (and children) into an Appendable using the provided indent after any new lines. The indent should not be applied to the first object dumped.- Specified by:
dump
in interfaceDumpable
- Overrides:
dump
in classContainerLifeCycle
- Parameters:
out
- The appendable to dump toindent
- The indent to apply after any new lines.- Throws:
IOException
- if unable to write to Appendable
-
from
public static Predicate<Request> from(String connectorName, String inetAddressPattern, String method, String pathSpec) - Parameters:
connectorName
- The connector name ornull
inetAddressPattern
- AnInetAddressPattern
string ornull
method
- AHttpMethod
name ornull
pathSpec
- APathSpec
string ornull
- Returns:
- the combined
Predicate
overRequest
-
from
public static Predicate<Request> from(String connectorName, InetAddressPattern inetAddressPattern, String method, PathSpec pathSpec) - Parameters:
connectorName
- The connector name ornull
inetAddressPattern
- AnInetAddressPattern
ornull
method
- AHttpMethod
name ornull
pathSpec
- APathSpec
ornull
- Returns:
- the combined
Predicate
overRequest
-