Search in sources :

Example 6 with Preference

use of org.restlet.data.Preference in project qi4j-sdk by Qi4j.

the class ExtensionMediaTypeFilter method beforeHandle.

@Override
protected int beforeHandle(Request request, Response response) {
    List<String> segments = request.getResourceRef().getSegments();
    if (segments.get(segments.size() - 1).equals("")) {
        return Filter.CONTINUE;
    }
    String extensions = request.getResourceRef().getExtensions();
    if (extensions != null) {
        int idx = extensions.lastIndexOf(".");
        if (idx != -1) {
            extensions = extensions.substring(idx + 1);
        }
        MetadataService metadataService = getApplication().getMetadataService();
        Metadata metadata = metadataService.getMetadata(extensions);
        if (metadata != null && metadata instanceof MediaType) {
            request.getClientInfo().setAcceptedMediaTypes(Collections.singletonList(new Preference<MediaType>((MediaType) metadata)));
            String path = request.getResourceRef().getPath();
            path = path.substring(0, path.length() - extensions.length() - 1);
            request.getResourceRef().setPath(path);
        }
    }
    return Filter.CONTINUE;
}
Also used : Preference(org.restlet.data.Preference) Metadata(org.restlet.data.Metadata) MediaType(org.restlet.data.MediaType) MetadataService(org.restlet.service.MetadataService)

Aggregations

Preference (org.restlet.data.Preference)6 MediaType (org.restlet.data.MediaType)4 ArrayList (java.util.ArrayList)2 Language (org.restlet.data.Language)2 Metadata (org.restlet.data.Metadata)2 MetadataService (org.restlet.service.MetadataService)2 ParseException (java.text.ParseException)1 Collection (java.util.Collection)1 Locale (java.util.Locale)1 Map (java.util.Map)1 RuntimeCamelException (org.apache.camel.RuntimeCamelException)1 NameValuePair (org.apache.http.NameValuePair)1 Request (org.restlet.Request)1 ChallengeResponse (org.restlet.data.ChallengeResponse)1 ClientInfo (org.restlet.data.ClientInfo)1 Form (org.restlet.data.Form)1 Header (org.restlet.data.Header)1 Method (org.restlet.data.Method)1 DecodeRepresentation (org.restlet.engine.application.DecodeRepresentation)1 ByteArrayRepresentation (org.restlet.representation.ByteArrayRepresentation)1