So, nobody provided a better solution, but here's mine, with source code 
straight out of the Prudence project!

I call it a FallbackRouter, an extension of the standard Restlet router, and 
pretty much a drop-in replacement for it in most circumstances. Whenever you 
attach new targets to it, it makes sure to automatically bunch together 
identical routes via a Fallback restlet. The Fallback restlet goes through its 
targets in order, trying to find the first one that can handle the request. It 
thus "falls back" to the next target for that route.

It also supports simple caching, so that the successful target can be 
remembered per reference, and wasteful re-attempts can be avoided in the (near) 
future.

Detaching targets from the FallbackRouter is also done with sensitivity to 
un-bunching.

This have proven very useful in Prudence, where we can allow serving "dynamic" 
HTML, static web files, and REST resources from the same base URI.

Here is the source code from the current Prudence revision, though if you read 
this email later, always best to check for the freshest revision:

http://prudence.tigris.org/source/browse/prudence/trunk/java/com.threecrickets.prudence/src/com/threecrickets/prudence/util/FallbackRouter.java?revision=145&view=markup

http://prudence.tigris.org/source/browse/prudence/trunk/java/com.threecrickets.prudence/src/com/threecrickets/prudence/util/Fallback.java?revision=145&view=markup

-Tal

> Here's my requirement:
> 
> I have two restlets to attach to a router, both with the same URI 
> template. What I want is that if the first restlet fails (specifically 
> setting the response status to 404), then the next restlet would be 
> tried. I'm calling this "fallback" routing.
> 
> So far, the only thing that works well is my own custom restlet that 
> tries these two (or more) restlets in series. But this solution is 
> awkward and also circumvents the router.
> 
> I've tried to create my own custom router with a custom routing mode, 
> but the best this solution can offer is selection of a route. There's no 
> simple way to "fallback" on the next route in line if the first one 
> "fails" its handle(). I even thought to use Router's built in retry 
> feature, but this would require runtime changing of the route list, 
> which also seems awkward to me.
> 
> Any better ideas?
> 
> -Tal

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2431841

Reply via email to