Search code examples
javahttprestgetrestlet

How to use Rest Get Annotation for application/x-octet-stream Accept header


I want to be able to invoke a certain method depending on the Accept type in the header of the GET request. Currently, I have the following in my resource class:

import org.restlet.resource.Get;

@Get("json")
public Representation getJson(Variant variant) throws Exception{
    return new StringRepresentation("json");
}

@Get("xml")
public Representation getXml(Variant variant) throws Exception {
    return new StringRepresentation("xml");
}

@Get("x-octet-stream")
public Representation getFile(Variant variant) throws Exception {
    return new StringRepresentation("octet-stream");
}

I can successfully invoke the methods getJson() and getXml() using an http GET with the Accept headers set to application/json and application/xml, repectively. When I issue a GET with the Accept header as application/x-octet-stream, the getJSon() method is invoked instead of the method annotated with x-octet-stream. Do you know why? and/or how I can invoke the getFile() method?

Does Rest only allow you to use json and xml for method entry points? Is there a list of recognized types? I have looked on the site, but there is no said list of anything of that type. Thanks


Solution

  • I believe that the @Get Annotations look up the method in your Application's MetadataService object, using a 'file extension'. See JavaDoc of this class (addCommonExtensions()) for the list of 'file extensions' supported by default.

    As a default catch all media type neither application/octet-stream nor it's compressed version have a default mapping. however you are also able to add as many custom mappings and MediaType instances as you would like. I would usually do this as part of my Application set-up, for example:

    public Application(final Context context)
    {
        super(context);
    
        getMetadataService().addExtension("html", MediaType.TEXT_HTML, true);
    }
    

    For Completeness: If you are attempting to Download pre-generated files from Disc you may also be interested in looking at using the Directory class.