@Override public void attributeUpdated(final Session session, final String name, final Object value, final Object old) { if(name.startsWith(IO_UNDERTOW)) { return; } final HttpSessionImpl httpSession = SecurityActions.forSession(session, servletContext, false); if (old != value) { if (old instanceof HttpSessionBindingListener) { ((HttpSessionBindingListener) old).valueUnbound(new HttpSessionBindingEvent(httpSession, name, old)); } applicationListeners.httpSessionAttributeReplaced(httpSession, name, old); } if (value instanceof HttpSessionBindingListener) { ((HttpSessionBindingListener) value).valueBound(new HttpSessionBindingEvent(httpSession, name, value)); } }
/** * Serialize the attributes of this session into an object that can be * turned into a byte array with standard Java serialization. * * @return a representation of this session's serialized state */ public Serializable serializeState() { HashMap<String, Serializable> state = new HashMap<String, Serializable>(); for (Iterator<Map.Entry<String, Object>> it = this.attributes.entrySet().iterator(); it.hasNext();) { Map.Entry<String, Object> entry = it.next(); String name = entry.getKey(); Object value = entry.getValue(); it.remove(); if (value instanceof Serializable) { state.put(name, (Serializable) value); } else { // Not serializable... Servlet containers usually automatically // unbind the attribute in this case. if (value instanceof HttpSessionBindingListener) { ((HttpSessionBindingListener) value).valueUnbound(new HttpSessionBindingEvent(this, name, value)); } } } return state; }
@Test public void shouldUnbindOnInvalidate() throws Exception { Map<String, Object> sampleData = new HashMap<>(); HttpSessionBindingListener mockA = mock(HttpSessionBindingListener.class); HttpSessionBindingListener mockC = mock(HttpSessionBindingListener.class); sampleData.put("a", mockA); sampleData.put("b", "b"); sampleData.put("c", mockC); sampleData.put("b", "b"); when(session.data()).thenReturn(sampleData); vertxWrappedSession.invalidate(); verify(session).destroy(); ArgumentCaptor<HttpSessionBindingEvent> sessionBindingEventCaptor = ArgumentCaptor.forClass(HttpSessionBindingEvent.class); verify(mockA).valueUnbound(sessionBindingEventCaptor.capture()); verify(mockC).valueUnbound(sessionBindingEventCaptor.capture()); assertThat(sessionBindingEventCaptor.getAllValues()).hasSize(2); assertHttpSessionBindingEvent("a", mockA, sessionBindingEventCaptor.getAllValues().get(0)); assertHttpSessionBindingEvent("c", mockC, sessionBindingEventCaptor.getAllValues().get(1)); }
/** * Removes the object bound with the specified name from * this session. */ public void removeAttribute(String name) { maybeThrowIllegalStateException(); checkValid(); Object value = getAttribute(name); // notify binding listeners if (value != null) { sessionData_.remove(name); updateCache(); if (value instanceof HttpSessionBindingListener) { ((HttpSessionBindingListener) value).valueUnbound( new HttpSessionBindingEvent(this, name)); } } // notify attribute listeners ServletSessionCache.getCache(cacheId_). notifySessionAttributeRemoved(this, name); }
/** * Serialize the attributes of this session into an object that can * be turned into a byte array with standard Java serialization. * @return a representation of this session's serialized state */ public Serializable serializeState() { HashMap state = new HashMap(); for (Iterator it = this.attributes.entrySet().iterator(); it.hasNext();) { Map.Entry entry = (Map.Entry) it.next(); String name = (String) entry.getKey(); Object value = entry.getValue(); it.remove(); if (value instanceof Serializable) { state.put(name, value); } else { // Not serializable... Servlet containers usually automatically // unbind the attribute in this case. if (value instanceof HttpSessionBindingListener) { ((HttpSessionBindingListener) value).valueUnbound(new HttpSessionBindingEvent(this, name, value)); } } } return state; }
public Serializable serializeState() { HashMap<String, Serializable> state = new HashMap<String, Serializable>(); for (Iterator<Map.Entry<String, Object>> it = this.attributes.entrySet().iterator(); it.hasNext();) { Map.Entry<String, Object> entry = it.next(); String name = entry.getKey(); Object value = entry.getValue(); it.remove(); if (value instanceof Serializable) { state.put(name, (Serializable) value); } else { // Not serializable... Servlet containers usually automatically // unbind the attribute in this case. if (value instanceof HttpSessionBindingListener) { ((HttpSessionBindingListener) value).valueUnbound(new HttpSessionBindingEvent(this, name, value)); } } } return state; }
/** * Serialize the attributes of this session into an object that can * be turned into a byte array with standard Java serialization. * @return a representation of this session's serialized state */ public Serializable serializeState() { HashMap<String, Object> state = new HashMap<String, Object>(); for (Iterator it = this.attributes.entrySet().iterator(); it.hasNext();) { Map.Entry entry = (Map.Entry) it.next(); String name = (String) entry.getKey(); Object value = entry.getValue(); it.remove(); if (value instanceof Serializable) { state.put(name, value); } else { if (value instanceof HttpSessionBindingListener) { ((HttpSessionBindingListener) value).valueUnbound(new HttpSessionBindingEvent(this, name, value)); } } } return state; }
/** * Binds an object to this session, using the name specified. If an object of * the same name is already bound to the session, the object is replaced. * @param name The name to which the object is bound; cannot be null. * @param value The object to be bound. */ public void setAttribute(String name, Object value) { if (!isValid()) throw new IllegalStateException( "Method is called on an invalidated session"); if (value == null) removeAttribute(name); else { attributes.put(name, value); try { HttpSessionBindingListener listener = (HttpSessionBindingListener) value; listener.valueBound(new HttpSessionBindingEvent(this, name, value)); listener.notifyAll(); } catch (ClassCastException e) { } } }
private void cleanUpAttributes() { if (!isValid()) throw new IllegalStateException( "Method is called on an invalidated session"); Iterator iter = attributes.entrySet().iterator(); while (iter.hasNext()) { Entry e = (Entry) iter.next(); String name = e.getKey() + ""; Object value = e.getValue(); iter.remove(); try { HttpSessionBindingListener listener = (HttpSessionBindingListener) value; listener.valueUnbound(new HttpSessionBindingEvent(this, name, value)); listener.notifyAll(); } catch (ClassCastException err) { } } }
/** * Serialize the attributes of this session into an object that can be * turned into a byte array with standard Java serialization. * @return a representation of this session's serialized state */ public Serializable serializeState() { HashMap<String, Serializable> state = new HashMap<>(); for (Iterator<Map.Entry<String, Object>> it = this.attributes.entrySet().iterator(); it.hasNext();) { Map.Entry<String, Object> entry = it.next(); String name = entry.getKey(); Object value = entry.getValue(); it.remove(); if (value instanceof Serializable) { state.put(name, (Serializable) value); } else { // Not serializable... Servlet containers usually automatically // unbind the attribute in this case. if (value instanceof HttpSessionBindingListener) { ((HttpSessionBindingListener) value).valueUnbound(new HttpSessionBindingEvent(this, name, value)); } } } return state; }
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 void removeAttribute(String name) { if (attributes != null) { Object value = attributes.get(name); if (value != null && value instanceof HttpSessionBindingListener) { ((HttpSessionBindingListener) value).valueUnbound(new HttpSessionBindingEvent(this, name, value)); } attributes.remove(name); } }
@Override public void setAttribute(String name, Object value) { attributes.put(name, value); if (value != null && value instanceof HttpSessionBindingListener) { ((HttpSessionBindingListener) value).valueBound(new HttpSessionBindingEvent(this, name, value)); } }
@Override public void attributeAdded(final Session session, final String name, final Object value) { if(name.startsWith(IO_UNDERTOW)) { return; } final HttpSessionImpl httpSession = SecurityActions.forSession(session, servletContext, false); applicationListeners.httpSessionAttributeAdded(httpSession, name, value); if (value instanceof HttpSessionBindingListener) { ((HttpSessionBindingListener) value).valueBound(new HttpSessionBindingEvent(httpSession, name, value)); } }
@Override public void attributeRemoved(final Session session, final String name, final Object old) { if(name.startsWith(IO_UNDERTOW)) { return; } final HttpSessionImpl httpSession = SecurityActions.forSession(session, servletContext, false); if (old != null) { applicationListeners.httpSessionAttributeRemoved(httpSession, name, old); if (old instanceof HttpSessionBindingListener) { ((HttpSessionBindingListener) old).valueUnbound(new HttpSessionBindingEvent(httpSession, name, old)); } } }
public void setAttribute(String string, Object object) { Object originalValue = attr.put(string, object); if (originalValue != null && originalValue instanceof HttpSessionBindingListener) { ((HttpSessionBindingListener) originalValue).valueUnbound(new HttpSessionBindingEvent(this, string)); } if (object != null && object instanceof HttpSessionBindingListener) { ((HttpSessionBindingListener) object).valueBound(new HttpSessionBindingEvent(this, string)); } }
/** * 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 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)); }
@Override public void setAttribute(String name, Object value) { assertIsValid(); Assert.notNull(name, "Attribute name must not be null"); if (value != null) { this.attributes.put(name, value); if (value instanceof HttpSessionBindingListener) { ((HttpSessionBindingListener) value).valueBound(new HttpSessionBindingEvent(this, name, value)); } } else { removeAttribute(name); } }
@Override public void removeAttribute(String name) { assertIsValid(); Assert.notNull(name, "Attribute name must not be null"); Object value = this.attributes.remove(name); if (value instanceof HttpSessionBindingListener) { ((HttpSessionBindingListener) value).valueUnbound(new HttpSessionBindingEvent(this, name, value)); } }
/** * Clear all of this session's attributes. */ public void clearAttributes() { for (Iterator<Map.Entry<String, Object>> it = this.attributes.entrySet().iterator(); it.hasNext();) { Map.Entry<String, Object> entry = it.next(); String name = entry.getKey(); Object value = entry.getValue(); it.remove(); if (value instanceof HttpSessionBindingListener) { ((HttpSessionBindingListener) value).valueUnbound(new HttpSessionBindingEvent(this, name, value)); } } }
protected void doClearAttributes(Map<String, Object> attributes) { for (Iterator<Map.Entry<String, Object>> it = attributes.entrySet().iterator(); it.hasNext();) { Map.Entry<String, Object> entry = it.next(); String name = entry.getKey(); Object value = entry.getValue(); it.remove(); if (value instanceof HttpSessionBindingListener) { ((HttpSessionBindingListener) value).valueUnbound( new HttpSessionBindingEvent(new MockHttpSession(), name, value)); } } }
@Override // TODO: catch HttpSessionBindingListener exceptions? public void invalidate() { checkSessionState(); Map<String, HttpSessionBindingListener> toUnbind = delegate.data().entrySet().stream() .filter( entry -> HttpSessionBindingListener.class.isInstance(entry.getValue())) .collect(toMap(Map.Entry::getKey, e -> HttpSessionBindingListener.class.cast(e.getValue()))); delegate.destroy(); toUnbind.forEach( (name, listener) -> listener.valueUnbound(createHttpSessionBindingEvent(name,listener))); toUnbind.clear(); }
public void valueBound(HttpSessionBindingEvent event) { for (EventListener listener: PluginWebInstanceRepository.getListeners()) { if (listener instanceof HttpSessionBindingListener) { ((HttpSessionBindingListener) listener).valueBound(event); } } }
public void valueUnbound(HttpSessionBindingEvent event) { for (EventListener listener: PluginWebInstanceRepository.getListeners()) { if (listener instanceof HttpSessionActivationListener) { ((HttpSessionBindingListener) listener).valueUnbound(event); } } }