I have looked everywhere for this and I can't find the answer. I even saw a copy of the Restlet book, which gives only a partial answer and even that partial answer is wrong.
What I am trying to do is very simple. I need to do a simple GET request to an HTTP URL. I know how to do this synchronously:
Engine.getInstance().getRegisteredClients().clear();
Engine.getInstance().getRegisteredClients().add(new HttpClientHelper(null));
ClientResource resource=new ClientResource(url);
Representation rep=resource.get();
String respText=rep.getText();
// handle the response in respText as you see fit
The problem of course is that this blocks on resource.get() until a response is received. What I really want to do is to do this asynchronously, ie set a callback (in using the resource.setOnResponse method?) and then firing off the request without blocking. I would also like to set a timeout value so that if I don't receive a timeout in a reasonable amount of time it fires off some sort of onTimeout or onError method.
One would think that this is a very common thing that someone might want to do with Restlet, yet I can find no documentation that discusses this. The only discussion I see is in the Restlet book, where it says in Listing 9.2 that the get() method does not block, when in fact it does. In other words, I tried this:
Engine.getInstance().getRegisteredClients().clear();
Engine.getInstance().getRegisteredClients().add(new HttpClientHelper(null));
ClientResource resource=new ClientResource(url);
resource.setOnResponse(new Uniform() {
public void handle(Request request, Response response) {
try {
int statusCode=response.getStatus().getCode();
// Print status code, should be 200
System.out.println("Status code is "+statusCode);
System.out.println("");
System.out.println("");
if (statusCode==200) {
onSuccess(response); // this is my own success handler method
} else {
System.out.println("ERROR: Bad response from server");
}
} catch (Exception ex) {
// handle exception
}
}
});
System.out.println("Before resource get");
resource.get(); // This blocks!!
Can someone please show me how to do this? Thanks.