Search in sources :

Example 1 with Types

use of ratpack.util.Types 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)

Example 2 with Types

use of ratpack.util.Types in project ratpack by ratpack.

the class NcsaRequestLogger method log.

@Override
public void log(RequestOutcome outcome) {
    if (!logger.isInfoEnabled()) {
        return;
    }
    // TODO - use one string builder here and remove use of String.format()
    Request request = outcome.getRequest();
    SentResponse response = outcome.getResponse();
    String responseSize = "-";
    String contentLength = response.getHeaders().get(HttpHeaderConstants.CONTENT_LENGTH);
    if (contentLength != null) {
        responseSize = contentLength;
    }
    StringBuilder logLine = new StringBuilder().append(ncsaLogFormat(request.getRemoteAddress(), "-", request.maybeGet(UserId.class).map(Types::cast), request.getTimestamp(), request.getMethod(), request.getRawUri(), request.getProtocol(), outcome.getResponse().getStatus(), responseSize));
    request.maybeGet(RequestId.class).ifPresent(id1 -> {
        logLine.append(" id=");
        logLine.append(id1);
    });
    logger.info(logLine.toString());
}
Also used : Types(ratpack.util.Types) RequestId(ratpack.handling.RequestId) UserId(ratpack.handling.UserId) Request(ratpack.http.Request) SentResponse(ratpack.http.SentResponse)

Aggregations

Types (ratpack.util.Types)2 ImmutableList (com.google.common.collect.ImmutableList)1 List (java.util.List)1 Optional (java.util.Optional)1 Client (org.pac4j.core.client.Client)1 Clients (org.pac4j.core.client.Clients)1 WebContext (org.pac4j.core.context.WebContext)1 Credentials (org.pac4j.core.credentials.Credentials)1 RequiresHttpAction (org.pac4j.core.exception.RequiresHttpAction)1 TechnicalException (org.pac4j.core.exception.TechnicalException)1 UserProfile (org.pac4j.core.profile.UserProfile)1 Blocking (ratpack.exec.Blocking)1 Promise (ratpack.exec.Promise)1 Context (ratpack.handling.Context)1 Handler (ratpack.handling.Handler)1 RequestId (ratpack.handling.RequestId)1 UserId (ratpack.handling.UserId)1 Request (ratpack.http.Request)1 SentResponse (ratpack.http.SentResponse)1 RatpackPac4j (ratpack.pac4j.RatpackPac4j)1