Search in sources :

Example 1 with AtmosphereHandler

use of org.atmosphere.runtime.AtmosphereHandler in project atmosphere by Atmosphere.

the class AtmosphereHandlerServiceProcessor method handle.

@Override
public void handle(AtmosphereFramework framework, Class<AtmosphereHandler> annotatedClass) {
    try {
        AtmosphereHandlerService a = annotatedClass.getAnnotation(AtmosphereHandlerService.class);
        atmosphereConfig(a.atmosphereConfig(), framework);
        filters(a.broadcastFilters(), framework);
        Class<?>[] interceptors = a.interceptors();
        LinkedList<AtmosphereInterceptor> l = new LinkedList<AtmosphereInterceptor>();
        for (Class i : interceptors) {
            try {
                AtmosphereInterceptor ai = (AtmosphereInterceptor) framework.newClassInstance(AtmosphereHandler.class, i);
                l.add(ai);
            } catch (Throwable e) {
                logger.warn("", e);
            }
        }
        AtmosphereInterceptor aa = listeners(a.listeners(), framework);
        if (aa != null) {
            l.add(aa);
        }
        if (a.path().contains("{")) {
            l.addFirst(framework.newClassInstance(AtmosphereInterceptor.class, AtmosphereHandlerServiceInterceptor.class));
        }
        framework.sessionSupport(a.supportSession());
        AtmosphereHandler handler = framework.newClassInstance(AtmosphereHandler.class, annotatedClass);
        for (String s : a.properties()) {
            String[] nv = s.split("=");
            IntrospectionUtils.setProperty(handler, nv[0], nv[1]);
            IntrospectionUtils.addProperty(handler, nv[0], nv[1]);
        }
        AnnotationUtil.interceptorsForHandler(framework, Arrays.asList(a.interceptors()), l);
        framework.addAtmosphereHandler(a.path(), handler, broadcaster(framework, a.broadcaster(), a.path()), l);
        framework.setBroadcasterCacheClassName(a.broadcasterCache().getName());
    } catch (Throwable e) {
        logger.warn("", e);
    }
}
Also used : AtmosphereInterceptor(org.atmosphere.runtime.AtmosphereInterceptor) AtmosphereHandlerService(org.atmosphere.config.service.AtmosphereHandlerService) AtmosphereHandlerServiceInterceptor(org.atmosphere.config.managed.AtmosphereHandlerServiceInterceptor) AtmosphereHandler(org.atmosphere.runtime.AtmosphereHandler) LinkedList(java.util.LinkedList)

Example 2 with AtmosphereHandler

use of org.atmosphere.runtime.AtmosphereHandler in project atmosphere by Atmosphere.

the class AtmosphereServiceProcessor method handle.

@Override
public void handle(AtmosphereFramework framework, Class<Object> annotatedClass) {
    try {
        Class<?> aClass = annotatedClass;
        AtmosphereService a = aClass.getAnnotation(AtmosphereService.class);
        framework.setBroadcasterCacheClassName(a.broadcasterCache().getName());
        atmosphereConfig(a.atmosphereConfig(), framework);
        framework.setDefaultBroadcasterClassName(a.broadcaster().getName());
        filters(a.broadcastFilters(), framework);
        LinkedList<AtmosphereInterceptor> l = new LinkedList<AtmosphereInterceptor>();
        AtmosphereInterceptor aa = listeners(a.listeners(), framework);
        if (aa != null) {
            l.add(aa);
        }
        if (!a.servlet().isEmpty()) {
            final ReflectorServletProcessor r = framework.newClassInstance(ReflectorServletProcessor.class, ReflectorServletProcessor.class);
            r.setServletClassName(a.servlet());
            String mapping = a.path();
            AnnotationUtil.interceptorsForHandler(framework, Arrays.asList(a.interceptors()), l);
            if (!a.dispatch()) {
                AtmosphereHandler proxy = new AtmosphereServletProcessor() {

                    private String method = "GET";

                    @Override
                    public void onRequest(AtmosphereResource resource) throws IOException {
                        if (!resource.getRequest().getMethod().equalsIgnoreCase(method)) {
                            r.onRequest(resource);
                        }
                    }

                    @Override
                    public void onStateChange(AtmosphereResourceEvent event) throws IOException {
                        r.onStateChange(event);
                    }

                    @Override
                    public void destroy() {
                        r.destroy();
                    }

                    @Override
                    public void init(AtmosphereConfig config) throws ServletException {
                        String s = config.getInitParameter(ATMOSPHERERESOURCE_INTERCEPTOR_METHOD);
                        if (s != null) {
                            method = s;
                        }
                        r.init(config);
                    }
                };
                framework.addAtmosphereHandler(mapping, proxy, l);
            } else {
                framework.addAtmosphereHandler(mapping, r, l);
            }
        } else {
            interceptors(a.interceptors(), framework);
        }
    } catch (Throwable e) {
        logger.warn("", e);
    }
}
Also used : AtmosphereService(org.atmosphere.config.service.AtmosphereService) AtmosphereInterceptor(org.atmosphere.runtime.AtmosphereInterceptor) AtmosphereConfig(org.atmosphere.runtime.AtmosphereConfig) AtmosphereResource(org.atmosphere.runtime.AtmosphereResource) LinkedList(java.util.LinkedList) AtmosphereHandler(org.atmosphere.runtime.AtmosphereHandler) AtmosphereServletProcessor(org.atmosphere.runtime.AtmosphereServletProcessor) AtmosphereResourceEvent(org.atmosphere.runtime.AtmosphereResourceEvent) ReflectorServletProcessor(org.atmosphere.handler.ReflectorServletProcessor)

