Search in sources :

Example 1 with ReflectorServletProcessor

use of org.atmosphere.handler.ReflectorServletProcessor in project atmosphere by Atmosphere.

the class AtmosphereFramework method detectSupportedFramework.

/**
     * Auto-detect Jersey when no atmosphere.xml file is specified.
     *
     * @param sc {@link ServletConfig}
     * @return true if Jersey classes are detected
     * @throws ClassNotFoundException
     */
protected boolean detectSupportedFramework(ServletConfig sc) throws Exception {
    String broadcasterClassNameTmp = null;
    boolean isJersey = false;
    try {
        IOUtils.loadClass(getClass(), JERSEY_CONTAINER);
        isJersey = true;
        if (!isBroadcasterSpecified) {
            broadcasterClassNameTmp = lookupDefaultBroadcasterType(JERSEY_BROADCASTER);
            IOUtils.loadClass(getClass(), broadcasterClassNameTmp);
        }
        useStreamForFlushingComments = true;
        StringBuilder packagesInit = new StringBuilder();
        for (String s : packages) {
            packagesInit.append(s).append(",");
        }
        initParams.put(FrameworkConfig.JERSEY_SCANNING_PACKAGE, packagesInit.toString());
    } catch (Throwable t) {
        logger.trace("", t);
        return false;
    }
    logger.debug("Missing META-INF/atmosphere.xml but found the Jersey runtime. Starting Jersey");
    // Atmosphere 1.1 : could add regressions
    // Jersey will itself handle the headers.
    //initParams.put(WRITE_HEADERS, "false");
    ReflectorServletProcessor rsp = newClassInstance(ReflectorServletProcessor.class, ReflectorServletProcessor.class);
    if (broadcasterClassNameTmp != null)
        broadcasterClassName = broadcasterClassNameTmp;
    configureDetectedFramework(rsp, isJersey);
    sessionSupport(false);
    initParams.put(DISABLE_ONSTATE_EVENT, "true");
    String mapping = sc.getInitParameter(ATMOSPHERE_HANDLER_MAPPING);
    if (mapping == null) {
        mapping = Broadcaster.ROOT_MASTER;
    }
    Class<? extends Broadcaster> bc = (Class<? extends Broadcaster>) IOUtils.loadClass(getClass(), broadcasterClassName);
    broadcasterFactory.destroy();
    broadcasterFactory = newClassInstance(BroadcasterFactory.class, DefaultBroadcasterFactory.class);
    broadcasterFactory.configure(bc, broadcasterLifeCyclePolicy, config);
    for (BroadcasterListener b : broadcasterListeners) {
        broadcasterFactory.addBroadcasterListener(b);
    }
    Broadcaster b;
    try {
        b = broadcasterFactory.get(bc, mapping);
    } catch (IllegalStateException ex) {
        logger.warn("Two Broadcaster's named {}. Renaming the second one to {}", mapping, sc.getServletName() + mapping);
        b = broadcasterFactory.get(bc, sc.getServletName() + mapping);
    }
    addAtmosphereHandler(mapping, rsp, b);
    return true;
}
Also used : ReflectorServletProcessor(org.atmosphere.handler.ReflectorServletProcessor)

Example 2 with ReflectorServletProcessor

use of org.atmosphere.handler.ReflectorServletProcessor in project atmosphere by Atmosphere.

the class AtmosphereResourceTest method verifyTestCompletionAwareForSync.

private void verifyTestCompletionAwareForSync(boolean aware) throws IOException, ServletException {
    Servlet s = mock(Servlet.class);
    if (aware) {
        framework.addInitParameter(ApplicationConfig.RESPONSE_COMPLETION_AWARE, "true");
    }
    ReflectorServletProcessor handler = new ReflectorServletProcessor(s);
    handler.init(framework.getAtmosphereConfig());
    AtmosphereRequest request = new AtmosphereRequestImpl.Builder().pathInfo("/a").build();
    AtmosphereResponseImpl response = mock(AtmosphereResponseImpl.class);
    AtmosphereResourceImpl res = new AtmosphereResourceImpl();
    res.initialize(framework.getAtmosphereConfig(), framework.getBroadcasterFactory().get(), request, response, null, null);
    res.transport(AtmosphereResource.TRANSPORT.WEBSOCKET);
    request.setAttribute(FrameworkConfig.ATMOSPHERE_RESOURCE, res);
    request.setAttribute(FrameworkConfig.INJECTED_ATMOSPHERE_RESOURCE, res);
    handler.onRequest(res);
    verify(response, times(aware ? 1 : 0)).onComplete();
}
Also used : Servlet(javax.servlet.Servlet) ReflectorServletProcessor(org.atmosphere.handler.ReflectorServletProcessor)

Example 3 with ReflectorServletProcessor

use of org.atmosphere.handler.ReflectorServletProcessor in project atmosphere by Atmosphere.

the class MeteorServiceInterceptor method mapAnnotatedService.

