Example usage for javax.servlet.http HttpSessionEvent getSession

List of usage examples for javax.servlet.http HttpSessionEvent getSession

Introduction

In this page you can find the example usage for javax.servlet.http HttpSessionEvent getSession.

Prototype

public HttpSession getSession() 

Source Link

Document

Return the session that changed.

Usage

From source file:org.red5.net.websocket.WebSocketPlugin.java

/**
 * Returns a new instance of WsServerContainer if one does not already exist.
 * /*w w w .  j av a 2s  .c o m*/
 * @param servletContext
 * @return WsServerContainer
 */
public static ServerContainer getWsServerContainerInstance(ServletContext servletContext) {
    String path = servletContext.getContextPath();
    // handle root
    if (path.length() == 0) {
        path = "/";
    }
    log.info("getWsServerContainerInstance: {}", path);
    DefaultWsServerContainer container;
    if (containerMap.containsKey(path)) {
        container = containerMap.get(path);
    } else {
        // instance a server container for WS
        container = new DefaultWsServerContainer(servletContext);
        if (log.isDebugEnabled()) {
            log.debug("Attributes: {} params: {}", Collections.list(servletContext.getAttributeNames()),
                    Collections.list(servletContext.getInitParameterNames()));
        }
        // get a configurator instance
        ServerEndpointConfig.Configurator configurator = (ServerEndpointConfig.Configurator) WebSocketPlugin
                .getWsConfiguratorInstance();
        // check for sub protocols
        log.debug("Checking for subprotocols");
        List<String> subProtocols = new ArrayList<>();
        Optional<Object> subProtocolsAttr = Optional
                .ofNullable(servletContext.getInitParameter("subProtocols"));
        if (subProtocolsAttr.isPresent()) {
            String attr = (String) subProtocolsAttr.get();
            log.debug("Subprotocols: {}", attr);
            if (StringUtils.isNotBlank(attr)) {
                if (attr.contains(",")) {
                    // split them up
                    Stream.of(attr.split(",")).forEach(entry -> {
                        subProtocols.add(entry);
                    });
                } else {
                    subProtocols.add(attr);
                }
            }
        } else {
            // default to allowing any subprotocol
            subProtocols.add("*");
        }
        log.debug("Checking for CORS");
        // check for allowed origins override in this servlet context
        Optional<Object> crossOpt = Optional.ofNullable(servletContext.getAttribute("crossOriginPolicy"));
        if (crossOpt.isPresent() && Boolean.valueOf((String) crossOpt.get())) {
            Optional<String> opt = Optional.ofNullable((String) servletContext.getAttribute("allowedOrigins"));
            if (opt.isPresent()) {
                ((DefaultServerEndpointConfigurator) configurator).setAllowedOrigins(opt.get().split(","));
            }
        }
        log.debug("Checking for endpoint override");
        // check for endpoint override and use default if not configured
        String wsEndpointClass = Optional.ofNullable((String) servletContext.getAttribute("wsEndpointClass"))
                .orElse("org.red5.net.websocket.server.DefaultWebSocketEndpoint");
        try {
            // locate the endpoint class
            Class<?> endpointClass = Class.forName(wsEndpointClass);
            log.debug("startWebSocket - endpointPath: {} endpointClass: {}", path, endpointClass);
            // build an endpoint config
            ServerEndpointConfig serverEndpointConfig = ServerEndpointConfig.Builder.create(endpointClass, path)
                    .configurator(configurator).subprotocols(subProtocols).build();
            // set the endpoint on the server container
            container.addEndpoint(serverEndpointConfig);
        } catch (Throwable t) {
            log.warn("WebSocket endpoint setup exception", t);
        }
        // store container for lookup
        containerMap.put(path, container);
        // add session listener
        servletContext.addListener(new HttpSessionListener() {

            @Override
            public void sessionCreated(HttpSessionEvent se) {
                log.debug("sessionCreated: {}", se.getSession().getId());
                ServletContext sc = se.getSession().getServletContext();
                // Don't trigger WebSocket initialization if a WebSocket Server Container is already present
                if (sc.getAttribute(Constants.SERVER_CONTAINER_SERVLET_CONTEXT_ATTRIBUTE) == null) {
                    // grab the container using the servlet context for lookup
                    DefaultWsServerContainer serverContainer = (DefaultWsServerContainer) WebSocketPlugin
                            .getWsServerContainerInstance(sc);
                    // set the container to the context for lookup
                    sc.setAttribute(Constants.SERVER_CONTAINER_SERVLET_CONTEXT_ATTRIBUTE, serverContainer);
                }
            }

            @Override
            public void sessionDestroyed(HttpSessionEvent se) {
                log.debug("sessionDestroyed: {}", se);
                container.closeAuthenticatedSession(se.getSession().getId());
            }

        });
    }
    // set the container to the context for lookup
    servletContext.setAttribute(Constants.SERVER_CONTAINER_SERVLET_CONTEXT_ATTRIBUTE, container);
    return container;
}

