On Fri, Mar 28, 2014 at 9:48 AM, Tim <[email protected]> wrote:

> I'm wondering about your suggestion above, would I have to implement my
> own code to match the templates and extract the parameters or could I take
> advantage of the framework's ability to do that?
>

You could take advantage of the framework. Here's a sketch:

public class YourApplication extends Application {
    ...
    @Override public Restlet createInboundRoot() {
        // A map from path template (e.g. /path/to/resource/{resid}) to
        // metadata about the corresponding stored procedure.
        Map<String, YourStoredProcInfo> pathTemplateToStoredProcInfo = *...
from db ...*;

        Router router = new Router(getContext());
        for (String pathTemplate : pathTemplateToStoredProcInfo.keySet()) {
            YourStoredProcInfo storedProcInfo =
pathTemplateToStoredProcInfo.get(pathTemplate);
            router.attach(pathTemplate,
createStoredProcHandler(storedProcInfo));
        }
        return router;
    }

    private Restlet createStoredProcHandler(YourStoredProcInfo
storedProcInfo) {
        final YourQueryTemplate queryTemplate =
storedProcInfo.createQueryTemplate();
        final YourResultHandler resultHandler =
storedProcInfo.createResultHandler();
        return new Restlet() {
            @Override public void handle(Request request, Response
response) {
                // Combine queryTemplate with method, attributes, and
parameters from request.
                YourQuery query = queryTemplate.createQuery(
                    request.getMethod(),     // i.e., GET, POST, PUT, or
DELETE
                    request.getAttributes(), // e.g., { "resid" : "ABC123" }
                    request.getResourceRef().getQueryAsForm() // e.g.,
"?sortBy=date"
                );
                YourResults results = query.execute();
                response.setStatus(resultHandler.getStatus(results));
                if (response.getStatus().isSuccess()) {
                    Representation rep =
resultHandler.createRepresentation(results);
                    response.setEntity(rep);
                } else {
                    // ... error handling
                }
            }
        };
    }
}

This of course just pushes the hard part into YourStoredProcInfo,
YourQueryTemplate, YourQuery, YourResultHandler, and YourResults, but this
way the hard parts are isolated from the Restlet framework code.

It also leaves out all sorts of details, like error handling, client-info,
caching, etc., but it wouldn't be hard to adapt this basic idea to include
them.


PS. I understand now what you mean about the coupling, hopefully but
> explanation answers your query.
>

I think so. Sounds like the legacy nature of the enterprise (and its
people!) is so restrictive that you are forced into a design that you
wouldn't consider under normal circumstances.

--tim

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

Reply via email to