use of org.restlet.security.MapVerifier 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.MapVerifier 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.MapVerifier 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();
}
use of org.restlet.security.MapVerifier 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);
}
}
Aggregations