You don't need to use Restlet ServerResource for this. You can write a
custom Restlet with an initialization method that reads and caches the
mapping from path to procedure, and a handle(Request, Resource) method that
extracts path and parameters from the request and uses the cached mapping
to call the appropriate procedure.
Restlet ServerResources are more appropriate when the behavior of resources
is defined in code, rather than in a database:
public class MyResource extends ServerResource {
/** How to handle GET requests on this resource. */
@Get public String getSomeText() {
String id = getAttribute("id");
return lookupSomeTextFromId(id);
}
String lookupSomeTextFromId(String id) { ... }
}
so that the mapping from path to resource behavior can be set up with calls
to Router.attach:
public class MyApplication extends Application {
@Override public Restlet createInboundRoot() {
Router router = new Router(getContext());
...
router.attach("/path/to/my/resource/{id}", MyResource.class);
...
return router;
}
}
I think it's usually a very good idea to decouple the resource structure
and behavior from the data. I find it surprising that you would want to do
otherwise.
--tim
On Thu, Mar 27, 2014 at 1:33 PM, Tim <[email protected]> wrote:
> I'm a new RESTlet user and I was wondering if it's possible to route
> dynamically based on templates retrieved from a database in order to allow
> new templates to be added without changing any code? My plan is to have a
> table of URL templates together with their mappings, per request type, to
> stored procedure calls. The resource would then construct the stored
> procedure call based on which template was matched and map the parameters
> in the template to the procedure parameters.
>
> For example:
>
> URL Request Stored Procedure
> /customers GET CustomersGet()
> POST CustomerCreate(name...)
> /customers/{cus} GET CustomersGetById(cus)
> /invoices GET InvoicesGet()
> /invoices/{inv} GET InvoicesGetById(inv)
>
> I wanted to have a single resource class that would be called for all
> templates that could, based on the template that matched and the request,
> look-up the stored procedure and construct the call to it dynamically by
> taking the parameters extracted by the framework and any supplied in the
> request body and matching them to the stored procedure parameters.
> Unfortunately I couldn't find a way to get the template that was matched.
>
> The background behind this is that I am trying to create a system whereby
> new web applications can be developed without changing any Java code,
> simply by implementing the relevant stored procedures in the database and
> changing the configuration (it's not a problem if a server restart is
> required). All resources, such as HTML pages, JPEGs, Freemarker templates
> etc. are stored in the backend database as BLOBs are are served from there
> so that they can be easily modified and new ones uploaded.
>
> Thanks in advance,
>
> Tim.
>
> ------------------------------------------------------
>
> http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=3075356
>
------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=3075371