Add HttpSessionListener Support
Fixes gh-4
This commit is contained in:
@@ -15,10 +15,13 @@
|
||||
*/
|
||||
package org.springframework.session.data.redis.config.annotation.web.http;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.http.HttpSessionListener;
|
||||
|
||||
import org.springframework.beans.factory.BeanClassLoaderAware;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
@@ -31,14 +34,12 @@ import org.springframework.context.annotation.ImportAware;
|
||||
import org.springframework.core.annotation.AnnotationAttributes;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
import org.springframework.data.redis.connection.MessageListener;
|
||||
import org.springframework.data.redis.connection.RedisConnection;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.core.RedisOperations;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.listener.PatternTopic;
|
||||
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
|
||||
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
|
||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
import org.springframework.session.ExpiringSession;
|
||||
@@ -47,6 +48,7 @@ import org.springframework.session.data.redis.RedisOperationsSessionRepository;
|
||||
import org.springframework.session.data.redis.config.ConfigureNotifyKeyspaceEventsAction;
|
||||
import org.springframework.session.data.redis.config.ConfigureRedisAction;
|
||||
import org.springframework.session.web.http.HttpSessionStrategy;
|
||||
import org.springframework.session.web.http.SessionEventHttpSessionListenerAdapter;
|
||||
import org.springframework.session.web.http.SessionRepositoryFilter;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
@@ -72,6 +74,13 @@ public class RedisHttpSessionConfiguration implements ImportAware, BeanClassLoad
|
||||
|
||||
private ConfigureRedisAction configureRedisAction = new ConfigureNotifyKeyspaceEventsAction();
|
||||
|
||||
private List<HttpSessionListener> httpSessionListeners = new ArrayList<HttpSessionListener>();
|
||||
|
||||
@Bean
|
||||
public SessionEventHttpSessionListenerAdapter sessionEventHttpSessionListenerAdapter() {
|
||||
return new SessionEventHttpSessionListenerAdapter(httpSessionListeners);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RedisMessageListenerContainer redisMessageListenerContainer(
|
||||
RedisConnectionFactory connectionFactory, RedisOperationsSessionRepository messageListener) {
|
||||
@@ -140,6 +149,11 @@ public class RedisHttpSessionConfiguration implements ImportAware, BeanClassLoad
|
||||
this.httpSessionStrategy = httpSessionStrategy;
|
||||
}
|
||||
|
||||
@Autowired(required = false)
|
||||
public void setHttpSessionListeners(List<HttpSessionListener> listeners) {
|
||||
this.httpSessionListeners = listeners;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public InitializingBean enableRedisKeyspaceNotificationsInitializer(RedisConnectionFactory connectionFactory) {
|
||||
return new EnableRedisKeyspaceNotificationsInitializer(connectionFactory, configureRedisAction);
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.session.web.http;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.http.HttpSession;
|
||||
import javax.servlet.http.HttpSessionEvent;
|
||||
import javax.servlet.http.HttpSessionListener;
|
||||
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.session.ExpiringSession;
|
||||
import org.springframework.session.events.AbstractSessionEvent;
|
||||
import org.springframework.session.events.SessionCreatedEvent;
|
||||
import org.springframework.session.events.SessionDestroyedEvent;
|
||||
import org.springframework.web.context.ServletContextAware;
|
||||
|
||||
/**
|
||||
* Receives {@link SessionDestroyedEvent} and {@link SessionCreatedEvent} and
|
||||
* translates them into {@link HttpSessionEvent} and submits the
|
||||
* {@link HttpSessionEvent} to every registered {@link HttpSessionListener}.
|
||||
*
|
||||
* @author Rob Winch
|
||||
* @since 1.1
|
||||
*/
|
||||
public class SessionEventHttpSessionListenerAdapter implements ApplicationListener<AbstractSessionEvent>, ServletContextAware {
|
||||
private final List<HttpSessionListener> listeners;
|
||||
|
||||
private ServletContext context;
|
||||
|
||||
public SessionEventHttpSessionListenerAdapter(List<HttpSessionListener> listeners) {
|
||||
super();
|
||||
this.listeners = listeners;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.context.ApplicationListener#onApplicationEvent(org.springframework.context.ApplicationEvent)
|
||||
*/
|
||||
public void onApplicationEvent(AbstractSessionEvent event) {
|
||||
if(listeners.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
HttpSessionEvent httpSessionEvent = createHttpSessionEvent(event);
|
||||
|
||||
for(HttpSessionListener listener : listeners) {
|
||||
if(event instanceof SessionDestroyedEvent) {
|
||||
listener.sessionDestroyed(httpSessionEvent);
|
||||
} else if(event instanceof SessionCreatedEvent) {
|
||||
listener.sessionCreated(httpSessionEvent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private HttpSessionEvent createHttpSessionEvent(AbstractSessionEvent event) {
|
||||
ExpiringSession session = event.getSession();
|
||||
HttpSession httpSession = new ExpiringSessionHttpSession<ExpiringSession>(session, context);
|
||||
HttpSessionEvent httpSessionEvent = new HttpSessionEvent(httpSession);
|
||||
return httpSessionEvent;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.web.context.ServletContextAware#setServletContext(javax.servlet.ServletContext)
|
||||
*/
|
||||
public void setServletContext(ServletContext servletContext) {
|
||||
this.context = servletContext;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.session.web.http;
|
||||
|
||||
import static org.fest.assertions.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.http.HttpSessionEvent;
|
||||
import javax.servlet.http.HttpSessionListener;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Captor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.springframework.session.MapSession;
|
||||
import org.springframework.session.Session;
|
||||
import org.springframework.session.events.SessionCreatedEvent;
|
||||
import org.springframework.session.events.SessionDestroyedEvent;
|
||||
|
||||
/**
|
||||
* @author Rob Winch
|
||||
* @since 1.1
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class SessionEventHttpSessionListenerAdapterTests {
|
||||
@Mock
|
||||
HttpSessionListener listener1;
|
||||
@Mock
|
||||
HttpSessionListener listener2;
|
||||
@Mock
|
||||
ServletContext servletContext;
|
||||
@Captor
|
||||
ArgumentCaptor<HttpSessionEvent> sessionEvent;
|
||||
|
||||
SessionDestroyedEvent destroyed;
|
||||
SessionCreatedEvent created;
|
||||
SessionEventHttpSessionListenerAdapter listener;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.listener = new SessionEventHttpSessionListenerAdapter(Arrays.asList(listener1, listener2));
|
||||
|
||||
Session session = new MapSession();
|
||||
destroyed = new SessionDestroyedEvent(this, session);
|
||||
created = new SessionCreatedEvent(this, session);
|
||||
}
|
||||
|
||||
// We want relaxed constructor that will allow for an empty listeners to
|
||||
// make configuration easier (i.e. autowire all HttpSessionListeners and might get none)
|
||||
@Test
|
||||
public void constructorEmptyWorks() {
|
||||
new SessionEventHttpSessionListenerAdapter(Collections.<HttpSessionListener>emptyList());
|
||||
}
|
||||
|
||||
/**
|
||||
* Make sure that we short circuit onApplicationEvent as early as possible if no listeners
|
||||
*/
|
||||
@Test
|
||||
public void onApplicationEventEmptyListenersDoesNotUseEvent() {
|
||||
listener = new SessionEventHttpSessionListenerAdapter(Collections.<HttpSessionListener>emptyList());
|
||||
destroyed = mock(SessionDestroyedEvent.class);
|
||||
|
||||
listener.onApplicationEvent(destroyed);
|
||||
|
||||
verifyZeroInteractions(destroyed, listener1, listener2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onApplicationEventDestroyed() {
|
||||
listener.onApplicationEvent(destroyed);
|
||||
|
||||
verify(listener1).sessionDestroyed(sessionEvent.capture());
|
||||
verify(listener2).sessionDestroyed(sessionEvent.capture());
|
||||
|
||||
assertThat(sessionEvent.getValue().getSession().getId()).isEqualTo(destroyed.getSessionId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onApplicationEventCreated() {
|
||||
listener.onApplicationEvent(created);
|
||||
|
||||
verify(listener1).sessionCreated(sessionEvent.capture());
|
||||
verify(listener2).sessionCreated(sessionEvent.capture());
|
||||
|
||||
assertThat(sessionEvent.getValue().getSession().getId()).isEqualTo(created.getSessionId());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user