Skip to content

Commit

Permalink
Merge pull request #2331 from fenik17/jsr356_mapping_revert
Browse files Browse the repository at this point in the history
Resolve an issue with the servlet mapping for JSR 356
  • Loading branch information
jfarcand authored May 7, 2018
2 parents 6622853 + abafc00 commit 2e8f3e9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,8 +33,9 @@
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) {
this(config, config.getServletContext());
}
Expand All @@ -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<String> 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("}");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
public class IOUtils {
private final static Logger logger = LoggerFactory.getLogger(IOUtils.class);
private final static List<String> knownClasses;
private final static Pattern SERVLET_PATH_PATTERN = Pattern.compile("([\\/]?[\\w-[.]]+|[\\/]\\*\\*)+");
private final static Pattern SERVLET_PATH_PATTERN = Pattern.compile("([/]?[\\w-+.]+|[/]\\*\\*)+");

static {
knownClasses = new ArrayList<String>() {
Expand Down
23 changes: 9 additions & 14 deletions modules/runtime/src/test/java/org/atmosphere/util/IOUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import org.testng.annotations.Test;

import static org.atmosphere.util.IOUtils.getCleanedServletPath;
import static org.testng.Assert.assertEquals;

/**
Expand All @@ -26,20 +27,14 @@ public class IOUtilsTest {

@Test
public void testGetCleanedServletPath() {
String testFullPath;
String testCleanedPath;

testFullPath = "/foo/bar/*";
testCleanedPath = IOUtils.getCleanedServletPath(testFullPath);
assertEquals(testCleanedPath, "/foo/bar");

testFullPath = "foo/bar/**/*";
testCleanedPath = IOUtils.getCleanedServletPath(testFullPath);
assertEquals(testCleanedPath, "/foo/bar/**");

testFullPath = "/com.zyxabc.abc.Abc/gwtCometEvent*";
testCleanedPath = IOUtils.getCleanedServletPath(testFullPath);
assertEquals(testCleanedPath, "/com.zyxabc.abc.Abc/gwtCometEvent");
assertEquals(getCleanedServletPath("/foo/bar/*"), "/foo/bar");
assertEquals(getCleanedServletPath("/foo/bar/"), "/foo/bar");
assertEquals(getCleanedServletPath("foo"), "/foo");
assertEquals(getCleanedServletPath("/foo/"), "/foo");
assertEquals(getCleanedServletPath("foo/bar/**/*"), "/foo/bar/**");
assertEquals(getCleanedServletPath("/com.zyxabc.abc.Abc/gwtCometEvent*"), "/com.zyxabc.abc.Abc/gwtCometEvent");
assertEquals(getCleanedServletPath("/com.abc_Abc-abc/"), "/com.abc_Abc-abc");
assertEquals(getCleanedServletPath("/com.abc_Abc-abc+bca/"), "/com.abc_Abc-abc+bca");
}

}

0 comments on commit 2e8f3e9

Please sign in to comment.