javax.servlet.http.HttpSessionBindingEvent Java Examples

The following examples show how to use javax.servlet.http.HttpSessionBindingEvent. 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: MockHttpSession.java    From gocd with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #2
Source File: MockHttpSession.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * 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;
}
 
Example #3
Source File: HttpFlexSession.java    From flex-blazeds with Apache License 2.0 6 votes vote down vote up
/**
 * HttpSessionAttributeListener callback; processes the replacement of an attribute in an HttpSession.
 *
 * NOTE: Callback is not made against an HttpFlexSession associated with a request
 * handling thread.
 * @param event the HttpSessionBindingEvent
 */
public void attributeReplaced(HttpSessionBindingEvent event)
{
    if (!event.getName().equals(SESSION_ATTRIBUTE))
    {
        // Accessing flexSession via map because it may have already been unbound from httpSession.
        Map httpSessionToFlexSessionMap = getHttpSessionToFlexSessionMap(event.getSession());
        HttpFlexSession flexSession = (HttpFlexSession)httpSessionToFlexSessionMap.get(event.getSession().getId());
        if (flexSession != null)
        {
            String name = event.getName();
            Object value = event.getValue();
            Object newValue = flexSession.getAttribute(name);
            flexSession.notifyAttributeUnbound(name, value);
            flexSession.notifyAttributeReplaced(name, value);
            flexSession.notifyAttributeBound(name, newValue);
        }
    }
}
 
Example #4
Source File: SessionListenerBridge.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@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));
    }
}
 
Example #5
Source File: MockHttpSession.java    From live-chat-engine with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #6
Source File: MockHttpSession.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public void setAttribute(String name, @Nullable Object value) {
	assertIsValid();
	Assert.notNull(name, "Attribute name must not be null");
	if (value != null) {
		Object oldValue = this.attributes.put(name, value);
		if (value != oldValue) {
			if (oldValue instanceof HttpSessionBindingListener) {
				((HttpSessionBindingListener) oldValue).valueUnbound(new HttpSessionBindingEvent(this, name, oldValue));
			}
			if (value instanceof HttpSessionBindingListener) {
				((HttpSessionBindingListener) value).valueBound(new HttpSessionBindingEvent(this, name, value));
			}
		}
	}
	else {
		removeAttribute(name);
	}
}
 
Example #7
Source File: VaadinSessionTest.java    From flow with Apache License 2.0 6 votes vote down vote up
@Test
public void testValueUnbound() {
    MockVaadinSession vaadinSession = new MockVaadinSession(mockService);

    vaadinSession.valueUnbound(
            EasyMock.createMock(HttpSessionBindingEvent.class));
    org.junit.Assert.assertEquals(
            "'valueUnbound' method doesn't call 'close' for the session", 1,
            vaadinSession.getCloseCount());

    vaadinSession.valueUnbound(
            EasyMock.createMock(HttpSessionBindingEvent.class));

    org.junit.Assert.assertEquals(
            "'valueUnbound' method may not call 'close' "
                    + "method for closing session",
            1, vaadinSession.getCloseCount());
}
 
Example #8
Source File: TestHttpSessionNotifier.java    From HttpSessionReplacer with MIT License 6 votes vote down vote up
@Test
public void testSessionDestroyed() {
  HttpSessionBindingListener bindingListener = mock(HttpSessionBindingListener.class);
  String dummy = "dummy";
  when(session.getAttribute("binding")).thenReturn(bindingListener);
  when(session.getAttribute("attribute")).thenReturn(dummy);
  when(session.getAttributeNamesWithValues()).thenReturn(Arrays.asList("binding", "attribute"));
  HttpSessionListener listener = mock(HttpSessionListener.class);
  descriptor.addHttpSessionListener(listener);
  notifier.sessionDestroyed(session, false);
  verify(bindingListener).valueUnbound(any(HttpSessionBindingEvent.class));
  verify(listener).sessionDestroyed(any(HttpSessionEvent.class));
  HttpSessionListener listener2 = mock(HttpSessionListener.class);
  HttpSessionBindingListener bindingListener2 = mock(HttpSessionBindingListener.class);
  when(session.getAttribute("binding2")).thenReturn(bindingListener2);
  when(session.getAttributeNamesWithValues()).thenReturn(Arrays.asList("binding", "attribute", "binding2"));
  descriptor.addHttpSessionListener(listener2);
  notifier.sessionDestroyed(session, false);
  verify(listener, times(2)).sessionDestroyed(any(HttpSessionEvent.class));
  verify(bindingListener, times(2)).valueUnbound(any(HttpSessionBindingEvent.class));
  verify(bindingListener2).valueUnbound(any(HttpSessionBindingEvent.class));
  verify(listener2).sessionDestroyed(any(HttpSessionEvent.class));
}
 
