javax.servlet.http.HttpSessionAttributeListener Java Examples

The following examples show how to use javax.servlet.http.HttpSessionAttributeListener. 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: 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 #2
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 #3
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 #4
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 #5
Source File: ApplicationListeners.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
public void httpSessionAttributeReplaced(final HttpSession session, final String name, final Object value) {
    if(!started) {
        return;
    }
    final HttpSessionBindingEvent sre = new HttpSessionBindingEvent(session, name, value);
    for (int i = 0; i < httpSessionAttributeListeners.length; ++i) {
        this.<HttpSessionAttributeListener>get(httpSessionAttributeListeners[i]).attributeReplaced(sre);
    }
}
 
Example #6
Source File: ApplicationListeners.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
public void httpSessionAttributeAdded(final HttpSession session, final String name, final Object value) {
    if(!started) {
        return;
    }
    final HttpSessionBindingEvent sre = new HttpSessionBindingEvent(session, name, value);
    for (int i = 0; i < httpSessionAttributeListeners.length; ++i) {
        this.<HttpSessionAttributeListener>get(httpSessionAttributeListeners[i]).attributeAdded(sre);
    }
}
 
Example #7
Source File: ApplicationContext.java    From tomcatsrc with Apache License 2.0 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 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 #8
Source File: TestHttpSessionNotifier.java    From HttpSessionReplacer with MIT License 5 votes vote down vote up
@Test
public void testAttributeRemoved() {
  notifier.attributeRemoved(session, "Test", "very-old-value");
  HttpSessionAttributeListener listener = mock(HttpSessionAttributeListener.class);
  descriptor.addHttpSessionAttributeListener(listener);
  notifier.attributeRemoved(session, "Test", "old-value");
  verify(listener).attributeRemoved(any(HttpSessionBindingEvent.class));
  HttpSessionBindingListener bindingListener = mock(HttpSessionBindingListener.class);
  notifier.attributeRemoved(session, "Test", bindingListener);
  verify(listener, times(2)).attributeRemoved(any(HttpSessionBindingEvent.class));
  verify(bindingListener).valueUnbound(any(HttpSessionBindingEvent.class));
}
 
Example #9
Source File: TestHttpSessionNotifier.java    From HttpSessionReplacer with MIT License 5 votes vote down vote up
@Test
public void testAttributeReplaced() {
  HttpSessionAttributeListener listener = mock(HttpSessionAttributeListener.class);
  notifier.attributeReplaced(session, "Test", "very-old-value");
  verify(listener, never()).attributeReplaced(any(HttpSessionBindingEvent.class));
  descriptor.addHttpSessionAttributeListener(listener);
  notifier.attributeReplaced(session, "Test", "old-value");
  verify(listener).attributeReplaced(any(HttpSessionBindingEvent.class));
  HttpSessionBindingListener bindingListener = mock(HttpSessionBindingListener.class);
  notifier.attributeReplaced(session, "Test", bindingListener);
  verify(listener, times(2)).attributeReplaced(any(HttpSessionBindingEvent.class));
  verify(bindingListener).valueUnbound(any(HttpSessionBindingEvent.class));
}
 
Example #10
Source File: TestHttpSessionNotifier.java    From HttpSessionReplacer with MIT License 5 votes vote down vote up
@Test
public void testAttributeAdded() {
  HttpSessionAttributeListener listener = mock(HttpSessionAttributeListener.class);
  descriptor.addHttpSessionAttributeListener(listener);
  notifier.attributeAdded(session, "Test", "value");
  verify(listener).attributeAdded(any(HttpSessionBindingEvent.class));
  HttpSessionBindingListener bindingListener = mock(HttpSessionBindingListener.class);
  notifier.attributeAdded(session, "Test", bindingListener);
  verify(listener, times(2)).attributeAdded(any(HttpSessionBindingEvent.class));
  verify(bindingListener).valueBound(any(HttpSessionBindingEvent.class));
}
 
Example #11
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 #12
Source File: HttpSessionNotifier.java    From HttpSessionReplacer with MIT License 5 votes vote down vote up
/**
 * Notifies listeners that attribute was added. See {@link SessionNotifier}
 * {@link #attributeAdded(RepositoryBackedSession, String, Object)}.
 * <p>
 * If the added attribute <code>value</code> is a HttpSessionBindingListener,
 * it will receive the {@link HttpSessionBindingEvent}. If there are
 * {@link HttpSessionAttributeListener} instances associated to
 * {@link ServletContext}, they will be notified via
 * {@link HttpSessionAttributeListener#attributeAdded(HttpSessionBindingEvent)}
 * .
 */
@Override
public void attributeAdded(RepositoryBackedSession session, String key, Object value) {
  // If the
  if (session instanceof HttpSession && value instanceof HttpSessionBindingListener) {
    ((HttpSessionBindingListener)value).valueBound(new HttpSessionBindingEvent((HttpSession)session, key));
  }
  HttpSessionBindingEvent event = new HttpSessionBindingEvent((HttpSession)session, key, value);
  for (HttpSessionAttributeListener listener : descriptor.getHttpSessionAttributeListeners()) {
    listener.attributeAdded(event);
  }
}
 
Example #13
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 #14
Source File: ApplicationListeners.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public void httpSessionAttributeReplaced(final HttpSession session, final String name, final Object value) {
    if(!started) {
        return;
    }
    final HttpSessionBindingEvent sre = new HttpSessionBindingEvent(session, name, value);
    for (int i = 0; i < httpSessionAttributeListeners.length; ++i) {
        this.<HttpSessionAttributeListener>get(httpSessionAttributeListeners[i]).attributeReplaced(sre);
    }
}
 
