Hi, I'm having a problem with returning a proper resource representation based
on the request Accept header value.
I'd like to return 2 representations: String or JSON base on corresponding
request media types: ALL, APPLICATION_JSON.
My understanding is that I have to add supported variants in my server resource
code like this:
protected void doInit() throws ResourceException {
getVariants().put(Method.GET,
Arrays.asList(new Variant[]{
new Variant(MediaType.ALL),
new Variant(MediaType.APPLICATION_JSON)
}));
}
I check the request media type in the @Get method and build a proper
representation:
@Get
public Representation getRepresentation(Variant variant) throws JSONException {
Representation result = null;
if(mtype.equals(MediaType.ALL)) {
// Return XML representation
result = new StringRepresentation("…");
} else if(mtype.equals(MediaType.APPLICATION_JSON)) {
// Return JSON representation
result = new StringRepresentation(XML.toJSONObject("…").toString());
} else {
setStatus(Status.CLIENT_ERROR_UNSUPPORTED_MEDIA_TYPE);
// Generate error representation here
}
return result;
}
In my Restlet test client I run 2 test requests expecting to get 2 different
representations like this:
Public void foo() {
ClientResource poiResource = new ClientResource("http://myhost:myport/path");
Representation rep = poiResource.get(MediaType.ALL);
if(!(poiResource.getStatus().isSuccess() &&
poiResource.getResponseEntity().isAvailable())) {
fail("The request has failed: " + rep.getText());
}
rep = poiResource.get(MediaType.APPLICATION_JSON);
if(!(poiResource.getStatus().isSuccess() &&
poiResource.getResponseEntity().isAvailable())) {
fail("The request has failed: " + rep.getText());
}
}
But I got 2 problems with that:
1. After I add the varian adding code all my requests fail with "The method
specified in the request is not allowed for the resource identified by the
request URI"
2. If I remove variant adding code from server resource Init() method, no
matter what media type I specify in a client resource, in the server resource's
@Get method the value of the 'variant' argument is always MEdiaType.ALL.
Am I doing something wrong?
Thanks!
------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2395647