From ae7e15c99a86d37f1106c24c18d7118701c22ddd Mon Sep 17 00:00:00 2001 From: jfarcand Date: Mon, 7 Apr 2014 09:19:02 -0400 Subject: [PATCH] Fixes #1548, #1550 --- .../container/JSR356AsyncSupport.java | 13 ++--- .../atmosphere/container/JSR356Endpoint.java | 48 ++++++++----------- 2 files changed, 25 insertions(+), 36 deletions(-) diff --git a/modules/cpr/src/main/java/org/atmosphere/container/JSR356AsyncSupport.java b/modules/cpr/src/main/java/org/atmosphere/container/JSR356AsyncSupport.java index ae474006f01..553de289575 100644 --- a/modules/cpr/src/main/java/org/atmosphere/container/JSR356AsyncSupport.java +++ b/modules/cpr/src/main/java/org/atmosphere/container/JSR356AsyncSupport.java @@ -53,14 +53,12 @@ public JSR356AsyncSupport(AtmosphereConfig config) { String servletPath = config.getInitParameter(ApplicationConfig.JSR356_MAPPING_PATH); if (servletPath == null) { servletPath = IOUtils.guestServletPath(config); - if (servletPath.equals("/")) { + if (servletPath.equals("/") || servletPath.equals("/*")) { servletPath = PATH +"}"; - } else if (servletPath.equals("/*")) { - servletPath = ""; } } logger.info("JSR 356 Mapping path {}", servletPath); - configurator = new AtmosphereConfigurator(config.framework(), servletPath); + configurator = new AtmosphereConfigurator(config.framework()); StringBuilder b = new StringBuilder(servletPath); for (int i = 0; i < pathLength; i++) { @@ -73,7 +71,6 @@ public JSR356AsyncSupport(AtmosphereConfig config) { logger.warn("Duplicate guess {}", servletPath, e); b.setLength(0); b.append(servletPath); - configurator.guessedServletPath = servletPath; } b.append(PATH).append(i).append("}"); } @@ -91,7 +88,6 @@ public String getContainerName() { public final static class AtmosphereConfigurator extends ServerEndpointConfig.Configurator { private final AtmosphereFramework framework; - private String guessedServletPath; /** * TODO: UGLY! * GlassFish/Jetty call modifyHandshake BEFORE getEndpointInstance() where other jsr356 do the reverse. @@ -99,14 +95,13 @@ public final static class AtmosphereConfigurator extends ServerEndpointConfig.Co final ThreadLocal endPoint = new ThreadLocal(); final ThreadLocal hRequest = new ThreadLocal(); - public AtmosphereConfigurator(AtmosphereFramework framework, String guessedServletPath) { + public AtmosphereConfigurator(AtmosphereFramework framework) { this.framework = framework; - this.guessedServletPath = guessedServletPath; } public T getEndpointInstance(java.lang.Class endpointClass) throws java.lang.InstantiationException { if (JSR356Endpoint.class.isAssignableFrom(endpointClass)) { - JSR356Endpoint e = new JSR356Endpoint(framework, WebSocketProcessorFactory.getDefault().getWebSocketProcessor(framework), guessedServletPath); + JSR356Endpoint e = new JSR356Endpoint(framework, WebSocketProcessorFactory.getDefault().getWebSocketProcessor(framework)); if (hRequest.get() != null) { e.handshakeRequest(hRequest.get()); hRequest.set(null); diff --git a/modules/cpr/src/main/java/org/atmosphere/container/JSR356Endpoint.java b/modules/cpr/src/main/java/org/atmosphere/container/JSR356Endpoint.java index 54b897b0ea5..f716935c1ef 100644 --- a/modules/cpr/src/main/java/org/atmosphere/container/JSR356Endpoint.java +++ b/modules/cpr/src/main/java/org/atmosphere/container/JSR356Endpoint.java @@ -34,6 +34,7 @@ import javax.websocket.Session; import javax.websocket.server.HandshakeRequest; import java.io.IOException; +import java.net.URI; import java.nio.ByteBuffer; import java.util.HashMap; import java.util.List; @@ -52,13 +53,11 @@ public class JSR356Endpoint extends Endpoint { private final AtmosphereFramework framework; private WebSocket webSocket; private final int webSocketWriteTimeout; - private final String servletPath; private HandshakeRequest handshakeRequest; - public JSR356Endpoint(AtmosphereFramework framework, WebSocketProcessor webSocketProcessor, String servletPath) { + public JSR356Endpoint(AtmosphereFramework framework, WebSocketProcessor webSocketProcessor) { this.framework = framework; this.webSocketProcessor = webSocketProcessor; - this.servletPath = servletPath; if (framework.isUseNativeImplementation()) { throw new IllegalStateException("You cannot use WebSocket native implementation with JSR356. Please set " + ApplicationConfig.PROPERTY_NATIVE_COMETSUPPORT + " to false"); @@ -114,32 +113,27 @@ public void onOpen(Session session, EndpointConfig endpointConfig) { headers.put(e.getKey(), e.getValue().size() > 0 ? e.getValue().get(0) : ""); } - String pathInfo = ""; - StringBuffer p = new StringBuffer("/"); - try { - boolean append = true; - for (Map.Entry e : session.getPathParameters().entrySet()) { - // Glasfish return reverse path!!! - if (append && !e.getKey().equalsIgnoreCase("{path}")) { - append = false; - } - - if (append) { - p.append(e.getValue()).append("/"); - } else { - p.insert(0, e.getValue()).insert(0, "/"); - } - } - if (p.length() > 1) { - p.deleteCharAt(p.length() - 1); - } + String servletPath = ""; + + URI uri = session.getRequestURI(); + String[] paths = uri.getPath() != null ? uri.getPath().split("/") : new String[]{}; - pathInfo = p.toString(); - if (pathInfo.equals(servletPath)) { - pathInfo = null; + // /contextPath/servletPath/pathInfo + StringBuffer b = new StringBuffer("/"); + for (int i = 0; i < paths.length; i++) { + if (i == 2) servletPath += "/" + paths[i]; + if (i >= 3) { + b.append(paths[i]).append("/"); } - } catch (Exception ex) { - logger.warn("Unexpected path decoding", ex); + } + + if (b.length() > 1) { + b.deleteCharAt(b.length() - 1); + } + + String pathInfo = b.toString(); + if (pathInfo.equals("/")) { + pathInfo = null; } try {