Search in sources :

Example 6 with Context

use of ratpack.handling.Context in project ratpack by ratpack.

the class DefaultDevelopmentErrorHandler method error.

/**
   * Prints the string "Client error «statusCode»" to the response as text with the given status code.
   *
   * @param ctx The ctx
   * @param statusCode The 4xx status code that explains the problem
   */
@Override
public void error(Context ctx, int statusCode) throws Exception {
    HttpResponseStatus status = HttpResponseStatus.valueOf(statusCode);
    Request request = ctx.getRequest();
    LOGGER.error(statusCode + " client error for request to " + request.getRawUri());
    ctx.getResponse().status(statusCode);
    ctx.byContent(s -> s.plainText(() -> ctx.render("Client error " + statusCode)).html(() -> new ErrorPageRenderer() {

        protected void render() {
            render(ctx, status.reasonPhrase(), w -> messages(w, "Client Error", () -> meta(w, m -> m.put("URI:", request.getRawUri()).put("Method:", request.getMethod().getName()).put("Status Code:", status.code()).put("Phrase:", status.reasonPhrase()))));
        }
    }).noMatch("text/plain"));
}
Also used : Request(ratpack.http.Request) Logger(org.slf4j.Logger) Context(ratpack.handling.Context) LoggerFactory(org.slf4j.LoggerFactory) Throwables(com.google.common.base.Throwables) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) Request(ratpack.http.Request)

Example 7 with Context

use of ratpack.handling.Context 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

Context (ratpack.handling.Context)7 List (java.util.List)3 Exceptions.uncheck (ratpack.util.Exceptions.uncheck)3 ImmutableList (com.google.common.collect.ImmutableList)2 ByteBuf (io.netty.buffer.ByteBuf)2 ByteBufAllocator (io.netty.buffer.ByteBufAllocator)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 Publisher (org.reactivestreams.Publisher)2 Blocking (ratpack.exec.Blocking)2 Promise (ratpack.exec.Promise)2 Request (ratpack.http.Request)2 Streams (ratpack.stream.Streams)2 Throwables (com.google.common.base.Throwables)1