/** * 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); } } }
/** * Init method called by the main servlet when the wrapping servlet is initialized. This means that the context is * taken into service by the system. * * @param parent The parent servlet context. Just for some delegation actions */ void init(ServletContext parent) { // Set up the tracking of event listeners. BundleContext bc = getOwner().getBundleContext(); delegate = parent; Collection<Class<? extends EventListener>> toTrack = Arrays.asList(HttpSessionListener.class, ServletRequestListener.class, HttpSessionAttributeListener.class, ServletRequestAttributeListener.class, ServletContextListener.class); Collection<String> objectFilters = toTrack.stream(). map((c) -> "(" + Constants.OBJECTCLASS + "=" + c.getName() + ")").collect(Collectors.toList()); String filterString = "|" + String.join("", objectFilters); eventListenerTracker = startTracking(filterString, new Tracker<EventListener, EventListener>(bc, getContextPath(), (e) -> e, (e) -> { /* No destruct */})); // Initialize the servlets. ServletContextEvent event = new ServletContextEvent(this); call(ServletContextListener.class, (l) -> l.contextInitialized(event)); servlets.values().forEach((s) -> init(s)); // And the filters. filters.values().forEach((f) -> init(f)); // Set up the tracking of servlets and filters. servletTracker = startTracking(Constants.OBJECTCLASS + "=" + Servlet.class.getName(), new Tracker<Servlet, String>(bc, getContextPath(), this::addServlet, this::removeServlet)); filterTracker = startTracking(Constants.OBJECTCLASS + "=" + Filter.class.getName(), new Tracker<Filter, String>(bc, getContextPath(), this::addFilter, this::removeFilter)); }
@Override public void setAttribute(String name, Object value) { // According to the specifications, a value of null should be handled as a remove. if (value == null) { removeAttribute(name); return; } checkValid(); // See how to notify our listeners: as a replacement or an addition. BiConsumer<HttpSessionAttributeListener, HttpSessionBindingEvent> notifier = (l, e) -> l.attributeAdded(e); if (unbindOriginal(name)) { notifier = (l, e) -> l.attributeReplaced(e); } // Update the data. attributes.put(name, value); // And perform the notification. notifyListeners(name, value, notifier); }
private void notifyListeners(String name, Object oldValue, Object value) { if (listeners != null) { HttpSessionBindingEvent event = new HttpSessionBindingEvent(this, name, value); HttpSessionBindingEvent oldEvent = oldValue == null ? null : new HttpSessionBindingEvent(this, name, oldValue); for (int i = 0, n = listeners.size(); i < n; i++) try { HttpSessionAttributeListener l = (HttpSessionAttributeListener) listeners.get(i); if (oldEvent != null) l.attributeReplaced(oldEvent); l.attributeAdded(event); } catch (ClassCastException cce) { } catch (NullPointerException npe) { } } }
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; }
@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())); } }
@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())); } }
/** * 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); } }
@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)); }
@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)); }
@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)); }
@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)); }
public void sessionDidActivate(HttpSessionEvent se) { for (EventListener listener: PluginWebInstanceRepository.getListeners()) { if (listener instanceof HttpSessionAttributeListener) { ((HttpSessionActivationListener) listener).sessionDidActivate(se); } } }
public void attributeAdded(HttpSessionBindingEvent se) { for (EventListener listener: PluginWebInstanceRepository.getListeners()) { if (listener instanceof HttpSessionAttributeListener) { ((HttpSessionAttributeListener) listener).attributeAdded(se); } } }
public void attributeRemoved(HttpSessionBindingEvent se) { for (EventListener listener: PluginWebInstanceRepository.getListeners()) { if (listener instanceof HttpSessionAttributeListener) { ((HttpSessionAttributeListener) listener).attributeRemoved(se); } } }
public void attributeReplaced(HttpSessionBindingEvent se) { for (EventListener listener: PluginWebInstanceRepository.getListeners()) { if (listener instanceof HttpSessionAttributeListener) { ((HttpSessionAttributeListener) listener).attributeReplaced(se); } } }
private void loadListeners(WebAppConfiguration wac) throws ClassNotFoundException, IllegalAccessException, InstantiationException, WebAppConfigurationException { for (WebAppListener listener : wac.getListeners()) { String className = listener.getListenerClass(); Class<?> clazz = classLoader.loadClass(className); Object obj = clazz.newInstance(); boolean used = false; if (obj instanceof ServletContextAttributeListener) { scaListeners.add((ServletContextAttributeListener) obj); used = true; } if (obj instanceof ServletContextListener) { scListeners.add((ServletContextListener) obj); used = true; } if (obj instanceof ServletRequestListener) { srListeners.add((ServletRequestListener) obj); used = true; } if (obj instanceof ServletRequestAttributeListener) { sraListeners.add((ServletRequestAttributeListener) obj); used = true; } if (obj instanceof HttpSessionAttributeListener) { sessionAttributeListeners.add((HttpSessionAttributeListener) obj); used = true; } if (obj instanceof HttpSessionListener) { sessionListeners.add((HttpSessionListener) obj); used = true; } if (!used) { throw new WebAppConfigurationException("Don't know what to do with '" + className + "'"); } } }
public WebAppSessionWrapper(HttpSession delegate, ServletContext context, HttpSessionListener sessionListener, HttpSessionAttributeListener sessionAttributeListener) { super(delegate); this.context = context; this.sessionListener = sessionListener; this.sessionAttributeListener = sessionAttributeListener; fireSessionCreated(); }
private void addListenerInstance(final Object listenerInstance, final List<ServletContextAttributeListener> contextAttributeListeners, final List<ServletContextListener> contextListeners, final List<ServletRequestAttributeListener> requestAttributeListeners, final List<ServletRequestListener> requestListeners, final List<HttpSessionActivationListener> sessionActivationListeners, final List<HttpSessionAttributeListener> sessionAttributeListeners, final List<HttpSessionListener> sessionListeners) { if (listenerInstance instanceof ServletContextAttributeListener) { contextAttributeListeners.add((ServletContextAttributeListener) listenerInstance); } if (listenerInstance instanceof ServletContextListener) { contextListeners.add((ServletContextListener) listenerInstance); } if (listenerInstance instanceof ServletRequestAttributeListener) { requestAttributeListeners.add((ServletRequestAttributeListener) listenerInstance); } if (listenerInstance instanceof ServletRequestListener) { requestListeners.add((ServletRequestListener) listenerInstance); } if (listenerInstance instanceof HttpSessionActivationListener) { sessionActivationListeners.add((HttpSessionActivationListener) listenerInstance); } if (listenerInstance instanceof HttpSessionAttributeListener) { sessionAttributeListeners.add((HttpSessionAttributeListener) listenerInstance); } if (listenerInstance instanceof HttpSessionListener) { sessionListeners.add((HttpSessionListener) listenerInstance); } }
@Override public void removeAttribute(String name) { access(); sessionManager.getSessionStorage().removeSessionAttribute(getId(), name); Object value = attributes.remove(name); if(null == value){ return; } HttpSessionBindingEvent event = null; if(null != value && value instanceof HttpSessionBindingListener){ event = new HttpSessionBindingEvent(this, name, value); try{ ((HttpSessionBindingListener)value).valueUnbound(event); }catch (Exception e){ logger.error("bingEvent error:", e); throw new RuntimeException(e); } } List<EventListener> listeners = sessionManager.getEventListeners(); for(EventListener eventListener : listeners){ if(!(eventListener instanceof HttpSessionAttributeListener)){ continue; } HttpSessionAttributeListener listener = (HttpSessionAttributeListener) eventListener; if(event == null){ event = new HttpSessionBindingEvent(this, name, value); } listener.attributeRemoved(event); } }
@Override public void removeAttribute(String name) { access(); Object value = getAttribute(name); if(null == value){ return; } HttpSessionBindingEvent event = null; sessionManager.getSessionStorage().removeSessionAttribute(getId(), name); if(null != value && value instanceof HttpSessionBindingListener){ event = new HttpSessionBindingEvent(this, name); try{ ((HttpSessionBindingListener)value).valueUnbound(event); }catch (Exception e){ logger.error("bingEvent error:", e); throw new RuntimeException(e); } } List<EventListener> listeners = sessionManager.getEventListeners(); for(EventListener eventListener : listeners){ if(!(eventListener instanceof HttpSessionAttributeListener)){ continue; } HttpSessionAttributeListener listener = (HttpSessionAttributeListener) eventListener; if(event == null){ event = new HttpSessionBindingEvent(this, name, value); } listener.attributeRemoved(event); } }
@Override public void attributeAdded(final HttpSessionBindingEvent se) { final Iterator<HttpSessionAttributeListener> listeners = getContextListeners(); while (listeners.hasNext()) { listeners.next().attributeAdded(se); } }
@Override public void attributeRemoved(final HttpSessionBindingEvent se) { final Iterator<HttpSessionAttributeListener> listeners = getContextListeners(); while (listeners.hasNext()) { listeners.next().attributeRemoved(se); } }
@Override public void attributeReplaced(final HttpSessionBindingEvent se) { final Iterator<HttpSessionAttributeListener> listeners = getContextListeners(); while (listeners.hasNext()) { listeners.next().attributeReplaced(se); } }
private void notifyListeners(String name, Object value) { if (listeners != null) { HttpSessionBindingEvent event = new HttpSessionBindingEvent(this, name, value); for (int i = 0, n = listeners.size(); i < n; i++) try { ((HttpSessionAttributeListener) listeners.get(i)).attributeRemoved(event); } catch (ClassCastException cce) { } catch (NullPointerException npe) { } } }
public void httpSessionAttributeAdded(final HttpSession session, final String name, final Object value) { final HttpSessionBindingEvent sre = new HttpSessionBindingEvent(session, name, value); for (int i = 0; i < httpSessionAttributeListeners.length; ++i) { this.<HttpSessionAttributeListener>get(httpSessionAttributeListeners[i]).attributeAdded(sre); } }
public void httpSessionAttributeRemoved(final HttpSession session, final String name, final Object value) { final HttpSessionBindingEvent sre = new HttpSessionBindingEvent(session, name, value); for (int i = 0; i < httpSessionAttributeListeners.length; ++i) { this.<HttpSessionAttributeListener>get(httpSessionAttributeListeners[i]).attributeRemoved(sre); } }
public void httpSessionAttributeReplaced(final HttpSession session, final String name, final Object value) { final HttpSessionBindingEvent sre = new HttpSessionBindingEvent(session, name, value); for (int i = 0; i < httpSessionAttributeListeners.length; ++i) { this.<HttpSessionAttributeListener>get(httpSessionAttributeListeners[i]).attributeReplaced(sre); } }
/** * Remove the object bound with the specified name from this session. If * the session does not have an object bound with this name, this method * does nothing. * <p> * After this method executes, and if the object implements * <code>HttpSessionBindingListener</code>, the container calls * <code>valueUnbound()</code> on the object. * * @param name Name of the object to remove from this session. * @param notify Should we notify interested listeners that this * attribute is being removed? * * @exception IllegalStateException if this method is called on an * invalidated session */ public void removeAttribute(String name, boolean notify) { // Validate our current state if (!expiring && !isValid) throw new IllegalStateException (sm.getString("standardSession.removeAttribute.ise")); // Remove this attribute from our collection Object value = null; boolean found = false; synchronized (attributes) { found = attributes.containsKey(name); if (found) { value = attributes.get(name); attributes.remove(name); } else { return; } } // Do we need to do valueUnbound() and attributeRemoved() notification? if (!notify) { return; } // Call the valueUnbound() method if necessary HttpSessionBindingEvent event = new HttpSessionBindingEvent((HttpSession) this, name, value); if ((value != null) && (value instanceof HttpSessionBindingListener)) ((HttpSessionBindingListener) value).valueUnbound(event); // Notify interested application event listeners Context context = (Context) manager.getContainer(); Object listeners[] = context.getApplicationListeners(); if (listeners == null) return; for (int i = 0; i < listeners.length; i++) { if (!(listeners[i] instanceof HttpSessionAttributeListener)) continue; HttpSessionAttributeListener listener = (HttpSessionAttributeListener) listeners[i]; try { fireContainerEvent(context, "beforeSessionAttributeRemoved", listener); listener.attributeRemoved(event); fireContainerEvent(context, "afterSessionAttributeRemoved", listener); } catch (Throwable t) { try { fireContainerEvent(context, "afterSessionAttributeRemoved", listener); } catch (Exception e) { ; } // FIXME - should we do anything besides log these? log(sm.getString("standardSession.attributeEvent"), t); } } }
@Override public void attributeAdded(HttpSessionBindingEvent event) { ((HttpSessionAttributeListener)delegate).attributeAdded(event); }
@Override public void attributeRemoved(HttpSessionBindingEvent event) { ((HttpSessionAttributeListener)delegate).attributeRemoved(event); }
@Override public void attributeReplaced(HttpSessionBindingEvent event) { ((HttpSessionAttributeListener)delegate).attributeReplaced(event); }
HttpSessionAttributeListener getHttpSessionAttributeListener() { return this.sessionAttributeListener; }
private void initHttpSessionAttributeListener(EventListener listener) { if (listener instanceof HttpSessionAttributeListener) { this.sessionAttributeListener = (HttpSessionAttributeListener) listener; } }
public HttpSessionAttributeListener getHttpSessionAttributeListener() { return sessionAttributeListeners; }
public HttpSessionAttributeListener getHttpSessionAttributeListener() { return null; }