Example 3 with AtmosphereHandler

use of org.atmosphere.runtime.AtmosphereHandler in project atmosphere by Atmosphere.

the class AtmosphereHandlerServiceInterceptor method mapAnnotatedService.

protected void mapAnnotatedService(boolean reMap, String path, AtmosphereRequest request, AtmosphereHandlerWrapper w) {
    synchronized (config.handlers()) {
        if (config.handlers().get(path) == null) {
            // AtmosphereHandlerService
            AtmosphereHandlerService m = w.atmosphereHandler.getClass().getAnnotation(AtmosphereHandlerService.class);
            if (m != null) {
                try {
                    String targetPath = m.path();
                    if (targetPath.indexOf("{") != -1 && targetPath.indexOf("}") != -1) {
                        boolean singleton = w.atmosphereHandler.getClass().getAnnotation(Singleton.class) != null;
                        AtmosphereHandler newW = w.atmosphereHandler;
                        if (!singleton) {
                            newW = config.framework().newClassInstance(AtmosphereHandler.class, w.atmosphereHandler.getClass());
                        }
                        request.localAttributes().put(Named.class.getName(), path.substring(targetPath.indexOf("{")));
                        AtmosphereResourceImpl.class.cast(request.resource()).atmosphereHandler(newW);
                        config.framework().addAtmosphereHandler(path, newW, config.getBroadcasterFactory().lookup(w.broadcaster.getClass(), path, true), w.interceptors);
                        request.setAttribute(FrameworkConfig.NEW_MAPPING, "true");
                    }
                } catch (Throwable e) {
                    logger.warn("Unable to create AtmosphereHandler", e);
                }
            }
        }
    }
}
Also used : AtmosphereHandlerService(org.atmosphere.config.service.AtmosphereHandlerService) Named(javax.inject.Named) AtmosphereHandler(org.atmosphere.runtime.AtmosphereHandler) Singleton(org.atmosphere.config.service.Singleton) AtmosphereResourceImpl(org.atmosphere.runtime.AtmosphereResourceImpl)

Example 4 with AtmosphereHandler

use of org.atmosphere.runtime.AtmosphereHandler 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)

Aggregations

AtmosphereHandler (org.atmosphere.runtime.AtmosphereHandler)4 LinkedList (java.util.LinkedList)3 AtmosphereInterceptor (org.atmosphere.runtime.AtmosphereInterceptor)3 AtmosphereHandlerService (org.atmosphere.config.service.AtmosphereHandlerService)2 Named (javax.inject.Named)1 AtmosphereHandlerServiceInterceptor (org.atmosphere.config.managed.AtmosphereHandlerServiceInterceptor)1 ManagedAtmosphereHandler (org.atmosphere.config.managed.ManagedAtmosphereHandler)1 AtmosphereService (org.atmosphere.config.service.AtmosphereService)1 ManagedService (org.atmosphere.config.service.ManagedService)1 Singleton (org.atmosphere.config.service.Singleton)1 ReflectorServletProcessor (org.atmosphere.handler.ReflectorServletProcessor)1 AtmosphereConfig (org.atmosphere.runtime.AtmosphereConfig)1 AtmosphereResource (org.atmosphere.runtime.AtmosphereResource)1 AtmosphereResourceEvent (org.atmosphere.runtime.AtmosphereResourceEvent)1 AtmosphereResourceImpl (org.atmosphere.runtime.AtmosphereResourceImpl)1 AtmosphereServletProcessor (org.atmosphere.runtime.AtmosphereServletProcessor)1 BroadcastFilter (org.atmosphere.runtime.BroadcastFilter)1 BroadcasterConfig (org.atmosphere.runtime.BroadcasterConfig)1