steve
steve

Reputation:

How does Restlet handle the HTTP Accept header?

The Restlet framework is supposed to handle automatic content negotiation; however, it doesn't seem to do it properly.

When a client sends and HTTP GET request with the Accept header with a value, Restlet doesn't automatically negotiates the content.

Does anyone know how the Accept header is treated?

Upvotes: 1

Views: 3067

Answers (1)

Jerome Louvel
Jerome Louvel

Reputation: 2892

The Restlet API has extensive support for the "Accept" header. The information is parsed and available via the Request.getClientInfo().getAcceptedMediaTypes() : List<Preference<MediaType>> method.

Now, in order to automatically negotiate content for you, the Restlet engine needs to have information about the available variants. This is the purpose org.restlet.resource.Resource class in Restlet 1.1 and the new org.restlet.resource.ServerResource class in the Restlet 2.0 version being developed.

In Restlet 1.1, you create a subclass of Resource, declare variants like this:

   getVariants().add(new Variant(MediaType.TEXT_PLAIN));

   getVariants().add(new Variant(MediaType.APPLICATION_XML));

Then override the represent(Variant) method like this:

public Representation represent(Variant){

  if(MediaType.TEXT_PLAIN.equals(variant.getMediaType()){

     ...

  else if(...)

     ...
}

Best regards, Jerome

PS: please use our mailing list for future questions and look in the archives: http://www.restlet.org/community/lists

Upvotes: 8

Related Questions