This is an automated email from the ASF dual-hosted git repository. markt pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/tomcat.git
commit 897f4f6f862904379662e215d3dc9d956fb5652f Author: Mark Thomas <ma...@apache.org> AuthorDate: Thu Oct 7 18:10:37 2021 +0100 Add new, standard method for direct upgrade to WebSocket --- java/jakarta/websocket/server/ServerContainer.java | 34 ++++++++++++++++++++-- .../tomcat/websocket/server/WsServerContainer.java | 18 ++++++++++++ webapps/docs/changelog.xml | 5 ++++ 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/java/jakarta/websocket/server/ServerContainer.java b/java/jakarta/websocket/server/ServerContainer.java index a210311..4820cb6 100644 --- a/java/jakarta/websocket/server/ServerContainer.java +++ b/java/jakarta/websocket/server/ServerContainer.java @@ -16,6 +16,9 @@ */ package jakarta.websocket.server; +import java.io.IOException; +import java.util.Map; + import jakarta.websocket.DeploymentException; import jakarta.websocket.WebSocketContainer; @@ -25,6 +28,33 @@ import jakarta.websocket.WebSocketContainer; public interface ServerContainer extends WebSocketContainer { public abstract void addEndpoint(Class<?> clazz) throws DeploymentException; - public abstract void addEndpoint(ServerEndpointConfig sec) - throws DeploymentException; + public abstract void addEndpoint(ServerEndpointConfig sec) throws DeploymentException; + + /** + * Upgrade the HTTP connection represented by the {@code HttpServletRequest} and {@code HttpServletResponse} to the + * WebSocket protocol and establish a WebSocket connection as per the provided {@link ServerEndpointConfig}. + * <p> + * This method is primarily intended to be used by frameworks that implement the front-controller pattern. It does + * not deploy the provided endpoint. + * <p> + * If the WebSocket implementation is not deployed as part of a Jakarta Servlet container, this method will throw an + * {@link UnsupportedOperationException}. + * + * @param httpServletRequest The {@code HttpServletRequest} to be processed as a WebSocket handshake as per + * section 4.0 of RFC 6455. + * @param httpServletResponse The {@code HttpServletResponse} to be used when processing the + * {@code httpServletRequest} as a WebSocket handshake as per section 4.0 of RFC 6455. + * @param sec The server endpoint configuration to use to configure the WebSocket endpoint + * @param pathParameters Provides a mapping of path parameter names and values, if any, to be used for the + * WebSocket connection established by the call to this method. If no such mapping is + * defined, an empty Map must be passed. + * + * @throws IllegalStateException if the provided request does not meet the requirements of the WebSocket handshake + * @throws UnsupportedOperationException if the WebSocket implementation is not deployed as part of a Jakarta + * Servlet container + * @throws IOException if an I/O error occurs during the establishment of a WebSocket connection + * @throws DeploymentException if a configuration error prevents the establishment of a WebSocket connection + */ + public void upgradeHttpToWebSocket(Object httpServletRequest, Object httpServletResponse, ServerEndpointConfig sec, + Map<String,String> pathParameters) throws IOException, DeploymentException; } diff --git a/java/org/apache/tomcat/websocket/server/WsServerContainer.java b/java/org/apache/tomcat/websocket/server/WsServerContainer.java index e269f8b..b6949a6 100644 --- a/java/org/apache/tomcat/websocket/server/WsServerContainer.java +++ b/java/org/apache/tomcat/websocket/server/WsServerContainer.java @@ -313,7 +313,12 @@ public class WsServerContainer extends WsWebSocketContainer * @throws ServletException If a configuration error prevents the upgrade * from taking place * @throws IOException If an I/O error occurs during the upgrade process + * + * @deprecated This method will be removed in Apache Tomcat 10.1 onwards. It + * has been replaced by {@link #upgradeHttpToWebSocket(Object, + * Object, ServerEndpointConfig, Map)} */ + @Deprecated public void doUpgrade(HttpServletRequest request, HttpServletResponse response, ServerEndpointConfig sec, Map<String,String> pathParams) @@ -322,6 +327,19 @@ public class WsServerContainer extends WsWebSocketContainer } + @Override + public void upgradeHttpToWebSocket(Object httpServletRequest, Object httpServletResponse, + ServerEndpointConfig sec, Map<String, String> pathParameters) + throws IOException, DeploymentException { + try { + UpgradeUtil.doUpgrade(this, (HttpServletRequest) httpServletRequest, (HttpServletResponse) httpServletResponse, + sec, pathParameters); + } catch (ServletException e) { + throw new DeploymentException(e.getMessage(), e); + } + } + + public WsMappingResult findMapping(String path) { // Prevent registering additional endpoints once the first attempt has diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml index e0a9205..589e0fa 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -139,6 +139,11 @@ </code>to align with recent updates in the WebSocket specification project. (markt) </update> + <update> + Add a new method <code>ServerContainer.upgradeHttpToWebSocket()</code> + to align with recent updates in the WebSocket specification project. + (markt) + </update> </changelog> </subsection> </section> --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org