Search in sources :

Example 1 with MutableHeaders

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

the class NettyHandlerAdapter method newRequest.

private void newRequest(ChannelHandlerContext ctx, HttpRequest nettyRequest) throws Exception {
    if (!nettyRequest.decoderResult().isSuccess()) {
        sendError(ctx, HttpResponseStatus.BAD_REQUEST);
        return;
    }
    Headers requestHeaders = new NettyHeadersBackedHeaders(nettyRequest.headers());
    // Find the content length we will use this as an indicator of a body
    Long contentLength = HttpUtil.getContentLength(nettyRequest, -1L);
    String transferEncoding = requestHeaders.get(HttpHeaderNames.TRANSFER_ENCODING);
    // If there is a content length or transfer encoding that indicates there is a body
    boolean hasBody = (contentLength > 0) || (transferEncoding != null);
    RequestBody requestBody = hasBody ? new RequestBody(contentLength, nettyRequest, ctx) : null;
    Channel channel = ctx.channel();
    if (requestBody != null) {
        channel.attr(BODY_ACCUMULATOR_KEY).set(requestBody);
    }
    InetSocketAddress remoteAddress = (InetSocketAddress) channel.remoteAddress();
    InetSocketAddress socketAddress = (InetSocketAddress) channel.localAddress();
    ConnectionIdleTimeout connectionIdleTimeout = ConnectionIdleTimeout.of(channel);
    DefaultRequest request = new DefaultRequest(Instant.now(), requestHeaders, nettyRequest.method(), nettyRequest.protocolVersion(), nettyRequest.uri(), remoteAddress, socketAddress, serverRegistry.get(ServerConfig.class), requestBody, connectionIdleTimeout, channel.attr(CLIENT_CERT_KEY).get());
    HttpHeaders nettyHeaders = new DefaultHttpHeaders(false);
    MutableHeaders responseHeaders = new NettyHeadersBackedMutableHeaders(nettyHeaders);
    AtomicBoolean transmitted = new AtomicBoolean(false);
    DefaultResponseTransmitter responseTransmitter = new DefaultResponseTransmitter(transmitted, channel, nettyRequest, request, nettyHeaders, requestBody);
    ctx.channel().attr(DefaultResponseTransmitter.ATTRIBUTE_KEY).set(responseTransmitter);
    Action<Action<Object>> subscribeHandler = thing -> {
        transmitted.set(true);
        ctx.channel().attr(CHANNEL_SUBSCRIBER_ATTRIBUTE_KEY).set(thing);
    };
    DefaultContext.RequestConstants requestConstants = new DefaultContext.RequestConstants(applicationConstants, request, channel, responseTransmitter, subscribeHandler);
    Response response = new DefaultResponse(responseHeaders, ctx.alloc(), responseTransmitter);
    requestConstants.response = response;
    DefaultContext.start(channel.eventLoop(), requestConstants, serverRegistry, handlers, execution -> {
        if (!transmitted.get()) {
            Handler lastHandler = requestConstants.handler;
            StringBuilder description = new StringBuilder();
            description.append("No response sent for ").append(request.getMethod().getName()).append(" request to ").append(request.getUri());
            if (lastHandler != null) {
                description.append(" (last handler: ");
                if (lastHandler instanceof DescribingHandler) {
                    ((DescribingHandler) lastHandler).describeTo(description);
                } else {
                    DescribingHandlers.describeTo(lastHandler, description);
                }
                description.append(")");
            }
            String message = description.toString();
            LOGGER.warn(message);
            response.getHeaders().clear();
            ByteBuf body;
            if (development) {
                CharBuffer charBuffer = CharBuffer.wrap(message);
                body = ByteBufUtil.encodeString(ctx.alloc(), charBuffer, CharsetUtil.UTF_8);
                response.contentType(HttpHeaderConstants.PLAIN_TEXT_UTF8);
            } else {
                body = Unpooled.EMPTY_BUFFER;
            }
            response.getHeaders().set(HttpHeaderConstants.CONTENT_LENGTH, body.readableBytes());
            responseTransmitter.transmit(HttpResponseStatus.INTERNAL_SERVER_ERROR, body);
        }
    });
}
Also used : AttributeKey(io.netty.util.AttributeKey) DescribingHandlers(ratpack.handling.internal.DescribingHandlers) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Unpooled(io.netty.buffer.Unpooled) SSLEngine(javax.net.ssl.SSLEngine) ByteBuf(io.netty.buffer.ByteBuf) MutableHeaders(ratpack.http.MutableHeaders) ServerConfig(ratpack.server.ServerConfig) SslHandshakeCompletionEvent(io.netty.handler.ssl.SslHandshakeCompletionEvent) Registry(ratpack.registry.Registry) CharsetUtil(io.netty.util.CharsetUtil) DefaultRenderController(ratpack.render.internal.DefaultRenderController) io.netty.channel(io.netty.channel) DefaultContext(ratpack.handling.internal.DefaultContext) IdleStateEvent(io.netty.handler.timeout.IdleStateEvent) Logger(org.slf4j.Logger) CharBuffer(java.nio.CharBuffer) ClosedChannelException(java.nio.channels.ClosedChannelException) ratpack.http.internal(ratpack.http.internal) ChainHandler(ratpack.handling.internal.ChainHandler) IOException(java.io.IOException) DescribingHandler(ratpack.handling.internal.DescribingHandler) X509Certificate(javax.security.cert.X509Certificate) Instant(java.time.Instant) InetSocketAddress(java.net.InetSocketAddress) ByteBufUtil(io.netty.buffer.ByteBufUtil) io.netty.handler.codec.http(io.netty.handler.codec.http) Response(ratpack.http.Response) Headers(ratpack.http.Headers) SslHandler(io.netty.handler.ssl.SslHandler) Action(ratpack.func.Action) Handler(ratpack.handling.Handler) ReferenceCountUtil(io.netty.util.ReferenceCountUtil) Handlers(ratpack.handling.Handlers) ExecController(ratpack.exec.ExecController) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) Action(ratpack.func.Action) MutableHeaders(ratpack.http.MutableHeaders) Headers(ratpack.http.Headers) InetSocketAddress(java.net.InetSocketAddress) CharBuffer(java.nio.CharBuffer) ByteBuf(io.netty.buffer.ByteBuf) ServerConfig(ratpack.server.ServerConfig) MutableHeaders(ratpack.http.MutableHeaders) DescribingHandler(ratpack.handling.internal.DescribingHandler) ChainHandler(ratpack.handling.internal.ChainHandler) DescribingHandler(ratpack.handling.internal.DescribingHandler) SslHandler(io.netty.handler.ssl.SslHandler) Handler(ratpack.handling.Handler) Response(ratpack.http.Response) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DefaultContext(ratpack.handling.internal.DefaultContext)

