Search in sources :

Example 1 with Request

use of ratpack.http.Request in project ratpack by ratpack.

the class FileHandler method currentUriWithTrailingSlash.

private String currentUriWithTrailingSlash(Context context) {
    Request request = context.getRequest();
    String redirectUri = "/" + request.getPath() + "/";
    String query = request.getQuery();
    if (!query.isEmpty()) {
        redirectUri += "?" + query;
    }
    return redirectUri;
}
Also used : Request(ratpack.http.Request)

Example 2 with Request

use of ratpack.http.Request in project ratpack by ratpack.

the class RatpackPac4j method initiateAuthentication.

private static void initiateAuthentication(Context ctx, Class<? extends Client<?, ?>> clientType) {
    Request request = ctx.getRequest();
    Clients clients = ctx.get(Clients.class);
    Client<?, ?> client = clients.findClient(clientType);
    RatpackWebContext.from(ctx, false).then(webContext -> {
        webContext.getSession().set(Pac4jSessionKeys.REQUESTED_URL, request.getUri());
        try {
            client.redirect(webContext, true);
        } catch (Exception e) {
            if (e instanceof RequiresHttpAction) {
                webContext.sendResponse((RequiresHttpAction) e);
                return;
            } else {
                ctx.error(new TechnicalException("Failed to redirect", e));
            }
        }
        webContext.sendResponse();
    });
}
Also used : RequiresHttpAction(org.pac4j.core.exception.RequiresHttpAction) TechnicalException(org.pac4j.core.exception.TechnicalException) Request(ratpack.http.Request) Clients(org.pac4j.core.client.Clients) TechnicalException(org.pac4j.core.exception.TechnicalException)

Example 3 with Request

use of ratpack.http.Request in project ratpack by ratpack.

the class WebSocketEngine method connect.

@SuppressWarnings("deprecation")
public static <T> void connect(final Context context, String path, int maxLength, final WebSocketHandler<T> handler) {
    PublicAddress publicAddress = context.get(PublicAddress.class);
    URI address = publicAddress.get(context);
    URI httpPath = address.resolve(path);
    URI wsPath;
    try {
        wsPath = new URI("ws", httpPath.getUserInfo(), httpPath.getHost(), httpPath.getPort(), httpPath.getPath(), httpPath.getQuery(), httpPath.getFragment());
    } catch (URISyntaxException e) {
        throw uncheck(e);
    }
    WebSocketServerHandshakerFactory factory = new WebSocketServerHandshakerFactory(wsPath.toString(), null, false, maxLength);
    Request request = context.getRequest();
    HttpMethod method = valueOf(request.getMethod().getName());
    FullHttpRequest nettyRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, method, request.getUri());
    nettyRequest.headers().add(SEC_WEBSOCKET_VERSION, request.getHeaders().get(SEC_WEBSOCKET_VERSION));
    nettyRequest.headers().add(SEC_WEBSOCKET_KEY, request.getHeaders().get(SEC_WEBSOCKET_KEY));
    final WebSocketServerHandshaker handshaker = factory.newHandshaker(nettyRequest);
    final DirectChannelAccess directChannelAccess = context.getDirectChannelAccess();
    final Channel channel = directChannelAccess.getChannel();
    if (!channel.config().isAutoRead()) {
        channel.config().setAutoRead(true);
    }
    handshaker.handshake(channel, nettyRequest).addListener(new HandshakeFutureListener<>(context, handshaker, handler));
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) DirectChannelAccess(ratpack.handling.direct.DirectChannelAccess) Channel(io.netty.channel.Channel) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) Request(ratpack.http.Request) PublicAddress(ratpack.server.PublicAddress) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) HttpMethod(io.netty.handler.codec.http.HttpMethod)

Example 4 with Request

use of ratpack.http.Request in project ratpack by ratpack.

the class InferringPublicAddress method get.

@Override
public URI get() {
    Request request = Execution.current().maybeGet(Request.class).orElseThrow(() -> new IllegalStateException("Inferring the public address is only supported during a request execution."));
    String scheme = determineScheme(request);
    String host;
    int port;
    HostAndPort forwardedHostData = getForwardedHostData(request);
    if (forwardedHostData != null) {
        host = forwardedHostData.getHost();
        port = forwardedHostData.getPortOrDefault(-1);
    } else {
        URI absoluteRequestURI = getAbsoluteRequestUri(request);
        if (absoluteRequestURI != null) {
            host = absoluteRequestURI.getHost();
            port = absoluteRequestURI.getPort();
        } else {
            HostAndPort hostData = getHostData(request);
            if (hostData != null) {
                host = hostData.getHost();
                port = hostData.getPortOrDefault(-1);
            } else {
                HostAndPort localAddress = request.getLocalAddress();
                host = localAddress.getHost();
                port = ProtocolUtil.isDefaultPortForScheme(localAddress.getPort(), scheme) ? -1 : localAddress.getPort();
            }
        }
    }
    try {
        return new URI(scheme, null, host, port, null, null, null);
    } catch (URISyntaxException ex) {
        throw Exceptions.uncheck(ex);
    }
}
Also used : HostAndPort(com.google.common.net.HostAndPort) Request(ratpack.http.Request) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 5 with Request

use of ratpack.http.Request in project ratpack by ratpack.

the class DefaultBlockingExecTimingInterceptor method intercept.

@Override
public void intercept(Execution execution, ExecType type, Block executionSegment) throws Exception {
    if (type == ExecType.BLOCKING) {
        Optional<Request> requestOpt = execution.maybeGet(Request.class);
        if (requestOpt.isPresent()) {
            Request request = requestOpt.get();
            String tag = buildBlockingTimerTag(request.getPath(), request.getMethod().getName());
            Timer.Context timer = metricRegistry.timer(tag).time();
            try {
                executionSegment.execute();
            } finally {
                timer.stop();
            }
            return;
        }
    }
    executionSegment.execute();
}
Also used : Timer(com.codahale.metrics.Timer) Request(ratpack.http.Request)

Aggregations

Request (ratpack.http.Request)9 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 Context (ratpack.handling.Context)2 Timer (com.codahale.metrics.Timer)1 Throwables (com.google.common.base.Throwables)1 HostAndPort (com.google.common.net.HostAndPort)1 ByteBuf (io.netty.buffer.ByteBuf)1 Channel (io.netty.channel.Channel)1 io.netty.handler.codec.http (io.netty.handler.codec.http)1 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)1 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)1 HttpMethod (io.netty.handler.codec.http.HttpMethod)1 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)1 Attribute (io.netty.handler.codec.http.multipart.Attribute)1 FileUpload (io.netty.handler.codec.http.multipart.FileUpload)1 HttpPostRequestDecoder (io.netty.handler.codec.http.multipart.HttpPostRequestDecoder)1 InterfaceHttpData (io.netty.handler.codec.http.multipart.InterfaceHttpData)1 IOException (java.io.IOException)1 Charset (java.nio.charset.Charset)1