Example #15
Source File: ApplicationListeners.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public void httpSessionAttributeRemoved(final HttpSession session, final String name, final Object value) {
    if(!started) {
        return;
    }
    final HttpSessionBindingEvent sre = new HttpSessionBindingEvent(session, name, value);
    for (int i = 0; i < httpSessionAttributeListeners.length; ++i) {
        this.<HttpSessionAttributeListener>get(httpSessionAttributeListeners[i]).attributeRemoved(sre);
    }
}
 
Example #16
Source File: ApplicationListeners.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public void httpSessionAttributeAdded(final HttpSession session, final String name, final Object value) {
    if(!started) {
        return;
    }
    final HttpSessionBindingEvent sre = new HttpSessionBindingEvent(session, name, value);
    for (int i = 0; i < httpSessionAttributeListeners.length; ++i) {
        this.<HttpSessionAttributeListener>get(httpSessionAttributeListeners[i]).attributeAdded(sre);
    }
}
 
Example #17
Source File: ApplicationContext.java    From Tomcat7.0.67 with Apache License 2.0 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 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 #18
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 #19
Source File: ApplicationListeners.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
public void httpSessionAttributeRemoved(final HttpSession session, final String name, final Object value) {
    if(!started) {
        return;
    }
    final HttpSessionBindingEvent sre = new HttpSessionBindingEvent(session, name, value);
    for (int i = 0; i < httpSessionAttributeListeners.length; ++i) {
        this.<HttpSessionAttributeListener>get(httpSessionAttributeListeners[i]).attributeRemoved(sre);
    }
}
 
Example #20
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 #21
Source File: HttpSessionNotifier.java    From HttpSessionReplacer with MIT License 3 votes vote down vote up
/**
 * Notifies listeners that attribute was replaced. See {@link SessionNotifier}
 * {@link #attributeReplaced(RepositoryBackedSession, String, Object)}.
 * <p>
 * If the the old value of attribute <code>replacedValue</code> is a
 * HttpSessionBindingListener, it will receive the
 * {@link HttpSessionBindingEvent}. If there are
 * {@link HttpSessionAttributeListener} instances associated to
 * {@link ServletContext}, they will be notified via
 * {@link HttpSessionAttributeListener#attributeReplaced(HttpSessionBindingEvent)}
 * .
 */
@Override
public void attributeReplaced(RepositoryBackedSession session, String key, Object replacedValue) {
  if (session instanceof HttpSession && replacedValue instanceof HttpSessionBindingListener) {
    ((HttpSessionBindingListener)replacedValue).valueUnbound(new HttpSessionBindingEvent((HttpSession)session, key));
  }
  HttpSessionBindingEvent event = new HttpSessionBindingEvent((HttpSession)session, key, replacedValue);
  for (HttpSessionAttributeListener listener : descriptor.getHttpSessionAttributeListeners()) {
    listener.attributeReplaced(event);
  }
}
 
Example #22
Source File: HttpSessionNotifier.java    From HttpSessionReplacer with MIT License 3 votes vote down vote up
/**
 * Notifies listeners that attribute was removed. See {@link SessionNotifier}
 * {@link #attributeRemoved(RepositoryBackedSession, String, Object)}.
 * <p>
 * If the the old value of attribute <code>removedValue</code> is a
 * HttpSessionBindingListener, it will receive the
 * {@link HttpSessionBindingEvent}. If there are
 * {@link HttpSessionAttributeListener} instances associated to
 * {@link ServletContext}, they will be notified via
 * {@link HttpSessionAttributeListener#attributeRemoved(HttpSessionBindingEvent)}
 * .
 */
@Override
public void attributeRemoved(RepositoryBackedSession session, String key, Object removedValue) {
  if (session instanceof HttpSession && removedValue instanceof HttpSessionBindingListener) {
    ((HttpSessionBindingListener)removedValue).valueUnbound(new HttpSessionBindingEvent((HttpSession)session, key));
  }
  HttpSessionBindingEvent event = new HttpSessionBindingEvent((HttpSession)session, key);
  for (HttpSessionAttributeListener listener : descriptor.getHttpSessionAttributeListeners()) {
    listener.attributeRemoved(event);
  }
}
 
Example #23
Source File: ServletContextDescriptor.java    From HttpSessionReplacer with MIT License 3 votes vote down vote up
/**
 * Adds {@link HttpSessionAttributeListener} associated to
 * {@link ServletContext} to list of known listeners. Listener is added only
 * if it has not been seen before.
 *
 * @param listener
 */
void addHttpSessionAttributeListener(HttpSessionAttributeListener listener) {
  if (httpSessionAttributeListenersSet.add(listener)) {
    httpSessionAttributeListeners.add(listener);
    logger.info("Registered HttpSessionAttributeListener {} for context {}", listener, contextPath);
  }
}
 
Example #24
Source File: ServletContextDescriptor.java    From HttpSessionReplacer with MIT License 2 votes vote down vote up
/**
 * Returns list of {@link HttpSessionAttributeListener}
 *
 * @return list of listeners or empty if there are nor registered listeners
 */
List<HttpSessionAttributeListener> getHttpSessionAttributeListeners() {
  return httpSessionAttributeListeners;
}