Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve an issue with the servlet mapping for JSR 356 #2331

Merged
merged 1 commit into from
May 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
}

}