Example #9
Source File: MockHttpSession.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #10
Source File: LoginSessionListener.java    From java-tutorial with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
@Override
public void attributeAdded(HttpSessionBindingEvent event) {
	String name = event.getName();

	// 登录
	if (name.equals("personInfo")) {

		PersonInfo personInfo = (PersonInfo) event.getValue();

		if (map.get(personInfo.getAccount()) != null) {

			// map 中有记录,表明该帐号在其他机器上登录过,将以前的登录失效
			HttpSession session = map.get(personInfo.getAccount());
			PersonInfo oldPersonInfo = (PersonInfo) session.getAttribute("personInfo");

			logger.debug("帐号" + oldPersonInfo.getAccount() + "在" + oldPersonInfo.getIp() + "已经登录,该登录将被迫下线。");

			session.removeAttribute("personInfo");
			session.setAttribute("msg", "您的帐号已经在其他机器上登录,您被迫下线。");
		}

		// 将session以用户名为索引,放入map中
		map.put(personInfo.getAccount(), event.getSession());
		logger.debug("帐号" + personInfo.getAccount() + "在" + personInfo.getIp() + "登录。");
	}
}
 
Example #11
Source File: NettyHttpSession.java    From Jinx with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example #12
Source File: UserSession.java    From olat with Apache License 2.0 5 votes vote down vote up
/**
*/
  @Override
  public void valueBound(HttpSessionBindingEvent be) {
      if (log.isDebugEnabled()) {
          log.debug("Opened UserSession:" + this.toString());
      }
  }
 
Example #13
Source File: SessionListener.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Record the fact that a servlet context attribute was removed.
 *
 * @param event
 *            The session attribute event
 */
@Override
public void attributeRemoved(HttpSessionBindingEvent event) {

    log("attributeRemoved('" + event.getSession().getId() + "', '"
            + event.getName() + "', '" + event.getValue() + "')");

}
 
Example #14
Source File: NettyHttpSession.java    From Jinx with Apache License 2.0 5 votes vote down vote up
@Override
public void setAttribute(String name, Object value) {
    if (attributes == null) attributes = new ConcurrentHashMap<String, Object>();

    attributes.put(name, value);

    if (value != null && value instanceof HttpSessionBindingListener) {
        ((HttpSessionBindingListener) value).valueBound(new HttpSessionBindingEvent(this, name, value));
    }

}
 
Example #15
Source File: WebSystemHttpSessionListener.java    From bamboobsc with Apache License 2.0 5 votes vote down vote up
@Override
public void attributeRemoved(HttpSessionBindingEvent event) {
	try {
		this.clearLoginUser(event);
	} catch (Exception e) {
		e.printStackTrace();
	}	
}
 
Example #16
Source File: DefaultHttpSessionManager.java    From piranha with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Attributed removed.
 *
 * @param session the HTTP session.
 * @param name the name.
 */
@Override
public void attributeRemoved(HttpSession session, String name) {
    attributeListeners.stream().forEach((listener) -> {
        listener.attributeRemoved(new HttpSessionBindingEvent(session, name));
    });
}
 
