Search in sources :

Example 11 with DataBufferFactory

use of org.springframework.core.io.buffer.DataBufferFactory in project spring-framework by spring-projects.

the class UndertowRequestUpgradeStrategy method upgrade.

@Override
public Mono<Void> upgrade(ServerWebExchange exchange, WebSocketHandler handler, @Nullable String subProtocol, Supplier<HandshakeInfo> handshakeInfoFactory) {
    HttpServerExchange httpExchange = ServerHttpRequestDecorator.getNativeRequest(exchange.getRequest());
    Set<String> protocols = (subProtocol != null ? Collections.singleton(subProtocol) : Collections.emptySet());
    Hybi13Handshake handshake = new Hybi13Handshake(protocols, false);
    List<Handshake> handshakes = Collections.singletonList(handshake);
    HandshakeInfo handshakeInfo = handshakeInfoFactory.get();
    DataBufferFactory bufferFactory = exchange.getResponse().bufferFactory();
    // Trigger WebFlux preCommit actions and upgrade
    return exchange.getResponse().setComplete().then(Mono.deferContextual(contextView -> {
        DefaultCallback callback = new DefaultCallback(handshakeInfo, ContextWebSocketHandler.decorate(handler, contextView), bufferFactory);
        try {
            new WebSocketProtocolHandshakeHandler(handshakes, callback).handleRequest(httpExchange);
        } catch (Exception ex) {
            return Mono.error(ex);
        }
        return Mono.empty();
    }));
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo) HttpServerExchange(io.undertow.server.HttpServerExchange) Hybi13Handshake(io.undertow.websockets.core.protocol.version13.Hybi13Handshake) RequestUpgradeStrategy(org.springframework.web.reactive.socket.server.RequestUpgradeStrategy) Set(java.util.Set) Mono(reactor.core.publisher.Mono) WebSocketConnectionCallback(io.undertow.websockets.WebSocketConnectionCallback) WebSocketProtocolHandshakeHandler(io.undertow.websockets.WebSocketProtocolHandshakeHandler) Supplier(java.util.function.Supplier) UndertowWebSocketHandlerAdapter(org.springframework.web.reactive.socket.adapter.UndertowWebSocketHandlerAdapter) ServerWebExchange(org.springframework.web.server.ServerWebExchange) List(java.util.List) UndertowWebSocketSession(org.springframework.web.reactive.socket.adapter.UndertowWebSocketSession) WebSocketHttpExchange(io.undertow.websockets.spi.WebSocketHttpExchange) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) Handshake(io.undertow.websockets.core.protocol.Handshake) WebSocketChannel(io.undertow.websockets.core.WebSocketChannel) ServerHttpRequestDecorator(org.springframework.http.server.reactive.ServerHttpRequestDecorator) Nullable(org.springframework.lang.Nullable) WebSocketHandler(org.springframework.web.reactive.socket.WebSocketHandler) ContextWebSocketHandler(org.springframework.web.reactive.socket.adapter.ContextWebSocketHandler) Collections(java.util.Collections) Hybi13Handshake(io.undertow.websockets.core.protocol.version13.Hybi13Handshake) WebSocketProtocolHandshakeHandler(io.undertow.websockets.WebSocketProtocolHandshakeHandler) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo) Hybi13Handshake(io.undertow.websockets.core.protocol.version13.Hybi13Handshake) Handshake(io.undertow.websockets.core.protocol.Handshake)

Example 12 with DataBufferFactory

use of org.springframework.core.io.buffer.DataBufferFactory in project spring-framework by spring-projects.

the class TomcatRequestUpgradeStrategy method upgrade.

