diff --git a/spring-web-reactive/src/main/java/org/springframework/web/reactive/function/server/DefaultServerRequest.java b/spring-web-reactive/src/main/java/org/springframework/web/reactive/function/server/DefaultServerRequest.java index 8229082a1b9..b5de5881a80 100644 --- a/spring-web-reactive/src/main/java/org/springframework/web/reactive/function/server/DefaultServerRequest.java +++ b/spring-web-reactive/src/main/java/org/springframework/web/reactive/function/server/DefaultServerRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-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. @@ -40,6 +40,7 @@ import org.springframework.util.Assert; import org.springframework.web.reactive.function.BodyExtractor; import org.springframework.web.reactive.function.BodyExtractors; import org.springframework.web.server.ServerWebExchange; +import org.springframework.web.server.WebSession; /** * {@code ServerRequest} implementation based on a {@link ServerWebExchange}. @@ -124,6 +125,11 @@ class DefaultServerRequest implements ServerRequest { orElseGet(Collections::emptyMap); } + @Override + public Mono session() { + return this.exchange.getSession(); + } + private ServerHttpRequest request() { return this.exchange.getRequest(); } diff --git a/spring-web-reactive/src/main/java/org/springframework/web/reactive/function/server/RequestPredicates.java b/spring-web-reactive/src/main/java/org/springframework/web/reactive/function/server/RequestPredicates.java index 4c0928fb3cd..0bf23ba27d0 100644 --- a/spring-web-reactive/src/main/java/org/springframework/web/reactive/function/server/RequestPredicates.java +++ b/spring-web-reactive/src/main/java/org/springframework/web/reactive/function/server/RequestPredicates.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-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. @@ -35,6 +35,7 @@ import org.springframework.util.AntPathMatcher; import org.springframework.util.Assert; import org.springframework.util.PathMatcher; import org.springframework.web.reactive.function.BodyExtractor; +import org.springframework.web.server.WebSession; /** * Implementations of {@link RequestPredicate} that implement various useful request matching operations, such as @@ -362,5 +363,10 @@ public abstract class RequestPredicates { public Map pathVariables() { return this.request.pathVariables(); } + + @Override + public Mono session() { + return this.request.session(); + } } } diff --git a/spring-web-reactive/src/main/java/org/springframework/web/reactive/function/server/ServerRequest.java b/spring-web-reactive/src/main/java/org/springframework/web/reactive/function/server/ServerRequest.java index 8355cdbb0c4..f7f6373ee96 100644 --- a/spring-web-reactive/src/main/java/org/springframework/web/reactive/function/server/ServerRequest.java +++ b/spring-web-reactive/src/main/java/org/springframework/web/reactive/function/server/ServerRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-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. @@ -34,6 +34,7 @@ import org.springframework.http.MediaType; import org.springframework.http.codec.json.AbstractJackson2Codec; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.web.reactive.function.BodyExtractor; +import org.springframework.web.server.WebSession; /** * Represents a server-side HTTP request, as handled by a {@code HandlerFunction}. @@ -152,6 +153,15 @@ public interface ServerRequest { */ Map pathVariables(); + /** + * Return the web session for the current request. Always guaranteed to + * return an instance either matching to the session id requested by the + * client, or with a new session id either because the client did not + * specify one or because the underlying session had expired. Use of this + * method does not automatically create a session. + */ + Mono session(); + /** * Represents the headers of the HTTP request. diff --git a/spring-web-reactive/src/main/java/org/springframework/web/reactive/function/server/support/ServerRequestWrapper.java b/spring-web-reactive/src/main/java/org/springframework/web/reactive/function/server/support/ServerRequestWrapper.java index b907279b357..d1a74e400dc 100644 --- a/spring-web-reactive/src/main/java/org/springframework/web/reactive/function/server/support/ServerRequestWrapper.java +++ b/spring-web-reactive/src/main/java/org/springframework/web/reactive/function/server/support/ServerRequestWrapper.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-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. @@ -36,6 +36,7 @@ import org.springframework.util.Assert; import org.springframework.web.reactive.function.BodyExtractor; import org.springframework.web.reactive.function.server.HandlerFunction; import org.springframework.web.reactive.function.server.ServerRequest; +import org.springframework.web.server.WebSession; /** * Implementation of the {@link ServerRequest} interface that can be subclassed to adapt the request to a @@ -131,6 +132,11 @@ public class ServerRequestWrapper implements ServerRequest { return this.request.pathVariables(); } + @Override + public Mono session() { + return this.request.session(); + } + /** * Implementation of the {@link Headers} interface that can be subclassed to adapt the headers to a * {@link HandlerFunction handler function}. All methods default to calling through to the wrapped headers. diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/function/server/DefaultServerRequestTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/function/server/DefaultServerRequestTests.java index 324bc14aada..d76ec3e1c96 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/function/server/DefaultServerRequestTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/function/server/DefaultServerRequestTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-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. @@ -48,6 +48,7 @@ import org.springframework.http.server.reactive.ServerHttpResponse; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.server.ServerWebExchange; +import org.springframework.web.server.WebSession; import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.mock; @@ -138,6 +139,14 @@ public class DefaultServerRequestTests { assertEquals(pathVariables, defaultRequest.pathVariables()); } + @Test + public void session() throws Exception { + WebSession session = mock(WebSession.class); + when(mockExchange.getSession()).thenReturn(Mono.just(session)); + + assertEquals(session, defaultRequest.session().block()); + } + @Test public void header() throws Exception { HttpHeaders httpHeaders = new HttpHeaders(); diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/function/server/MockServerRequest.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/function/server/MockServerRequest.java index 87b73b490ef..ed8e2c549f4 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/function/server/MockServerRequest.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/function/server/MockServerRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-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. @@ -41,6 +41,7 @@ import org.springframework.util.Assert; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.reactive.function.BodyExtractor; +import org.springframework.web.server.WebSession; /** * @author Arjen Poutsma @@ -61,10 +62,12 @@ public class MockServerRequest implements ServerRequest { private final Map pathVariables; + private final WebSession session; + private MockServerRequest(HttpMethod method, URI uri, MockHeaders headers, Object body, Map attributes, MultiValueMap queryParams, - Map pathVariables) { + Map pathVariables, WebSession session) { this.method = method; this.uri = uri; this.headers = headers; @@ -72,6 +75,7 @@ public class MockServerRequest implements ServerRequest { this.attributes = attributes; this.queryParams = queryParams; this.pathVariables = pathVariables; + this.session = session; } public static Builder builder() { @@ -132,6 +136,11 @@ public class MockServerRequest implements ServerRequest { return Collections.unmodifiableMap(this.pathVariables); } + @Override + public Mono session() { + return Mono.justOrEmpty(this.session); + } + public interface Builder { Builder method(HttpMethod method); @@ -154,6 +163,8 @@ public class MockServerRequest implements ServerRequest { Builder pathVariables(Map pathVariables); + Builder session(WebSession session); + MockServerRequest body(Object body); MockServerRequest build(); @@ -176,6 +187,8 @@ public class MockServerRequest implements ServerRequest { private Map pathVariables = new LinkedHashMap<>(); + private WebSession session; + @Override public Builder method(HttpMethod method) { Assert.notNull(method, "'method' must not be null"); @@ -250,17 +263,24 @@ public class MockServerRequest implements ServerRequest { return this; } + @Override + public Builder session(WebSession session) { + Assert.notNull(session, "'session' must not be null"); + this.session = session; + return this; + } + @Override public MockServerRequest body(Object body) { this.body = body; return new MockServerRequest(this.method, this.uri, this.headers, this.body, - this.attributes, this.queryParams, this.pathVariables); + this.attributes, this.queryParams, this.pathVariables, this.session); } @Override public MockServerRequest build() { return new MockServerRequest(this.method, this.uri, this.headers, null, - this.attributes, this.queryParams, this.pathVariables); + this.attributes, this.queryParams, this.pathVariables, this.session); } }