use of ratpack.registry.Registry in project ratpack by ratpack.
the class DefaultRequestFixture method handleChain.
@Override
public HandlingResult handleChain(Action<? super Chain> chainAction) throws Exception {
final DefaultHandlingResult.ResultsHolder results = new DefaultHandlingResult.ResultsHolder();
Registry registry = getEffectiveRegistry(results);
ServerConfig serverConfig = registry.get(ServerConfig.class);
Handler handler = Handlers.chain(serverConfig, registry, chainAction);
return invoke(handler, registry, results);
}
use of ratpack.registry.Registry in project ratpack by ratpack.
the class DefaultRequestFixture method getEffectiveRegistry.
private Registry getEffectiveRegistry(final DefaultHandlingResult.ResultsHolder results) {
ClientErrorHandler clientErrorHandler = (context, statusCode) -> {
results.setClientError(statusCode);
context.getResponse().status(statusCode);
results.getLatch().countDown();
};
ServerErrorHandler serverErrorHandler = (context, throwable1) -> {
results.setThrowable(throwable1);
results.getLatch().countDown();
};
final Registry userRegistry = Registry.builder().add(ClientErrorHandler.class, clientErrorHandler).add(ServerErrorHandler.class, serverErrorHandler).build();
return Exceptions.uncheck(() -> {
ServerConfig serverConfig = serverConfigBuilder.build();
DefaultExecController execController = new DefaultExecController(serverConfig.getThreads());
return ServerRegistry.serverRegistry(new TestServer(), Impositions.none(), execController, serverConfig, r -> userRegistry.join(registryBuilder.build()));
});
}
use of ratpack.registry.Registry in project ratpack by ratpack.
the class Guice method buildInjector.
static Injector buildInjector(Registry baseRegistry, Action<? super BindingsSpec> bindingsAction, Function<? super Module, ? extends Injector> injectorFactory) throws Exception {
List<Action<? super Binder>> binderActions = Lists.newLinkedList();
List<Module> modules = Lists.newLinkedList();
ServerConfig serverConfig = baseRegistry.get(ServerConfig.class);
BindingsSpec bindings = new DefaultBindingsSpec(serverConfig, binderActions, modules);
modules.add(new RatpackBaseRegistryModule(baseRegistry));
modules.add(new ConfigModule(serverConfig.getRequiredConfig()));
try {
bindingsAction.execute(bindings);
} catch (Exception e) {
throw uncheck(e);
}
modules.add(new AdHocModule(binderActions));
Optional<BindingsImposition> bindingsImposition = Impositions.current().get(BindingsImposition.class);
if (bindingsImposition.isPresent()) {
BindingsImposition imposition = bindingsImposition.get();
List<Action<? super Binder>> imposedBinderActions = Lists.newLinkedList();
List<Module> imposedModules = Lists.newLinkedList();
BindingsSpec imposedBindings = new DefaultBindingsSpec(serverConfig, imposedBinderActions, imposedModules);
imposition.getBindings().execute(imposedBindings);
imposedModules.add(new AdHocModule(imposedBinderActions));
Module imposedModule = imposedModules.stream().reduce((acc, next) -> Modules.override(acc).with(next)).get();
modules.add(imposedModule);
}
Module masterModule = modules.stream().reduce((acc, next) -> Modules.override(acc).with(next)).get();
return injectorFactory.apply(masterModule);
}
use of ratpack.registry.Registry in project ratpack by ratpack.
the class ServerRegistry method serverRegistry.
public static Registry serverRegistry(RatpackServer ratpackServer, Impositions impositions, ExecControllerInternal execController, ServerConfig serverConfig, Function<? super Registry, ? extends Registry> userRegistryFactory) {
Registry baseRegistry = buildBaseRegistry(ratpackServer, impositions, execController, serverConfig);
Registry userRegistry = buildUserRegistry(userRegistryFactory, baseRegistry);
execController.setInterceptors(ImmutableList.copyOf(userRegistry.getAll(ExecInterceptor.class)));
execController.setInitializers(ImmutableList.copyOf(userRegistry.getAll(ExecInitializer.class)));
return baseRegistry.join(userRegistry);
}
use of ratpack.registry.Registry in project ratpack by ratpack.
the class Pac4jAuthenticator method handle.
@Override
public void handle(Context ctx) throws Exception {
PathBinding pathBinding = ctx.getPathBinding();
String pastBinding = pathBinding.getPastBinding();
if (pastBinding.equals(path)) {
RatpackWebContext.from(ctx, true).flatMap(webContext -> {
SessionData sessionData = webContext.getSession();
return createClients(ctx, pathBinding).map(clients -> clients.findClient(webContext)).map(Types::<Client<Credentials, UserProfile>>cast).flatMap(client -> getProfile(webContext, client)).map(profile -> {
if (profile != null) {
sessionData.set(Pac4jSessionKeys.USER_PROFILE, profile);
}
Optional<String> originalUrl = sessionData.get(Pac4jSessionKeys.REQUESTED_URL);
sessionData.remove(Pac4jSessionKeys.REQUESTED_URL);
return originalUrl;
}).onError(t -> {
if (t instanceof RequiresHttpAction) {
webContext.sendResponse((RequiresHttpAction) t);
} else {
ctx.error(new TechnicalException("Failed to get user profile", t));
}
});
}).then(originalUrlOption -> {
ctx.redirect(originalUrlOption.orElse("/"));
});
} else {
createClients(ctx, pathBinding).then(clients -> {
Registry registry = Registry.singleLazy(Clients.class, () -> uncheck(() -> clients));
ctx.next(registry);
});
}
}
Aggregations