0

In my ServerResource are two different "Get-Functions"

public class ApiKeyRestlet extends ServerResource {

@Get("json:json")
public ApiKeyResponse apikeyAsJson() throws RecordDoesNotExist {
   ...
}

@Get("text:text")
public Representation apikeyAsFile() throws RecordDoesNotExist {
    ... 
    Representation representation = new StringRepresentation(data, MediaType.TEXT_PLAIN);
    Disposition disposition = new Disposition(Disposition.TYPE_ATTACHMENT);
    disposition.setFilename("apikey.properties");
    representation.setDisposition(disposition);

    return representation;
}
...

The path .../apikey is bound to ApiKeyRestlet. If the client sends Accept: text/plain then apikeyAsFile should be called and if the client sends Accept: application/json apikeyAsJson should come into effect. But I can send what I want as Accept-Header its always apikeyAsJson that's called.

I think it has something todo with compatibility between these two formats but how can I handle this? I don't want to define two different routes for this.

[Update] I debugged through the code and I found that the return type of Representation adds the default MediaType [*/*]. Scoring is alway 0.5 and so always the first function defined in ServerResource will be called - seems to be a bug??? If I define a Accept-Header the score for this request should go up...

Mike Mitterer
  • 6,810
  • 4
  • 41
  • 62
  • Shouldn't it just be "json" not "json:json"? Also I think text is txt in this context (see http://restlet.com/learn/javadocs/2.2/jse/api/org/restlet/service/MetadataService.html#addCommonExtensions()) – tom Sep 09 '14 at 10:48

1 Answers1

0

Yes, you use wrong values within annotations. In fact, the value corresponds to what Restlet calls extension. The latter maps a set of media types. For example:

  • Extension xml is mapped on media types application/xml and text/xml
  • Extension txt is mapped on media type text/plain

You can reach all the supported extensions within the javadocs (http://restlet.com/technical-resources/restlet-framework/javadocs/2.2/jse/api/) at the level of the method MetadataService#addCommonExtensions. This method is responsible of registering all the supported extensions.

In your case, I would refactor your code like this:

public class ApiKeyRestlet extends ServerResource {
    @Get("json")
    public ApiKeyResponse apikeyAsJson() throws RecordDoesNotExist {
        (...)
    }

    @Get("txt")
    public Representation apikeyAsFile() throws RecordDoesNotExist {
        (...)
    }
}

Method apikeyAsJson will be called if the header Accept contains application/json and method apikeyAsFile with text/plain.

Hope it helps you, Thierry

Thierry Templier
  • 198,364
  • 44
  • 396
  • 360