Search in sources :

Example 1 with HttpServerResponse

use of reactor.netty.http.server.HttpServerResponse in project spring-framework by spring-projects.

the class ReactorHttpHandlerAdapter method apply.

@Override
public Mono<Void> apply(HttpServerRequest reactorRequest, HttpServerResponse reactorResponse) {
    NettyDataBufferFactory bufferFactory = new NettyDataBufferFactory(reactorResponse.alloc());
    try {
        ReactorServerHttpRequest request = new ReactorServerHttpRequest(reactorRequest, bufferFactory);
        ServerHttpResponse response = new ReactorServerHttpResponse(reactorResponse, bufferFactory);
        if (request.getMethod() == HttpMethod.HEAD) {
            response = new HttpHeadResponseDecorator(response);
        }
        return this.httpHandler.handle(request, response).doOnError(ex -> logger.trace(request.getLogPrefix() + "Failed to complete: " + ex.getMessage())).doOnSuccess(aVoid -> logger.trace(request.getLogPrefix() + "Handling completed"));
    } catch (URISyntaxException ex) {
        if (logger.isDebugEnabled()) {
            logger.debug("Failed to get request URI: " + ex.getMessage());
        }
        reactorResponse.status(HttpResponseStatus.BAD_REQUEST);
        return Mono.empty();
    }
}
Also used : HttpLogging(org.springframework.http.HttpLogging) HttpServerRequest(reactor.netty.http.server.HttpServerRequest) HttpServerResponse(reactor.netty.http.server.HttpServerResponse) NettyDataBufferFactory(org.springframework.core.io.buffer.NettyDataBufferFactory) URISyntaxException(java.net.URISyntaxException) BiFunction(java.util.function.BiFunction) HttpMethod(org.springframework.http.HttpMethod) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) Log(org.apache.commons.logging.Log) Mono(reactor.core.publisher.Mono) Assert(org.springframework.util.Assert) URISyntaxException(java.net.URISyntaxException) NettyDataBufferFactory(org.springframework.core.io.buffer.NettyDataBufferFactory)

Example 2 with HttpServerResponse

use of reactor.netty.http.server.HttpServerResponse in project spring-framework by spring-projects.

the class ReactorNettyRequestUpgradeStrategy method upgrade.

@Override
public Mono<Void> upgrade(ServerWebExchange exchange, WebSocketHandler handler, @Nullable String subProtocol, Supplier<HandshakeInfo> handshakeInfoFactory) {
    ServerHttpResponse response = exchange.getResponse();
    HttpServerResponse reactorResponse = ServerHttpResponseDecorator.getNativeResponse(response);
    HandshakeInfo handshakeInfo = handshakeInfoFactory.get();
    NettyDataBufferFactory bufferFactory = (NettyDataBufferFactory) response.bufferFactory();
    URI uri = exchange.getRequest().getURI();
    // Trigger WebFlux preCommit actions and upgrade
    return response.setComplete().then(Mono.defer(() -> {
        WebsocketServerSpec spec = buildSpec(subProtocol);
        return reactorResponse.sendWebsocket((in, out) -> {
            ReactorNettyWebSocketSession session = new ReactorNettyWebSocketSession(in, out, handshakeInfo, bufferFactory, spec.maxFramePayloadLength());
            return handler.handle(session).checkpoint(uri + " [ReactorNettyRequestUpgradeStrategy]");
        }, spec);
    }));
}
Also used : HttpServerResponse(reactor.netty.http.server.HttpServerResponse) NettyDataBufferFactory(org.springframework.core.io.buffer.NettyDataBufferFactory) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo) RequestUpgradeStrategy(org.springframework.web.reactive.socket.server.RequestUpgradeStrategy) Mono(reactor.core.publisher.Mono) ReactorNettyWebSocketSession(org.springframework.web.reactive.socket.adapter.ReactorNettyWebSocketSession) Supplier(java.util.function.Supplier) WebsocketServerSpec(reactor.netty.http.server.WebsocketServerSpec) ServerWebExchange(org.springframework.web.server.ServerWebExchange) ServerHttpResponseDecorator(org.springframework.http.server.reactive.ServerHttpResponseDecorator) Nullable(org.springframework.lang.Nullable) WebSocketHandler(org.springframework.web.reactive.socket.WebSocketHandler) URI(java.net.URI) Assert(org.springframework.util.Assert) HttpServerResponse(reactor.netty.http.server.HttpServerResponse) WebsocketServerSpec(reactor.netty.http.server.WebsocketServerSpec) ReactorNettyWebSocketSession(org.springframework.web.reactive.socket.adapter.ReactorNettyWebSocketSession) NettyDataBufferFactory(org.springframework.core.io.buffer.NettyDataBufferFactory) URI(java.net.URI) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo)

Example 3 with HttpServerResponse

use of reactor.netty.http.server.HttpServerResponse in project pinpoint by naver.

the class AbstractHttpServerHandleInterceptor method after.

