Search in sources :

Example 1 with NettyEmbeddedServices

use of io.micronaut.http.server.netty.NettyEmbeddedServices in project micronaut-core by micronaut-projects.

the class NettyServerWebSocketUpgradeHandler method channelRead0.

@Override
protected final void channelRead0(ChannelHandlerContext ctx, NettyHttpRequest<?> msg) {
    ServerRequestContext.set(msg);
    Optional<UriRouteMatch<Object, Object>> optionalRoute = router.find(HttpMethod.GET, msg.getUri().toString(), msg).filter(rm -> rm.isAnnotationPresent(OnMessage.class) || rm.isAnnotationPresent(OnOpen.class)).findFirst();
    MutableHttpResponse<?> proceed = HttpResponse.ok();
    AtomicReference<HttpRequest<?>> requestReference = new AtomicReference<>(msg);
    Flux<MutableHttpResponse<?>> responsePublisher;
    if (optionalRoute.isPresent()) {
        UriRouteMatch<Object, Object> rm = optionalRoute.get();
        msg.setAttribute(HttpAttributes.ROUTE_MATCH, rm);
        msg.setAttribute(HttpAttributes.ROUTE_INFO, rm);
        proceed.setAttribute(HttpAttributes.ROUTE_MATCH, rm);
        proceed.setAttribute(HttpAttributes.ROUTE_INFO, rm);
        responsePublisher = Flux.just(proceed);
    } else {
        responsePublisher = routeExecutor.onError(new HttpStatusException(HttpStatus.NOT_FOUND, "WebSocket Not Found"), msg);
    }
    Publisher<? extends MutableHttpResponse<?>> finalPublisher = routeExecutor.filterPublisher(requestReference, responsePublisher);
    final Scheduler scheduler = Schedulers.fromExecutorService(ctx.channel().eventLoop());
    Mono.from(finalPublisher).publishOn(scheduler).subscribeOn(scheduler).contextWrite(reactorContext -> reactorContext.put(ServerRequestContext.KEY, requestReference.get())).subscribe((Consumer<MutableHttpResponse<?>>) actualResponse -> {
        if (actualResponse == proceed) {
            UriRouteMatch routeMatch = actualResponse.getAttribute(HttpAttributes.ROUTE_MATCH, UriRouteMatch.class).get();
            WebSocketBean<?> webSocketBean = webSocketBeanRegistry.getWebSocket(routeMatch.getTarget().getClass());
            handleHandshake(ctx, msg, webSocketBean, actualResponse);
            ChannelPipeline pipeline = ctx.pipeline();
            try {
                NettyServerWebSocketHandler webSocketHandler = new NettyServerWebSocketHandler(nettyEmbeddedServices, webSocketSessionRepository, handshaker, webSocketBean, msg, routeMatch, ctx, routeExecutor.getCoroutineHelper().orElse(null));
                pipeline.addBefore(ctx.name(), NettyServerWebSocketHandler.ID, webSocketHandler);
                pipeline.remove(ChannelPipelineCustomizer.HANDLER_HTTP_STREAM);
                pipeline.remove(NettyServerWebSocketUpgradeHandler.this);
                ChannelHandler accessLoggerHandler = pipeline.get(ChannelPipelineCustomizer.HANDLER_ACCESS_LOGGER);
                if (accessLoggerHandler != null) {
                    pipeline.remove(accessLoggerHandler);
                }
            } catch (Throwable e) {
                if (LOG.isErrorEnabled()) {
                    LOG.error("Error opening WebSocket: " + e.getMessage(), e);
                }
                ctx.writeAndFlush(new CloseWebSocketFrame(CloseReason.INTERNAL_ERROR.getCode(), CloseReason.INTERNAL_ERROR.getReason()));
            }
        } else {
            ctx.writeAndFlush(actualResponse);
        }
    });
}
Also used : RouteExecutor(io.micronaut.http.server.RouteExecutor) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) WebSocketServerHandshakerFactory(io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory) LoggerFactory(org.slf4j.LoggerFactory) AsciiString(io.netty.util.AsciiString) Internal(io.micronaut.core.annotation.Internal) CloseReason(io.micronaut.websocket.CloseReason) OnOpen(io.micronaut.websocket.annotation.OnOpen) HttpStatus(io.micronaut.http.HttpStatus) Map(java.util.Map) HttpResponse(io.micronaut.http.HttpResponse) RequestBinderRegistry(io.micronaut.http.bind.RequestBinderRegistry) NettyHttpRequest(io.micronaut.http.server.netty.NettyHttpRequest) MutableHttpResponse(io.micronaut.http.MutableHttpResponse) MutableHttpHeaders(io.micronaut.http.MutableHttpHeaders) ChannelPipeline(io.netty.channel.ChannelPipeline) StringUtils(io.micronaut.core.util.StringUtils) List(java.util.List) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) SslHandler(io.netty.handler.ssl.SslHandler) Optional(java.util.Optional) HttpAttributes(io.micronaut.http.HttpAttributes) WebSocketSessionRepository(io.micronaut.http.netty.websocket.WebSocketSessionRepository) WebSocketBean(io.micronaut.websocket.context.WebSocketBean) OnMessage(io.micronaut.websocket.annotation.OnMessage) Scheduler(reactor.core.scheduler.Scheduler) AtomicReference(java.util.concurrent.atomic.AtomicReference) WebSocketBeanRegistry(io.micronaut.websocket.context.WebSocketBeanRegistry) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) NettyHttpHeaders(io.micronaut.http.netty.NettyHttpHeaders) UriRouteMatch(io.micronaut.web.router.UriRouteMatch) Schedulers(reactor.core.scheduler.Schedulers) CloseWebSocketFrame(io.netty.handler.codec.http.websocketx.CloseWebSocketFrame) HttpRequest(io.micronaut.http.HttpRequest) ServerRequestContext(io.micronaut.http.context.ServerRequestContext) MediaTypeCodecRegistry(io.micronaut.http.codec.MediaTypeCodecRegistry) HttpStatusException(io.micronaut.http.exceptions.HttpStatusException) HttpMethod(io.micronaut.http.HttpMethod) Logger(org.slf4j.Logger) HttpHeaderValues(io.netty.handler.codec.http.HttpHeaderValues) Publisher(org.reactivestreams.Publisher) Mono(reactor.core.publisher.Mono) ChannelFuture(io.netty.channel.ChannelFuture) Channel(io.netty.channel.Channel) Consumer(java.util.function.Consumer) NonNull(io.micronaut.core.annotation.NonNull) Flux(reactor.core.publisher.Flux) NettyEmbeddedServices(io.micronaut.http.server.netty.NettyEmbeddedServices) SimpleChannelInboundHandler(io.netty.channel.SimpleChannelInboundHandler) ServerWebSocket(io.micronaut.websocket.annotation.ServerWebSocket) WebSocketServerHandshaker(io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker) ChannelPipelineCustomizer(io.micronaut.http.netty.channel.ChannelPipelineCustomizer) ChannelHandler(io.netty.channel.ChannelHandler) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) Router(io.micronaut.web.router.Router) NettyHttpRequest(io.micronaut.http.server.netty.NettyHttpRequest) HttpRequest(io.micronaut.http.HttpRequest) CloseWebSocketFrame(io.netty.handler.codec.http.websocketx.CloseWebSocketFrame) MutableHttpResponse(io.micronaut.http.MutableHttpResponse) Scheduler(reactor.core.scheduler.Scheduler) WebSocketBean(io.micronaut.websocket.context.WebSocketBean) HttpStatusException(io.micronaut.http.exceptions.HttpStatusException) AtomicReference(java.util.concurrent.atomic.AtomicReference) OnMessage(io.micronaut.websocket.annotation.OnMessage) OnOpen(io.micronaut.websocket.annotation.OnOpen) ChannelHandler(io.netty.channel.ChannelHandler) ChannelPipeline(io.netty.channel.ChannelPipeline) UriRouteMatch(io.micronaut.web.router.UriRouteMatch)

