Search in sources :

Example 1 with ChallengeAuthenticator

use of org.restlet.security.ChallengeAuthenticator 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 ChallengeAuthenticator

use of org.restlet.security.ChallengeAuthenticator 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 ChallengeAuthenticator

use of org.restlet.security.ChallengeAuthenticator 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 4 with ChallengeAuthenticator

use of org.restlet.security.ChallengeAuthenticator in project camel by apache.

the class RestletComponent method attachUriPatternToRestlet.

private void attachUriPatternToRestlet(String offsetPath, String uriPattern, RestletEndpoint endpoint, Restlet target) throws Exception {
    uriPattern = decodePattern(uriPattern);
    MethodBasedRouter router = getMethodRouter(uriPattern, true);
    Map<String, String> realm = endpoint.getRestletRealm();
    if (realm != null && realm.size() > 0) {
        ChallengeAuthenticator guard = new ChallengeAuthenticator(component.getContext().createChildContext(), ChallengeScheme.HTTP_BASIC, "Camel-Restlet Endpoint Realm");
        MapVerifier verifier = new MapVerifier();
        for (Map.Entry<String, String> entry : realm.entrySet()) {
            verifier.getLocalSecrets().put(entry.getKey(), entry.getValue().toCharArray());
        }
        guard.setVerifier(verifier);
        guard.setNext(target);
        target = guard;
        LOG.debug("Target has been set to guard: {}", guard);
    }
    if (endpoint.getRestletMethods() != null) {
        Method[] methods = endpoint.getRestletMethods();
        for (Method method : methods) {
            router.addRoute(method, target);
            LOG.debug("Attached restlet uriPattern: {} method: {}", uriPattern, method);
        }
    } else {
        Method method = endpoint.getRestletMethod();
        router.addRoute(method, target);
        LOG.debug("Attached restlet uriPattern: {} method: {}", uriPattern, method);
    }
    if (!router.hasBeenAttached()) {
        component.getDefaultHost().attach(offsetPath == null ? uriPattern : offsetPath + uriPattern, router);
        LOG.debug("Attached methodRouter uriPattern: {}", uriPattern);
    }
    if (!router.isStarted()) {
        router.start();
        LOG.debug("Started methodRouter uriPattern: {}", uriPattern);
    }
}
Also used : ChallengeAuthenticator(org.restlet.security.ChallengeAuthenticator) MapVerifier(org.restlet.security.MapVerifier) Method(org.restlet.data.Method) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ChallengeAuthenticator (org.restlet.security.ChallengeAuthenticator)4 MapVerifier (org.restlet.security.MapVerifier)4 ContextRestlet (org.qi4j.library.rest.server.api.ContextRestlet)3 Server (org.restlet.Server)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 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Application (org.qi4j.api.structure.Application)1 Energy4Java (org.qi4j.bootstrap.Energy4Java)1 ForumAssembler (org.qi4j.samples.forum.assembler.ForumAssembler)1