Search in sources :

Example 1 with JettyWebSocketHandlerAdapter

use of org.springframework.web.reactive.socket.adapter.JettyWebSocketHandlerAdapter in project spring-framework by spring-projects.

the class JettyWebSocketClient method createJettyHandler.

private Object createJettyHandler(URI url, WebSocketHandler handler, MonoProcessor<Void> completion) {
    return new JettyWebSocketHandlerAdapter(handler, session -> {
        UpgradeResponse response = session.getUpgradeResponse();
        HttpHeaders responseHeaders = new HttpHeaders();
        response.getHeaders().forEach(responseHeaders::put);
        HandshakeInfo info = afterHandshake(url, responseHeaders);
        return new JettyWebSocketSession(session, info, this.bufferFactory, completion);
    });
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) UpgradeResponse(org.eclipse.jetty.websocket.api.UpgradeResponse) JettyWebSocketSession(org.springframework.web.reactive.socket.adapter.JettyWebSocketSession) JettyWebSocketHandlerAdapter(org.springframework.web.reactive.socket.adapter.JettyWebSocketHandlerAdapter) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo)

Example 2 with JettyWebSocketHandlerAdapter

use of org.springframework.web.reactive.socket.adapter.JettyWebSocketHandlerAdapter in project spring-framework by spring-projects.

the class JettyRequestUpgradeStrategy method upgrade.

@Override
public Mono<Void> upgrade(ServerWebExchange exchange, WebSocketHandler handler, Optional<String> subProtocol) {
    ServerHttpRequest request = exchange.getRequest();
    ServerHttpResponse response = exchange.getResponse();
    HttpServletRequest servletRequest = getHttpServletRequest(request);
    HttpServletResponse servletResponse = getHttpServletResponse(response);
    JettyWebSocketHandlerAdapter adapter = new JettyWebSocketHandlerAdapter(handler, session -> {
        HandshakeInfo info = getHandshakeInfo(exchange, subProtocol);
        DataBufferFactory factory = response.bufferFactory();
        return new JettyWebSocketSession(session, info, factory);
    });
    startLazily(servletRequest);
    boolean isUpgrade = this.factory.isUpgradeRequest(servletRequest, servletResponse);
    Assert.isTrue(isUpgrade, "Not a WebSocket handshake");
    try {
        adapterHolder.set(new WebSocketHandlerContainer(adapter, subProtocol));
        this.factory.acceptWebSocket(servletRequest, servletResponse);
    } catch (IOException ex) {
        return Mono.error(ex);
    } finally {
        adapterHolder.remove();
    }
    return Mono.empty();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletServerHttpRequest(org.springframework.http.server.reactive.ServletServerHttpRequest) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) JettyWebSocketSession(org.springframework.web.reactive.socket.adapter.JettyWebSocketSession) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) ServletServerHttpResponse(org.springframework.http.server.reactive.ServletServerHttpResponse) JettyWebSocketHandlerAdapter(org.springframework.web.reactive.socket.adapter.JettyWebSocketHandlerAdapter) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo)

Aggregations

HandshakeInfo (org.springframework.web.reactive.socket.HandshakeInfo)2 JettyWebSocketHandlerAdapter (org.springframework.web.reactive.socket.adapter.JettyWebSocketHandlerAdapter)2 JettyWebSocketSession (org.springframework.web.reactive.socket.adapter.JettyWebSocketSession)2 IOException (java.io.IOException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 UpgradeResponse (org.eclipse.jetty.websocket.api.UpgradeResponse)1 DataBufferFactory (org.springframework.core.io.buffer.DataBufferFactory)1 HttpHeaders (org.springframework.http.HttpHeaders)1 ServerHttpRequest (org.springframework.http.server.reactive.ServerHttpRequest)1 ServerHttpResponse (org.springframework.http.server.reactive.ServerHttpResponse)1 ServletServerHttpRequest (org.springframework.http.server.reactive.ServletServerHttpRequest)1 ServletServerHttpResponse (org.springframework.http.server.reactive.ServletServerHttpResponse)1