Aggregations

Internal (io.micronaut.core.annotation.Internal)1 NonNull (io.micronaut.core.annotation.NonNull)1 StringUtils (io.micronaut.core.util.StringUtils)1 HttpAttributes (io.micronaut.http.HttpAttributes)1 HttpMethod (io.micronaut.http.HttpMethod)1 HttpRequest (io.micronaut.http.HttpRequest)1 HttpResponse (io.micronaut.http.HttpResponse)1 HttpStatus (io.micronaut.http.HttpStatus)1 MutableHttpHeaders (io.micronaut.http.MutableHttpHeaders)1 MutableHttpResponse (io.micronaut.http.MutableHttpResponse)1 RequestBinderRegistry (io.micronaut.http.bind.RequestBinderRegistry)1 MediaTypeCodecRegistry (io.micronaut.http.codec.MediaTypeCodecRegistry)1 ServerRequestContext (io.micronaut.http.context.ServerRequestContext)1 HttpStatusException (io.micronaut.http.exceptions.HttpStatusException)1 NettyHttpHeaders (io.micronaut.http.netty.NettyHttpHeaders)1 ChannelPipelineCustomizer (io.micronaut.http.netty.channel.ChannelPipelineCustomizer)1 WebSocketSessionRepository (io.micronaut.http.netty.websocket.WebSocketSessionRepository)1 RouteExecutor (io.micronaut.http.server.RouteExecutor)1 NettyEmbeddedServices (io.micronaut.http.server.netty.NettyEmbeddedServices)1 NettyHttpRequest (io.micronaut.http.server.netty.NettyHttpRequest)1