From f30f83c687b72fba3d165bf03b5f9ceb4504a20a Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Sat, 5 Nov 2022 13:37:25 +0000 Subject: [PATCH] Make WebSocket upgrade strategies compatible with Tomcat 10.1 --- .../upgrade/TomcatRequestUpgradeStrategy.java | 6 ++---- .../standard/TomcatRequestUpgradeStrategy.java | 15 ++++----------- 2 files changed, 6 insertions(+), 15 deletions(-) diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/socket/server/upgrade/TomcatRequestUpgradeStrategy.java b/spring-webflux/src/main/java/org/springframework/web/reactive/socket/server/upgrade/TomcatRequestUpgradeStrategy.java index 4cecb3ea3c0b..ce3a3719d60a 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/socket/server/upgrade/TomcatRequestUpgradeStrategy.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/socket/server/upgrade/TomcatRequestUpgradeStrategy.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -121,8 +121,6 @@ public Integer getMaxBinaryMessageBufferSize() { return this.maxBinaryMessageBufferSize; } - - @SuppressWarnings("deprecation") // for old doUpgrade variant in Tomcat 9.0.55 @Override public Mono upgrade(ServerWebExchange exchange, WebSocketHandler handler, @Nullable String subProtocol, Supplier handshakeInfoFactory){ @@ -150,7 +148,7 @@ public Mono upgrade(ServerWebExchange exchange, WebSocketHandler handler, WsServerContainer container = getContainer(servletRequest); try { - container.doUpgrade(servletRequest, servletResponse, config, Collections.emptyMap()); + container.upgradeHttpToWebSocket(servletRequest, servletResponse, config, Collections.emptyMap()); } catch (Exception ex) { return Mono.error(ex); diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/server/standard/TomcatRequestUpgradeStrategy.java b/spring-websocket/src/main/java/org/springframework/web/socket/server/standard/TomcatRequestUpgradeStrategy.java index b13eb66b36b2..20e5f7a435db 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/server/standard/TomcatRequestUpgradeStrategy.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/server/standard/TomcatRequestUpgradeStrategy.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,12 +16,10 @@ package org.springframework.web.socket.server.standard; -import java.io.IOException; import java.util.Collections; import java.util.List; import java.util.Map; -import jakarta.servlet.ServletException; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import jakarta.websocket.Endpoint; @@ -35,7 +33,7 @@ /** * A WebSocket {@code RequestUpgradeStrategy} for Apache Tomcat. Compatible with - * all versions of Tomcat that support JSR-356, i.e. Tomcat 7.0.47+ and higher. + * Tomcat 10 and higher. * *

To modify properties of the underlying {@link jakarta.websocket.server.ServerContainer} * you can use {@link ServletServerContainerFactoryBean} in XML configuration or, @@ -52,7 +50,6 @@ public String[] getSupportedVersions() { return new String[] {"13"}; } - @SuppressWarnings("deprecation") // for old doUpgrade variant in Tomcat 9.0.55 @Override public void upgradeInternal(ServerHttpRequest request, ServerHttpResponse response, @Nullable String selectedProtocol, List selectedExtensions, Endpoint endpoint) @@ -70,16 +67,12 @@ public void upgradeInternal(ServerHttpRequest request, ServerHttpResponse respon endpointConfig.setExtensions(selectedExtensions); try { - getContainer(servletRequest).doUpgrade(servletRequest, servletResponse, endpointConfig, pathParams); + getContainer(servletRequest).upgradeHttpToWebSocket(servletRequest, servletResponse, endpointConfig, pathParams); } - catch (ServletException ex) { + catch (Exception ex) { throw new HandshakeFailureException( "Servlet request failed to upgrade to WebSocket: " + requestUrl, ex); } - catch (IOException ex) { - throw new HandshakeFailureException( - "Response update failed during upgrade to WebSocket: " + requestUrl, ex); - } } @Override