Search in sources :

Example 1 with Registry

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);
}
Also used : ServerConfig(ratpack.server.ServerConfig) ClientErrorHandler(ratpack.error.ClientErrorHandler) ServerErrorHandler(ratpack.error.ServerErrorHandler) Handler(ratpack.handling.Handler) ServerRegistry(ratpack.server.internal.ServerRegistry) Registry(ratpack.registry.Registry)

Example 2 with Registry

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()));
    });
}
Also used : RatpackServer(ratpack.server.RatpackServer) HttpVersion(io.netty.handler.codec.http.HttpVersion) ClientErrorHandler(ratpack.error.ClientErrorHandler) RequestFixture(ratpack.test.handling.RequestFixture) RootPathBinding(ratpack.path.internal.RootPathBinding) HandlerTimeoutException(ratpack.test.handling.HandlerTimeoutException) Unpooled.unreleasableBuffer(io.netty.buffer.Unpooled.unreleasableBuffer) ServerRegistry(ratpack.server.internal.ServerRegistry) ByteBuf(io.netty.buffer.ByteBuf) MutableHeaders(ratpack.http.MutableHeaders) PathBindingStorage(ratpack.path.internal.PathBindingStorage) RequestBodyReader(ratpack.server.internal.RequestBodyReader) ServerConfig(ratpack.server.ServerConfig) ServerConfigBuilder(ratpack.server.ServerConfigBuilder) Registry(ratpack.registry.Registry) Map(java.util.Map) CharsetUtil(io.netty.util.CharsetUtil) DefaultPathBinding(ratpack.path.internal.DefaultPathBinding) DefaultRequest(ratpack.http.internal.DefaultRequest) Unpooled.buffer(io.netty.buffer.Unpooled.buffer) DefaultExecController(ratpack.exec.internal.DefaultExecController) Streams(ratpack.stream.Streams) RegistryBuilder(ratpack.registry.RegistryBuilder) ServerErrorHandler(ratpack.error.ServerErrorHandler) ImmutableMap(com.google.common.collect.ImmutableMap) HttpMethod(io.netty.handler.codec.http.HttpMethod) Promise(ratpack.exec.Promise) Block(ratpack.func.Block) Instant(java.time.Instant) HostAndPort(com.google.common.net.HostAndPort) InetSocketAddress(java.net.InetSocketAddress) Chain(ratpack.handling.Chain) RegistrySpec(ratpack.registry.RegistrySpec) Impositions(ratpack.impose.Impositions) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) HandlingResult(ratpack.test.handling.HandlingResult) Action(ratpack.func.Action) Handler(ratpack.handling.Handler) Exceptions(ratpack.util.Exceptions) Optional(java.util.Optional) TransformablePublisher(ratpack.stream.TransformablePublisher) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) Handlers(ratpack.handling.Handlers) NettyHeadersBackedMutableHeaders(ratpack.http.internal.NettyHeadersBackedMutableHeaders) ExecController(ratpack.exec.ExecController) Collections(java.util.Collections) ServerErrorHandler(ratpack.error.ServerErrorHandler) ServerConfig(ratpack.server.ServerConfig) ClientErrorHandler(ratpack.error.ClientErrorHandler) DefaultExecController(ratpack.exec.internal.DefaultExecController) ServerRegistry(ratpack.server.internal.ServerRegistry) Registry(ratpack.registry.Registry)

Example 3 with Registry

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);
}
Also used : Function(ratpack.func.Function) Module(com.google.inject.Module) RatpackBaseRegistryModule(ratpack.guice.internal.RatpackBaseRegistryModule) Context(ratpack.handling.Context) Modules(com.google.inject.util.Modules) Guice.createInjector(com.google.inject.Guice.createInjector) Exceptions.uncheck(ratpack.util.Exceptions.uncheck) Injector(com.google.inject.Injector) Stage(com.google.inject.Stage) Chain(ratpack.handling.Chain) ConfigObject(ratpack.config.ConfigObject) List(java.util.List) Lists(com.google.common.collect.Lists) Binder(com.google.inject.Binder) Impositions(ratpack.impose.Impositions) GuiceUtil(ratpack.guice.internal.GuiceUtil) InjectorRegistryBacking(ratpack.guice.internal.InjectorRegistryBacking) Action(ratpack.func.Action) Handler(ratpack.handling.Handler) ServerConfig(ratpack.server.ServerConfig) Registry(ratpack.registry.Registry) Optional(java.util.Optional) DefaultBindingsSpec(ratpack.guice.internal.DefaultBindingsSpec) Action(ratpack.func.Action) DefaultBindingsSpec(ratpack.guice.internal.DefaultBindingsSpec) DefaultBindingsSpec(ratpack.guice.internal.DefaultBindingsSpec) Binder(com.google.inject.Binder) ServerConfig(ratpack.server.ServerConfig) RatpackBaseRegistryModule(ratpack.guice.internal.RatpackBaseRegistryModule) Module(com.google.inject.Module) RatpackBaseRegistryModule(ratpack.guice.internal.RatpackBaseRegistryModule)

Example 4 with Registry

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);
}
Also used : Registry(ratpack.registry.Registry)

Example 5 with Registry

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);
        });
    }
}
Also used : Types(ratpack.util.Types) Context(ratpack.handling.Context) RatpackPac4j(ratpack.pac4j.RatpackPac4j) Exceptions.uncheck(ratpack.util.Exceptions.uncheck) Promise(ratpack.exec.Promise) PublicAddress(ratpack.server.PublicAddress) Blocking(ratpack.exec.Blocking) RequiresHttpAction(org.pac4j.core.exception.RequiresHttpAction) WebContext(org.pac4j.core.context.WebContext) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Clients(org.pac4j.core.client.Clients) Client(org.pac4j.core.client.Client) Handler(ratpack.handling.Handler) Registry(ratpack.registry.Registry) Optional(java.util.Optional) PathBinding(ratpack.path.PathBinding) TechnicalException(org.pac4j.core.exception.TechnicalException) UserProfile(org.pac4j.core.profile.UserProfile) SessionData(ratpack.session.SessionData) Credentials(org.pac4j.core.credentials.Credentials) Types(ratpack.util.Types) RequiresHttpAction(org.pac4j.core.exception.RequiresHttpAction) TechnicalException(org.pac4j.core.exception.TechnicalException) UserProfile(org.pac4j.core.profile.UserProfile) SessionData(ratpack.session.SessionData) Registry(ratpack.registry.Registry) PathBinding(ratpack.path.PathBinding) Credentials(org.pac4j.core.credentials.Credentials)

Aggregations

Registry (ratpack.registry.Registry)8 Handler (ratpack.handling.Handler)5 ServerConfig (ratpack.server.ServerConfig)5 Optional (java.util.Optional)4 ClientErrorHandler (ratpack.error.ClientErrorHandler)4 ServerErrorHandler (ratpack.error.ServerErrorHandler)4 Promise (ratpack.exec.Promise)4 Action (ratpack.func.Action)4 Instant (java.time.Instant)3 ExecController (ratpack.exec.ExecController)3 Block (ratpack.func.Block)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 Lists (com.google.common.collect.Lists)2 HostAndPort (com.google.common.net.HostAndPort)2 ByteBuf (io.netty.buffer.ByteBuf)2 Unpooled.buffer (io.netty.buffer.Unpooled.buffer)2 Unpooled.unreleasableBuffer (io.netty.buffer.Unpooled.unreleasableBuffer)2 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)2 HttpHeaderNames (io.netty.handler.codec.http.HttpHeaderNames)2 HttpMethod (io.netty.handler.codec.http.HttpMethod)2