Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restlet ServerResource method parameters?

Tags:

java

restlet

This is probably a really stupid/simple question with such an obvious answer that it seems not worth stating in restlet documentation. Where and how (if at all) can Restlet pass parameters to methods in ServerResource classes?

Given this class:

public class FooServerResource extends ServerResource {
    @Get
    public String foo(String f) {
        return f;
    }
}

and a Router attachment router.attach("/foo", FooServerResource.class);

I know that if I use a Restlet client connector I could create a proxy for this class and invoke methods directly, but what if I am making calls to this ServerResource from some other non-java language, e.g. PHP?

like image 525
Finbarr Avatar asked Jan 22 '23 06:01

Finbarr


2 Answers

You can access the query parameters using from the resource reference. Typically, something like this:

@Get
public String foo() {
    Form queryParams = getReference().getQueryAsForm();
    String f = queryParams.getFirstValue("f");
    return f;
}

Generally speaking (and this would work for other methods that GET), you can access whatever is passed to the request (including the entity, when appropriate) using getRequest() within the ServerResource.

like image 57
Bruno Avatar answered Jan 29 '23 08:01

Bruno


hi

the question is about one year old, but I just started with Restlet and stumbled into the "same" problem. I am talking about the server, not the client (as Bruno marked it, the original question is mixing server and client part)
I think the question is not completely answered. If you, for instance, prefer to separate the Restlet resource from semantic handling of the request (separating business logics from infrastructure) it is quite likely that you need some parameters, like an Observer, or a callback, or sth. else. So, as I fas as I see, no parameter could be transmitted into this instantiation process. The resource is instantiated by Restlet engine per request. Thus I found no way to pass a parameter directly (is there one?)

Fortunately it is possible to access the Application object of the Restlet engine from within the resource class, and thus also to the class that creates the component, the server, etc.

In the resource class I have sth. like this

protected Application initObjLinkage(){

    Context cx = this.getContext();
    Client cli = cx.getClientDispatcher();
    Application app = cli.getApplication() ;
    return app;
}

Subsequently you may use reflection and an interface to access a method in the Application class (still within the resource class), check reflection about this...

Method cbMethod = app.getClass().getMethod("getFoo", parameterTypes) ;
CallbackIntf getmethodFoo = ( CallbackIntf )cbMethod.invoke( app, arguments );
String str = getmethodFoo()

In my application I use this mechanism to get access to an observer that supplies the received data to the classes for the business logics. This approach is a standard for all my resource classes which renders them quite uniform, standardized and small.
So... I just hope this is being helpful and that there is {no/some} way to do it in a much more simple way :)

like image 25
monnoo Avatar answered Jan 29 '23 09:01

monnoo