javax.servlet.http.HttpSessionIdListener Java Examples

The following examples show how to use javax.servlet.http.HttpSessionIdListener. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DefaultWebApplication.java    From piranha with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Add the listener.
 *
 * @param <T> the type.
 * @param listener the listener
 */
@Override
public <T extends EventListener> void addListener(T listener) {
    if (listener instanceof ServletContextListener) {
        contextListeners.add((ServletContextListener) listener);
    }
    if (listener instanceof ServletContextAttributeListener) {
        contextAttributeListeners.add((ServletContextAttributeListener) listener);
    }
    if (listener instanceof ServletRequestListener) {
        requestListeners.add((ServletRequestListener) listener);
    }
    if (listener instanceof ServletRequestAttributeListener) {
        httpRequestManager.addListener((ServletRequestAttributeListener) listener);
    }
    if (listener instanceof HttpSessionAttributeListener) {
        httpSessionManager.addListener(listener);
    }
    if (listener instanceof HttpSessionIdListener) {
        httpSessionManager.addListener(listener);
    }
    if (listener instanceof HttpSessionListener) {
        httpSessionManager.addListener(listener);
    }
}
 
Example #2
Source File: DefaultWebApplication.java    From piranha with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Create the listener.
 *
 * @param <T> the type.
 * @param clazz the class of the listener to create.
 * @return the listener.
 * @throws ServletException when it fails to create the listener.
 */
@Override
public <T extends EventListener> T createListener(Class<T> clazz) throws ServletException {
    T result = objectInstanceManager.createListener(clazz);
    boolean ok = false;
    if (result instanceof ServletContextListener || result instanceof ServletContextAttributeListener || result instanceof ServletRequestListener
            || result instanceof ServletRequestAttributeListener || result instanceof HttpSessionAttributeListener
            || result instanceof HttpSessionIdListener || result instanceof HttpSessionListener) {
        ok = true;
    }

    if (!ok) {
        LOGGER.log(WARNING, "Unable to create listener: {0}", clazz);
        throw new IllegalArgumentException("Invalid type");
    }

    return result;
}
 
Example #3
Source File: SessionHelpers.java    From HttpSessionReplacer with MIT License 6 votes vote down vote up
/**
 * This method is used by injected code to register listeners for {@link ServletContext}. If object argument is a
 * {@link ServletContext} and listener argument contains {@link HttpSessionListener} or
 * {@link HttpSessionAttributeListener}, the method will add them to list of known listeners associated to
 * {@link ServletContext}
 *
 * @param servletContext
 *          the active servlet context
 * @param listener
 *          the listener to use
 */
public void onAddListener(ServletContext servletContext, Object listener) {
  String contextPath = servletContext.getContextPath();
  ServletContextDescriptor scd = getDescriptor(servletContext);
  logger.debug("Registering listener {} for context {}", listener, contextPath);
  // As theoretically one class can implement many listener interfaces we
  // check if it implements each of supported ones
  if (listener instanceof HttpSessionListener) {
    scd.addHttpSessionListener((HttpSessionListener)listener);
  }
  if (listener instanceof HttpSessionAttributeListener) {
    scd.addHttpSessionAttributeListener((HttpSessionAttributeListener)listener);
  }
  if (ServletLevel.isServlet31) {
    // Guard the code inside block to avoid use of classes
    // that are not available in versions before Servlet 3.1
    if (listener instanceof HttpSessionIdListener) { // NOSONAR
      scd.addHttpSessionIdListener((HttpSessionIdListener)listener);
    }
  }
}
 
Example #4
Source File: WebContext.java    From tomee with Apache License 2.0 6 votes vote down vote up
private static boolean isWeb(final Class<?> beanClass) {
    if (Servlet.class.isAssignableFrom(beanClass)
        || Filter.class.isAssignableFrom(beanClass)) {
        return true;
    }
    if (EventListener.class.isAssignableFrom(beanClass)) {
        return HttpSessionAttributeListener.class.isAssignableFrom(beanClass)
               || ServletContextListener.class.isAssignableFrom(beanClass)
               || ServletRequestListener.class.isAssignableFrom(beanClass)
               || ServletContextAttributeListener.class.isAssignableFrom(beanClass)
               || HttpSessionListener.class.isAssignableFrom(beanClass)
               || HttpSessionBindingListener.class.isAssignableFrom(beanClass)
               || HttpSessionActivationListener.class.isAssignableFrom(beanClass)
               || HttpSessionIdListener.class.isAssignableFrom(beanClass)
               || ServletRequestAttributeListener.class.isAssignableFrom(beanClass);
    }

    return false;
}
 
