Browse Source

Expose WebSession on ServerRequest

This commit exposes the WebSession on ServerRequest.
pull/1294/head
Arjen Poutsma 9 years ago
parent
commit
d724644588
  1. 8
      spring-web-reactive/src/main/java/org/springframework/web/reactive/function/server/DefaultServerRequest.java
  2. 8
      spring-web-reactive/src/main/java/org/springframework/web/reactive/function/server/RequestPredicates.java
  3. 12
      spring-web-reactive/src/main/java/org/springframework/web/reactive/function/server/ServerRequest.java
  4. 8
      spring-web-reactive/src/main/java/org/springframework/web/reactive/function/server/support/ServerRequestWrapper.java
  5. 11
      spring-web-reactive/src/test/java/org/springframework/web/reactive/function/server/DefaultServerRequestTests.java
  6. 28
      spring-web-reactive/src/test/java/org/springframework/web/reactive/function/server/MockServerRequest.java

8
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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.BodyExtractor;
import org.springframework.web.reactive.function.BodyExtractors; import org.springframework.web.reactive.function.BodyExtractors;
import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebSession;
/** /**
* {@code ServerRequest} implementation based on a {@link ServerWebExchange}. * {@code ServerRequest} implementation based on a {@link ServerWebExchange}.
@ -124,6 +125,11 @@ class DefaultServerRequest implements ServerRequest {
orElseGet(Collections::emptyMap); orElseGet(Collections::emptyMap);
} }
@Override
public Mono<WebSession> session() {
return this.exchange.getSession();
}
private ServerHttpRequest request() { private ServerHttpRequest request() {
return this.exchange.getRequest(); return this.exchange.getRequest();
} }

8
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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.Assert;
import org.springframework.util.PathMatcher; import org.springframework.util.PathMatcher;
import org.springframework.web.reactive.function.BodyExtractor; 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 * Implementations of {@link RequestPredicate} that implement various useful request matching operations, such as
@ -362,5 +363,10 @@ public abstract class RequestPredicates {
public Map<String, String> pathVariables() { public Map<String, String> pathVariables() {
return this.request.pathVariables(); return this.request.pathVariables();
} }
@Override
public Mono<WebSession> session() {
return this.request.session();
}
} }
} }

12
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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.codec.json.AbstractJackson2Codec;
import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.reactive.function.BodyExtractor; 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}. * Represents a server-side HTTP request, as handled by a {@code HandlerFunction}.
@ -152,6 +153,15 @@ public interface ServerRequest {
*/ */
Map<String, String> pathVariables(); Map<String, String> 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<WebSession> session();
/** /**
* Represents the headers of the HTTP request. * Represents the headers of the HTTP request.

8
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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.BodyExtractor;
import org.springframework.web.reactive.function.server.HandlerFunction; import org.springframework.web.reactive.function.server.HandlerFunction;
import org.springframework.web.reactive.function.server.ServerRequest; 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 * 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(); return this.request.pathVariables();
} }
@Override
public Mono<WebSession> session() {
return this.request.session();
}
/** /**
* Implementation of the {@link Headers} interface that can be subclassed to adapt the headers to a * 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. * {@link HandlerFunction handler function}. All methods default to calling through to the wrapped headers.

11
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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap; import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebSession;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
@ -138,6 +139,14 @@ public class DefaultServerRequestTests {
assertEquals(pathVariables, defaultRequest.pathVariables()); 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 @Test
public void header() throws Exception { public void header() throws Exception {
HttpHeaders httpHeaders = new HttpHeaders(); HttpHeaders httpHeaders = new HttpHeaders();

28
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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap; import org.springframework.util.MultiValueMap;
import org.springframework.web.reactive.function.BodyExtractor; import org.springframework.web.reactive.function.BodyExtractor;
import org.springframework.web.server.WebSession;
/** /**
* @author Arjen Poutsma * @author Arjen Poutsma
@ -61,10 +62,12 @@ public class MockServerRequest implements ServerRequest {
private final Map<String, String> pathVariables; private final Map<String, String> pathVariables;
private final WebSession session;
private MockServerRequest(HttpMethod method, URI uri, private MockServerRequest(HttpMethod method, URI uri,
MockHeaders headers, Object body, Map<String, Object> attributes, MockHeaders headers, Object body, Map<String, Object> attributes,
MultiValueMap<String, String> queryParams, MultiValueMap<String, String> queryParams,
Map<String, String> pathVariables) { Map<String, String> pathVariables, WebSession session) {
this.method = method; this.method = method;
this.uri = uri; this.uri = uri;
this.headers = headers; this.headers = headers;
@ -72,6 +75,7 @@ public class MockServerRequest implements ServerRequest {
this.attributes = attributes; this.attributes = attributes;
this.queryParams = queryParams; this.queryParams = queryParams;
this.pathVariables = pathVariables; this.pathVariables = pathVariables;
this.session = session;
} }
public static Builder builder() { public static Builder builder() {
@ -132,6 +136,11 @@ public class MockServerRequest implements ServerRequest {
return Collections.unmodifiableMap(this.pathVariables); return Collections.unmodifiableMap(this.pathVariables);
} }
@Override
public Mono<WebSession> session() {
return Mono.justOrEmpty(this.session);
}
public interface Builder { public interface Builder {
Builder method(HttpMethod method); Builder method(HttpMethod method);
@ -154,6 +163,8 @@ public class MockServerRequest implements ServerRequest {
Builder pathVariables(Map<String, String> pathVariables); Builder pathVariables(Map<String, String> pathVariables);
Builder session(WebSession session);
MockServerRequest body(Object body); MockServerRequest body(Object body);
MockServerRequest build(); MockServerRequest build();
@ -176,6 +187,8 @@ public class MockServerRequest implements ServerRequest {
private Map<String, String> pathVariables = new LinkedHashMap<>(); private Map<String, String> pathVariables = new LinkedHashMap<>();
private WebSession session;
@Override @Override
public Builder method(HttpMethod method) { public Builder method(HttpMethod method) {
Assert.notNull(method, "'method' must not be null"); Assert.notNull(method, "'method' must not be null");
@ -250,17 +263,24 @@ public class MockServerRequest implements ServerRequest {
return this; return this;
} }
@Override
public Builder session(WebSession session) {
Assert.notNull(session, "'session' must not be null");
this.session = session;
return this;
}
@Override @Override
public MockServerRequest body(Object body) { public MockServerRequest body(Object body) {
this.body = body; this.body = body;
return new MockServerRequest(this.method, this.uri, this.headers, this.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 @Override
public MockServerRequest build() { public MockServerRequest build() {
return new MockServerRequest(this.method, this.uri, this.headers, null, return new MockServerRequest(this.method, this.uri, this.headers, null,
this.attributes, this.queryParams, this.pathVariables); this.attributes, this.queryParams, this.pathVariables, this.session);
} }
} }

Loading…
Cancel
Save