CRUD Operation for Calling ServerResouce with ClientProxy

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

CRUD Operation for Calling ServerResouce with ClientProxy

Xybrek
I have this ServerResource that is called by a ClientProxy:

public class GaeThingServerResource extends SelfInjectingServerResource
         implements ThingResource {
     private static final Logger LOG
             = Logger.getLogger(GaeThingServerResource.class.getName());
     @Override
     public ThingItem createThing(ThingItem Thing) {
         store().put(Thing);
         return Thing;
     }
     @Override
     public ThingItem readThing(Long id) {
         LOG.info("Read Thing id=" + id);
         ThingItem result = store().get(ThingItem.class, id);
         return result;
     }
     @Override
     public ThingItem updateThing(ThingItem Thing) {
         LOG.info("Updating Thing=" + Thing.toString());
         store().put(Thing);
         return Thing;
     }
     @Override
     public void deleteThing(Long id) {
         LOG.info("Delete Thing id=" + id);
         store().delete(ThingItem.class, id);
     }
}

This is called by:

public interface ThingResourceProxy extends ClientProxy {
     @Get
     public void readThing(Long id, Result<ThingItem> callback);
     @Delete
     public void deleteThing(Long id, Result<Void> callback);
     @Post
     public void createThing(ThingItem thing, Result<ThingItem> callback);
     @Put
     public void updateThing(ThingItem thing, Result<ThingItem> callback);
     @Get
     public void list(Result<ThingResultItem> callback);
}

With this code:

                 ThingResourceProxy thingResource =
GWT.create(ThingResourceProxy.class);
 
thingResource.getClientResource().setReference("/rest/thing/" + id);
                 thingResource.readThing(id, new Result<ThingItem>() {
                    //...
                 };


However readThing method the Long id is always null, when is called,
what could be the problem?

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=3094183
Reply | Threaded
Open this post in threaded view
|

RE: CRUD Operation for Calling ServerResouce with ClientProxy

Thierry Boileau-4
Hello,

I guess I should explain the mapping between Java methods and HTTP request.
Using Restlet Framework, the annotated methods follow these conventions:
 - there is either one or no parameter to a Java method.
 - such parameter is either an instance of the Representation (that wraps the flow of bytes coming from the socket) or an instance of a Java bean which is the conversion of the entity sent by the client in the incoming request.

In your case, the annotated method is as follow:

@Get
public void readThing(Long id, Result<ThingItem> callback);

Which can't happen since a GET request has no entity. Same issue for the DELETE method.

Here is my first refactoring:
 - GaeThingServerResource is attached as follow
router.attach("/thingitems/{thingitemid}"

 - Here is content of the GaeThingServerResource class.
public class GaeThingServerResource extends SelfInjectingServerResource
         implements ThingResource {
     private static final Logger LOG
             = Logger.getLogger(Gae​ThingServerResource.​class.getName());

     /** The identifier of the resource. */
     private Long id;

     @Override
     public ThingItem doInit() {
         id = Long.parseLong(getAttribute("thingitemid"));
         // You may check that the resource really exist
        // setExisting(true or false);
     }

     @Override
     public ThingItem createThing(ThingItem Thing) {
         store().put(Thing);
         return Thing;
     }

     @Override
     public ThingItem readThing() {
         LOG.info("Read Thing id=" + id);
         ThingItem result = store().get(ThingItem.class, id);
         return result;
     }

     @Override
     public ThingItem updateThing(ThingItem Thing) {
         LOG.info("Updating Thing=" + Thing.toString());
         store().put(Thing);
         return Thing;
     }

     @Override
     public void deleteThing() {
         LOG.info("Delete Thing id=" + id);
         store().delete(ThingItem.class, id);
     }
}

I guess you may have another question about how to create an entity.

Best regards,
Thierry Boileau

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=3094353
Reply | Threaded
Open this post in threaded view
|

Re: CRUD Operation for Calling ServerResouce with ClientProxy

Tim Peierls
On Fri, Jan 16, 2015 at 5:20 AM, Thierry Boileau <[hidden email]> wrote:
Here is my first refactoring:
public class GaeThingServerResource extends SelfInjectingServerResource
         implements ThingResource {
...
     @Override
     public ThingItem doInit() {
         id = Long.parseLong(getAttribute("thingitemid"));
         // You may check that the resource really exist
        // setExisting(true or false);
     }
...
}

Side note: If GaeThingServerResource is to extend SelfInjectingServerResource, then doInit should call super.doInit().

(I don't see any @Inject annotations, so currently there's no point in extending SelfInjectingServerResource.)

--tim