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;
}
Aggregations