Search in sources :

Example 1 with User

use of org.restlet.security.User in project qi4j-sdk by Qi4j.

the class ContextResourceClient method invokeQuery.

private HandlerCommand invokeQuery(Reference ref, Object queryRequest, ResponseHandler resourceHandler, ResponseHandler processingErrorHandler) {
    Request request = new Request(Method.GET, ref);
    if (queryRequest != null) {
        contextResourceFactory.writeRequest(request, queryRequest);
    }
    contextResourceFactory.updateQueryRequest(request);
    User user = request.getClientInfo().getUser();
    if (user != null)
        request.setChallengeResponse(new ChallengeResponse(ChallengeScheme.HTTP_BASIC, user.getName(), user.getSecret()));
    Response response = new Response(request);
    contextResourceFactory.getClient().handle(request, response);
    if (response.getStatus().isSuccess()) {
        contextResourceFactory.updateCache(response);
        return resourceHandler.handleResponse(response, this);
    } else if (response.getStatus().isRedirection()) {
        Reference redirectedTo = response.getLocationRef();
        return invokeQuery(redirectedTo, queryRequest, resourceHandler, processingErrorHandler);
    } else {
        if (response.getStatus().equals(Status.CLIENT_ERROR_UNPROCESSABLE_ENTITY) && processingErrorHandler != null) {
            return processingErrorHandler.handleResponse(response, this);
        } else {
            // TODO This needs to be expanded to allow custom handling of all the various cases
            return errorHandler.handleResponse(response, this);
        }
    }
}
Also used : ChallengeResponse(org.restlet.data.ChallengeResponse) Response(org.restlet.Response) User(org.restlet.security.User) Reference(org.restlet.data.Reference) Request(org.restlet.Request) ChallengeResponse(org.restlet.data.ChallengeResponse)

Example 2 with User

use of org.restlet.security.User 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 3 with User

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

use of org.restlet.security.User in project qi4j-sdk by Qi4j.

the class ContextResourceClient method invokeCommand.

private HandlerCommand invokeCommand(Reference ref, Method method, Object requestObject, ResponseHandler responseHandler, ResponseHandler processingErrorHandler) {
    Request request = new Request(method, ref);
    if (requestObject == null)
        requestObject = new EmptyRepresentation();
    contextResourceFactory.writeRequest(request, requestObject);
    contextResourceFactory.updateCommandRequest(request);
    User user = request.getClientInfo().getUser();
    if (user != null)
        request.setChallengeResponse(new ChallengeResponse(ChallengeScheme.HTTP_BASIC, user.getName(), user.getSecret()));
    Response response = new Response(request);
    contextResourceFactory.getClient().handle(request, response);
    try {
        if (response.getStatus().isSuccess()) {
            contextResourceFactory.updateCache(response);
            if (responseHandler != null)
                return responseHandler.handleResponse(response, this);
        } else {
            if (response.getStatus().equals(Status.CLIENT_ERROR_UNPROCESSABLE_ENTITY) && processingErrorHandler != null) {
                return processingErrorHandler.handleResponse(response, this);
            } else {
                // TODO This needs to be expanded to allow custom handling of all the various cases
                return errorHandler.handleResponse(response, this);
            }
        }
        // No handler found
        return null;
    } finally {
        try {
            response.getEntity().exhaust();
        } catch (Throwable e) {
        // Ignore
        }
    }
}
Also used : ChallengeResponse(org.restlet.data.ChallengeResponse) Response(org.restlet.Response) User(org.restlet.security.User) EmptyRepresentation(org.restlet.representation.EmptyRepresentation) Request(org.restlet.Request) ChallengeResponse(org.restlet.data.ChallengeResponse)

Aggregations

Response (org.restlet.Response)4 User (org.restlet.security.User)4 Reference (org.restlet.data.Reference)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 ContextRestlet (org.qi4j.library.rest.server.api.ContextRestlet)2 Client (org.restlet.Client)2 Request (org.restlet.Request)2 Server (org.restlet.Server)2 ChallengeResponse (org.restlet.data.ChallengeResponse)2 ResourceException (org.restlet.resource.ResourceException)2 ChallengeAuthenticator (org.restlet.security.ChallengeAuthenticator)2 MapVerifier (org.restlet.security.MapVerifier)2 EmptyRepresentation (org.restlet.representation.EmptyRepresentation)1