opps, I have to finish my mail.
I send you a sample HelloWorldResource that :
- declares the supported variants via a "variants" attribute.
- generates the right representation via the overriden get(Variant) method.
Best regards,
Thierry Boileau
------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2649282
package firstSteps;
import java.util.ArrayList;
import java.util.List;
import org.restlet.data.Language;
import org.restlet.data.MediaType;
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;
import org.restlet.representation.Variant;
import org.restlet.resource.ResourceException;
import org.restlet.resource.ServerResource;
/**
* Resource which has only one representation.
*
*/
public class HelloWorldResource extends ServerResource {
List<Variant> variants;
@Override
protected void doInit() throws ResourceException {
super.doInit();
variants = new ArrayList<Variant>();
variants.add(new Variant(MediaType.TEXT_PLAIN, Language.FRENCH));
variants.add(new Variant(MediaType.TEXT_PLAIN, Language.ENGLISH));
}
@Override
public List<Variant> getVariants() {
return variants;
}
@Override
protected Representation get(Variant variant) throws ResourceException {
String result = null;
if (!variant.getLanguages().isEmpty()) {
if (Language.FRENCH.includes(variant.getLanguages().get(0))) {
result = "en francais";
} else {
result = "in english";
}
} else {
result = "...";
}
Representation rep = new StringRepresentation(result, variant
.getMediaType());
rep.setLanguages(variant.getLanguages());
return rep;
}
}