Add ExpiringSession.isExpired
Fixes gh-58
This commit is contained in:
@@ -36,4 +36,11 @@ public interface ExpiringSession extends Session {
|
||||
*/
|
||||
int getMaxInactiveInterval();
|
||||
|
||||
/**
|
||||
* Returns true if the session is expired.
|
||||
*
|
||||
* @return true if the session is expired, else false.
|
||||
*/
|
||||
boolean isExpired();
|
||||
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
@@ -109,6 +110,17 @@ public final class MapSession implements ExpiringSession {
|
||||
return maxInactiveInterval;
|
||||
}
|
||||
|
||||
public boolean isExpired() {
|
||||
return isExpired(System.currentTimeMillis());
|
||||
}
|
||||
|
||||
boolean isExpired(long now) {
|
||||
if(maxInactiveInterval < 0) {
|
||||
return false;
|
||||
}
|
||||
return now - TimeUnit.SECONDS.toMillis(maxInactiveInterval) >= lastAccessedTime;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getAttribute(String attributeName) {
|
||||
return sessionAttrs.get(attributeName);
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
package org.springframework.session;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import static org.fest.assertions.Assertions.assertThat;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import static org.fest.assertions.Assertions.assertThat;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class MapSessionTests {
|
||||
|
||||
@@ -14,6 +14,7 @@ public class MapSessionTests {
|
||||
@Before
|
||||
public void setup() {
|
||||
session = new MapSession();
|
||||
session.setLastAccessedTime(1413258262962L);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
@@ -50,6 +51,24 @@ public class MapSessionTests {
|
||||
assertThat(session.hashCode()).isEqualTo(session.getId().hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isExpiredExact() {
|
||||
long now = 1413260062962L;
|
||||
assertThat(session.isExpired(now)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isExpiredOneMsTooSoon() {
|
||||
long now = 1413260062961L;
|
||||
assertThat(session.isExpired(now)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isExpiredOneMsAfter() {
|
||||
long now = 1413260062963L;
|
||||
assertThat(session.isExpired(now)).isTrue();
|
||||
}
|
||||
|
||||
static class CustomSession implements ExpiringSession {
|
||||
|
||||
@Override
|
||||
@@ -96,6 +115,11 @@ public class MapSessionTests {
|
||||
public void removeAttribute(String attributeName) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isExpired() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user