javax.servlet.http.HttpSessionAttributeListener#javax.servlet.http.HttpSessionBindingEvent源码实例Demo

下面列出了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));
}
 
源代码2 项目: spring-analysis-note   文件: MockHttpSession.java
@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);
	}
}
 
源代码3 项目: live-chat-engine   文件: MockHttpSession.java
/**
 * 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;
}
 
源代码4 项目: spring-analysis-note   文件: MockHttpSession.java
/**
 * 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;
}
 
源代码5 项目: java-tutorial   文件: LoginSessionListener.java
@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() + "登录。");
	}
}
 
源代码6 项目: flow   文件: VaadinSessionTest.java
@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());
}
 
源代码7 项目: gocd   文件: MockHttpSession.java
/**
 * 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;
}
 
源代码8 项目: lams   文件: SessionListenerBridge.java
@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));
    }
}
 
源代码9 项目: flex-blazeds   文件: HttpFlexSession.java
/**
 * 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);
        }
    }
}
 
源代码10 项目: spring4-understanding   文件: MockHttpSession.java
/**
 * 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;
}
 
源代码11 项目: knopflerfish.org   文件: HttpSessionImpl.java
@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));
  }
}
 
源代码12 项目: Tomcat8-Source-Read   文件: SessionListener.java
/**
 * 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() + "')");

}
 
源代码13 项目: lams   文件: ApplicationListeners.java
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);
    }
}
 
源代码14 项目: Tomcat8-Source-Read   文件: SessionListener.java
/**
 * 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() + "')");

}
 
源代码15 项目: tomcatsrc   文件: SessionListener.java
/**
 * 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() + "')");

}
 
源代码16 项目: khan-session   文件: SessionLoginManager.java
/**
 * 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.");
    }
}
 
源代码17 项目: gocd   文件: MockHttpSession.java
/**
 * 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));
		}
	}
}
 
源代码18 项目: Tomcat7.0.67   文件: CrawlerSessionManagerValve.java
@Override
public void valueUnbound(HttpSessionBindingEvent event) {
    String clientIp = sessionIdClientIp.remove(event.getSession().getId());
    if (clientIp != null) {
        clientIpSessionId.remove(clientIp);
    }
}
 
源代码19 项目: sakai   文件: EntityHttpServletRequest.java
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));
    }
}
 
源代码20 项目: flow   文件: VaadinSession.java
/**
 * @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;
}
 
源代码21 项目: spring-analysis-note   文件: MockHttpSession.java
@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));
	}
}
 
源代码22 项目: spring-analysis-note   文件: MockHttpSession.java
/**
 * 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));
		}
	}
}
 
源代码23 项目: spring4-understanding   文件: MockHttpSession.java
/**
 * 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));
		}
	}
}
 
源代码24 项目: tomcatsrc   文件: SessionListener.java
/**
 * 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() + "')");

}
 
源代码25 项目: mycore   文件: MCRSessionResolver.java
@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);
    }

}
 
源代码26 项目: quarkus-http   文件: SessionListenerBridge.java
@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));
    }
}
 
源代码27 项目: keycloak   文件: IdMapperUpdaterSessionListener.java
@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());
    }
}
 
源代码28 项目: cxf   文件: NettyHttpSession.java
@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);
    }
}
 
源代码29 项目: sakai   文件: EntityHttpServletRequest.java
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));
    }
}
 
源代码30 项目: lams   文件: ApplicationListeners.java
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);
    }
}