Skip to content

Commit

Permalink
Merge pull request #2316 from Artur-/slash-mappings
Browse files Browse the repository at this point in the history
Register websocket endpoints for /mapping/ for /mapping/* servlets
  • Loading branch information
jfarcand committed Mar 7, 2018
2 parents 144bf27 + 14367ba commit e8bf1f2
Showing 1 changed file with 29 additions and 7 deletions.
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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}/
// /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 {
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("}");
}
}

Expand Down

0 comments on commit e8bf1f2

Please sign in to comment.