Search in sources :

Example 1 with MetadataService

use of org.restlet.service.MetadataService 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 MetadataService

use of org.restlet.service.MetadataService in project qi4j-sdk by Qi4j.

the class Main method main.

public static void main(String[] args) throws Exception {
    Energy4Java is = new Energy4Java();
    Server server = new Server(Protocol.HTTP, 8888);
    Application app = is.newApplication(new ForumAssembler(), new MetadataService());
    app.activate();
    ContextRestlet restlet = app.findModule("REST", "Restlet").newObject(ContextRestlet.class, new org.restlet.Context());
    ChallengeAuthenticator guard = new ChallengeAuthenticator(null, ChallengeScheme.HTTP_BASIC, "testRealm");
    MapVerifier mapVerifier = new MapVerifier();
    mapVerifier.getLocalSecrets().put("rickard", "secret".toCharArray());
    guard.setVerifier(mapVerifier);
    guard.setNext(restlet);
    server.setNext(restlet);
    server.start();
}
Also used : ContextRestlet(org.qi4j.library.rest.server.api.ContextRestlet) Server(org.restlet.Server) ForumAssembler(org.qi4j.samples.forum.assembler.ForumAssembler) Energy4Java(org.qi4j.bootstrap.Energy4Java) ChallengeAuthenticator(org.restlet.security.ChallengeAuthenticator) MapVerifier(org.restlet.security.MapVerifier) Application(org.qi4j.api.structure.Application) MetadataService(org.restlet.service.MetadataService)

Example 3 with MetadataService

use of org.restlet.service.MetadataService 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

MetadataService (org.restlet.service.MetadataService)3 MediaType (org.restlet.data.MediaType)2 Metadata (org.restlet.data.Metadata)2 Preference (org.restlet.data.Preference)2 Application (org.qi4j.api.structure.Application)1 Energy4Java (org.qi4j.bootstrap.Energy4Java)1 ContextRestlet (org.qi4j.library.rest.server.api.ContextRestlet)1 ForumAssembler (org.qi4j.samples.forum.assembler.ForumAssembler)1 Server (org.restlet.Server)1 ChallengeAuthenticator (org.restlet.security.ChallengeAuthenticator)1 MapVerifier (org.restlet.security.MapVerifier)1