Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
jfarcand committed Oct 27, 2015
1 parent 1b43b53 commit 879c4e5
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,11 @@ public AtmosphereFramework addAtmosphereHandler(String mapping, AtmosphereHandle
}

private AtmosphereFramework addMapping(String path, AtmosphereHandlerWrapper w) {
atmosphereHandlers.put(normalizePath(path), w);
return this;
}

public String normalizePath(String path) {
// We are using JAXRS mapping algorithm.
if (path.contains("*")) {
path = path.replace("*", mappingRegex);
Expand All @@ -671,9 +676,7 @@ private AtmosphereFramework addMapping(String path, AtmosphereHandlerWrapper w)
if (path.endsWith("/")) {
path = path + mappingRegex;
}

atmosphereHandlers.put(path, w);
return this;
return path;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.atmosphere.cpr.AsynchronousProcessor;
import org.atmosphere.cpr.AtmosphereConfig;
import org.atmosphere.cpr.AtmosphereFramework;
import org.atmosphere.cpr.AtmosphereInterceptor;
import org.atmosphere.cpr.AtmosphereMappingException;
import org.atmosphere.cpr.AtmosphereRequest;
import org.atmosphere.cpr.AtmosphereResource;
Expand Down Expand Up @@ -379,35 +380,48 @@ private void invokeInterceptors(WebSocketHandlerProxy webSocketHandler,
request.body(new ByteArrayInputStream((byte[]) webSocketMessageAsBody, offset, length));
}

// Globally defined
int tracing = 0;
Action a = asynchronousProcessor.invokeInterceptors(framework.interceptors(), resource, tracing);
if (a.type() != Action.TYPE.CONTINUE && a.type() != Action.TYPE.SKIP_ATMOSPHEREHANDLER) {
return;
String path = webSocketHandler.proxied.getClass().isAnnotationPresent(WebSocketHandlerService.class) ?
webSocketHandler.proxied.getClass().getAnnotation(WebSocketHandlerService.class).path() : "/";

AtmosphereFramework.AtmosphereHandlerWrapper w = framework.getAtmosphereHandlers().get(framework.normalizePath(path));
List<AtmosphereInterceptor> l;
if (w == null) {
l = framework.interceptors();
} else {
l = w.interceptors;
}

//Unit test mock the request and will throw NPE.
boolean skipAtmosphereHandler = request.getAttribute(SKIP_ATMOSPHEREHANDLER.name()) != null
? (Boolean) request.getAttribute(SKIP_ATMOSPHEREHANDLER.name()) : Boolean.FALSE;
if (!skipAtmosphereHandler) {
try {
if (String.class.isAssignableFrom(webSocketMessageAsBody.getClass())) {
webSocketHandler.onTextMessage(webSocket, String.class.cast(webSocketMessageAsBody));
} else if (Reader.class.isAssignableFrom(webSocketMessageAsBody.getClass())) {
WebSocketStreamingHandler.class.cast(webSocketHandler.proxied).onTextStream(webSocket, Reader.class.cast(webSocketMessageAsBody));
} else if (InputStream.class.isAssignableFrom(webSocketMessageAsBody.getClass())) {
WebSocketStreamingHandler.class.cast(webSocketHandler.proxied()).onBinaryStream(webSocket, InputStream.class.cast(webSocketMessageAsBody));
} else {
webSocketHandler.onByteMessage(webSocket, (byte[]) webSocketMessageAsBody, offset, length);
try {
// Globally defined
int tracing = 0;
Action a = asynchronousProcessor.invokeInterceptors(l, resource, tracing);
if (a.type() != Action.TYPE.CONTINUE && a.type() != Action.TYPE.SKIP_ATMOSPHEREHANDLER) {
return;
}

//Unit test mock the request and will throw NPE.
boolean skipAtmosphereHandler = request.getAttribute(SKIP_ATMOSPHEREHANDLER.name()) != null
? (Boolean) request.getAttribute(SKIP_ATMOSPHEREHANDLER.name()) : Boolean.FALSE;
if (!skipAtmosphereHandler) {
try {
if (String.class.isAssignableFrom(webSocketMessageAsBody.getClass())) {
webSocketHandler.onTextMessage(webSocket, String.class.cast(webSocketMessageAsBody));
} else if (Reader.class.isAssignableFrom(webSocketMessageAsBody.getClass())) {
WebSocketStreamingHandler.class.cast(webSocketHandler.proxied).onTextStream(webSocket, Reader.class.cast(webSocketMessageAsBody));
} else if (InputStream.class.isAssignableFrom(webSocketMessageAsBody.getClass())) {
WebSocketStreamingHandler.class.cast(webSocketHandler.proxied()).onBinaryStream(webSocket, InputStream.class.cast(webSocketMessageAsBody));
} else {
webSocketHandler.onByteMessage(webSocket, (byte[]) webSocketMessageAsBody, offset, length);
}
} catch (IOException t) {
resource.onThrowable(t);
throw t;
}
} catch (IOException t) {
resource.onThrowable(t);
throw t;
}
request.setAttribute(SKIP_ATMOSPHEREHANDLER.name(), Boolean.FALSE);
} finally {
asynchronousProcessor.postInterceptors(l, resource);
}
request.setAttribute(SKIP_ATMOSPHEREHANDLER.name(), Boolean.FALSE);

asynchronousProcessor.postInterceptors(framework.interceptors(), resource);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.atmosphere.cpr.AsynchronousProcessor;
import org.atmosphere.cpr.AtmosphereConfig;
import org.atmosphere.cpr.AtmosphereFramework;
import org.atmosphere.cpr.AtmosphereInterceptor;
import org.atmosphere.cpr.AtmosphereInterceptorAdapter;
import org.atmosphere.cpr.AtmosphereRequest;
import org.atmosphere.cpr.AtmosphereResource;
Expand Down Expand Up @@ -76,6 +77,7 @@
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertSame;
import static org.testng.Assert.assertTrue;

public class ManagedAtmosphereHandlerTest {
private AtmosphereFramework framework;
Expand Down Expand Up @@ -659,7 +661,33 @@ public void testPostConstruct() throws IOException, ServletException {

}

@WebSocketHandlerService(path = "/websocketfactory")
public final static class Interceptor implements AtmosphereInterceptor {

public static boolean invoked = false;

@Override
public Action inspect(AtmosphereResource r) {
invoked = true;
return Action.CONTINUE;
}

@Override
public void postInspect(AtmosphereResource r) {

}

@Override
public void destroy() {

}

@Override
public void configure(AtmosphereConfig config) {

}
}

@WebSocketHandlerService(path = "/websocketfactory", interceptors = {Interceptor.class })
public final static class WebSocketfactoryTest extends WebSocketHandlerAdapter {

@Inject
Expand All @@ -684,5 +712,7 @@ public void testWebSocketFactory() throws IOException, ServletException {
processor.open(w, request, AtmosphereResponse.newInstance(framework.getAtmosphereConfig(), request, w));

assertNotNull(r.get());
assertTrue(Interceptor.invoked);

}
}

0 comments on commit 879c4e5

Please sign in to comment.