Search in sources :

Example 1 with JSR356AsyncSupport

use of org.atmosphere.container.JSR356AsyncSupport in project atmosphere by Atmosphere.

the class ContainerInitializer method onStartup.

@Override
public void onStartup(Set<Class<?>> classes, final ServletContext c) {
    c.log("Initializing AtmosphereFramework");
    for (Map.Entry<String, ? extends ServletRegistration> reg : c.getServletRegistrations().entrySet()) {
        String disableSwitchValue = reg.getValue().getInitParameter(ApplicationConfig.DISABLE_ATMOSPHERE_INITIALIZER);
        // check if AtmosphereInitializer is disabled via web.xml see: https://github.com/Atmosphere/atmosphere/issues/1695
        if (Boolean.parseBoolean(disableSwitchValue)) {
            c.log("Container managed initialization disabled for servlet: " + reg.getValue().getName());
            continue;
        }
        if (c.getAttribute(reg.getKey()) == null && IOUtils.isAtmosphere(reg.getValue().getClassName())) {
            final AtmosphereFramework framework = AtmosphereFrameworkInitializer.newAtmosphereFramework(c, false, true);
            // Hack to make jsr356 works. Pretty ugly.
            DefaultAsyncSupportResolver resolver = new DefaultAsyncSupportResolver(framework.getAtmosphereConfig());
            List<Class<? extends AsyncSupport>> l = resolver.detectWebSocketPresent(false, true);
            // Don't use WebLogic Native WebSocket support if JSR356 is available
            int size = c.getServerInfo().toLowerCase().contains("weblogic") ? 1 : 0;
            String s = reg.getValue().getInitParameter(ApplicationConfig.PROPERTY_COMET_SUPPORT);
            boolean force = c.getServerInfo().toLowerCase().contains("glassfish") || c.getServerInfo().toLowerCase().contains("payara");
            if (s != null && s.equals(JSR356AsyncSupport.class.getName())) {
                force = true;
            } else if (s != null) {
                force = false;
            }
            if (force || l.size() == size && resolver.testClassExists(DefaultAsyncSupportResolver.JSR356_WEBSOCKET)) {
                try {
                    framework.setAsyncSupport(new JSR356AsyncSupport(framework.getAtmosphereConfig(), c));
                } catch (IllegalStateException ex) {
                    framework.initializationError(ex);
                }
            }
            try {
                c.addListener(new ServletRequestListener() {

                    @Override
                    public void requestDestroyed(ServletRequestEvent sre) {
                    }

                    @Override
                    public void requestInitialized(ServletRequestEvent sre) {
                        HttpServletRequest r = HttpServletRequest.class.cast(sre.getServletRequest());
                        AtmosphereConfig config = framework.getAtmosphereConfig();
                        if (config.isSupportSession() && Utils.webSocketEnabled(r)) {
                            r.getSession(config.getInitParameter(ApplicationConfig.PROPERTY_SESSION_CREATE, true));
                        }
                    }
                });
            } catch (Throwable t) {
                c.log("AtmosphereFramework : Unable to install WebSocket Session Creator", t);
            }
            try {
                s = c.getInitParameter(PROPERTY_SESSION_SUPPORT);
                if (s != null) {
                    boolean sessionSupport = Boolean.valueOf(s);
                    if (sessionSupport && c.getMajorVersion() > 2) {
                        c.addListener(SessionSupport.class);
                        c.log("AtmosphereFramework : Installed " + SessionSupport.class);
                    }
                }
            } catch (Throwable t) {
                c.log("AtmosphereFramework : SessionSupport error. Make sure you also define {} as a listener in web.xml, see https://github.com/Atmosphere/atmosphere/wiki/Enabling-HttpSession-Support", t);
            }
            c.setAttribute(reg.getKey(), framework);
        }
    }
}
Also used : ServletRequestListener(jakarta.servlet.ServletRequestListener) JSR356AsyncSupport(org.atmosphere.container.JSR356AsyncSupport) JSR356AsyncSupport(org.atmosphere.container.JSR356AsyncSupport) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ServletRequestEvent(jakarta.servlet.ServletRequestEvent) Map(java.util.Map)

