Search in sources :

Example 1 with PathBinding

use of ratpack.path.PathBinding in project ratpack by ratpack.

the class PathHandler method handle.

public void handle(Context ctx) throws ExecutionException {
    PathBindingStorage pathBindings = ctx.getExecution().get(PathBindingStorage.TYPE);
    PathBinding pathBinding = pathBindings.peek();
    Optional<PathBinding> newBinding = cache.get(pathBinding, binder::bind);
    if (newBinding.isPresent()) {
        pathBindings.push(newBinding.get());
        ctx.insert(handler);
    } else {
        ctx.next();
    }
}
Also used : PathBinding(ratpack.path.PathBinding)

Example 2 with PathBinding

use of ratpack.path.PathBinding in project ratpack by ratpack.

the class Pac4jAuthenticator method createClients.

private Promise<Clients> createClients(Context ctx, PathBinding pathBinding) throws Exception {
    String boundTo = pathBinding.getBoundTo();
    PublicAddress publicAddress = ctx.get(PublicAddress.class);
    String absoluteCallbackUrl = publicAddress.get(b -> b.maybeEncodedPath(boundTo).maybeEncodedPath(path)).toASCIIString();
    Iterable<? extends Client<?, ?>> result = clientsProvider.get(ctx);
    @SuppressWarnings("rawtypes") List<Client> clients;
    if (result instanceof List) {
        clients = Types.cast(result);
    } else {
        clients = ImmutableList.copyOf(result);
    }
    return Promise.value(new Clients(absoluteCallbackUrl, clients));
}
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) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) PublicAddress(ratpack.server.PublicAddress) Client(org.pac4j.core.client.Client) Clients(org.pac4j.core.client.Clients)

Example 3 with PathBinding

use of ratpack.path.PathBinding 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

PathBinding (ratpack.path.PathBinding)3 ImmutableList (com.google.common.collect.ImmutableList)2 List (java.util.List)2 Optional (java.util.Optional)2 Client (org.pac4j.core.client.Client)2 Clients (org.pac4j.core.client.Clients)2 WebContext (org.pac4j.core.context.WebContext)2 Credentials (org.pac4j.core.credentials.Credentials)2 RequiresHttpAction (org.pac4j.core.exception.RequiresHttpAction)2 TechnicalException (org.pac4j.core.exception.TechnicalException)2 UserProfile (org.pac4j.core.profile.UserProfile)2 Blocking (ratpack.exec.Blocking)2 Promise (ratpack.exec.Promise)2 Context (ratpack.handling.Context)2 Handler (ratpack.handling.Handler)2 RatpackPac4j (ratpack.pac4j.RatpackPac4j)2 Registry (ratpack.registry.Registry)2 PublicAddress (ratpack.server.PublicAddress)2 SessionData (ratpack.session.SessionData)2 Exceptions.uncheck (ratpack.util.Exceptions.uncheck)2