// for old doUpgrade variant in Tomcat 9.0.55
@SuppressWarnings("deprecation")
@Override
public Mono<Void> upgrade(ServerWebExchange exchange, WebSocketHandler handler, @Nullable String subProtocol, Supplier<HandshakeInfo> handshakeInfoFactory) {
    ServerHttpRequest request = exchange.getRequest();
    ServerHttpResponse response = exchange.getResponse();
    HttpServletRequest servletRequest = ServerHttpRequestDecorator.getNativeRequest(request);
    HttpServletResponse servletResponse = ServerHttpResponseDecorator.getNativeResponse(response);
    HandshakeInfo handshakeInfo = handshakeInfoFactory.get();
    DataBufferFactory bufferFactory = response.bufferFactory();
    // Trigger WebFlux preCommit actions and upgrade
    return exchange.getResponse().setComplete().then(Mono.deferContextual(contextView -> {
        Endpoint endpoint = new StandardWebSocketHandlerAdapter(ContextWebSocketHandler.decorate(handler, contextView), session -> new TomcatWebSocketSession(session, handshakeInfo, bufferFactory));
        String requestURI = servletRequest.getRequestURI();
        DefaultServerEndpointConfig config = new DefaultServerEndpointConfig(requestURI, endpoint);
        config.setSubprotocols(subProtocol != null ? Collections.singletonList(subProtocol) : Collections.emptyList());
        WsServerContainer container = getContainer(servletRequest);
        try {
            container.doUpgrade(servletRequest, servletResponse, config, Collections.emptyMap());
        } catch (Exception ex) {
            return Mono.error(ex);
        }
        return Mono.empty();
    }));
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) 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) ServerContainer(jakarta.websocket.server.ServerContainer) Supplier(java.util.function.Supplier) TomcatWebSocketSession(org.springframework.web.reactive.socket.adapter.TomcatWebSocketSession) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Endpoint(jakarta.websocket.Endpoint) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) WsServerContainer(org.apache.tomcat.websocket.server.WsServerContainer) ServerHttpRequestDecorator(org.springframework.http.server.reactive.ServerHttpRequestDecorator) ServerHttpResponseDecorator(org.springframework.http.server.reactive.ServerHttpResponseDecorator) Nullable(org.springframework.lang.Nullable) WebSocketHandler(org.springframework.web.reactive.socket.WebSocketHandler) ContextWebSocketHandler(org.springframework.web.reactive.socket.adapter.ContextWebSocketHandler) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) Collections(java.util.Collections) StandardWebSocketHandlerAdapter(org.springframework.web.reactive.socket.adapter.StandardWebSocketHandlerAdapter) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) Assert(org.springframework.util.Assert) WsServerContainer(org.apache.tomcat.websocket.server.WsServerContainer) StandardWebSocketHandlerAdapter(org.springframework.web.reactive.socket.adapter.StandardWebSocketHandlerAdapter) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) TomcatWebSocketSession(org.springframework.web.reactive.socket.adapter.TomcatWebSocketSession) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) Endpoint(jakarta.websocket.Endpoint) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo)

Example 13 with DataBufferFactory

use of org.springframework.core.io.buffer.DataBufferFactory in project spring-framework by spring-projects.

the class UndertowWebSocketClient method handleChannel.

private void handleChannel(URI url, WebSocketHandler handler, Sinks.Empty<Void> completionSink, DefaultNegotiation negotiation, WebSocketChannel channel) {
    HandshakeInfo info = createHandshakeInfo(url, negotiation);
    DataBufferFactory bufferFactory = DefaultDataBufferFactory.sharedInstance;
    UndertowWebSocketSession session = new UndertowWebSocketSession(channel, info, bufferFactory, completionSink);
    UndertowWebSocketHandlerAdapter adapter = new UndertowWebSocketHandlerAdapter(session);
    channel.getReceiveSetter().set(adapter);
    channel.resumeReceives();
    handler.handle(session).checkpoint(url + " [UndertowWebSocketClient]").subscribe(session);
}
Also used : UndertowWebSocketSession(org.springframework.web.reactive.socket.adapter.UndertowWebSocketSession) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo) UndertowWebSocketHandlerAdapter(org.springframework.web.reactive.socket.adapter.UndertowWebSocketHandlerAdapter)

Example 14 with DataBufferFactory

use of org.springframework.core.io.buffer.DataBufferFactory in project spring-framework by spring-projects.

the class DefaultMetadataExtractorTests method setUp.

@BeforeEach
public void setUp() {
    DataBufferFactory bufferFactory = new LeakAwareNettyDataBufferFactory(PooledByteBufAllocator.DEFAULT);
    this.strategies = RSocketStrategies.builder().dataBufferFactory(bufferFactory).build();
    this.extractor = new DefaultMetadataExtractor(StringDecoder.allMimeTypes());
}
Also used : DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 15 with DataBufferFactory

use of org.springframework.core.io.buffer.DataBufferFactory in project spring-framework by spring-projects.

the class DefaultMetadataExtractorTests method tearDown.

@AfterEach
public void tearDown() throws InterruptedException {
    DataBufferFactory bufferFactory = this.strategies.dataBufferFactory();
    ((LeakAwareNettyDataBufferFactory) bufferFactory).checkForLeaks(Duration.ofSeconds(5));
}
Also used : DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) AfterEach(org.junit.jupiter.api.AfterEach)

Aggregations

DataBufferFactory (org.springframework.core.io.buffer.DataBufferFactory)28 DataBuffer (org.springframework.core.io.buffer.DataBuffer)14 Mono (reactor.core.publisher.Mono)12 Flux (reactor.core.publisher.Flux)11 List (java.util.List)9 Publisher (org.reactivestreams.Publisher)8 Nullable (org.springframework.lang.Nullable)8 IOException (java.io.IOException)7 MediaType (org.springframework.http.MediaType)7 Assert (org.springframework.util.Assert)7 HandshakeInfo (org.springframework.web.reactive.socket.HandshakeInfo)7 Collections (java.util.Collections)6 Resource (org.springframework.core.io.Resource)6 ServerHttpRequest (org.springframework.http.server.reactive.ServerHttpRequest)6 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 Supplier (java.util.function.Supplier)5 ResolvableType (org.springframework.core.ResolvableType)5 DataBufferUtils (org.springframework.core.io.buffer.DataBufferUtils)5 Test (org.junit.Test)4