@Override
public void after(Object target, Object[] args, Object result, Throwable throwable) {
    if (isDebug) {
        logger.afterInterceptor(target, args, result, throwable);
    }
    try {
        if (Boolean.FALSE == isReceived(args)) {
            return;
        }
        final HttpServerRequest request = (HttpServerRequest) args[0];
        final HttpServerResponse response = (HttpServerResponse) args[0];
        final int statusCode = getStatusCode(response);
        // must before request listener due to trace block ending
        this.servletResponseListener.destroyed(response, throwable, statusCode);
        this.servletRequestListener.destroyed(request, throwable, statusCode);
    } catch (Throwable t) {
        if (isInfo) {
            logger.info("Failed to servlet request event handle.", t);
        }
    }
}
Also used : HttpServerRequest(reactor.netty.http.server.HttpServerRequest) HttpServerResponse(reactor.netty.http.server.HttpServerResponse)

Example 4 with HttpServerResponse

use of reactor.netty.http.server.HttpServerResponse in project pinpoint by naver.

the class AbstractHttpServerHandleInterceptor method before.

@Override
public void before(Object target, Object[] args) {
    if (isDebug) {
        logger.beforeInterceptor(target, args);
    }
    if (traceContext.currentRawTraceObject() != null) {
        if (isDisconnecting(args)) {
            final AsyncContext asyncContext = AsyncContextAccessorUtils.getAsyncContext(args, 0);
            if (asyncContext != null) {
                if (asyncContext instanceof AsyncStateSupport) {
                    final AsyncStateSupport asyncStateSupport = (AsyncStateSupport) asyncContext;
                    AsyncState asyncState = asyncStateSupport.getAsyncState();
                    asyncState.finish();
                    if (isDebug) {
                        logger.debug("Finished asyncState. asyncTraceId={}", asyncContext);
                    }
                }
            }
        }
        // duplicate trace.
        return;
    }
    try {
        if (Boolean.FALSE == isReceived(args)) {
            // invalid args
            return;
        }
        final HttpServerRequest request = (HttpServerRequest) args[0];
        final HttpServerResponse response = (HttpServerResponse) args[0];
        this.servletRequestListener.initialized(request, ReactorNettyConstants.REACTOR_NETTY_INTERNAL, this.methodDescriptor);
        // must after request listener due to trace block begin
        this.servletResponseListener.initialized(response, ReactorNettyConstants.REACTOR_NETTY_INTERNAL, this.methodDescriptor);
        // Set end-point
        final Trace trace = this.traceContext.currentTraceObject();
        if (trace == null) {
            return;
        }
        final SpanEventRecorder recorder = trace.currentSpanEventRecorder();
        if (recorder != null) {
            // make asynchronous trace-id
            final boolean asyncStateSupport = enableAsyncEndPoint;
            final AsyncContext asyncContext = recorder.recordNextAsyncContext(asyncStateSupport);
            ((AsyncContextAccessor) args[0])._$PINPOINT$_setAsyncContext(asyncContext);
            if (isDebug) {
                if (enableAsyncEndPoint) {
                    logger.debug("Set closeable-AsyncContext {}", asyncContext);
                } else {
                    logger.debug("Set AsyncContext {}", asyncContext);
                }
            }
        }
    } catch (Throwable t) {
        if (isInfo) {
            logger.info("Failed to servlet request event handle.", t);
        }
    }
}
Also used : Trace(com.navercorp.pinpoint.bootstrap.context.Trace) AsyncState(com.navercorp.pinpoint.bootstrap.context.AsyncState) HttpServerRequest(reactor.netty.http.server.HttpServerRequest) HttpServerResponse(reactor.netty.http.server.HttpServerResponse) SpanEventRecorder(com.navercorp.pinpoint.bootstrap.context.SpanEventRecorder) AsyncContextAccessor(com.navercorp.pinpoint.bootstrap.async.AsyncContextAccessor) AsyncContext(com.navercorp.pinpoint.bootstrap.context.AsyncContext) AsyncStateSupport(com.navercorp.pinpoint.bootstrap.context.AsyncStateSupport)

Aggregations

HttpServerResponse (reactor.netty.http.server.HttpServerResponse)4 HttpServerRequest (reactor.netty.http.server.HttpServerRequest)3 NettyDataBufferFactory (org.springframework.core.io.buffer.NettyDataBufferFactory)2 Assert (org.springframework.util.Assert)2 Mono (reactor.core.publisher.Mono)2 AsyncContextAccessor (com.navercorp.pinpoint.bootstrap.async.AsyncContextAccessor)1 AsyncContext (com.navercorp.pinpoint.bootstrap.context.AsyncContext)1 AsyncState (com.navercorp.pinpoint.bootstrap.context.AsyncState)1 AsyncStateSupport (com.navercorp.pinpoint.bootstrap.context.AsyncStateSupport)1 SpanEventRecorder (com.navercorp.pinpoint.bootstrap.context.SpanEventRecorder)1 Trace (com.navercorp.pinpoint.bootstrap.context.Trace)1 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 BiFunction (java.util.function.BiFunction)1 Supplier (java.util.function.Supplier)1 Log (org.apache.commons.logging.Log)1 HttpLogging (org.springframework.http.HttpLogging)1 HttpMethod (org.springframework.http.HttpMethod)1 ServerHttpResponse (org.springframework.http.server.reactive.ServerHttpResponse)1