From a9ed1bfba365ebcca27eae9045a57792369c229e Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Wed, 25 Apr 2018 09:51:13 +0300 Subject: [PATCH 1/2] Revert "Register websocket endpoints for /mapping/ for /mapping/* servlets" This reverts commit 14367ba9f5d51ea57b4772131caa42734e21da8c. --- .../container/JSR356AsyncSupport.java | 36 ++++--------------- 1 file changed, 7 insertions(+), 29 deletions(-) diff --git a/modules/runtime/src/main/java/org/atmosphere/container/JSR356AsyncSupport.java b/modules/runtime/src/main/java/org/atmosphere/container/JSR356AsyncSupport.java index 504dda20fe6..950fb784300 100644 --- a/modules/runtime/src/main/java/org/atmosphere/container/JSR356AsyncSupport.java +++ b/modules/runtime/src/main/java/org/atmosphere/container/JSR356AsyncSupport.java @@ -23,9 +23,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.ArrayList; -import java.util.List; - import javax.servlet.ServletContext; import javax.websocket.DeploymentException; import javax.websocket.HandshakeResponse; @@ -36,6 +33,7 @@ public class JSR356AsyncSupport extends Servlet30CometSupport { private static final Logger logger = LoggerFactory.getLogger(JSR356AsyncSupport.class); + private static final String PATH = "/{path"; private final AtmosphereConfigurator configurator; public JSR356AsyncSupport(AtmosphereConfig config) { @@ -63,46 +61,26 @@ public JSR356AsyncSupport(AtmosphereConfig config, ServletContext ctx) { String servletPath = config.getInitParameter(ApplicationConfig.JSR356_MAPPING_PATH); if (servletPath == null) { servletPath = IOUtils.guestServletPath(config); - if (servletPath.equals("/") || servletPath.equals("/*")) { - servletPath = ""; + if (servletPath.equals("") || servletPath.equals("/") || servletPath.equals("/*")) { + servletPath = PATH +"}"; } } logger.info("JSR 356 Mapping path {}", servletPath); configurator = new AtmosphereConfigurator(config.framework()); - // Register endpoints at - // /servletPath - // /servletPath/ - // /servletPath/{path1} - // /servletPath/{path1}/ - // /servletPath/{path1}/{path2} - // etc with up to `pathLength` parameters - StringBuilder b = new StringBuilder(servletPath); - List endpointPaths = new ArrayList<>(); - endpointPaths.add(servletPath); for (int i = 0; i < pathLength; i++) { - b.append("/"); - endpointPaths.add(b.toString()); - b.append("{path" + i + "}"); - endpointPaths.add(b.toString()); - } - - for (String endpointPath : endpointPaths) { - if ("".equals(endpointPath)) { - // Spec says: The path is always non-null and always begins with a leading "/". - // Root mapping is covered by /{path1} - continue; - } try { - ServerEndpointConfig endpointConfig = ServerEndpointConfig.Builder.create(JSR356Endpoint.class, endpointPath).configurator(configurator).build(); - container.addEndpoint(endpointConfig); + container.addEndpoint(ServerEndpointConfig.Builder.create(JSR356Endpoint.class, b.toString()).configurator(configurator).build()); } catch (DeploymentException e) { logger.warn("Duplicate Servlet Mapping Path {}. Use {} init-param to prevent this message", servletPath, ApplicationConfig.JSR356_MAPPING_PATH); logger.trace("", e); servletPath = IOUtils.guestServletPath(config); logger.warn("Duplicate guess {}", servletPath, e); + b.setLength(0); + b.append(servletPath); } + b.append(PATH).append(i).append("}"); } } From e98242be108b01a98cf9c044f0047dddd0ed30a8 Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Wed, 7 Mar 2018 15:01:56 +0200 Subject: [PATCH 2/2] Register websocket endpoints for /mapping/ for /mapping/* servlets Previously for a servlet mapped using /mapping/*, there were mappings done for: * /mapping * /mapping/{path1} * /mapping/{path1}/{path2} etc which do not match /mapping/ Tomcat disallows registering websocket endpoints using a trailing slash, except for /servletMapping/. According to the code this is because "As per EG discussion, all other empty segments are invalid" --- .../container/JSR356AsyncSupport.java | 36 +++++++++++++++---- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/modules/runtime/src/main/java/org/atmosphere/container/JSR356AsyncSupport.java b/modules/runtime/src/main/java/org/atmosphere/container/JSR356AsyncSupport.java index 950fb784300..606bd4cc3d6 100644 --- a/modules/runtime/src/main/java/org/atmosphere/container/JSR356AsyncSupport.java +++ b/modules/runtime/src/main/java/org/atmosphere/container/JSR356AsyncSupport.java @@ -23,6 +23,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.ArrayList; +import java.util.List; + import javax.servlet.ServletContext; import javax.websocket.DeploymentException; import javax.websocket.HandshakeResponse; @@ -33,7 +36,6 @@ public class JSR356AsyncSupport extends Servlet30CometSupport { private static final Logger logger = LoggerFactory.getLogger(JSR356AsyncSupport.class); - private static final String PATH = "/{path"; private final AtmosphereConfigurator configurator; public JSR356AsyncSupport(AtmosphereConfig config) { @@ -61,26 +63,46 @@ public JSR356AsyncSupport(AtmosphereConfig config, ServletContext ctx) { String servletPath = config.getInitParameter(ApplicationConfig.JSR356_MAPPING_PATH); if (servletPath == null) { servletPath = IOUtils.guestServletPath(config); - if (servletPath.equals("") || servletPath.equals("/") || servletPath.equals("/*")) { - servletPath = PATH +"}"; + if (servletPath.equals("/") || servletPath.equals("/*")) { + servletPath = ""; } } logger.info("JSR 356 Mapping path {}", servletPath); configurator = new AtmosphereConfigurator(config.framework()); + // Register endpoints at + // /servletPath + // /servletPath/ + // /servletPath/{path1} + // /servletPath/{path1}/{path2} + // etc with up to `pathLength` parameters + StringBuilder b = new StringBuilder(servletPath); + List endpointPaths = new ArrayList<>(); + endpointPaths.add(servletPath); + if (!servletPath.endsWith("/")) { + endpointPaths.add(servletPath+"/"); + } for (int i = 0; i < pathLength; i++) { + b.append("/{path" + i + "}"); + endpointPaths.add(b.toString()); + } + + for (String endpointPath : endpointPaths) { + if ("".equals(endpointPath)) { + // Spec says: The path is always non-null and always begins with a leading "/". + // Root mapping is covered by /{path1} + continue; + } try { - container.addEndpoint(ServerEndpointConfig.Builder.create(JSR356Endpoint.class, b.toString()).configurator(configurator).build()); + ServerEndpointConfig endpointConfig = ServerEndpointConfig.Builder.create(JSR356Endpoint.class, endpointPath).configurator(configurator).build(); + container.addEndpoint(endpointConfig); } catch (DeploymentException e) { logger.warn("Duplicate Servlet Mapping Path {}. Use {} init-param to prevent this message", servletPath, ApplicationConfig.JSR356_MAPPING_PATH); logger.trace("", e); servletPath = IOUtils.guestServletPath(config); logger.warn("Duplicate guess {}", servletPath, e); - b.setLength(0); - b.append(servletPath); } - b.append(PATH).append(i).append("}"); } }