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