Skip to content

Commit

Permalink
Fixes #1548, #1550
Browse files Browse the repository at this point in the history
  • Loading branch information
jfarcand committed Apr 7, 2014
1 parent 738144b commit ae7e15c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand All @@ -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("}");
}
Expand All @@ -91,22 +88,20 @@ 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.
*/
final ThreadLocal<JSR356Endpoint> endPoint = new ThreadLocal<JSR356Endpoint>();
final ThreadLocal<HandshakeRequest> hRequest = new ThreadLocal<HandshakeRequest>();

public AtmosphereConfigurator(AtmosphereFramework framework, String guessedServletPath) {
public AtmosphereConfigurator(AtmosphereFramework framework) {
this.framework = framework;
this.guessedServletPath = guessedServletPath;
}

public <T> T getEndpointInstance(java.lang.Class<T> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
Expand Down Expand Up @@ -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<String, String> 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 {
Expand Down

0 comments on commit ae7e15c

Please sign in to comment.