private void handleExpired(RedisSession session) { publishEvent(new SessionExpiredEvent(this, session)); }
private void handleDeleted(RedisSession session) { publishEvent(new SessionDeletedEvent(this, session)); }
private void handleCreated(Map<Object, Object> loaded, String channel) { String id = channel.substring(channel.lastIndexOf(":") + 1); Session session = loadSession(id, loaded); publishEvent(new SessionCreatedEvent(this, session)); }
RedissonSession session = getSession(body); if (session != null) { publishEvent(new SessionCreatedEvent(this, session)); if (session.load()) { session.clearPrincipal(); publishEvent(new SessionDeletedEvent(this, session)); } else { publishEvent(new SessionDeletedEvent(this, id)); if (session.load()) { session.clearPrincipal(); publishEvent(new SessionExpiredEvent(this, session)); } else { publishEvent(new SessionExpiredEvent(this, id));
@Before public void setup() { String sessionId = "session-id"; MapSession session = new MapSession(sessionId); this.attributes = new HashMap<>(); SessionRepositoryMessageInterceptor.setSessionId(this.attributes, sessionId); given(this.wsSession.getAttributes()).willReturn(this.attributes); given(this.wsSession.getPrincipal()).willReturn(this.principal); given(this.wsSession.getId()).willReturn("wsSession-id"); given(this.wsSession2.getAttributes()).willReturn(this.attributes); given(this.wsSession2.getPrincipal()).willReturn(this.principal); given(this.wsSession2.getId()).willReturn("wsSession-id2"); Map<String, Object> headers = new HashMap<>(); headers.put(SimpMessageHeaderAccessor.SESSION_ATTRIBUTES, this.attributes); given(this.message.getHeaders()).willReturn(new MessageHeaders(headers)); this.listener = new WebSocketRegistryListener(); this.connect = new SessionConnectEvent(this.listener, this.wsSession); this.connect2 = new SessionConnectEvent(this.listener, this.wsSession2); this.disconnect = new SessionDisconnectEvent(this.listener, this.message, this.wsSession.getId(), CloseStatus.NORMAL); this.deleted = new SessionDeletedEvent(this.listener, session); this.expired = new SessionExpiredEvent(this.listener, session); }
@Before public void setup() { this.listener = new SessionEventHttpSessionListenerAdapter( Arrays.asList(this.listener1, this.listener2)); this.listener.setServletContext(new MockServletContext()); Session session = new MapSession(); this.destroyed = new SessionDestroyedEvent(this, session); this.created = new SessionCreatedEvent(this, session); }
@Test public void springSessionDestroyedTranslatedToSpringSecurityDestroyed() { Session session = new MapSession(); this.publisher.publishEvent( new org.springframework.session.events.SessionDestroyedEvent(this, session)); assertThat(this.listener.getEvent().getId()).isEqualTo(session.getId()); }
@Test public void onMessageCreated() { MapSession session = this.cached; byte[] pattern = "".getBytes(StandardCharsets.UTF_8); String channel = "spring:session:event:0:created:" + session.getId(); JdkSerializationRedisSerializer defaultSerailizer = new JdkSerializationRedisSerializer(); this.redisRepository.setDefaultSerializer(defaultSerailizer); byte[] body = defaultSerailizer.serialize(new HashMap()); DefaultMessage message = new DefaultMessage( channel.getBytes(StandardCharsets.UTF_8), body); this.redisRepository.setApplicationEventPublisher(this.publisher); this.redisRepository.onMessage(message, pattern); verify(this.publisher).publishEvent(this.event.capture()); assertThat(this.event.getValue().getSessionId()).isEqualTo(session.getId()); }
@Test public void onApplicationEventDestroyed() { this.listener.onApplicationEvent(this.destroyed); verify(this.listener1).sessionDestroyed(this.sessionEvent.capture()); verify(this.listener2).sessionDestroyed(this.sessionEvent.capture()); assertThat(this.sessionEvent.getValue().getSession().getId()) .isEqualTo(this.destroyed.getSessionId()); }
private HttpSessionEvent createHttpSessionEvent(AbstractSessionEvent event) { Session session = event.getSession(); HttpSession httpSession = new HttpSessionAdapter<>(session, this.context); return new HttpSessionEvent(httpSession); }
@Test public void onApplicationEventCreated() { this.listener.onApplicationEvent(this.created); verify(this.listener1).sessionCreated(this.sessionEvent.capture()); verify(this.listener2).sessionCreated(this.sessionEvent.capture()); assertThat(this.sessionEvent.getValue().getSession().getId()) .isEqualTo(this.created.getSessionId()); }
RedissonSession session = getSession(body); if (session != null) { publishEvent(new SessionCreatedEvent(this, session)); if (session.load()) { session.clearPrincipal(); publishEvent(new SessionDeletedEvent(this, session)); } else { publishEvent(new SessionDeletedEvent(this, id)); if (session.load()) { session.clearPrincipal(); publishEvent(new SessionExpiredEvent(this, session)); } else { publishEvent(new SessionExpiredEvent(this, id));
@Override public void entryAdded(EntryEvent<String, MapSession> event) { MapSession session = event.getValue(); if (session.getId().equals(session.getOriginalId())) { if (logger.isDebugEnabled()) { logger.debug("Session created with id: " + session.getId()); } this.eventPublisher.publishEvent(new SessionCreatedEvent(this, session)); } }
@Override public void entryEvicted(EntryEvent<String, MapSession> event) { if (logger.isDebugEnabled()) { logger.debug("Session expired with id: " + event.getOldValue().getId()); } this.eventPublisher .publishEvent(new SessionExpiredEvent(this, event.getOldValue())); }
@Override public void entryRemoved(EntryEvent<String, MapSession> event) { MapSession session = event.getOldValue(); if (session != null) { if (logger.isDebugEnabled()) { logger.debug("Session deleted with id: " + session.getId()); } this.eventPublisher.publishEvent(new SessionDeletedEvent(this, session)); } }
@Test // gh-309 public void onMessageCreatedCustomSerializer() { MapSession session = this.cached; byte[] pattern = "".getBytes(StandardCharsets.UTF_8); byte[] body = new byte[0]; String channel = "spring:session:event:0:created:" + session.getId(); given(this.defaultSerializer.deserialize(body)) .willReturn(new HashMap<String, Object>()); DefaultMessage message = new DefaultMessage( channel.getBytes(StandardCharsets.UTF_8), body); this.redisRepository.setApplicationEventPublisher(this.publisher); this.redisRepository.onMessage(message, pattern); verify(this.publisher).publishEvent(this.event.capture()); assertThat(this.event.getValue().getSessionId()).isEqualTo(session.getId()); verify(this.defaultSerializer).deserialize(body); }
@Override public void onApplicationEvent(ApplicationEvent event) { if (event instanceof SessionDestroyedEvent) { SessionDestroyedEvent e = (SessionDestroyedEvent) event; closeWsSessions(e.getSessionId()); } else if (event instanceof SessionConnectEvent) { SessionConnectEvent e = (SessionConnectEvent) event; afterConnectionEstablished(e.getWebSocketSession()); } else if (event instanceof SessionDisconnectEvent) { SessionDisconnectEvent e = (SessionDisconnectEvent) event; Map<String, Object> sessionAttributes = SimpMessageHeaderAccessor .getSessionAttributes(e.getMessage().getHeaders()); String httpSessionId = (sessionAttributes != null) ? SessionRepositoryMessageInterceptor.getSessionId(sessionAttributes) : null; afterConnectionClosed(httpSessionId, e.getSessionId()); } }
/** * Constructs a new {@link SessionCreatedEvent} initialized with the given {@link Session}, * using the {@link #getSessionRepository() SessionRepository} as the event source. * * @param session {@link Session} that is the subject of the {@link AbstractSessionEvent event}. * @return a new {@link SessionCreatedEvent}. * @see org.springframework.session.events.SessionCreatedEvent * @see org.springframework.session.Session * @see #getSessionRepository() */ protected SessionCreatedEvent newSessionCreatedEvent(Session session) { return new SessionCreatedEvent(getSessionRepository(), session); }
@Test public void onMessageDeletedSessionFound() { String deletedId = "deleted-id"; given(this.redisOperations.boundHashOps(getKey(deletedId))) .willReturn(this.boundHashOperations); Map map = map(RedisOperationsSessionRepository.MAX_INACTIVE_ATTR, 0, RedisOperationsSessionRepository.LAST_ACCESSED_ATTR, System.currentTimeMillis() - TimeUnit.MINUTES.toMillis(5)); given(this.boundHashOperations.entries()).willReturn(map); String channel = "__keyevent@0__:del"; String body = "spring:session:sessions:expires:" + deletedId; DefaultMessage message = new DefaultMessage( channel.getBytes(StandardCharsets.UTF_8), body.getBytes(StandardCharsets.UTF_8)); this.redisRepository.setApplicationEventPublisher(this.publisher); this.redisRepository.onMessage(message, "".getBytes(StandardCharsets.UTF_8)); verify(this.redisOperations).boundHashOps(eq(getKey(deletedId))); verify(this.boundHashOperations).entries(); verify(this.publisher).publishEvent(this.event.capture()); assertThat(this.event.getValue().getSessionId()).isEqualTo(deletedId); verifyZeroInteractions(this.defaultSerializer); verifyZeroInteractions(this.publisher); verifyZeroInteractions(this.redisOperations); verifyZeroInteractions(this.boundHashOperations); }
@Test public void onMessageExpiredSessionFound() { String expiredId = "expired-id"; given(this.redisOperations.boundHashOps(getKey(expiredId))) .willReturn(this.boundHashOperations); Map map = map(RedisOperationsSessionRepository.MAX_INACTIVE_ATTR, 1, RedisOperationsSessionRepository.LAST_ACCESSED_ATTR, System.currentTimeMillis() - TimeUnit.MINUTES.toMillis(5)); given(this.boundHashOperations.entries()).willReturn(map); String channel = "__keyevent@0__:expired"; String body = "spring:session:sessions:expires:" + expiredId; DefaultMessage message = new DefaultMessage( channel.getBytes(StandardCharsets.UTF_8), body.getBytes(StandardCharsets.UTF_8)); this.redisRepository.setApplicationEventPublisher(this.publisher); this.redisRepository.onMessage(message, "".getBytes(StandardCharsets.UTF_8)); verify(this.redisOperations).boundHashOps(eq(getKey(expiredId))); verify(this.boundHashOperations).entries(); verify(this.publisher).publishEvent(this.event.capture()); assertThat(this.event.getValue().getSessionId()).isEqualTo(expiredId); verifyZeroInteractions(this.defaultSerializer); verifyZeroInteractions(this.publisher); verifyZeroInteractions(this.redisOperations); verifyZeroInteractions(this.boundHashOperations); }