Search in sources :

Example 1 with HttpRequestReceivedEvent

use of io.micronaut.http.context.event.HttpRequestReceivedEvent in project micronaut-core by micronaut-projects.

the class HttpRequestDecoder method decode.

@Override
protected void decode(ChannelHandlerContext ctx, HttpRequest msg, List<Object> out) {
    if (LOG.isTraceEnabled()) {
        LOG.trace("Server {}:{} Received Request: {} {}", embeddedServer.getHost(), embeddedServer.getPort(), msg.method(), msg.uri());
    }
    try {
        NettyHttpRequest<Object> request = new NettyHttpRequest<>(msg, ctx, conversionService, configuration);
        if (httpRequestReceivedEventPublisher != ApplicationEventPublisher.NO_OP) {
            try {
                ctx.executor().execute(() -> {
                    try {
                        httpRequestReceivedEventPublisher.publishEvent(new HttpRequestReceivedEvent(request));
                    } catch (Exception e) {
                        if (LOG.isErrorEnabled()) {
                            LOG.error("Error publishing Http request received event: " + e.getMessage(), e);
                        }
                    }
                });
            } catch (Exception e) {
                if (LOG.isErrorEnabled()) {
                    LOG.error("Error publishing Http request received event: " + e.getMessage(), e);
                }
            }
        }
        out.add(request);
    } catch (IllegalArgumentException e) {
        // this configured the request in the channel as an attribute
        new NettyHttpRequest<>(new DefaultHttpRequest(msg.protocolVersion(), msg.method(), "/"), ctx, conversionService, configuration);
        final Throwable cause = e.getCause();
        ctx.fireExceptionCaught(cause != null ? cause : e);
        if (msg instanceof StreamedHttpRequest) {
            // discard any data that may come in
            ((StreamedHttpRequest) msg).closeIfNoSubscriber();
        }
    }
}
Also used : StreamedHttpRequest(io.micronaut.http.netty.stream.StreamedHttpRequest) HttpRequestReceivedEvent(io.micronaut.http.context.event.HttpRequestReceivedEvent) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) NettyHttpRequest(io.micronaut.http.server.netty.NettyHttpRequest)

Aggregations

HttpRequestReceivedEvent (io.micronaut.http.context.event.HttpRequestReceivedEvent)1 StreamedHttpRequest (io.micronaut.http.netty.stream.StreamedHttpRequest)1 NettyHttpRequest (io.micronaut.http.server.netty.NettyHttpRequest)1 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)1