Search in sources :

Example 1 with ContextRestlet

use of org.qi4j.library.rest.server.api.ContextRestlet in project qi4j-sdk by Qi4j.

the class ContextResourceClientFactoryTest method startWebServer.

@Before
public void startWebServer() throws Exception {
    server = new Server(Protocol.HTTP, 8888);
    ContextRestlet restlet = module.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(guard);
    server.start();
    // START SNIPPET: client-create1
    Client client = new Client(Protocol.HTTP);
    ContextResourceClientFactory contextResourceClientFactory = module.newObject(ContextResourceClientFactory.class, client);
    contextResourceClientFactory.setAcceptedMediaTypes(MediaType.APPLICATION_JSON);
    // END SNIPPET: client-create1
    // START SNIPPET: client-create2
    contextResourceClientFactory.setErrorHandler(new ErrorHandler().onError(ErrorHandler.AUTHENTICATION_REQUIRED, new ResponseHandler() {

        boolean tried = false;

        @Override
        public HandlerCommand handleResponse(Response response, ContextResourceClient client) {
            if (tried)
                throw new ResourceException(response.getStatus());
            tried = true;
            client.getContextResourceClientFactory().getInfo().setUser(new User("rickard", "secret"));
            // Try again
            return refresh();
        }
    }).onError(ErrorHandler.RECOVERABLE_ERROR, new ResponseHandler() {

        @Override
        public HandlerCommand handleResponse(Response response, ContextResourceClient client) {
            // Try to restart
            return refresh();
        }
    }));
    // END SNIPPET: client-create2
    // START SNIPPET: client-create3
    Reference ref = new Reference("http://localhost:8888/");
    crc = contextResourceClientFactory.newClient(ref);
// END SNIPPET: client-create3
}
Also used : ErrorHandler(org.qi4j.library.rest.client.api.ErrorHandler) ContextResourceClientFactory(org.qi4j.library.rest.client.api.ContextResourceClientFactory) User(org.restlet.security.User) Server(org.restlet.Server) ResponseHandler(org.qi4j.library.rest.client.spi.ResponseHandler) Reference(org.restlet.data.Reference) Response(org.restlet.Response) HandlerCommand(org.qi4j.library.rest.client.api.HandlerCommand) ContextRestlet(org.qi4j.library.rest.server.api.ContextRestlet) ChallengeAuthenticator(org.restlet.security.ChallengeAuthenticator) MapVerifier(org.restlet.security.MapVerifier) ResourceException(org.restlet.resource.ResourceException) ContextResourceClient(org.qi4j.library.rest.client.api.ContextResourceClient) Client(org.restlet.Client) ContextResourceClient(org.qi4j.library.rest.client.api.ContextResourceClient) Before(org.junit.Before)

Example 2 with ContextRestlet

use of org.qi4j.library.rest.server.api.ContextRestlet in project qi4j-sdk by Qi4j.

the class ContinuousIntegrationTest method startWebServer.

@Before
public void startWebServer() throws Exception {
    server = new Server(Protocol.HTTP, 8888);
    ContextRestlet restlet = module.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(guard);
    server.start();
    // START SNIPPET: client-create1
    Client client = new Client(Protocol.HTTP);
    ContextResourceClientFactory contextResourceClientFactory = module.newObject(ContextResourceClientFactory.class, client);
    contextResourceClientFactory.setAcceptedMediaTypes(MediaType.APPLICATION_JSON);
    // END SNIPPET: client-create1
    // START SNIPPET: client-create2
    contextResourceClientFactory.setErrorHandler(new ErrorHandler().onError(ErrorHandler.AUTHENTICATION_REQUIRED, new ResponseHandler() {

        boolean tried = false;

        @Override
        public HandlerCommand handleResponse(Response response, ContextResourceClient client) {
            if (tried) {
                throw new ResourceException(response.getStatus());
            }
            tried = true;
            client.getContextResourceClientFactory().getInfo().setUser(new User("rickard", "secret"));
            // Try again
            return refresh();
        }
    }).onError(ErrorHandler.RECOVERABLE_ERROR, new ResponseHandler() {

        @Override
        public HandlerCommand handleResponse(Response response, ContextResourceClient client) {
            // Try to restart
            return refresh();
        }
    }));
    // END SNIPPET: client-create2
    // START SNIPPET: client-create3
    Reference ref = new Reference("http://localhost:8888/");
    crc = contextResourceClientFactory.newClient(ref);
// END SNIPPET: client-create3
}
Also used : ErrorHandler(org.qi4j.library.rest.client.api.ErrorHandler) ContextResourceClientFactory(org.qi4j.library.rest.client.api.ContextResourceClientFactory) User(org.restlet.security.User) Server(org.restlet.Server) ResponseHandler(org.qi4j.library.rest.client.spi.ResponseHandler) Reference(org.restlet.data.Reference) Response(org.restlet.Response) HandlerCommand(org.qi4j.library.rest.client.api.HandlerCommand) ContextRestlet(org.qi4j.library.rest.server.api.ContextRestlet) ChallengeAuthenticator(org.restlet.security.ChallengeAuthenticator) MapVerifier(org.restlet.security.MapVerifier) ResourceException(org.restlet.resource.ResourceException) ContextResourceClient(org.qi4j.library.rest.client.api.ContextResourceClient) Client(org.restlet.Client) ContextResourceClient(org.qi4j.library.rest.client.api.ContextResourceClient) Before(org.junit.Before)

Example 3 with ContextRestlet

use of org.qi4j.library.rest.server.api.ContextRestlet 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)

Aggregations

ContextRestlet (org.qi4j.library.rest.server.api.ContextRestlet)3 Server (org.restlet.Server)3 ChallengeAuthenticator (org.restlet.security.ChallengeAuthenticator)3 MapVerifier (org.restlet.security.MapVerifier)3 Before (org.junit.Before)2 ContextResourceClient (org.qi4j.library.rest.client.api.ContextResourceClient)2 ContextResourceClientFactory (org.qi4j.library.rest.client.api.ContextResourceClientFactory)2 ErrorHandler (org.qi4j.library.rest.client.api.ErrorHandler)2 HandlerCommand (org.qi4j.library.rest.client.api.HandlerCommand)2 ResponseHandler (org.qi4j.library.rest.client.spi.ResponseHandler)2 Client (org.restlet.Client)2 Response (org.restlet.Response)2 Reference (org.restlet.data.Reference)2 ResourceException (org.restlet.resource.ResourceException)2 User (org.restlet.security.User)2 Application (org.qi4j.api.structure.Application)1 Energy4Java (org.qi4j.bootstrap.Energy4Java)1 ForumAssembler (org.qi4j.samples.forum.assembler.ForumAssembler)1 MetadataService (org.restlet.service.MetadataService)1