Example #17
Source File: PageStoreManager.java    From onedev with MIT License 5 votes vote down vote up
@Override
public void valueUnbound(HttpSessionBindingEvent event)
{
	if (STORING_TOUCHED_PAGES.get())
	{
		// triggered by #storeTouchedPages(), so do not remove the data
		return;
	}
	clear();
}
 
Example #18
Source File: MockHttpSession.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * 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));
		}
	}
}
 
Example #19
Source File: RubyConsole.java    From emissary with Apache License 2.0 5 votes vote down vote up
/**
 * From javax.servlet.http.SessionBindingListener
 */
@Override
public void valueUnbound(HttpSessionBindingEvent e) {
    if (logger.isDebugEnabled()) {
        logger.debug("Received value unbound event on " + e.getName() + " from session " + e.getSession().getId());
    }
    this.stop();
}
 
Example #20
Source File: MockHttpSession.java    From live-chat-engine with Apache License 2.0 5 votes vote down vote up
@Override
public void setAttribute(String name, Object value) {
	if (value != null) {
		this.attributes.put(name, value);
		if (value instanceof HttpSessionBindingListener) {
			((HttpSessionBindingListener) value).valueBound(new HttpSessionBindingEvent(this, name, value));
		}
	}
	else {
		removeAttribute(name);
	}
}
 
Example #21
Source File: CrawlerSessionManagerHandler.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void valueUnbound(HttpSessionBindingEvent event) {
    if (sessionIdClientIp != null) {
        String clientIp = sessionIdClientIp.remove(event.getSession().getId());
        if (clientIp != null) {
            clientIpSessionId.remove(clientIp);
        }
    }
}
 
Example #22
Source File: VertxWrappedSessionUT.java    From vertx-vaadin with MIT License 5 votes vote down vote up
@Test
public void removeAttributeShuoldInvokeValueUnboundForHttpSessionBindingListeners() throws Exception {
    String attrName = "attributeName";
    when(session.remove(attrName)).thenReturn(sessionBindingListenerObject);
    vertxWrappedSession.removeAttribute(attrName);
    ArgumentCaptor<HttpSessionBindingEvent> sessionBindingEventCaptor = ArgumentCaptor.forClass(HttpSessionBindingEvent.class);
    verify(sessionBindingListenerObject).valueUnbound(sessionBindingEventCaptor.capture());
    assertHttpSessionBindingEvent(attrName, sessionBindingEventCaptor.getValue());
}
 
Example #23
Source File: EntityHttpServletRequest.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * Clear all of this session's attributes.
 */
public void clearAttributes() {
    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 HttpSessionBindingListener) {
            ((HttpSessionBindingListener) value).valueUnbound(new HttpSessionBindingEvent(this, name, value));
        }
    }
}
 
Example #24
Source File: SessionListener.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Record the fact that a servlet context attribute was added.
 *
 * @param event
 *            The session attribute event
 */
@Override
public void attributeAdded(HttpSessionBindingEvent event) {

    log("attributeAdded('" + event.getSession().getId() + "', '"
            + event.getName() + "', '" + event.getValue() + "')");

}
 
Example #25
Source File: MockPortletSession.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
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));
		}
	}
}
 
Example #26
Source File: MockHttpSession.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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);
	}
}
 
Example #27
Source File: MockPortletSession.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
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));
		}
	}
}
 
Example #28
Source File: MockHttpSession.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * 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));
		}
	}
}
 
Example #29
Source File: MockHttpSession.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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));
	}
}
 
Example #30
Source File: ChartDeleter.java    From SIMVA-SoS with Apache License 2.0 5 votes vote down vote up
/**
 * When this object is unbound from the session (including upon session
 * expiry) the files that have been added to the ArrayList are iterated
 * and deleted.
 *
 * @param event  the session unbind event.
 */
@Override
public void valueUnbound(HttpSessionBindingEvent event) {
    Iterator iter = this.chartNames.listIterator();
    while (iter.hasNext()) {
        String filename = (String) iter.next();
        File file = new File(
            System.getProperty("java.io.tmpdir"), filename
        );
        if (file.exists()) {
            file.delete();
        }
    }
}