下面列出了javax.servlet.http.HttpSessionAttributeListener#javax.servlet.http.HttpSessionBindingEvent 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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));
}
@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);
}
}
/**
* 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;
}
/**
* 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;
}
@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() + "登录。");
}
}
@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());
}
/**
* 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;
}
@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));
}
}
/**
* 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);
}
}
}
/**
* 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;
}
@Override
public synchronized void setAttribute(String name, Object value)
{
if (invalid) {
throw new IllegalStateException("Invalid session");
}
attributes.setAttribute(name, value);
if (value instanceof HttpSessionBindingListener) {
final HttpSessionBindingListener listener = (HttpSessionBindingListener) value;
listener.valueBound(new HttpSessionBindingEvent(this, name));
}
}
/**
* 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() + "')");
}
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);
}
}
/**
* 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() + "')");
}
/**
* Record the fact that a servlet context attribute was replaced.
*
* @param event
* The session attribute event
*/
@Override
public void attributeReplaced(HttpSessionBindingEvent event) {
log("attributeReplaced('" + event.getSession().getId() + "', '"
+ event.getName() + "', '" + event.getValue() + "')");
}
/**
* Session value bound event / do nothing
* @param event
*/
@Override
public void valueBound(HttpSessionBindingEvent event) {
if( log.isDebugEnabled() ) {
log.debug("******** valueBound=" + event.getName());
log.debug("******** valueBound=" + event.getValue().toString());
log.debug("******** valueBound=" + event.getSession().getAttribute("khan.uid"));
log.debug(" " + event.getName() + " Session created.");
}
}
/**
* 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));
}
}
}
@Override
public void valueUnbound(HttpSessionBindingEvent event) {
String clientIp = sessionIdClientIp.remove(event.getSession().getId());
if (clientIp != null) {
clientIpSessionId.remove(clientIp);
}
}
public void removeAttribute(String name) {
if (name == null) {
throw new IllegalArgumentException("name must not be null");
}
Object value = this.attributes.remove(name);
if (value instanceof HttpSessionBindingListener) {
((HttpSessionBindingListener) value).valueUnbound(new HttpSessionBindingEvent(this, name, value));
}
}
/**
* @see javax.servlet.http.HttpSessionBindingListener#valueUnbound(HttpSessionBindingEvent)
*/
@Override
public void valueUnbound(HttpSessionBindingEvent event) {
// If we are going to be unbound from the session, the session must be
// closing
// Notify the service
if (service == null) {
getLogger()
.warn("A VaadinSession instance not associated to any service is getting unbound. "
+ "Session destroy events will not be fired and UIs in the session will not get detached. "
+ "This might happen if a session is deserialized but never used before it expires.");
} else if (VaadinService.getCurrentRequest() != null
&& getCurrent() == this) {
checkHasLock();
// Ignore if the session is being moved to a different backing
// session or if GAEVaadinServlet is doing its normal cleanup.
if (getAttribute(
VaadinService.PRESERVE_UNBOUND_SESSION_ATTRIBUTE) == Boolean.TRUE) {
return;
}
// There is still a request in progress for this session. The
// session will be destroyed after the response has been written.
if (getState() == VaadinSessionState.OPEN) {
close();
}
} else {
// We are not in a request related to this session so we can destroy
// it as soon as we acquire the lock.
service.fireSessionDestroy(this);
}
session = null;
}
@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));
}
}
}
/**
* 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));
}
}
}
/**
* Record the fact that a servlet context attribute was replaced.
*
* @param event
* The session attribute event
*/
@Override
public void attributeReplaced(HttpSessionBindingEvent event) {
log("attributeReplaced('" + event.getSession().getId() + "', '"
+ event.getName() + "', '" + event.getValue() + "')");
}
@Override
public void valueUnbound(HttpSessionBindingEvent hsbe) {
// hsbe.getValue() does not work right with tomcat
Optional<MCRSessionResolver> newSessionResolver = Optional
.ofNullable(hsbe.getSession().getAttribute(MCRServlet.ATTR_MYCORE_SESSION))
.filter(o -> o instanceof MCRSessionResolver)
.map(MCRSessionResolver.class::cast);
MCRSessionResolver oldResolver = this;
if (newSessionResolver.isPresent() && !oldResolver.equals(newSessionResolver.get())) {
LOGGER.warn("Attribute {} is beeing unbound from session {} and replaced by {}!", hsbe.getName(),
oldResolver.getSessionID(), newSessionResolver.get());
oldResolver.resolveSession().ifPresent(MCRSession::close);
}
}
@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(HttpSessionBindingEvent hsbe) {
HttpSession session = hsbe.getSession();
if (Objects.equals(hsbe.getName(), SamlSession.class.getName())) {
LOG.debugf("Attribute removed");
unmap(session.getId(), hsbe.getValue());
}
}
@Override
public void removeAttribute(String name) {
if (attributes != null) {
Object value = attributes.get(name);
if (value instanceof HttpSessionBindingListener) {
((HttpSessionBindingListener) value)
.valueUnbound(new HttpSessionBindingEvent(this, name,
value));
}
attributes.remove(name);
}
}
public void removeAttribute(String name) {
if (name == null) {
throw new IllegalArgumentException("name must not be null");
}
Object value = this.attributes.remove(name);
if (value instanceof HttpSessionBindingListener) {
((HttpSessionBindingListener) value).valueUnbound(new HttpSessionBindingEvent(this, name, value));
}
}
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);
}
}