protected void mapAnnotatedService(boolean reMap, String path, AtmosphereRequest request, AtmosphereFramework.AtmosphereHandlerWrapper w) {
    synchronized (config.handlers()) {
        if (config.handlers().get(path) == null) {
            // MeteorService
            if (ReflectorServletProcessor.class.isAssignableFrom(w.atmosphereHandler.getClass())) {
                ReflectorServletProcessor r = (ReflectorServletProcessor) w.atmosphereHandler;
                Servlet s = r.getServlet();
                if (s == null) {
                    logger.warn("Invalid ReflectorServletProcessor state. Servlet is null");
                    return;
                }
                MeteorService m = s.getClass().getAnnotation(MeteorService.class);
                if (m != null) {
                    String targetPath = m.path();
                    if (targetPath.contains("{") && targetPath.contains("}")) {
                        try {
                            boolean singleton = s.getClass().getAnnotation(Singleton.class) != null;
                            if (!singleton) {
                                r = config.framework().newClassInstance(ReflectorServletProcessor.class, ReflectorServletProcessor.class);
                                r.setServlet(config.framework().newClassInstance(Servlet.class, s.getClass()));
                                r.init(config);
                            }
                            request.localAttributes().put(Named.class.getName(), path.substring(targetPath.indexOf("{")));
                            ((AtmosphereResourceImpl) request.resource()).atmosphereHandler(r);
                            config.framework().addAtmosphereHandler(path, r, 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);
                        }
                    }
                }
            }
        } else if (reMap) {
            request.setAttribute(FrameworkConfig.NEW_MAPPING, "true");
        }
    }
}
Also used : MeteorService(org.atmosphere.config.service.MeteorService) Named(jakarta.inject.Named) Singleton(org.atmosphere.config.service.Singleton) Servlet(jakarta.servlet.Servlet) ReflectorServletProcessor(org.atmosphere.handler.ReflectorServletProcessor) AtmosphereResourceImpl(org.atmosphere.cpr.AtmosphereResourceImpl)

Example 4 with ReflectorServletProcessor

use of org.atmosphere.handler.ReflectorServletProcessor in project atmosphere by Atmosphere.

the class MeteorServiceProcessor method handle.

@Override
public void handle(AtmosphereFramework framework, Class<Servlet> annotatedClass) {
    try {
        ReflectorServletProcessor r = framework.newClassInstance(ReflectorServletProcessor.class, ReflectorServletProcessor.class);
        r.setServletClassName(annotatedClass.getName());
        LinkedList<AtmosphereInterceptor> l = new LinkedList<>();
        MeteorService m = annotatedClass.getAnnotation(MeteorService.class);
        framework.setBroadcasterCacheClassName(m.broadcasterCache().getName());
        String mapping = m.path();
        atmosphereConfig(m.atmosphereConfig(), framework);
        filters(m.broadcastFilters(), framework);
        AtmosphereInterceptor aa = listeners(m.listeners(), framework);
        if (aa != null) {
            l.add(aa);
        }
        AnnotationUtil.interceptorsForHandler(framework, Arrays.asList(m.interceptors()), l);
        if (m.path().contains("{")) {
            l.addFirst(framework.newClassInstance(AtmosphereInterceptor.class, MeteorServiceInterceptor.class));
        }
        framework.addAtmosphereHandler(mapping, r, broadcaster(framework, m.broadcaster(), m.path()), l);
    } catch (Throwable e) {
        logger.warn("", e);
    }
}
Also used : AtmosphereInterceptor(org.atmosphere.cpr.AtmosphereInterceptor) MeteorService(org.atmosphere.config.service.MeteorService) ReflectorServletProcessor(org.atmosphere.handler.ReflectorServletProcessor) MeteorServiceInterceptor(org.atmosphere.config.managed.MeteorServiceInterceptor) LinkedList(java.util.LinkedList)

Example 5 with ReflectorServletProcessor

use of org.atmosphere.handler.ReflectorServletProcessor 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)

Aggregations

ReflectorServletProcessor (org.atmosphere.handler.ReflectorServletProcessor)13 Test (org.testng.annotations.Test)4 Servlet (jakarta.servlet.Servlet)3 HttpServlet (jakarta.servlet.http.HttpServlet)2 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)2 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)2 LinkedList (java.util.LinkedList)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Servlet (javax.servlet.Servlet)2 MeteorService (org.atmosphere.config.service.MeteorService)2 AtmosphereInterceptor (org.atmosphere.cpr.AtmosphereInterceptor)2 Named (jakarta.inject.Named)1 Filter (jakarta.servlet.Filter)1 AsyncContext (javax.servlet.AsyncContext)1 MeteorServiceInterceptor (org.atmosphere.config.managed.MeteorServiceInterceptor)1 AtmosphereService (org.atmosphere.config.service.AtmosphereService)1 Singleton (org.atmosphere.config.service.Singleton)1 AtmosphereConfig (org.atmosphere.cpr.AtmosphereConfig)1 AtmosphereHandler (org.atmosphere.cpr.AtmosphereHandler)1 AtmosphereResource (org.atmosphere.cpr.AtmosphereResource)1