Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2016 the original author or authors.
* Copyright 2014-2017 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.
Expand Down Expand Up @@ -29,16 +29,11 @@
*/
@SuppressWarnings("serial")
public abstract class AbstractSessionEvent extends ApplicationEvent {

private final String sessionId;

private final Session session;

protected AbstractSessionEvent(Object source, String sessionId) {
super(source);
this.sessionId = sessionId;
this.session = null;
}

AbstractSessionEvent(Object source, Session session) {
super(source);
this.session = session;
Expand All @@ -62,4 +57,5 @@ public <S extends Session> S getSession() {
public String getSessionId() {
return this.sessionId;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2016 the original author or authors.
* Copyright 2014-2017 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.
Expand All @@ -25,19 +25,14 @@
*
* @author Rob Winch
* @since 1.0
*
*/
@SuppressWarnings("serial")
public class SessionCreatedEvent extends AbstractSessionEvent {

public SessionCreatedEvent(Object source, String sessionId) {
super(source, sessionId);
}

/**
* Create a new {@link SessionCreatedEvent}.
* @param source The Source of the SessionCreatedEvent
* @param session the Session that was created
* @param source the source of the event
* @param session the session that was created
*/
public SessionCreatedEvent(Object source, Session session) {
super(source, session);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2016 the original author or authors.
* Copyright 2014-2017 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.
Expand All @@ -26,16 +26,17 @@
* @author Mark Anderson
* @author Rob Winch
* @since 1.1
*
*/
@SuppressWarnings("serial")
public class SessionDeletedEvent extends SessionDestroyedEvent {

public SessionDeletedEvent(Object source, String sessionId) {
super(source, sessionId);
}

/**
* Create a new {@link SessionDeletedEvent}.
* @param source the source of the event
* @param session the session that was created
*/
public SessionDeletedEvent(Object source, Session session) {
super(source, session);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2016 the original author or authors.
* Copyright 2014-2017 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.
Expand All @@ -23,21 +23,17 @@
*
* @author Rob Winch
* @since 1.0
*
*/
@SuppressWarnings("serial")
public class SessionDestroyedEvent extends AbstractSessionEvent {

public SessionDestroyedEvent(Object source, String sessionId) {
super(source, sessionId);
}

/**
* Create a new {@link SessionDestroyedEvent}.
* @param source The Source of the SessionDestoryedEvent
* @param session the Session that was created
* @param source the source of the event
* @param session the session that was created
*/
public SessionDestroyedEvent(Object source, Session session) {
super(source, session);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2016 the original author or authors.
* Copyright 2014-2017 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.
Expand All @@ -26,16 +26,17 @@
* @author Mark Anderson
* @author Rob Winch
* @since 1.1
*
*/
@SuppressWarnings("serial")
public class SessionExpiredEvent extends SessionDestroyedEvent {

public SessionExpiredEvent(Object source, String sessionId) {
super(source, sessionId);
}

/**
* Create a new {@link SessionExpiredEvent}.
* @param source the source of the event
* @param session the session that was created
*/
public SessionExpiredEvent(Object source, Session session) {
super(source, session);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,22 @@
*/
@SuppressWarnings("deprecation")
class HttpSessionAdapter<S extends Session> implements HttpSession {

private S session;

private final ServletContext servletContext;

private boolean invalidated;

private boolean old;

HttpSessionAdapter(S session, ServletContext servletContext) {
if (session == null) {
throw new IllegalArgumentException("session cannot be null");
}
if (servletContext == null) {
throw new IllegalArgumentException("servletContext cannot be null");
}
this.session = session;
this.servletContext = servletContext;
}
Expand Down Expand Up @@ -162,6 +172,7 @@ private void checkState() {
}

private static final HttpSessionContext NOOP_SESSION_CONTEXT = new HttpSessionContext() {

@Override
public HttpSession getSession(String sessionId) {
return null;
Expand All @@ -171,9 +182,11 @@ public HttpSession getSession(String sessionId) {
public Enumeration<String> getIds() {
return EMPTY_ENUMERATION;
}

};

private static final Enumeration<String> EMPTY_ENUMERATION = new Enumeration<String>() {

@Override
public boolean hasMoreElements() {
return false;
Expand All @@ -183,5 +196,7 @@ public boolean hasMoreElements() {
public String nextElement() {
throw new NoSuchElementException("a");
}

};

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import org.springframework.mock.web.MockServletContext;
import org.springframework.session.MapSession;
import org.springframework.session.Session;
import org.springframework.session.events.SessionCreatedEvent;
Expand All @@ -42,28 +43,37 @@
import static org.mockito.Mockito.verifyZeroInteractions;

/**
* Tests for {@link SessionEventHttpSessionListenerAdapter}.
*
* @author Rob Winch
* @since 1.1
*/
@RunWith(MockitoJUnitRunner.class)
public class SessionEventHttpSessionListenerAdapterTests {

@Mock
HttpSessionListener listener1;
private HttpSessionListener listener1;

@Mock
HttpSessionListener listener2;
private HttpSessionListener listener2;

@Mock
ServletContext servletContext;
private ServletContext servletContext;

@Captor
ArgumentCaptor<HttpSessionEvent> sessionEvent;
private ArgumentCaptor<HttpSessionEvent> sessionEvent;

private SessionDestroyedEvent destroyed;

SessionDestroyedEvent destroyed;
SessionCreatedEvent created;
SessionEventHttpSessionListenerAdapter listener;
private SessionCreatedEvent created;

private SessionEventHttpSessionListenerAdapter listener;

@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);
Expand Down Expand Up @@ -114,4 +124,5 @@ public void onApplicationEventCreated() {
assertThat(this.sessionEvent.getValue().getSession().getId())
.isEqualTo(this.created.getSessionId());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
import org.springframework.session.MapSession;
import org.springframework.session.events.SessionDeletedEvent;
import org.springframework.session.events.SessionExpiredEvent;
import org.springframework.session.web.socket.events.SessionConnectEvent;
Expand All @@ -46,36 +47,40 @@

@RunWith(MockitoJUnitRunner.class)
public class WebSocketRegistryListenerTests {

@Mock
WebSocketSession wsSession;
@Mock
WebSocketSession wsSession2;
private WebSocketSession wsSession;

@Mock
Message<byte[]> message;
private WebSocketSession wsSession2;

@Mock
Principal principal;
private Message<byte[]> message;

SessionConnectEvent connect;
@Mock
private Principal principal;

SessionConnectEvent connect2;
private SessionConnectEvent connect;

SessionDisconnectEvent disconnect;
private SessionConnectEvent connect2;

SessionDeletedEvent deleted;
private SessionDisconnectEvent disconnect;

SessionExpiredEvent expired;
private SessionDeletedEvent deleted;

Map<String, Object> attributes;
private SessionExpiredEvent expired;

String sessionId;
private Map<String, Object> attributes;

WebSocketRegistryListener listener;
private WebSocketRegistryListener listener;

@Before
public void setup() {
this.sessionId = "session-id";
String sessionId = "session-id";
MapSession session = new MapSession(sessionId);

this.attributes = new HashMap<>();
SessionRepositoryMessageInterceptor.setSessionId(this.attributes, this.sessionId);
SessionRepositoryMessageInterceptor.setSessionId(this.attributes, sessionId);

given(this.wsSession.getAttributes()).willReturn(this.attributes);
given(this.wsSession.getPrincipal()).willReturn(this.principal);
Expand All @@ -94,8 +99,8 @@ public void setup() {
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, this.sessionId);
this.expired = new SessionExpiredEvent(this.listener, this.sessionId);
this.deleted = new SessionDeletedEvent(this.listener, session);
this.expired = new SessionExpiredEvent(this.listener, session);
}

@Test
Expand Down Expand Up @@ -170,4 +175,5 @@ public void onApplicationEventConnectConnectDisonnect() throws Exception {
verify(this.wsSession2).close(WebSocketRegistryListener.SESSION_EXPIRED_STATUS);
verify(this.wsSession, times(0)).close(any(CloseStatus.class));
}

}
Loading