Search in sources :

Example 6 with AtmosphereInterceptor

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);
    }
}
Also used : ManagedService(org.atmosphere.config.service.ManagedService) AtmosphereInterceptor(org.atmosphere.runtime.AtmosphereInterceptor) BroadcasterConfig(org.atmosphere.runtime.BroadcasterConfig) LinkedList(java.util.LinkedList) ManagedAtmosphereHandler(org.atmosphere.config.managed.ManagedAtmosphereHandler) ManagedAtmosphereHandler(org.atmosphere.config.managed.ManagedAtmosphereHandler) AtmosphereHandler(org.atmosphere.runtime.AtmosphereHandler) BroadcastFilter(org.atmosphere.runtime.BroadcastFilter)

Example 7 with AtmosphereInterceptor

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);
    }
}
Also used : AtmosphereInterceptor(org.atmosphere.runtime.AtmosphereInterceptor) WebSocketHandlerService(org.atmosphere.config.service.WebSocketHandlerService) WebSocketProcessor(org.atmosphere.websocket.WebSocketProcessor) LinkedList(java.util.LinkedList)

Example 8 with AtmosphereInterceptor

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);
    }
}
Also used : AtmosphereInterceptor(org.atmosphere.runtime.AtmosphereInterceptor) Action(org.atmosphere.runtime.Action) WebSocketHandlerService(org.atmosphere.config.service.WebSocketHandlerService) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Reader(java.io.Reader) StringReader(java.io.StringReader) IOException(java.io.IOException) AtmosphereRequest(org.atmosphere.runtime.AtmosphereRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) AtmosphereFramework(org.atmosphere.runtime.AtmosphereFramework) AtmosphereResourceImpl(org.atmosphere.runtime.AtmosphereResourceImpl)

Aggregations

AtmosphereInterceptor (org.atmosphere.runtime.AtmosphereInterceptor)8 LinkedList (java.util.LinkedList)5 AtmosphereHandler (org.atmosphere.runtime.AtmosphereHandler)3 IOException (java.io.IOException)2 WebSocketHandlerService (org.atmosphere.config.service.WebSocketHandlerService)2 ReflectorServletProcessor (org.atmosphere.handler.ReflectorServletProcessor)2 AtmosphereConfig (org.atmosphere.runtime.AtmosphereConfig)2 AtmosphereFramework (org.atmosphere.runtime.AtmosphereFramework)2 AtmosphereRequest (org.atmosphere.runtime.AtmosphereRequest)2 AtmosphereResourceEvent (org.atmosphere.runtime.AtmosphereResourceEvent)2 AtmosphereResourceImpl (org.atmosphere.runtime.AtmosphereResourceImpl)2 BroadcastFilter (org.atmosphere.runtime.BroadcastFilter)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 AtmosphereHandlerServiceInterceptor (org.atmosphere.config.managed.AtmosphereHandlerServiceInterceptor)1 ManagedAtmosphereHandler (org.atmosphere.config.managed.ManagedAtmosphereHandler)1 MeteorServiceInterceptor (org.atmosphere.config.managed.MeteorServiceInterceptor)1