public class Fallback extends Restlet
Restlet.handle(Request, Response)
to a
series of targets in sequence, stopping at the first target that satisfies
the condition of wasHandled(Request, Response)
. This is very useful
for allowing multiple restlets a chance to handle a request, while
"falling back" to subsequent restlets when those "fail."
If none of the targets "succeeds" in this respect, the response will be left in the same condition as it was in the last attempt.
Supports a simple timed cache that "remembers" which target handled which reference, in order to avoid unnecessary attempts on targets known to fail. Note that in situations in which targets may sometimes fail and sometimes succeed for the same reference, you would want to disable the cache or keep it low.
By default, wasHandled(Request, Response)
checks that the response
status is not Status.CLIENT_ERROR_NOT_FOUND
or
Status.CLIENT_ERROR_METHOD_NOT_ALLOWED
.
Constructor and Description |
---|
Fallback(Context context)
Construct a fallback with a default cache duration of 5 seconds.
|
Fallback(Context context,
int cacheDuration)
Construct a fallback.
|
Fallback(Context context,
int cacheDuration,
Restlet... targets)
Construct a fallback for an array of target restlets.
|
Fallback(Context context,
Restlet... targets)
Construct a fallback for an array of target restlets with a default cache
duration of 5 seconds.
|
Modifier and Type | Method and Description |
---|---|
void |
addTarget(Restlet target)
Adds a target restlet.
|
int |
getCacheDuration()
The cache duration, in milliseconds.
|
List<Restlet> |
getTargets()
The target restlets.
|
void |
handle(Request request,
Response response) |
boolean |
replaceTarget(Restlet originalTarget,
Restlet newTarget)
Replaces one target restlet with another.
|
void |
setCacheDuration(int cacheDuration)
The cache duration, in milliseconds.
|
void |
start() |
void |
stop() |
String |
toString() |
protected boolean |
wasHandled(Request request,
Response response)
Checks to see if a request was handled after a call to
Restlet.handle(Request, Response) . |
createFinder, finalize, getApplication, getAuthor, getContext, getDescription, getFinderClass, getLogger, getName, getOwner, handle, handle, handle, isStarted, isStopped, setAuthor, setContext, setDescription, setFinderClass, setName, setOwner
public Fallback(Context context)
context
- The contextpublic Fallback(Context context, Restlet... targets)
context
- The contexttargets
- The target restletspublic Fallback(Context context, int cacheDuration)
context
- The contextcacheDuration
- The cache duration, in millisecondspublic List<Restlet> getTargets()
public void addTarget(Restlet target)
target
- A target restletpublic boolean replaceTarget(Restlet originalTarget, Restlet newTarget)
Note: this is not an atomic operation, though it is thread-safe.
originalTarget
- The target restlet to replacenewTarget
- The replacement target restletpublic int getCacheDuration()
public void setCacheDuration(int cacheDuration)
cacheDuration
- The cache duration, in millisecondsprotected boolean wasHandled(Request request, Response response)
Restlet.handle(Request, Response)
.
This default implementation checks that the response status is not
Status.CLIENT_ERROR_NOT_FOUND
or
Status.CLIENT_ERROR_METHOD_NOT_ALLOWED
.
request
- The requestresponse
- The responseCopyright © 2009-2016 Three Crickets LLC.