Search in sources :

Example 1 with Metadata

use of org.restlet.data.Metadata 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)

Example 2 with Metadata

use of org.restlet.data.Metadata 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

MediaType (org.restlet.data.MediaType)2 Metadata (org.restlet.data.Metadata)2 Preference (org.restlet.data.Preference)2 MetadataService (org.restlet.service.MetadataService)2