From source file:com.boundlessgeo.geoserver.AppSessionDebugger.java

@Override
public void sessionDestroyed(HttpSessionEvent se) {
    sessions.remove(se.getSession().getId());
}

From source file:com.boundlessgeo.geoserver.AppSessionDebugger.java

@Override
public void sessionCreated(HttpSessionEvent se) {
    HttpSession session = se.getSession();
    sessions.put(session.getId(), session);
}

From source file:org.araneaframework.servlet.core.StandardSessionListener.java

public void sessionDestroyed(HttpSessionEvent sessEvent) {
    if (sessEvent.getSession().getAttribute(StandardServletSessionRouterService.SESSION_SERVICE_KEY) != null) {
        StandardRelocatableServiceDecorator service = (StandardRelocatableServiceDecorator) sessEvent
                .getSession().getAttribute(StandardServletSessionRouterService.SESSION_SERVICE_KEY);

        try {//from  w  ww.j  a v a2s.  co  m
            service._getRelocatable().overrideEnvironment(new StandardEnvironment(null, new HashMap()));
            service._getComponent().destroy();
        } catch (Exception e) {
            log.error("Exception while destroying service in an expired session", e);
        }
    }
    log.debug("Session " + sessEvent.getSession().getId() + " destroyed");
}

From source file:be.fedict.eid.applet.beta.webapp.SessionContextListener.java

public void sessionDestroyed(HttpSessionEvent event) {
    HttpSession session = event.getSession();
    String sessionId = session.getId();
    LOG.debug("session destroyed: " + sessionId);
    this.sessionContextManager.deactivateSessionContext(sessionId);
}

From source file:be.fedict.eid.applet.beta.webapp.SessionContextListener.java

public void sessionCreated(HttpSessionEvent event) {
    HttpSession session = event.getSession();
    String sessionId = session.getId();
    LOG.debug("session created: " + sessionId);
    int contextId = this.sessionContextManager.getSessionContextId(sessionId);
    LOG.debug("context Id: " + contextId);
}

From source file:org.openmrs.web.SessionListener.java

/**
 * Called whenever a session times out or a user logs out (and so the session is closed)
 *
 * @see HttpSessionListener#sessionDestroyed(HttpSessionEvent)
 *///from  w  ww.j  a v a2s .c o  m
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
    CurrentUsers.removeUser(httpSessionEvent.getSession());
}

From source file:de.berlios.gpon.wui.session.InitSessionListener.java

public void sessionCreated(HttpSessionEvent event) {

    HttpSession session = event.getSession();

    log.debug("Got session: " + session.getId());

    SessionUtility su = new SessionUtility(session);

    // Dunno what to do, but su does
    su.init();/*from w w w .  j  a  va  2 s.c om*/
}

From source file:org.araneaframework.servlet.core.StandardSessionListener.java

public void sessionCreated(HttpSessionEvent sessEvent) {
    log.debug("Session '" + sessEvent.getSession().getId() + "' created");
}

From source file:org.apache.struts2.showcase.chat.ChatSessionListener.java

public void sessionDestroyed(HttpSessionEvent event) {
    HttpSession session = event.getSession();
    WebApplicationContext context = WebApplicationContextUtils
            .getWebApplicationContext(session.getServletContext());
    if (context != null) {
        User user = (User) session.getAttribute(ChatInterceptor.CHAT_USER_SESSION_KEY);
        if (user != null) {
            ChatService service = (ChatService) context.getBean("chatService");
            service.logout(user.getName());

            _log.info("session expired, logged user [" + user.getName() + "] out");
        }//from  w  w w.j  a va2s .c  o m
    }
}