Browse Source

Add getAttribute to WebSession and ServerWebExchange

pull/1111/head
Rossen Stoyanchev 9 years ago
parent
commit
a173c78d98
  1. 9
      spring-web-reactive/src/main/java/org/springframework/web/server/ServerWebExchange.java
  2. 9
      spring-web-reactive/src/main/java/org/springframework/web/server/WebSession.java
  3. 6
      spring-web-reactive/src/main/java/org/springframework/web/server/adapter/DefaultServerWebExchange.java
  4. 6
      spring-web-reactive/src/main/java/org/springframework/web/server/session/DefaultWebSession.java

9
spring-web-reactive/src/main/java/org/springframework/web/server/ServerWebExchange.java

@ -16,6 +16,7 @@ @@ -16,6 +16,7 @@
package org.springframework.web.server;
import java.util.Map;
import java.util.Optional;
import reactor.core.publisher.Mono;
@ -46,6 +47,14 @@ public interface ServerWebExchange { @@ -46,6 +47,14 @@ public interface ServerWebExchange {
*/
Map<String, Object> getAttributes();
/**
* Return the request attribute value if present.
* @param name the attribute name
* @param <T> the attribute type
* @return the attribute value
*/
<T> Optional<T> getAttribute(String name);
/**
* Return the web session for the current request. Always guaranteed to
* return an instance either matching to the session id requested by the

9
spring-web-reactive/src/main/java/org/springframework/web/server/WebSession.java

@ -18,6 +18,7 @@ package org.springframework.web.server; @@ -18,6 +18,7 @@ package org.springframework.web.server;
import java.time.Duration;
import java.time.Instant;
import java.util.Map;
import java.util.Optional;
import reactor.core.publisher.Mono;
@ -44,6 +45,14 @@ public interface WebSession { @@ -44,6 +45,14 @@ public interface WebSession {
*/
Map<String, Object> getAttributes();
/**
* Return the attribute value if present.
* @param name the attribute name
* @param <T> the attribute type
* @return the attribute value
*/
<T> Optional<T> getAttribute(String name);
/**
* Force the creation of a session causing the session id to be sent when
* {@link #save()} is called.

6
spring-web-reactive/src/main/java/org/springframework/web/server/adapter/DefaultServerWebExchange.java

@ -16,6 +16,7 @@ @@ -16,6 +16,7 @@
package org.springframework.web.server.adapter;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import reactor.core.publisher.EmitterProcessor;
@ -78,6 +79,11 @@ public class DefaultServerWebExchange implements ServerWebExchange { @@ -78,6 +79,11 @@ public class DefaultServerWebExchange implements ServerWebExchange {
return this.attributes;
}
@Override @SuppressWarnings("unchecked")
public <T> Optional<T> getAttribute(String name) {
return Optional.ofNullable((T) this.attributes.get(name));
}
@Override
public Mono<WebSession> getSession() {
if (this.sessionMono == null) {

6
spring-web-reactive/src/main/java/org/springframework/web/server/session/DefaultWebSession.java

@ -20,6 +20,7 @@ import java.time.Clock; @@ -20,6 +20,7 @@ import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;
@ -101,6 +102,11 @@ public class DefaultWebSession implements ConfigurableWebSession, Serializable { @@ -101,6 +102,11 @@ public class DefaultWebSession implements ConfigurableWebSession, Serializable {
return this.attributes;
}
@Override @SuppressWarnings("unchecked")
public <T> Optional<T> getAttribute(String name) {
return Optional.ofNullable((T) this.attributes.get(name));
}
@Override
public Instant getCreationTime() {
return this.creationTime;

Loading…
Cancel
Save