Search in sources :

Example 1 with AtmosphereHandler

use of org.atmosphere.cpr.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.contains("{") && targetPath.contains("}")) {
                        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) 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(jakarta.inject.Named) AtmosphereHandler(org.atmosphere.cpr.AtmosphereHandler) Singleton(org.atmosphere.config.service.Singleton) AtmosphereResourceImpl(org.atmosphere.cpr.AtmosphereResourceImpl)

Example 2 with AtmosphereHandler

use of org.atmosphere.cpr.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<? extends AtmosphereInterceptor>[] interceptors = a.interceptors();
        LinkedList<AtmosphereInterceptor> l = new LinkedList<>();
        for (Class<? extends AtmosphereInterceptor> i : interceptors) {
            try {
                AtmosphereInterceptor ai = framework.newClassInstance(AtmosphereInterceptor.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.cpr.AtmosphereInterceptor) AtmosphereHandlerService(org.atmosphere.config.service.AtmosphereHandlerService) AtmosphereHandlerServiceInterceptor(org.atmosphere.config.managed.AtmosphereHandlerServiceInterceptor) AtmosphereHandler(org.atmosphere.cpr.AtmosphereHandler) LinkedList(java.util.LinkedList)

Example 3 with AtmosphereHandler

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

the class AtmosphereServiceProcessor method handle.

@Override
public void handle(AtmosphereFramework framework, Class<Object> annotatedClass) {
    try {
        AtmosphereService a = annotatedClass.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 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.cpr.AtmosphereInterceptor) AtmosphereConfig(org.atmosphere.cpr.AtmosphereConfig) AtmosphereHandler(org.atmosphere.cpr.AtmosphereHandler) AtmosphereResource(org.atmosphere.cpr.AtmosphereResource) AtmosphereServletProcessor(org.atmosphere.cpr.AtmosphereServletProcessor) AtmosphereResourceEvent(org.atmosphere.cpr.AtmosphereResourceEvent) ReflectorServletProcessor(org.atmosphere.handler.ReflectorServletProcessor) LinkedList(java.util.LinkedList)

Example 4 with AtmosphereHandler

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

the class ManagedServiceProcessor method handle.

@Override
public void handle(AtmosphereFramework framework, Class<Object> annotatedClass) {
    try {
        ManagedService a = annotatedClass.getAnnotation(ManagedService.class);
        framework.setBroadcasterCacheClassName(a.broadcasterCache().getName());
        List<AtmosphereInterceptor> l = new LinkedList<>();
        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, annotatedClass);
        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) 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.cpr.AtmosphereInterceptor) BroadcasterConfig(org.atmosphere.cpr.BroadcasterConfig) LinkedList(java.util.LinkedList) ManagedAtmosphereHandler(org.atmosphere.config.managed.ManagedAtmosphereHandler) ManagedAtmosphereHandler(org.atmosphere.config.managed.ManagedAtmosphereHandler) AtmosphereHandler(org.atmosphere.cpr.AtmosphereHandler) BroadcastFilter(org.atmosphere.cpr.BroadcastFilter)

Aggregations

AtmosphereHandler (org.atmosphere.cpr.AtmosphereHandler)4 LinkedList (java.util.LinkedList)3 AtmosphereInterceptor (org.atmosphere.cpr.AtmosphereInterceptor)3 AtmosphereHandlerService (org.atmosphere.config.service.AtmosphereHandlerService)2 Named (jakarta.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 AtmosphereConfig (org.atmosphere.cpr.AtmosphereConfig)1 AtmosphereResource (org.atmosphere.cpr.AtmosphereResource)1 AtmosphereResourceEvent (org.atmosphere.cpr.AtmosphereResourceEvent)1 AtmosphereResourceImpl (org.atmosphere.cpr.AtmosphereResourceImpl)1 AtmosphereServletProcessor (org.atmosphere.cpr.AtmosphereServletProcessor)1 BroadcastFilter (org.atmosphere.cpr.BroadcastFilter)1 BroadcasterConfig (org.atmosphere.cpr.BroadcasterConfig)1 ReflectorServletProcessor (org.atmosphere.handler.ReflectorServletProcessor)1