Example #5
Source File: StandardSession.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Inform the listeners about the change session ID.
 *
 * @param newId  new session ID
 * @param oldId  old session ID
 * @param notifySessionListeners  Should any associated sessionListeners be
 *        notified that session ID has been changed?
 * @param notifyContainerListeners  Should any associated ContainerListeners
 *        be notified that session ID has been changed?
 */
@Override
public void tellChangedSessionId(String newId, String oldId,
        boolean notifySessionListeners, boolean notifyContainerListeners) {
    Context context = manager.getContext();
     // notify ContainerListeners
    if (notifyContainerListeners) {
        context.fireContainerEvent(Context.CHANGE_SESSION_ID_EVENT,
                new String[] {oldId, newId});
    }

    // notify HttpSessionIdListener
    if (notifySessionListeners) {
        Object listeners[] = context.getApplicationEventListeners();
        if (listeners != null && listeners.length > 0) {
            HttpSessionEvent event =
                new HttpSessionEvent(getSession());

            for(Object listener : listeners) {
                if (!(listener instanceof HttpSessionIdListener))
                    continue;

                HttpSessionIdListener idListener =
                    (HttpSessionIdListener)listener;
                try {
                    idListener.sessionIdChanged(event, oldId);
                } catch (Throwable t) {
                    manager.getContext().getLogger().error
                        (sm.getString("standardSession.sessionEvent"), t);
                }
            }
        }
    }
}
 
Example #6
Source File: ApplicationContext.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public <T extends EventListener> void addListener(T t) {
    if (!context.getState().equals(LifecycleState.STARTING_PREP)) {
        throw new IllegalStateException(
                sm.getString("applicationContext.addListener.ise",
                        getContextPath()));
    }

    boolean match = false;
    if (t instanceof ServletContextAttributeListener ||
            t instanceof ServletRequestListener ||
            t instanceof ServletRequestAttributeListener ||
            t instanceof HttpSessionIdListener ||
            t instanceof HttpSessionAttributeListener) {
        context.addApplicationEventListener(t);
        match = true;
    }

    if (t instanceof HttpSessionListener ||
            (t instanceof ServletContextListener && newServletContextListenerAllowed)) {
        // Add listener directly to the list of instances rather than to
        // the list of class names.
        context.addApplicationLifecycleListener(t);
        match = true;
    }

    if (match) return;

    if (t instanceof ServletContextListener) {
        throw new IllegalArgumentException(sm.getString(
                "applicationContext.addListener.iae.sclNotAllowed",
                t.getClass().getName()));
    } else {
        throw new IllegalArgumentException(sm.getString(
                "applicationContext.addListener.iae.wrongType",
                t.getClass().getName()));
    }
}
 
Example #7
Source File: ApplicationListeners.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
public void httpSessionIdChanged(final HttpSession session, final String oldSessionId) {
    if(!started) {
        return;
    }
    final HttpSessionEvent sre = new HttpSessionEvent(session);
    for (int i = 0; i < httpSessionIdListeners.length; ++i) {
        this.<HttpSessionIdListener>get(httpSessionIdListeners[i]).sessionIdChanged(sre, oldSessionId);
    }
}
 
Example #8
Source File: ServletContext.java    From spring-boot-protocol with Apache License 2.0 5 votes vote down vote up
@Override
public <T extends EventListener> void addListener(T listener) {
    Objects.requireNonNull(listener);

    boolean addFlag = false;
    ServletEventListenerManager listenerManager = getServletEventListenerManager();
    if(listener instanceof ServletContextAttributeListener){
        listenerManager.addServletContextAttributeListener((ServletContextAttributeListener) listener);
        addFlag = true;
    }
    if(listener instanceof ServletRequestListener){
        listenerManager.addServletRequestListener((ServletRequestListener) listener);
        addFlag = true;
    }
    if(listener instanceof ServletRequestAttributeListener){
        listenerManager.addServletRequestAttributeListener((ServletRequestAttributeListener) listener);
        addFlag = true;
    }
    if(listener instanceof HttpSessionIdListener){
        listenerManager.addHttpSessionIdListenerListener((HttpSessionIdListener) listener);
        addFlag = true;
    }
    if(listener instanceof HttpSessionAttributeListener){
        listenerManager.addHttpSessionAttributeListener((HttpSessionAttributeListener) listener);
        addFlag = true;
    }
    if(listener instanceof HttpSessionListener){
        listenerManager.addHttpSessionListener((HttpSessionListener) listener);
        addFlag = true;
    }
    if(listener instanceof ServletContextListener){
        listenerManager.addServletContextListener((ServletContextListener) listener);
        addFlag = true;
    }
    if(!addFlag){
        throw new IllegalArgumentException("applicationContext.addListener.iae.wrongType"+
                listener.getClass().getName());
    }
}
 
Example #9
Source File: DefaultHttpSessionManager.java    From piranha with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Add a listener.
 *
 * @param <T> the type.
 * @param listener the listener.
 */