Example 2 with MutableHeaders

use of ratpack.http.MutableHeaders in project tutorials by eugenp.

the class RequestValidatorFilter method handle.

@Override
public void handle(Context ctx) throws Exception {
    MutableHeaders headers = ctx.getResponse().getHeaders();
    headers.set("Access-Control-Allow-Origin", "*");
    ctx.next();
}
Also used : MutableHeaders(ratpack.http.MutableHeaders)

Aggregations

MutableHeaders (ratpack.http.MutableHeaders)2 ByteBuf (io.netty.buffer.ByteBuf)1 ByteBufUtil (io.netty.buffer.ByteBufUtil)1 Unpooled (io.netty.buffer.Unpooled)1 io.netty.channel (io.netty.channel)1 io.netty.handler.codec.http (io.netty.handler.codec.http)1 SslHandler (io.netty.handler.ssl.SslHandler)1 SslHandshakeCompletionEvent (io.netty.handler.ssl.SslHandshakeCompletionEvent)1 IdleStateEvent (io.netty.handler.timeout.IdleStateEvent)1 AttributeKey (io.netty.util.AttributeKey)1 CharsetUtil (io.netty.util.CharsetUtil)1 ReferenceCountUtil (io.netty.util.ReferenceCountUtil)1 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 CharBuffer (java.nio.CharBuffer)1 ClosedChannelException (java.nio.channels.ClosedChannelException)1 Instant (java.time.Instant)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 SSLEngine (javax.net.ssl.SSLEngine)1 SSLPeerUnverifiedException (javax.net.ssl.SSLPeerUnverifiedException)1