Example 2 with JSR356AsyncSupport

use of org.atmosphere.container.JSR356AsyncSupport in project atmosphere by Atmosphere.

the class ContainerInitializer method onStartup.

@Override
public void onStartup(Set<Class<?>> classes, final ServletContext c) throws ServletException {
    c.log("Initializing AtmosphereFramework");
    for (Map.Entry<String, ? extends ServletRegistration> reg : c.getServletRegistrations().entrySet()) {
        String disableSwitchValue = reg.getValue().getInitParameter(ApplicationConfig.DISABLE_ATMOSPHERE_INITIALIZER);
        // check if AtmosphereInitializer is disabled via web.xml see: https://github.com/Atmosphere/atmosphere/issues/1695
        if (Boolean.parseBoolean(disableSwitchValue)) {
            c.log("Container managed initialization disabled for servlet: " + reg.getValue().getName());
            continue;
        }
        if (c.getAttribute(reg.getKey()) == null && IOUtils.isAtmosphere(reg.getValue().getClassName())) {
            final AtmosphereFramework framework = AtmosphereFrameworkInitializer.newAtmosphereFramework(c, false, true);
            // Hack to make jsr356 works. Pretty ugly.
            DefaultAsyncSupportResolver resolver = new DefaultAsyncSupportResolver(framework.getAtmosphereConfig());
            List<Class<? extends AsyncSupport>> l = resolver.detectWebSocketPresent(false, true);
            if (resolver.testClassExists(DefaultAsyncSupportResolver.JSR356_WEBSOCKET)) {
                try {
                    framework.setAsyncSupport(new JSR356AsyncSupport(framework.getAtmosphereConfig(), c));
                } catch (IllegalStateException ex) {
                    framework.initializationError(ex);
                }
            }
            try {
                c.addListener(new ServletRequestListener() {

                    @Override
                    public void requestDestroyed(ServletRequestEvent sre) {
                    }

                    @Override
                    public void requestInitialized(ServletRequestEvent sre) {
                        HttpServletRequest r = HttpServletRequest.class.cast(sre.getServletRequest());
                        AtmosphereConfig config = framework.getAtmosphereConfig();
                        if (config.isSupportSession() && Utils.webSocketEnabled(r)) {
                            r.getSession(config.getInitParameter(ApplicationConfig.PROPERTY_SESSION_CREATE, true));
                        }
                    }
                });
            } catch (Throwable t) {
                c.log("AtmosphereFramework : Unable to install WebSocket Session Creator", t);
            }
            try {
                String s = c.getInitParameter(PROPERTY_SESSION_SUPPORT);
                if (s != null) {
                    boolean sessionSupport = Boolean.valueOf(s);
                    if (sessionSupport && c.getMajorVersion() > 2) {
                        c.addListener(SessionSupport.class);
                        c.log("AtmosphereFramework : Installed " + SessionSupport.class);
                    }
                }
            } catch (Throwable t) {
                c.log("AtmosphereFramework : SessionSupport error. Make sure you also define {} as a listener in web.xml, see https://github.com/Atmosphere/atmosphere/wiki/Enabling-HttpSession-Support", t);
            }
            c.setAttribute(reg.getKey(), framework);
        }
    }
}
Also used : ServletRequestListener(javax.servlet.ServletRequestListener) JSR356AsyncSupport(org.atmosphere.container.JSR356AsyncSupport) JSR356AsyncSupport(org.atmosphere.container.JSR356AsyncSupport) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequestEvent(javax.servlet.ServletRequestEvent) Map(java.util.Map)

Aggregations

Map (java.util.Map)2 JSR356AsyncSupport (org.atmosphere.container.JSR356AsyncSupport)2 ServletRequestEvent (jakarta.servlet.ServletRequestEvent)1 ServletRequestListener (jakarta.servlet.ServletRequestListener)1 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)1 ServletRequestEvent (javax.servlet.ServletRequestEvent)1 ServletRequestListener (javax.servlet.ServletRequestListener)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1