@Override
public <T extends EventListener> void addListener(T listener) {
    if (listener instanceof HttpSessionAttributeListener) {
        attributeListeners.add((HttpSessionAttributeListener) listener);
    }

    if (listener instanceof HttpSessionIdListener) {
        idListeners.add((HttpSessionIdListener) listener);
    }

    if (listener instanceof HttpSessionListener) {
        sessionListeners.add((HttpSessionListener) listener);
    }
}
 
Example #10
Source File: ApplicationListeners.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public void httpSessionIdChanged(final HttpSession session, final String oldSessionId) {
    if(!started) {
        return;
    }
    final HttpSessionEvent sre = new HttpSessionEvent(session);
    for (int i = 0; i < httpSessionIdListeners.length; ++i) {
        this.<HttpSessionIdListener>get(httpSessionIdListeners[i]).sessionIdChanged(sre, oldSessionId);
    }
}
 
Example #11
Source File: HttpSessionNotifier.java    From HttpSessionReplacer with MIT License 5 votes vote down vote up
@Override
public void sessionIdChanged(RepositoryBackedSession session, String oldId) {
  // Session id change is only supported for Servlet 3.1+
  if (!ServletLevel.isServlet31) {
    return;
  }
  if (session instanceof HttpSession) {
    HttpSessionEvent event = new HttpSessionEvent((HttpSession)session);
    for (EventListener listener : descriptor.getHttpSessionIdListeners()) {
      ((HttpSessionIdListener)listener).sessionIdChanged(event, oldId);
    }
  }
}
 
Example #12
Source File: TestSessionHelpers.java    From HttpSessionReplacer with MIT License 5 votes vote down vote up
@Test
public void testOnAddListener() {
  ServletContextDescriptor scd = new ServletContextDescriptor(servletContext);
  when(servletContext.getAttribute(Attributes.SERVLET_CONTEXT_DESCRIPTOR)).thenReturn(scd);
  sessionHelpers.onAddListener(servletContext, "Dummy");
  assertTrue(scd.getHttpSessionListeners().isEmpty());
  assertTrue(scd.getHttpSessionIdListeners().isEmpty());
  assertTrue(scd.getHttpSessionAttributeListeners().isEmpty());
  HttpSessionListener listener = mock(HttpSessionListener.class);
  HttpSessionIdListener idListener = mock(HttpSessionIdListener.class);
  HttpSessionAttributeListener attributeListener = mock(HttpSessionAttributeListener.class);
  HttpSessionListener multiListener = mock(HttpSessionListener.class,
      withSettings().extraInterfaces(HttpSessionAttributeListener.class));
  HttpSessionAttributeListener attributeMultiListener = (HttpSessionAttributeListener)multiListener;
  sessionHelpers.onAddListener(servletContext, listener);
  assertThat(scd.getHttpSessionListeners(), hasItem(listener));
  assertTrue(scd.getHttpSessionIdListeners().isEmpty());
  assertTrue(scd.getHttpSessionAttributeListeners().isEmpty());
  sessionHelpers.onAddListener(servletContext, idListener);
  assertThat(scd.getHttpSessionListeners(), hasItem(listener));
  assertThat(scd.getHttpSessionIdListeners(), hasItem(idListener));
  assertTrue(scd.getHttpSessionAttributeListeners().isEmpty());
  sessionHelpers.onAddListener(servletContext, attributeListener);
  assertThat(scd.getHttpSessionListeners(), hasItem(listener));
  assertThat(scd.getHttpSessionIdListeners(), hasItem(idListener));
  assertThat(scd.getHttpSessionAttributeListeners(), hasItem(attributeListener));
  sessionHelpers.onAddListener(servletContext, multiListener);
  assertThat(scd.getHttpSessionListeners(), hasItem(listener));
  assertThat(scd.getHttpSessionListeners(), hasItem(multiListener));
  assertThat(scd.getHttpSessionIdListeners(), hasItem(idListener));
  assertThat(scd.getHttpSessionAttributeListeners(), hasItem(attributeListener));
  assertThat(scd.getHttpSessionAttributeListeners(), hasItem(attributeMultiListener));
}
 
Example #13
Source File: SessionManager.java    From appengine-java-vm-runtime with Apache License 2.0 5 votes vote down vote up
/**
 * Call any session id listeners registered.
 * Usually done by renewSessionId() method, but that is not used in appengine.
 * @param session
 * @param oldId
 */
public void callSessionIdListeners (AbstractSession session, String oldId) {
  HttpSessionEvent event = new HttpSessionEvent(session);
  for (HttpSessionIdListener l:_sessionIdListeners)
  {
      l.sessionIdChanged(event, oldId);
  }
}