use of org.atmosphere.runtime.AtmosphereInterceptor in project atmosphere by Atmosphere.
the class ManagedServiceProcessor method handle.
@Override
public void handle(AtmosphereFramework framework, Class<Object> annotatedClass) {
try {
Class<?> aClass = annotatedClass;
ManagedService a = aClass.getAnnotation(ManagedService.class);
framework.setBroadcasterCacheClassName(a.broadcasterCache().getName());
List<AtmosphereInterceptor> l = new LinkedList<AtmosphereInterceptor>();
AnnotationUtil.defaultManagedServiceInterceptors(framework, l);
atmosphereConfig(a.atmosphereConfig(), framework);
filters(a.broadcastFilters(), framework);
AtmosphereInterceptor aa = listeners(a.listeners(), framework);
if (aa != null) {
l.add(aa);
}
Object c = framework.newClassInstance(Object.class, aClass);
AtmosphereHandler handler = framework.newClassInstance(ManagedAtmosphereHandler.class, ManagedAtmosphereHandler.class).configure(framework.getAtmosphereConfig(), c);
framework.filterManipulator(new BroadcasterConfig.FilterManipulator() {
@Override
public Object unwrap(Object o) {
if (o != null && ManagedAtmosphereHandler.Managed.class.isAssignableFrom(o.getClass())) {
o = ManagedAtmosphereHandler.Managed.class.cast(o).object();
}
return o;
}
@Override
public BroadcastFilter.BroadcastAction wrap(BroadcastFilter.BroadcastAction a, boolean wasWrapped) {
if (wasWrapped) {
return new BroadcastFilter.BroadcastAction(a.action(), new ManagedAtmosphereHandler.Managed(a.message()));
} else {
return a;
}
}
});
AnnotationUtil.interceptorsForManagedService(framework, Arrays.asList(a.interceptors()), l);
framework.addAtmosphereHandler(a.path(), handler, broadcaster(framework, a.broadcaster(), a.path()), l);
} catch (Throwable e) {
logger.warn("", e);
}
}
use of org.atmosphere.runtime.AtmosphereInterceptor in project atmosphere by Atmosphere.
the class WebSocketHandlerServiceProcessor method handle.
@Override
public void handle(AtmosphereFramework framework, Class<WebSocketHandler> annotatedClass) {
try {
WebSocketHandlerService m = annotatedClass.getAnnotation(WebSocketHandlerService.class);
atmosphereConfig(m.atmosphereConfig(), framework);
framework.addAtmosphereHandler(m.path(), AtmosphereFramework.REFLECTOR_ATMOSPHEREHANDLER).initWebSocket();
framework.setDefaultBroadcasterClassName(m.broadcaster().getName());
filters(m.broadcastFilters(), framework);
final LinkedList<AtmosphereInterceptor> l = new LinkedList<AtmosphereInterceptor>();
AtmosphereInterceptor aa = listeners(m.listeners(), framework);
if (aa != null) {
l.add(aa);
}
AnnotationUtil.interceptorsForHandler(framework, Arrays.asList(m.interceptors()), l);
framework.setBroadcasterCacheClassName(m.broadcasterCache().getName());
WebSocketProcessor p = WebSocketProcessorFactory.getDefault().getWebSocketProcessor(framework);
framework.addAtmosphereHandler(m.path(), REFLECTOR_ATMOSPHEREHANDLER, l);
p.registerWebSocketHandler(m.path(), new WebSocketProcessor.WebSocketHandlerProxy(broadcasterClass(framework, m.broadcaster()), framework.newClassInstance(WebSocketHandler.class, annotatedClass)));
} catch (Throwable e) {
logger.warn("", e);
}
}
use of org.atmosphere.runtime.AtmosphereInterceptor in project atmosphere by Atmosphere.
the class DefaultWebSocketProcessor method invokeInterceptors.
private void invokeInterceptors(WebSocketHandlerProxy webSocketHandler, WebSocket webSocket, Object webSocketMessageAsBody, int offset, int length) throws IOException {
AtmosphereResourceImpl resource = AtmosphereResourceImpl.class.cast(webSocket.resource());
if (resource == null) {
return;
}
AtmosphereRequest request = resource.getRequest(false);
if (String.class.isAssignableFrom(webSocketMessageAsBody.getClass())) {
request.body(String.class.cast(webSocketMessageAsBody));
} else if (Reader.class.isAssignableFrom(webSocketMessageAsBody.getClass())) {
request.body(Reader.class.cast(webSocketMessageAsBody));
} else if (InputStream.class.isAssignableFrom(webSocketMessageAsBody.getClass())) {
request.body(InputStream.class.cast(webSocketMessageAsBody));
} else {
request.body(new ByteArrayInputStream((byte[]) webSocketMessageAsBody, offset, length));
}
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;
}
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;
}
}
request.setAttribute(SKIP_ATMOSPHEREHANDLER.name(), Boolean.FALSE);
} finally {
asynchronousProcessor.postInterceptors(l, resource);
}
}
Aggregations