You've asked serveral questions. Responses in line.

On Thu, Jan 19, 2012 at 1:44 PM, martin <[email protected]> wrote:

> On the server side, I have a simple SQLite database that provides and
> store resources. Although it is discouraged, I went into extending the
> Restlet class to keep a hook to the database, in order to write:
>
> router.attach("/users/create", new CreateUserRestlet(database));
>
> Indeed, I don't understand how I may pass the database to such a resource
> mapping:
>
> router.attach("/users/create", CreateUserResource.class);
>

The simplest way is to put a reference to your database in the application
context, which is accessible from your resource implementations:

// At startup:
app.getContext().getAttributes().put("com.example.my.database", database);

// In resource method:
Database database =
getContext().getAttributes().get("com.example.my.database");

If you are already using a dependency injection framework, it can be more
convenient to arrange to have resource instances injected by your framework
than having to pull information out of the context. The Restlet extensions
for Spring and Guice can help with that.



> Now, in the CreateUserRestlet, I don't understand how I can convert the
> resource back to a java object:
>
> public class CreateUserRestlet extends Restlet {
> public void handle(Request request, Response response) {
>  System.out.println("request as text:" + request.getEntityAsText());
>  // fine: I can read the object as a json string
>
>  try {
>    JsonConverter c = new JsonConverter();
>    CreateUserQuery q = c.toObject(request.getEntity(),
> CreateUserQuery.class, null);
>    // "null" should be replaced by "the calling resource", but I can't
> find a method in Request that returns a resource object.
>
>    System.out.println("query:" + q);
>    // output is null
>  } catch (IOException e) {
>    // TODO Auto-generated catch block
>    e.printStackTrace();
>  }
> }
> }
>
> Do you have a suggestion to make the manual conversion?
>

You shouldn't have to do this at all.



> Do you think it is a wrong idea to write custom restlet directly?
>

For this purpose, yes. You should use ServerResource and take advantage of
the built-in ConverterService, making use of a Restlet extension that
supplies appropriate converters to that service, like Jackson or XStream.

--tim

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

Reply via email to