Session Optional<T> getAttribute -> T getAttribute

Issue gh-819
This commit is contained in:
Rob Winch
2017-06-30 10:07:58 -05:00
parent ab3e280993
commit 8ef36e4f3e
19 changed files with 120 additions and 167 deletions

View File

@@ -21,7 +21,6 @@ import java.time.Duration;
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
@@ -94,8 +93,10 @@ public final class MapSession implements Session, Serializable {
this.sessionAttrs = new HashMap<>(
session.getAttributeNames().size());
for (String attrName : session.getAttributeNames()) {
session.getAttribute(attrName)
.ifPresent(attrValue -> this.sessionAttrs.put(attrName, attrValue));
Object attrValue = session.getAttribute(attrName);
if(attrValue != null) {
this.sessionAttrs.put(attrName, attrValue);
}
}
this.lastAccessedTime = session.getLastAccessedTime();
this.creationTime = session.getCreationTime();
@@ -138,8 +139,8 @@ public final class MapSession implements Session, Serializable {
}
@SuppressWarnings("unchecked")
public <T> Optional<T> getAttribute(String attributeName) {
return Optional.ofNullable((T) this.sessionAttrs.get(attributeName));
public <T> T getAttribute(String attributeName) {
return (T) this.sessionAttrs.get(attributeName);
}
public Set<String> getAttributeNames() {

View File

@@ -18,7 +18,6 @@ package org.springframework.session;
import java.time.Duration;
import java.time.Instant;
import java.util.Optional;
import java.util.Set;
/**
@@ -42,12 +41,12 @@ public interface Session {
* Gets the Object associated with the specified name or null if no Object is
* associated to that name.
*
* @param <T> The return type of the attribute
* @param attributeName the name of the attribute to get
* @return the Object associated with the specified name or null if no Object is
* associated to that name
* @param <T> The return type of the attribute
*/
<T> Optional<T> getAttribute(String attributeName);
<T> T getAttribute(String attributeName);
/**
* Gets the attribute names that have a value associated with it. Each value can be

View File

@@ -17,7 +17,6 @@
package org.springframework.session.security;
import java.util.Date;
import java.util.Optional;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -55,11 +54,10 @@ class SpringSessionBackedSessionInformation<S extends Session>
super(resolvePrincipal(session), session.getId(),
Date.from(session.getLastAccessedTime()));
this.sessionRepository = sessionRepository;
session.getAttribute(EXPIRED_ATTR).ifPresent(expired -> {
if (Boolean.TRUE.equals(expired)) {
super.expireNow();
}
});
Boolean expired = session.getAttribute(EXPIRED_ATTR);
if (Boolean.TRUE.equals(expired)) {
super.expireNow();
}
}
/**
@@ -69,16 +67,16 @@ class SpringSessionBackedSessionInformation<S extends Session>
* @return the principal's name, or empty String if it couldn't be determined
*/
private static String resolvePrincipal(Session session) {
Optional<String> principalName = session
String principalName = session
.getAttribute(FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME);
if (principalName.isPresent()) {
return principalName.get();
if (principalName != null) {
return principalName;
}
Optional<SecurityContext> securityContext = session
SecurityContext securityContext = session
.getAttribute(SPRING_SECURITY_CONTEXT);
if (securityContext.isPresent()
&& securityContext.get().getAuthentication() != null) {
return securityContext.get().getAuthentication().getName();
if (securityContext != null
&& securityContext.getAuthentication() != null) {
return securityContext.getAuthentication().getName();
}
return "";
}

View File

@@ -69,7 +69,7 @@ public class SpringSessionBackedSessionRegistry<S extends Session>
List<SessionInformation> infos = new ArrayList<>();
for (S session : sessions) {
if (includeExpiredSessions || !Boolean.TRUE.equals(session
.getAttribute(SpringSessionBackedSessionInformation.EXPIRED_ATTR).orElse(false))) {
.getAttribute(SpringSessionBackedSessionInformation.EXPIRED_ATTR))) {
infos.add(new SpringSessionBackedSessionInformation<>(session,
this.sessionRepository));
}

View File

@@ -16,18 +16,17 @@
package org.springframework.session.web.http;
import org.springframework.session.Session;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionContext;
import java.time.Duration;
import java.util.Collections;
import java.util.Enumeration;
import java.util.NoSuchElementException;
import java.util.Set;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionContext;
import org.springframework.session.Session;
/**
* Adapts Spring Session's {@link Session} to an {@link HttpSession}.
*
@@ -87,7 +86,7 @@ class HttpSessionAdapter<S extends Session> implements HttpSession {
public Object getAttribute(String name) {
checkState();
return this.session.getAttribute(name).orElse(null);
return this.session.getAttribute(name);
}
public Object getValue(String name) {

View File

@@ -16,14 +16,13 @@
package org.springframework.session;
import java.time.Duration;
import java.time.Instant;
import java.util.Optional;
import java.util.Set;
import org.junit.Before;
import org.junit.Test;
import java.time.Duration;
import java.time.Instant;
import java.util.Set;
import static org.assertj.core.api.Assertions.assertThat;
public class MapSessionTests {
@@ -114,8 +113,8 @@ public class MapSessionTests {
return Duration.ZERO;
}
public <T> Optional<T> getAttribute(String attributeName) {
return Optional.empty();
public <T> T getAttribute(String attributeName) {
return null;
}
public Set<String> getAttributeNames() {

View File

@@ -16,20 +16,12 @@
package org.springframework.session.security;
import java.time.Instant;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.security.core.session.SessionInformation;
@@ -38,10 +30,14 @@ import org.springframework.session.FindByIndexNameSessionRepository;
import org.springframework.session.MapSession;
import org.springframework.session.Session;
import java.time.Instant;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.mock;
import static org.mockito.BDDMockito.verify;
import static org.mockito.BDDMockito.when;
import static org.mockito.BDDMockito.*;
/**
* Tests for {@link SpringSessionBackedSessionRegistry}.
@@ -140,7 +136,7 @@ public class SpringSessionBackedSessionRegistryTest {
verify(this.sessionRepository).save(captor.capture());
assertThat(captor.getValue().<Boolean>getAttribute(
SpringSessionBackedSessionInformation.EXPIRED_ATTR))
.isEqualTo(Optional.of(Boolean.TRUE));
.isEqualTo(Boolean.TRUE);
}
private Session createSession(String sessionId, String userName,