|
| 1 | +/* |
| 2 | + * Copyright 2002-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.web.reactive.socket.server.upgrade; |
| 18 | + |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.function.Supplier; |
| 22 | + |
| 23 | +import jakarta.servlet.http.HttpServletRequest; |
| 24 | +import jakarta.servlet.http.HttpServletResponse; |
| 25 | +import jakarta.websocket.Endpoint; |
| 26 | +import jakarta.websocket.server.ServerContainer; |
| 27 | +import jakarta.websocket.server.ServerEndpointConfig; |
| 28 | +import reactor.core.publisher.Mono; |
| 29 | + |
| 30 | +import org.springframework.core.io.buffer.DataBufferFactory; |
| 31 | +import org.springframework.http.server.reactive.ServerHttpRequest; |
| 32 | +import org.springframework.http.server.reactive.ServerHttpRequestDecorator; |
| 33 | +import org.springframework.http.server.reactive.ServerHttpResponse; |
| 34 | +import org.springframework.http.server.reactive.ServerHttpResponseDecorator; |
| 35 | +import org.springframework.lang.Nullable; |
| 36 | +import org.springframework.util.Assert; |
| 37 | +import org.springframework.web.reactive.socket.HandshakeInfo; |
| 38 | +import org.springframework.web.reactive.socket.WebSocketHandler; |
| 39 | +import org.springframework.web.reactive.socket.adapter.ContextWebSocketHandler; |
| 40 | +import org.springframework.web.reactive.socket.adapter.StandardWebSocketHandlerAdapter; |
| 41 | +import org.springframework.web.reactive.socket.adapter.TomcatWebSocketSession; |
| 42 | +import org.springframework.web.reactive.socket.server.RequestUpgradeStrategy; |
| 43 | +import org.springframework.web.server.ServerWebExchange; |
| 44 | + |
| 45 | +/** |
| 46 | + * A WebSocket {@code RequestUpgradeStrategy} for the Jakarta WebSocket API 2.1+. |
| 47 | + * |
| 48 | + * <p>This strategy serves as a fallback if no specific server has been detected. |
| 49 | + * It can also be used with Jakarta EE 10 level servers such as Tomcat 10.1 and |
| 50 | + * Undertow 2.3 directly, relying on their built-in Jakarta WebSocket 2.1 support. |
| 51 | + * |
| 52 | + * @author Juergen Hoeller |
| 53 | + * @author Violeta Georgieva |
| 54 | + * @author Rossen Stoyanchev |
| 55 | + * @since 6.0 |
| 56 | + * @see jakarta.websocket.server.ServerContainer#upgradeHttpToWebSocket |
| 57 | + */ |
| 58 | +public class StandardWebSocketUpgradeStrategy implements RequestUpgradeStrategy { |
| 59 | + |
| 60 | + private static final String SERVER_CONTAINER_ATTR = "jakarta.websocket.server.ServerContainer"; |
| 61 | + |
| 62 | + |
| 63 | + @Nullable |
| 64 | + private Long asyncSendTimeout; |
| 65 | + |
| 66 | + @Nullable |
| 67 | + private Long maxSessionIdleTimeout; |
| 68 | + |
| 69 | + @Nullable |
| 70 | + private Integer maxTextMessageBufferSize; |
| 71 | + |
| 72 | + @Nullable |
| 73 | + private Integer maxBinaryMessageBufferSize; |
| 74 | + |
| 75 | + @Nullable |
| 76 | + private ServerContainer serverContainer; |
| 77 | + |
| 78 | + |
| 79 | + /** |
| 80 | + * Exposes the underlying config option on |
| 81 | + * {@link ServerContainer#setAsyncSendTimeout(long)}. |
| 82 | + */ |
| 83 | + public void setAsyncSendTimeout(Long timeoutInMillis) { |
| 84 | + this.asyncSendTimeout = timeoutInMillis; |
| 85 | + } |
| 86 | + |
| 87 | + @Nullable |
| 88 | + public Long getAsyncSendTimeout() { |
| 89 | + return this.asyncSendTimeout; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Exposes the underlying config option on |
| 94 | + * {@link ServerContainer#setDefaultMaxSessionIdleTimeout(long)}. |
| 95 | + */ |
| 96 | + public void setMaxSessionIdleTimeout(Long timeoutInMillis) { |
| 97 | + this.maxSessionIdleTimeout = timeoutInMillis; |
| 98 | + } |
| 99 | + |
| 100 | + @Nullable |
| 101 | + public Long getMaxSessionIdleTimeout() { |
| 102 | + return this.maxSessionIdleTimeout; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Exposes the underlying config option on |
| 107 | + * {@link ServerContainer#setDefaultMaxTextMessageBufferSize(int)}. |
| 108 | + */ |
| 109 | + public void setMaxTextMessageBufferSize(Integer bufferSize) { |
| 110 | + this.maxTextMessageBufferSize = bufferSize; |
| 111 | + } |
| 112 | + |
| 113 | + @Nullable |
| 114 | + public Integer getMaxTextMessageBufferSize() { |
| 115 | + return this.maxTextMessageBufferSize; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Exposes the underlying config option on |
| 120 | + * {@link ServerContainer#setDefaultMaxBinaryMessageBufferSize(int)}. |
| 121 | + */ |
| 122 | + public void setMaxBinaryMessageBufferSize(Integer bufferSize) { |
| 123 | + this.maxBinaryMessageBufferSize = bufferSize; |
| 124 | + } |
| 125 | + |
| 126 | + @Nullable |
| 127 | + public Integer getMaxBinaryMessageBufferSize() { |
| 128 | + return this.maxBinaryMessageBufferSize; |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public Mono<Void> upgrade(ServerWebExchange exchange, WebSocketHandler handler, |
| 133 | + @Nullable String subProtocol, Supplier<HandshakeInfo> handshakeInfoFactory){ |
| 134 | + |
| 135 | + ServerHttpRequest request = exchange.getRequest(); |
| 136 | + ServerHttpResponse response = exchange.getResponse(); |
| 137 | + |
| 138 | + HttpServletRequest servletRequest = ServerHttpRequestDecorator.getNativeRequest(request); |
| 139 | + HttpServletResponse servletResponse = ServerHttpResponseDecorator.getNativeResponse(response); |
| 140 | + |
| 141 | + HandshakeInfo handshakeInfo = handshakeInfoFactory.get(); |
| 142 | + DataBufferFactory bufferFactory = response.bufferFactory(); |
| 143 | + |
| 144 | + // Trigger WebFlux preCommit actions and upgrade |
| 145 | + return exchange.getResponse().setComplete() |
| 146 | + .then(Mono.deferContextual(contextView -> { |
| 147 | + Endpoint endpoint = new StandardWebSocketHandlerAdapter( |
| 148 | + ContextWebSocketHandler.decorate(handler, contextView), |
| 149 | + session -> new TomcatWebSocketSession(session, handshakeInfo, bufferFactory)); |
| 150 | + |
| 151 | + String requestURI = servletRequest.getRequestURI(); |
| 152 | + DefaultServerEndpointConfig config = new DefaultServerEndpointConfig(requestURI, endpoint); |
| 153 | + config.setSubprotocols(subProtocol != null ? |
| 154 | + Collections.singletonList(subProtocol) : Collections.emptyList()); |
| 155 | + |
| 156 | + try { |
| 157 | + upgradeHttpToWebSocket(servletRequest, servletResponse, config, Collections.emptyMap()); |
| 158 | + } |
| 159 | + catch (Exception ex) { |
| 160 | + return Mono.error(ex); |
| 161 | + } |
| 162 | + return Mono.empty(); |
| 163 | + })); |
| 164 | + } |
| 165 | + |
| 166 | + |
| 167 | + protected void upgradeHttpToWebSocket(HttpServletRequest request, HttpServletResponse response, |
| 168 | + ServerEndpointConfig endpointConfig, Map<String,String> pathParams) throws Exception { |
| 169 | + |
| 170 | + getContainer(request).upgradeHttpToWebSocket(request, response, endpointConfig, pathParams); |
| 171 | + } |
| 172 | + |
| 173 | + protected ServerContainer getContainer(HttpServletRequest request) { |
| 174 | + if (this.serverContainer == null) { |
| 175 | + Object container = request.getServletContext().getAttribute(SERVER_CONTAINER_ATTR); |
| 176 | + Assert.state(container instanceof ServerContainer, |
| 177 | + "ServletContext attribute 'jakarta.websocket.server.ServerContainer' not found."); |
| 178 | + this.serverContainer = (ServerContainer) container; |
| 179 | + initServerContainer(this.serverContainer); |
| 180 | + } |
| 181 | + return this.serverContainer; |
| 182 | + } |
| 183 | + |
| 184 | + private void initServerContainer(ServerContainer serverContainer) { |
| 185 | + if (this.asyncSendTimeout != null) { |
| 186 | + serverContainer.setAsyncSendTimeout(this.asyncSendTimeout); |
| 187 | + } |
| 188 | + if (this.maxSessionIdleTimeout != null) { |
| 189 | + serverContainer.setDefaultMaxSessionIdleTimeout(this.maxSessionIdleTimeout); |
| 190 | + } |
| 191 | + if (this.maxTextMessageBufferSize != null) { |
| 192 | + serverContainer.setDefaultMaxTextMessageBufferSize(this.maxTextMessageBufferSize); |
| 193 | + } |
| 194 | + if (this.maxBinaryMessageBufferSize != null) { |
| 195 | + serverContainer.setDefaultMaxBinaryMessageBufferSize(this.maxBinaryMessageBufferSize); |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | +} |
0 commit comments