提交 7e907c1e 编写于 作者: A Arjen Poutsma

Align web.reactive.function with WebClient

This commit changes web.reactive.function to reflect the introduction of
the new WebClient. Changes include:

- Request -> ServerRequest
- Response -> ServerResponse
- FilterFunction -> HandlerFilterFunction
- StrategiesSupplier -> HandlerStrategies
上级 0cfb6b37
...@@ -40,22 +40,22 @@ import org.springframework.util.ClassUtils; ...@@ -40,22 +40,22 @@ import org.springframework.util.ClassUtils;
import org.springframework.web.reactive.result.view.ViewResolver; import org.springframework.web.reactive.result.view.ViewResolver;
/** /**
* Default implementation of {@link StrategiesSupplier.Builder}. * Default implementation of {@link HandlerStrategies.Builder}.
* *
* @author Arjen Poutsma * @author Arjen Poutsma
* @since 5.0 * @since 5.0
*/ */
class DefaultStrategiesSupplierBuilder implements StrategiesSupplier.Builder { class DefaultHandlerStrategiesBuilder implements HandlerStrategies.Builder {
private static final boolean jackson2Present = private static final boolean jackson2Present =
ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper", ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper",
DefaultStrategiesSupplierBuilder.class.getClassLoader()) && DefaultHandlerStrategiesBuilder.class.getClassLoader()) &&
ClassUtils.isPresent("com.fasterxml.jackson.core.JsonGenerator", ClassUtils.isPresent("com.fasterxml.jackson.core.JsonGenerator",
DefaultStrategiesSupplierBuilder.class.getClassLoader()); DefaultHandlerStrategiesBuilder.class.getClassLoader());
private static final boolean jaxb2Present = private static final boolean jaxb2Present =
ClassUtils.isPresent("javax.xml.bind.Binder", ClassUtils.isPresent("javax.xml.bind.Binder",
DefaultStrategiesSupplierBuilder.class.getClassLoader()); DefaultHandlerStrategiesBuilder.class.getClassLoader());
private final List<HttpMessageReader<?>> messageReaders = new ArrayList<>(); private final List<HttpMessageReader<?>> messageReaders = new ArrayList<>();
...@@ -86,33 +86,33 @@ class DefaultStrategiesSupplierBuilder implements StrategiesSupplier.Builder { ...@@ -86,33 +86,33 @@ class DefaultStrategiesSupplierBuilder implements StrategiesSupplier.Builder {
} }
@Override @Override
public StrategiesSupplier.Builder messageReader(HttpMessageReader<?> messageReader) { public HandlerStrategies.Builder messageReader(HttpMessageReader<?> messageReader) {
Assert.notNull(messageReader, "'messageReader' must not be null"); Assert.notNull(messageReader, "'messageReader' must not be null");
this.messageReaders.add(messageReader); this.messageReaders.add(messageReader);
return this; return this;
} }
@Override @Override
public StrategiesSupplier.Builder messageWriter(HttpMessageWriter<?> messageWriter) { public HandlerStrategies.Builder messageWriter(HttpMessageWriter<?> messageWriter) {
Assert.notNull(messageWriter, "'messageWriter' must not be null"); Assert.notNull(messageWriter, "'messageWriter' must not be null");
this.messageWriters.add(messageWriter); this.messageWriters.add(messageWriter);
return this; return this;
} }
@Override @Override
public StrategiesSupplier.Builder viewResolver(ViewResolver viewResolver) { public HandlerStrategies.Builder viewResolver(ViewResolver viewResolver) {
Assert.notNull(viewResolver, "'viewResolver' must not be null"); Assert.notNull(viewResolver, "'viewResolver' must not be null");
this.viewResolvers.add(viewResolver); this.viewResolvers.add(viewResolver);
return this; return this;
} }
@Override @Override
public StrategiesSupplier build() { public HandlerStrategies build() {
return new DefaultStrategiesSupplier(this.messageReaders, this.messageWriters, return new DefaultHandlerStrategies(this.messageReaders, this.messageWriters,
this.viewResolvers); this.viewResolvers);
} }
private static class DefaultStrategiesSupplier implements StrategiesSupplier { private static class DefaultHandlerStrategies implements HandlerStrategies {
private final List<HttpMessageReader<?>> messageReaders; private final List<HttpMessageReader<?>> messageReaders;
...@@ -120,7 +120,7 @@ class DefaultStrategiesSupplierBuilder implements StrategiesSupplier.Builder { ...@@ -120,7 +120,7 @@ class DefaultStrategiesSupplierBuilder implements StrategiesSupplier.Builder {
private final List<ViewResolver> viewResolvers; private final List<ViewResolver> viewResolvers;
public DefaultStrategiesSupplier( public DefaultHandlerStrategies(
List<HttpMessageReader<?>> messageReaders, List<HttpMessageReader<?>> messageReaders,
List<HttpMessageWriter<?>> messageWriters, List<HttpMessageWriter<?>> messageWriters,
List<ViewResolver> viewResolvers) { List<ViewResolver> viewResolvers) {
......
...@@ -37,19 +37,19 @@ import org.springframework.http.server.reactive.ServerHttpRequest; ...@@ -37,19 +37,19 @@ import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.ServerWebExchange;
/** /**
* {@code Request} implementation based on a {@link ServerWebExchange}. * {@code ServerRequest} implementation based on a {@link ServerWebExchange}.
* @author Arjen Poutsma * @author Arjen Poutsma
*/ */
class DefaultRequest implements Request { class DefaultServerRequest implements ServerRequest {
private final ServerWebExchange exchange; private final ServerWebExchange exchange;
private final Headers headers; private final Headers headers;
private final StrategiesSupplier strategies; private final HandlerStrategies strategies;
DefaultRequest(ServerWebExchange exchange, StrategiesSupplier strategies) { DefaultServerRequest(ServerWebExchange exchange, HandlerStrategies strategies) {
this.exchange = exchange; this.exchange = exchange;
this.strategies = strategies; this.strategies = strategies;
this.headers = new DefaultHeaders(); this.headers = new DefaultHeaders();
...@@ -77,7 +77,7 @@ class DefaultRequest implements Request { ...@@ -77,7 +77,7 @@ class DefaultRequest implements Request {
new BodyExtractor.Context() { new BodyExtractor.Context() {
@Override @Override
public Supplier<Stream<HttpMessageReader<?>>> messageReaders() { public Supplier<Stream<HttpMessageReader<?>>> messageReaders() {
return DefaultRequest.this.strategies.messageReaders(); return DefaultServerRequest.this.strategies.messageReaders();
} }
}); });
} }
......
...@@ -26,7 +26,6 @@ import java.util.LinkedHashMap; ...@@ -26,7 +26,6 @@ import java.util.LinkedHashMap;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
...@@ -36,12 +35,14 @@ import reactor.core.publisher.Flux; ...@@ -36,12 +35,14 @@ import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import org.springframework.core.Conventions; import org.springframework.core.Conventions;
import org.springframework.core.ResolvableType;
import org.springframework.http.CacheControl; import org.springframework.http.CacheControl;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.codec.BodyInserter; import org.springframework.http.codec.BodyInserter;
import org.springframework.http.codec.BodyInserters;
import org.springframework.http.codec.HttpMessageWriter; import org.springframework.http.codec.HttpMessageWriter;
import org.springframework.http.server.reactive.ServerHttpResponse; import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.util.Assert; import org.springframework.util.Assert;
...@@ -50,22 +51,22 @@ import org.springframework.web.reactive.result.view.ViewResolver; ...@@ -50,22 +51,22 @@ import org.springframework.web.reactive.result.view.ViewResolver;
import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.ServerWebExchange;
/** /**
* Default {@link Response.BodyBuilder} implementation. * Default {@link ServerResponse.BodyBuilder} implementation.
* *
* @author Arjen Poutsma * @author Arjen Poutsma
*/ */
class DefaultResponseBuilder implements Response.BodyBuilder { class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder {
private final int statusCode; private final int statusCode;
private final HttpHeaders headers = new HttpHeaders(); private final HttpHeaders headers = new HttpHeaders();
public DefaultResponseBuilder(int statusCode) { public DefaultServerResponseBuilder(int statusCode) {
this.statusCode = statusCode; this.statusCode = statusCode;
} }
@Override @Override
public Response.BodyBuilder header(String headerName, String... headerValues) { public ServerResponse.BodyBuilder header(String headerName, String... headerValues) {
for (String headerValue : headerValues) { for (String headerValue : headerValues) {
this.headers.add(headerName, headerValue); this.headers.add(headerName, headerValue);
} }
...@@ -73,7 +74,7 @@ class DefaultResponseBuilder implements Response.BodyBuilder { ...@@ -73,7 +74,7 @@ class DefaultResponseBuilder implements Response.BodyBuilder {
} }
@Override @Override
public Response.BodyBuilder headers(HttpHeaders headers) { public ServerResponse.BodyBuilder headers(HttpHeaders headers) {
if (headers != null) { if (headers != null) {
this.headers.putAll(headers); this.headers.putAll(headers);
} }
...@@ -81,25 +82,25 @@ class DefaultResponseBuilder implements Response.BodyBuilder { ...@@ -81,25 +82,25 @@ class DefaultResponseBuilder implements Response.BodyBuilder {
} }
@Override @Override
public Response.BodyBuilder allow(HttpMethod... allowedMethods) { public ServerResponse.BodyBuilder allow(HttpMethod... allowedMethods) {
this.headers.setAllow(new LinkedHashSet<>(Arrays.asList(allowedMethods))); this.headers.setAllow(new LinkedHashSet<>(Arrays.asList(allowedMethods)));
return this; return this;
} }
@Override @Override
public Response.BodyBuilder contentLength(long contentLength) { public ServerResponse.BodyBuilder contentLength(long contentLength) {
this.headers.setContentLength(contentLength); this.headers.setContentLength(contentLength);
return this; return this;
} }
@Override @Override
public Response.BodyBuilder contentType(MediaType contentType) { public ServerResponse.BodyBuilder contentType(MediaType contentType) {
this.headers.setContentType(contentType); this.headers.setContentType(contentType);
return this; return this;
} }
@Override @Override
public Response.BodyBuilder eTag(String eTag) { public ServerResponse.BodyBuilder eTag(String eTag) {
if (eTag != null) { if (eTag != null) {
if (!eTag.startsWith("\"") && !eTag.startsWith("W/\"")) { if (!eTag.startsWith("\"") && !eTag.startsWith("W/\"")) {
eTag = "\"" + eTag; eTag = "\"" + eTag;
...@@ -113,7 +114,7 @@ class DefaultResponseBuilder implements Response.BodyBuilder { ...@@ -113,7 +114,7 @@ class DefaultResponseBuilder implements Response.BodyBuilder {
} }
@Override @Override
public Response.BodyBuilder lastModified(ZonedDateTime lastModified) { public ServerResponse.BodyBuilder lastModified(ZonedDateTime lastModified) {
ZonedDateTime gmt = lastModified.withZoneSameInstant(ZoneId.of("GMT")); ZonedDateTime gmt = lastModified.withZoneSameInstant(ZoneId.of("GMT"));
String headerValue = DateTimeFormatter.RFC_1123_DATE_TIME.format(gmt); String headerValue = DateTimeFormatter.RFC_1123_DATE_TIME.format(gmt);
this.headers.set(HttpHeaders.LAST_MODIFIED, headerValue); this.headers.set(HttpHeaders.LAST_MODIFIED, headerValue);
...@@ -121,13 +122,13 @@ class DefaultResponseBuilder implements Response.BodyBuilder { ...@@ -121,13 +122,13 @@ class DefaultResponseBuilder implements Response.BodyBuilder {
} }
@Override @Override
public Response.BodyBuilder location(URI location) { public ServerResponse.BodyBuilder location(URI location) {
this.headers.setLocation(location); this.headers.setLocation(location);
return this; return this;
} }
@Override @Override
public Response.BodyBuilder cacheControl(CacheControl cacheControl) { public ServerResponse.BodyBuilder cacheControl(CacheControl cacheControl) {
String ccValue = cacheControl.getHeaderValue(); String ccValue = cacheControl.getHeaderValue();
if (ccValue != null) { if (ccValue != null) {
this.headers.setCacheControl(cacheControl.getHeaderValue()); this.headers.setCacheControl(cacheControl.getHeaderValue());
...@@ -136,20 +137,20 @@ class DefaultResponseBuilder implements Response.BodyBuilder { ...@@ -136,20 +137,20 @@ class DefaultResponseBuilder implements Response.BodyBuilder {
} }
@Override @Override
public Response.BodyBuilder varyBy(String... requestHeaders) { public ServerResponse.BodyBuilder varyBy(String... requestHeaders) {
this.headers.setVary(Arrays.asList(requestHeaders)); this.headers.setVary(Arrays.asList(requestHeaders));
return this; return this;
} }
@Override @Override
public Response<Void> build() { public ServerResponse<Void> build() {
return body(BodyInserter.of( return body(BodyInserter.of(
(response, context) -> response.setComplete(), (response, context) -> response.setComplete(),
() -> null)); () -> null));
} }
@Override @Override
public <T extends Publisher<Void>> Response<T> build(T voidPublisher) { public <T extends Publisher<Void>> ServerResponse<T> build(T voidPublisher) {
Assert.notNull(voidPublisher, "'voidPublisher' must not be null"); Assert.notNull(voidPublisher, "'voidPublisher' must not be null");
return body(BodyInserter.of( return body(BodyInserter.of(
(response, context) -> Flux.from(voidPublisher).then(response.setComplete()), (response, context) -> Flux.from(voidPublisher).then(response.setComplete()),
...@@ -157,19 +158,23 @@ class DefaultResponseBuilder implements Response.BodyBuilder { ...@@ -157,19 +158,23 @@ class DefaultResponseBuilder implements Response.BodyBuilder {
} }
@Override @Override
public <T> Response<T> body(BiFunction<ServerHttpResponse, BodyInserter.Context, Mono<Void>> writer, public <T> ServerResponse<T> body(BodyInserter<T, ? super ServerHttpResponse> inserter) {
Supplier<T> supplier) { Assert.notNull(inserter, "'inserter' must not be null");
return body(BodyInserter.of(writer, supplier)); return new BodyInserterServerResponse<T>(this.statusCode, this.headers, inserter);
} }
@Override @Override
public <T> Response<T> body(BodyInserter<T, ? super ServerHttpResponse> inserter) { public <S extends Publisher<T>, T> ServerResponse<S> body(S publisher, Class<T> elementClass) {
Assert.notNull(inserter, "'inserter' must not be null"); return body(BodyInserters.fromPublisher(publisher, elementClass));
return new BodyInserterResponse<T>(this.statusCode, this.headers, inserter); }
@Override
public <S extends Publisher<T>, T> ServerResponse<S> body(S publisher, ResolvableType elementType) {
return body(BodyInserters.fromPublisher(publisher, elementType));
} }
@Override @Override
public Response<Rendering> render(String name, Object... modelAttributes) { public ServerResponse<Rendering> render(String name, Object... modelAttributes) {
Assert.hasLength(name, "'name' must not be empty"); Assert.hasLength(name, "'name' must not be empty");
return render(name, toModelMap(modelAttributes)); return render(name, toModelMap(modelAttributes));
} }
...@@ -190,24 +195,24 @@ class DefaultResponseBuilder implements Response.BodyBuilder { ...@@ -190,24 +195,24 @@ class DefaultResponseBuilder implements Response.BodyBuilder {
} }
@Override @Override
public Response<Rendering> render(String name, Map<String, ?> model) { public ServerResponse<Rendering> render(String name, Map<String, ?> model) {
Assert.hasLength(name, "'name' must not be empty"); Assert.hasLength(name, "'name' must not be empty");
Map<String, Object> modelMap = new LinkedHashMap<>(); Map<String, Object> modelMap = new LinkedHashMap<>();
if (model != null) { if (model != null) {
modelMap.putAll(model); modelMap.putAll(model);
} }
return new RenderingResponse(this.statusCode, this.headers, name, modelMap); return new RenderingServerResponse(this.statusCode, this.headers, name, modelMap);
} }
private static abstract class AbstractResponse<T> implements Response<T> { private static abstract class AbstractServerResponse<T> implements ServerResponse<T> {
private final int statusCode; private final int statusCode;
private final HttpHeaders headers; private final HttpHeaders headers;
protected AbstractResponse(int statusCode, HttpHeaders headers) { protected AbstractServerResponse(int statusCode, HttpHeaders headers) {
this.statusCode = statusCode; this.statusCode = statusCode;
this.headers = HttpHeaders.readOnlyHttpHeaders(headers); this.headers = HttpHeaders.readOnlyHttpHeaders(headers);
} }
...@@ -235,12 +240,12 @@ class DefaultResponseBuilder implements Response.BodyBuilder { ...@@ -235,12 +240,12 @@ class DefaultResponseBuilder implements Response.BodyBuilder {
} }
} }
private static final class BodyInserterResponse<T> extends AbstractResponse<T> { private static final class BodyInserterServerResponse<T> extends AbstractServerResponse<T> {
private final BodyInserter<T, ? super ServerHttpResponse> inserter; private final BodyInserter<T, ? super ServerHttpResponse> inserter;
public BodyInserterResponse(int statusCode, HttpHeaders headers, public BodyInserterServerResponse(int statusCode, HttpHeaders headers,
BodyInserter<T, ? super ServerHttpResponse> inserter) { BodyInserter<T, ? super ServerHttpResponse> inserter) {
super(statusCode, headers); super(statusCode, headers);
...@@ -253,7 +258,7 @@ class DefaultResponseBuilder implements Response.BodyBuilder { ...@@ -253,7 +258,7 @@ class DefaultResponseBuilder implements Response.BodyBuilder {
} }
@Override @Override
public Mono<Void> writeTo(ServerWebExchange exchange, StrategiesSupplier strategies) { public Mono<Void> writeTo(ServerWebExchange exchange, HandlerStrategies strategies) {
ServerHttpResponse response = exchange.getResponse(); ServerHttpResponse response = exchange.getResponse();
writeStatusAndHeaders(response); writeStatusAndHeaders(response);
return this.inserter.insert(response, new BodyInserter.Context() { return this.inserter.insert(response, new BodyInserter.Context() {
...@@ -267,7 +272,7 @@ class DefaultResponseBuilder implements Response.BodyBuilder { ...@@ -267,7 +272,7 @@ class DefaultResponseBuilder implements Response.BodyBuilder {
} }
private static final class RenderingResponse extends AbstractResponse<Rendering> { private static final class RenderingServerResponse extends AbstractServerResponse<Rendering> {
private final String name; private final String name;
...@@ -275,7 +280,7 @@ class DefaultResponseBuilder implements Response.BodyBuilder { ...@@ -275,7 +280,7 @@ class DefaultResponseBuilder implements Response.BodyBuilder {
private final Rendering rendering; private final Rendering rendering;
public RenderingResponse(int statusCode, HttpHeaders headers, String name, public RenderingServerResponse(int statusCode, HttpHeaders headers, String name,
Map<String, Object> model) { Map<String, Object> model) {
super(statusCode, headers); super(statusCode, headers);
this.name = name; this.name = name;
...@@ -289,7 +294,7 @@ class DefaultResponseBuilder implements Response.BodyBuilder { ...@@ -289,7 +294,7 @@ class DefaultResponseBuilder implements Response.BodyBuilder {
} }
@Override @Override
public Mono<Void> writeTo(ServerWebExchange exchange, StrategiesSupplier strategies) { public Mono<Void> writeTo(ServerWebExchange exchange, HandlerStrategies strategies) {
ServerHttpResponse response = exchange.getResponse(); ServerHttpResponse response = exchange.getResponse();
writeStatusAndHeaders(response); writeStatusAndHeaders(response);
MediaType contentType = exchange.getResponse().getHeaders().getContentType(); MediaType contentType = exchange.getResponse().getHeaders().getContentType();
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
package org.springframework.web.reactive.function; package org.springframework.web.reactive.function;
import org.springframework.web.reactive.function.support.RequestWrapper; import org.springframework.web.reactive.function.support.ServerRequestWrapper;
/** /**
* Represents a function that filters a {@linkplain HandlerFunction handler function}. * Represents a function that filters a {@linkplain HandlerFunction handler function}.
...@@ -25,22 +25,22 @@ import org.springframework.web.reactive.function.support.RequestWrapper; ...@@ -25,22 +25,22 @@ import org.springframework.web.reactive.function.support.RequestWrapper;
* @param <R> the type of the response of the function * @param <R> the type of the response of the function
* @author Arjen Poutsma * @author Arjen Poutsma
* @since 5.0 * @since 5.0
* @see RouterFunction#filter(FilterFunction) * @see RouterFunction#filter(HandlerFilterFunction)
*/ */
@FunctionalInterface @FunctionalInterface
public interface FilterFunction<T, R> { public interface HandlerFilterFunction<T, R> {
/** /**
* Apply this filter to the given handler function. The given * Apply this filter to the given handler function. The given
* {@linkplain HandlerFunction handler function} represents the next entity in the * {@linkplain HandlerFunction handler function} represents the next entity in the
* chain, and can be {@linkplain HandlerFunction#handle(Request) invoked} in order * chain, and can be {@linkplain HandlerFunction#handle(ServerRequest) invoked} in order
* to proceed to this entity, or not invoked to block the chain. * to proceed to this entity, or not invoked to block the chain.
* *
* @param request the request * @param request the request
* @param next the next handler or filter function in the chain * @param next the next handler or filter function in the chain
* @return the filtered response * @return the filtered response
* @see RequestWrapper * @see ServerRequestWrapper
*/ */
Response<R> filter(Request request, HandlerFunction<T> next); ServerResponse<R> filter(ServerRequest request, HandlerFunction<T> next);
} }
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
package org.springframework.web.reactive.function; package org.springframework.web.reactive.function;
/** /**
* Represents a function that handles a {@linkplain Request request}. * Represents a function that handles a {@linkplain ServerRequest request}.
* *
* @param <T> the type of the response of the function * @param <T> the type of the response of the function
* @author Arjen Poutsma * @author Arjen Poutsma
...@@ -31,6 +31,6 @@ public interface HandlerFunction<T> { ...@@ -31,6 +31,6 @@ public interface HandlerFunction<T> {
* @param request the request to handle * @param request the request to handle
* @return the response * @return the response
*/ */
Response<T> handle(Request request); ServerResponse<T> handle(ServerRequest request);
} }
...@@ -29,16 +29,16 @@ import org.springframework.web.reactive.result.view.ViewResolver; ...@@ -29,16 +29,16 @@ import org.springframework.web.reactive.result.view.ViewResolver;
* Defines the strategies to be used for processing {@link HandlerFunction}s. An instance of * Defines the strategies to be used for processing {@link HandlerFunction}s. An instance of
* this class is immutable; instances are typically created through the mutable {@link Builder}: * this class is immutable; instances are typically created through the mutable {@link Builder}:
* either through {@link #builder()} to set up default strategies, or {@link #empty()} to start from * either through {@link #builder()} to set up default strategies, or {@link #empty()} to start from
* scratch. Alternatively, {@code StrategiesSupplier} instances can be created through * scratch. Alternatively, {@code HandlerStrategies} instances can be created through
* {@link #of(Supplier, Supplier, Supplier)}. * {@link #of(Supplier, Supplier, Supplier)}.
* *
* @author Arjen Poutsma * @author Arjen Poutsma
* @author Juergen Hoeller * @author Juergen Hoeller
* @since 5.0 * @since 5.0
* @see RouterFunctions#toHttpHandler(RouterFunction, StrategiesSupplier) * @see RouterFunctions#toHttpHandler(RouterFunction, HandlerStrategies)
* @see RouterFunctions#toHandlerMapping(RouterFunction, StrategiesSupplier) * @see RouterFunctions#toHandlerMapping(RouterFunction, HandlerStrategies)
*/ */
public interface StrategiesSupplier { public interface HandlerStrategies {
// Instance methods // Instance methods
...@@ -67,41 +67,41 @@ public interface StrategiesSupplier { ...@@ -67,41 +67,41 @@ public interface StrategiesSupplier {
// Static methods // Static methods
/** /**
* Return a new {@code StrategiesSupplier} with default initialization. * Return a new {@code HandlerStrategies} with default initialization.
* @return the new {@code StrategiesSupplier} * @return the new {@code HandlerStrategies}
*/ */
static StrategiesSupplier withDefaults() { static HandlerStrategies withDefaults() {
return builder().build(); return builder().build();
} }
/** /**
* Return a new {@code StrategiesSupplier} based on the given * Return a new {@code HandlerStrategies} based on the given
* {@linkplain ApplicationContext application context}. * {@linkplain ApplicationContext application context}.
* The returned supplier will search for all {@link HttpMessageReader}, {@link HttpMessageWriter}, * The returned supplier will search for all {@link HttpMessageReader}, {@link HttpMessageWriter},
* and {@link ViewResolver} instances in the given application context and return them for * and {@link ViewResolver} instances in the given application context and return them for
* {@link #messageReaders()}, {@link #messageWriters()}, and {@link #viewResolvers()} * {@link #messageReaders()}, {@link #messageWriters()}, and {@link #viewResolvers()}
* respectively. * respectively.
* @param applicationContext the application context to base the strategies on * @param applicationContext the application context to base the strategies on
* @return the new {@code StrategiesSupplier} * @return the new {@code HandlerStrategies}
*/ */
static StrategiesSupplier of(ApplicationContext applicationContext) { static HandlerStrategies of(ApplicationContext applicationContext) {
return builder(applicationContext).build(); return builder(applicationContext).build();
} }
/** /**
* Return a new {@code StrategiesSupplier} described by the given supplier functions. * Return a new {@code HandlerStrategies} described by the given supplier functions.
* All provided supplier function parameters can be {@code null} to indicate an empty * All provided supplier function parameters can be {@code null} to indicate an empty
* stream is to be returned. * stream is to be returned.
* @param messageReaders the supplier function for {@link HttpMessageReader} instances (can be {@code null}) * @param messageReaders the supplier function for {@link HttpMessageReader} instances (can be {@code null})
* @param messageWriters the supplier function for {@link HttpMessageWriter} instances (can be {@code null}) * @param messageWriters the supplier function for {@link HttpMessageWriter} instances (can be {@code null})
* @param viewResolvers the supplier function for {@link ViewResolver} instances (can be {@code null}) * @param viewResolvers the supplier function for {@link ViewResolver} instances (can be {@code null})
* @return the new {@code StrategiesSupplier} * @return the new {@code HandlerStrategies}
*/ */
static StrategiesSupplier of(Supplier<Stream<HttpMessageReader<?>>> messageReaders, static HandlerStrategies of(Supplier<Stream<HttpMessageReader<?>>> messageReaders,
Supplier<Stream<HttpMessageWriter<?>>> messageWriters, Supplier<Stream<HttpMessageWriter<?>>> messageWriters,
Supplier<Stream<ViewResolver>> viewResolvers) { Supplier<Stream<ViewResolver>> viewResolvers) {
return new StrategiesSupplier() { return new HandlerStrategies() {
@Override @Override
public Supplier<Stream<HttpMessageReader<?>>> messageReaders() { public Supplier<Stream<HttpMessageReader<?>>> messageReaders() {
return checkForNull(messageReaders); return checkForNull(messageReaders);
...@@ -124,11 +124,11 @@ public interface StrategiesSupplier { ...@@ -124,11 +124,11 @@ public interface StrategiesSupplier {
// Builder methods // Builder methods
/** /**
* Return a mutable builder for a {@code StrategiesSupplier} with default initialization. * Return a mutable builder for a {@code HandlerStrategies} with default initialization.
* @return the builder * @return the builder
*/ */
static Builder builder() { static Builder builder() {
DefaultStrategiesSupplierBuilder builder = new DefaultStrategiesSupplierBuilder(); DefaultHandlerStrategiesBuilder builder = new DefaultHandlerStrategiesBuilder();
builder.defaultConfiguration(); builder.defaultConfiguration();
return builder; return builder;
} }
...@@ -144,22 +144,22 @@ public interface StrategiesSupplier { ...@@ -144,22 +144,22 @@ public interface StrategiesSupplier {
*/ */
static Builder builder(ApplicationContext applicationContext) { static Builder builder(ApplicationContext applicationContext) {
Assert.notNull(applicationContext, "ApplicationContext must not be null"); Assert.notNull(applicationContext, "ApplicationContext must not be null");
DefaultStrategiesSupplierBuilder builder = new DefaultStrategiesSupplierBuilder(); DefaultHandlerStrategiesBuilder builder = new DefaultHandlerStrategiesBuilder();
builder.applicationContext(applicationContext); builder.applicationContext(applicationContext);
return builder; return builder;
} }
/** /**
* Return a mutable, empty builder for a {@code StrategiesSupplier}. * Return a mutable, empty builder for a {@code HandlerStrategies}.
* @return the builder * @return the builder
*/ */
static Builder empty() { static Builder empty() {
return new DefaultStrategiesSupplierBuilder(); return new DefaultHandlerStrategiesBuilder();
} }
/** /**
* A mutable builder for a {@link StrategiesSupplier}. * A mutable builder for a {@link HandlerStrategies}.
*/ */
interface Builder { interface Builder {
...@@ -185,10 +185,10 @@ public interface StrategiesSupplier { ...@@ -185,10 +185,10 @@ public interface StrategiesSupplier {
Builder viewResolver(ViewResolver viewResolver); Builder viewResolver(ViewResolver viewResolver);
/** /**
* Builds the {@link StrategiesSupplier}. * Builds the {@link HandlerStrategies}.
* @return the built strategies * @return the built strategies
*/ */
StrategiesSupplier build(); HandlerStrategies build();
} }
} }
...@@ -19,7 +19,7 @@ package org.springframework.web.reactive.function; ...@@ -19,7 +19,7 @@ package org.springframework.web.reactive.function;
import org.springframework.util.Assert; import org.springframework.util.Assert;
/** /**
* Represents a function that evaluates on a given {@link Request}. * Represents a function that evaluates on a given {@link ServerRequest}.
* Instances of this function that evaluate on common request properties can be found in {@link RequestPredicates}. * Instances of this function that evaluate on common request properties can be found in {@link RequestPredicates}.
* *
* @author Arjen Poutsma * @author Arjen Poutsma
...@@ -38,7 +38,7 @@ public interface RequestPredicate { ...@@ -38,7 +38,7 @@ public interface RequestPredicate {
* @param request the request to match against * @param request the request to match against
* @return {@code true} if the request matches the predicate; {@code false} otherwise * @return {@code true} if the request matches the predicate; {@code false} otherwise
*/ */
boolean test(Request request); boolean test(ServerRequest request);
/** /**
* Returns a composed request predicate that tests against both this predicate AND the {@code other} predicate. * Returns a composed request predicate that tests against both this predicate AND the {@code other} predicate.
...@@ -52,12 +52,12 @@ public interface RequestPredicate { ...@@ -52,12 +52,12 @@ public interface RequestPredicate {
Assert.notNull(other, "'other' must not be null"); Assert.notNull(other, "'other' must not be null");
return new RequestPredicate() { return new RequestPredicate() {
@Override @Override
public boolean test(Request t) { public boolean test(ServerRequest t) {
return RequestPredicate.this.test(t) && other.test(t); return RequestPredicate.this.test(t) && other.test(t);
} }
@Override @Override
public Request subRequest(Request request) { public ServerRequest subRequest(ServerRequest request) {
return other.subRequest(RequestPredicate.this.subRequest(request)); return other.subRequest(RequestPredicate.this.subRequest(request));
} }
}; };
...@@ -85,7 +85,7 @@ public interface RequestPredicate { ...@@ -85,7 +85,7 @@ public interface RequestPredicate {
return (t) -> test(t) || other.test(t); return (t) -> test(t) || other.test(t);
} }
default Request subRequest(Request request) { default ServerRequest subRequest(ServerRequest request) {
return request; return request;
} }
} }
...@@ -90,13 +90,13 @@ public abstract class RequestPredicates { ...@@ -90,13 +90,13 @@ public abstract class RequestPredicates {
* @param headersPredicate a predicate that tests against the request headers * @param headersPredicate a predicate that tests against the request headers
* @return a predicate that tests against the given header predicate * @return a predicate that tests against the given header predicate
*/ */
public static RequestPredicate headers(Predicate<Request.Headers> headersPredicate) { public static RequestPredicate headers(Predicate<ServerRequest.Headers> headersPredicate) {
return new HeaderPredicates(headersPredicate); return new HeaderPredicates(headersPredicate);
} }
/** /**
* Return a {@code RequestPredicate} that tests if the request's * Return a {@code RequestPredicate} that tests if the request's
* {@linkplain Request.Headers#contentType() content type} is {@linkplain MediaType#includes(MediaType) included} * {@linkplain ServerRequest.Headers#contentType() content type} is {@linkplain MediaType#includes(MediaType) included}
* by any of the given media types. * by any of the given media types.
* *
* @param mediaTypes the media types to match the request's content type against * @param mediaTypes the media types to match the request's content type against
...@@ -115,7 +115,7 @@ public abstract class RequestPredicates { ...@@ -115,7 +115,7 @@ public abstract class RequestPredicates {
/** /**
* Return a {@code RequestPredicate} that tests if the request's * Return a {@code RequestPredicate} that tests if the request's
* {@linkplain Request.Headers#accept() accept} header is * {@linkplain ServerRequest.Headers#accept() accept} header is
* {@linkplain MediaType#isCompatibleWith(MediaType) compatible} with any of the given media types. * {@linkplain MediaType#isCompatibleWith(MediaType) compatible} with any of the given media types.
* *
* @param mediaTypes the media types to match the request's accept header against * @param mediaTypes the media types to match the request's accept header against
...@@ -227,7 +227,7 @@ public abstract class RequestPredicates { ...@@ -227,7 +227,7 @@ public abstract class RequestPredicates {
} }
@Override @Override
public boolean test(Request request) { public boolean test(ServerRequest request) {
return this.httpMethod == request.method(); return this.httpMethod == request.method();
} }
} }
...@@ -246,11 +246,11 @@ public abstract class RequestPredicates { ...@@ -246,11 +246,11 @@ public abstract class RequestPredicates {
} }
@Override @Override
public boolean test(Request request) { public boolean test(ServerRequest request) {
String path = request.path(); String path = request.path();
if (this.pathMatcher.match(this.pattern, path)) { if (this.pathMatcher.match(this.pattern, path)) {
if (request instanceof DefaultRequest) { if (request instanceof DefaultServerRequest) {
DefaultRequest defaultRequest = (DefaultRequest) request; DefaultServerRequest defaultRequest = (DefaultServerRequest) request;
Map<String, String> uriTemplateVariables = this.pathMatcher.extractUriTemplateVariables(this.pattern, path); Map<String, String> uriTemplateVariables = this.pathMatcher.extractUriTemplateVariables(this.pattern, path);
defaultRequest.exchange().getAttributes().put(RouterFunctions.URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriTemplateVariables); defaultRequest.exchange().getAttributes().put(RouterFunctions.URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriTemplateVariables);
} }
...@@ -262,35 +262,35 @@ public abstract class RequestPredicates { ...@@ -262,35 +262,35 @@ public abstract class RequestPredicates {
} }
@Override @Override
public Request subRequest(Request request) { public ServerRequest subRequest(ServerRequest request) {
String requestPath = request.path(); String requestPath = request.path();
String subPath = this.pathMatcher.extractPathWithinPattern(this.pattern, requestPath); String subPath = this.pathMatcher.extractPathWithinPattern(this.pattern, requestPath);
return new SubPathRequestWrapper(request, subPath); return new SubPathServerRequestWrapper(request, subPath);
} }
} }
private static class HeaderPredicates implements RequestPredicate { private static class HeaderPredicates implements RequestPredicate {
private final Predicate<Request.Headers> headersPredicate; private final Predicate<ServerRequest.Headers> headersPredicate;
public HeaderPredicates(Predicate<Request.Headers> headersPredicate) { public HeaderPredicates(Predicate<ServerRequest.Headers> headersPredicate) {
Assert.notNull(headersPredicate, "'headersPredicate' must not be null"); Assert.notNull(headersPredicate, "'headersPredicate' must not be null");
this.headersPredicate = headersPredicate; this.headersPredicate = headersPredicate;
} }
@Override @Override
public boolean test(Request request) { public boolean test(ServerRequest request) {
return this.headersPredicate.test(request.headers()); return this.headersPredicate.test(request.headers());
} }
} }
private static class SubPathRequestWrapper implements Request { private static class SubPathServerRequestWrapper implements ServerRequest {
private final Request request; private final ServerRequest request;
private final String subPath; private final String subPath;
public SubPathRequestWrapper(Request request, String subPath) { public SubPathServerRequestWrapper(ServerRequest request, String subPath) {
this.request = request; this.request = request;
this.subPath = subPath; this.subPath = subPath;
} }
......
...@@ -35,7 +35,7 @@ public interface RouterFunction<T> { ...@@ -35,7 +35,7 @@ public interface RouterFunction<T> {
* @return an {@code Optional} describing the {@code HandlerFunction} that matches this request, * @return an {@code Optional} describing the {@code HandlerFunction} that matches this request,
* or an empty {@code Optional} if there is no match * or an empty {@code Optional} if there is no match
*/ */
Optional<HandlerFunction<T>> route(Request request); Optional<HandlerFunction<T>> route(ServerRequest request);
/** /**
* Return a composed routing function that first invokes this function, * Return a composed routing function that first invokes this function,
...@@ -73,13 +73,13 @@ public interface RouterFunction<T> { ...@@ -73,13 +73,13 @@ public interface RouterFunction<T> {
/** /**
* Filter all {@linkplain HandlerFunction handler functions} routed by this function with the given * Filter all {@linkplain HandlerFunction handler functions} routed by this function with the given
* {@linkplain FilterFunction filter function}. * {@linkplain HandlerFilterFunction filter function}.
* *
* @param filterFunction the filter to apply * @param filterFunction the filter to apply
* @param <S> the filter return type * @param <S> the filter return type
* @return the filtered routing function * @return the filtered routing function
*/ */
default <S> RouterFunction<S> filter(FilterFunction<T, S> filterFunction) { default <S> RouterFunction<S> filter(HandlerFilterFunction<T, S> filterFunction) {
return request -> this.route(request) return request -> this.route(request)
.map(handlerFunction -> filterRequest -> filterFunction.filter(filterRequest, handlerFunction)); .map(handlerFunction -> filterRequest -> filterFunction.filter(filterRequest, handlerFunction));
} }
......
...@@ -38,7 +38,7 @@ import org.springframework.web.server.adapter.HttpWebHandlerAdapter; ...@@ -38,7 +38,7 @@ import org.springframework.web.server.adapter.HttpWebHandlerAdapter;
* <p>Additionally, this class can {@linkplain #toHttpHandler(RouterFunction) transform} a * <p>Additionally, this class can {@linkplain #toHttpHandler(RouterFunction) transform} a
* {@code RouterFunction} into an {@code HttpHandler}, which can be run in Servlet 3.1+, * {@code RouterFunction} into an {@code HttpHandler}, which can be run in Servlet 3.1+,
* Reactor, RxNetty, or Undertow. * Reactor, RxNetty, or Undertow.
* And it can {@linkplain #toHandlerMapping(RouterFunction, StrategiesSupplier) transform} a * And it can {@linkplain #toHandlerMapping(RouterFunction, HandlerStrategies) transform} a
* {@code RouterFunction} into an {@code HandlerMapping}, which can be run in a * {@code RouterFunction} into an {@code HandlerMapping}, which can be run in a
* {@code DispatcherHandler}. * {@code DispatcherHandler}.
* *
...@@ -48,7 +48,7 @@ import org.springframework.web.server.adapter.HttpWebHandlerAdapter; ...@@ -48,7 +48,7 @@ import org.springframework.web.server.adapter.HttpWebHandlerAdapter;
public abstract class RouterFunctions { public abstract class RouterFunctions {
/** /**
* Name of the {@link ServerWebExchange} attribute that contains the {@link Request}. * Name of the {@link ServerWebExchange} attribute that contains the {@link ServerRequest}.
*/ */
public static final String REQUEST_ATTRIBUTE = RouterFunctions.class.getName() + ".request"; public static final String REQUEST_ATTRIBUTE = RouterFunctions.class.getName() + ".request";
...@@ -59,7 +59,7 @@ public abstract class RouterFunctions { ...@@ -59,7 +59,7 @@ public abstract class RouterFunctions {
public static final String URI_TEMPLATE_VARIABLES_ATTRIBUTE = public static final String URI_TEMPLATE_VARIABLES_ATTRIBUTE =
RouterFunctions.class.getName() + ".uriTemplateVariables"; RouterFunctions.class.getName() + ".uriTemplateVariables";
private static final HandlerFunction<Void> NOT_FOUND_HANDLER = request -> Response.notFound().build(); private static final HandlerFunction<Void> NOT_FOUND_HANDLER = request -> ServerResponse.notFound().build();
/** /**
...@@ -93,7 +93,7 @@ public abstract class RouterFunctions { ...@@ -93,7 +93,7 @@ public abstract class RouterFunctions {
return request -> { return request -> {
if (predicate.test(request)) { if (predicate.test(request)) {
Request subRequest = predicate.subRequest(request); ServerRequest subRequest = predicate.subRequest(request);
return routerFunction.route(subRequest); return routerFunction.route(subRequest);
} }
else { else {
...@@ -104,7 +104,7 @@ public abstract class RouterFunctions { ...@@ -104,7 +104,7 @@ public abstract class RouterFunctions {
/** /**
* Convert the given {@linkplain RouterFunction routing function} into a {@link HttpHandler}. * Convert the given {@linkplain RouterFunction routing function} into a {@link HttpHandler}.
* This conversion uses {@linkplain StrategiesSupplier#builder() default strategies}. * This conversion uses {@linkplain HandlerStrategies#builder() default strategies}.
* <p>The returned {@code HttpHandler} can be adapted to run in * <p>The returned {@code HttpHandler} can be adapted to run in
* <ul> * <ul>
* <li>Servlet 3.1+ using the * <li>Servlet 3.1+ using the
...@@ -120,7 +120,7 @@ public abstract class RouterFunctions { ...@@ -120,7 +120,7 @@ public abstract class RouterFunctions {
* @return an http handler that handles HTTP request using the given routing function * @return an http handler that handles HTTP request using the given routing function
*/ */
public static HttpHandler toHttpHandler(RouterFunction<?> routerFunction) { public static HttpHandler toHttpHandler(RouterFunction<?> routerFunction) {
return toHttpHandler(routerFunction, StrategiesSupplier.withDefaults()); return toHttpHandler(routerFunction, HandlerStrategies.withDefaults());
} }
/** /**
...@@ -141,31 +141,31 @@ public abstract class RouterFunctions { ...@@ -141,31 +141,31 @@ public abstract class RouterFunctions {
* @param strategies the strategies to use * @param strategies the strategies to use
* @return an http handler that handles HTTP request using the given routing function * @return an http handler that handles HTTP request using the given routing function
*/ */
public static HttpHandler toHttpHandler(RouterFunction<?> routerFunction, StrategiesSupplier strategies) { public static HttpHandler toHttpHandler(RouterFunction<?> routerFunction, HandlerStrategies strategies) {
Assert.notNull(routerFunction, "RouterFunction must not be null"); Assert.notNull(routerFunction, "RouterFunction must not be null");
Assert.notNull(strategies, "StrategiesSupplier must not be null"); Assert.notNull(strategies, "HandlerStrategies must not be null");
return new HttpWebHandlerAdapter(exchange -> { return new HttpWebHandlerAdapter(exchange -> {
Request request = new DefaultRequest(exchange, strategies); ServerRequest request = new DefaultServerRequest(exchange, strategies);
addAttributes(exchange, request); addAttributes(exchange, request);
HandlerFunction<?> handlerFunction = routerFunction.route(request).orElse(notFound()); HandlerFunction<?> handlerFunction = routerFunction.route(request).orElse(notFound());
Response<?> response = handlerFunction.handle(request); ServerResponse<?> response = handlerFunction.handle(request);
return response.writeTo(exchange, strategies); return response.writeTo(exchange, strategies);
}); });
} }
/** /**
* Convert the given {@code RouterFunction} into a {@code HandlerMapping}. * Convert the given {@code RouterFunction} into a {@code HandlerMapping}.
* This conversion uses {@linkplain StrategiesSupplier#builder() default strategies}. * This conversion uses {@linkplain HandlerStrategies#builder() default strategies}.
* <p>The returned {@code HandlerMapping} can be run in a * <p>The returned {@code HandlerMapping} can be run in a
* {@link org.springframework.web.reactive.DispatcherHandler}. * {@link org.springframework.web.reactive.DispatcherHandler}.
* @param routerFunction the routing function to convert * @param routerFunction the routing function to convert
* @return an handler mapping that maps HTTP request to a handler using the given routing function * @return an handler mapping that maps HTTP request to a handler using the given routing function
* @see org.springframework.web.reactive.function.support.HandlerFunctionAdapter * @see org.springframework.web.reactive.function.support.HandlerFunctionAdapter
* @see org.springframework.web.reactive.function.support.ResponseResultHandler * @see org.springframework.web.reactive.function.support.ServerResponseResultHandler
*/ */
public static HandlerMapping toHandlerMapping(RouterFunction<?> routerFunction) { public static HandlerMapping toHandlerMapping(RouterFunction<?> routerFunction) {
return toHandlerMapping(routerFunction, StrategiesSupplier.withDefaults()); return toHandlerMapping(routerFunction, HandlerStrategies.withDefaults());
} }
/** /**
...@@ -177,14 +177,14 @@ public abstract class RouterFunctions { ...@@ -177,14 +177,14 @@ public abstract class RouterFunctions {
* @param strategies the strategies to use * @param strategies the strategies to use
* @return an handler mapping that maps HTTP request to a handler using the given routing function * @return an handler mapping that maps HTTP request to a handler using the given routing function
* @see org.springframework.web.reactive.function.support.HandlerFunctionAdapter * @see org.springframework.web.reactive.function.support.HandlerFunctionAdapter
* @see org.springframework.web.reactive.function.support.ResponseResultHandler * @see org.springframework.web.reactive.function.support.ServerResponseResultHandler
*/ */
public static HandlerMapping toHandlerMapping(RouterFunction<?> routerFunction, StrategiesSupplier strategies) { public static HandlerMapping toHandlerMapping(RouterFunction<?> routerFunction, HandlerStrategies strategies) {
Assert.notNull(routerFunction, "RouterFunction must not be null"); Assert.notNull(routerFunction, "RouterFunction must not be null");
Assert.notNull(strategies, "StrategiesSupplier must not be null"); Assert.notNull(strategies, "HandlerStrategies must not be null");
return exchange -> { return exchange -> {
Request request = new DefaultRequest(exchange, strategies); ServerRequest request = new DefaultServerRequest(exchange, strategies);
addAttributes(exchange, request); addAttributes(exchange, request);
Optional<? extends HandlerFunction<?>> route = routerFunction.route(request); Optional<? extends HandlerFunction<?>> route = routerFunction.route(request);
return Mono.justOrEmpty(route); return Mono.justOrEmpty(route);
...@@ -192,7 +192,7 @@ public abstract class RouterFunctions { ...@@ -192,7 +192,7 @@ public abstract class RouterFunctions {
} }
private static void addAttributes(ServerWebExchange exchange, Request request) { private static void addAttributes(ServerWebExchange exchange, ServerRequest request) {
Map<String, Object> attributes = exchange.getAttributes(); Map<String, Object> attributes = exchange.getAttributes();
attributes.put(REQUEST_ATTRIBUTE, request); attributes.put(REQUEST_ATTRIBUTE, request);
} }
......
...@@ -32,14 +32,14 @@ import org.springframework.http.codec.BodyExtractor; ...@@ -32,14 +32,14 @@ import org.springframework.http.codec.BodyExtractor;
import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpRequest;
/** /**
* Represents an HTTP request, as handled by a {@code HandlerFunction}. * Represents a server-side HTTP request, as handled by a {@code HandlerFunction}.
* Access to headers and body is offered by {@link Headers} and * Access to headers and body is offered by {@link Headers} and
* {@link #body(BodyExtractor)} respectively. * {@link #body(BodyExtractor)} respectively.
* *
* @author Arjen Poutsma * @author Arjen Poutsma
* @since 5.0 * @since 5.0
*/ */
public interface Request { public interface ServerRequest {
/** /**
* Return the HTTP method. * Return the HTTP method.
...@@ -115,7 +115,7 @@ public interface Request { ...@@ -115,7 +115,7 @@ public interface Request {
/** /**
* Represents the headers of the HTTP request. * Represents the headers of the HTTP request.
* @see Request#headers() * @see ServerRequest#headers()
*/ */
interface Headers { interface Headers {
......
...@@ -21,31 +21,31 @@ import java.time.ZonedDateTime; ...@@ -21,31 +21,31 @@ import java.time.ZonedDateTime;
import java.util.Collection; import java.util.Collection;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.Supplier;
import org.reactivestreams.Publisher; import org.reactivestreams.Publisher;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import org.springframework.core.ResolvableType;
import org.springframework.http.CacheControl; import org.springframework.http.CacheControl;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.codec.BodyInserter; import org.springframework.http.codec.BodyInserter;
import org.springframework.http.codec.BodyInserters;
import org.springframework.http.server.reactive.ServerHttpResponse; import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.ServerWebExchange;
/** /**
* Represents a typed HTTP response, as returned by a {@linkplain HandlerFunction handler function} or * Represents a typed, server-side HTTP response, as returned by a
* {@linkplain FilterFunction filter function}. * {@linkplain HandlerFunction handler function} or {@linkplain HandlerFilterFunction filter function}.
* *
* @author Arjen Poutsma * @author Arjen Poutsma
* @since 5.0 * @since 5.0
* @param <T> the type of the body that this response contains * @param <T> the type of the body that this response contains
*/ */
public interface Response<T> { public interface ServerResponse<T> {
// Instance methods // Instance methods
...@@ -69,9 +69,9 @@ public interface Response<T> { ...@@ -69,9 +69,9 @@ public interface Response<T> {
* *
* @param exchange the web exchange to write to * @param exchange the web exchange to write to
* @param strategies the strategies to use when writing * @param strategies the strategies to use when writing
* @return {@code Mono<Void>} to indicate when request handling is complete * @return {@code Mono<Void>} to indicate when writing is complete
*/ */
Mono<Void> writeTo(ServerWebExchange exchange, StrategiesSupplier strategies); Mono<Void> writeTo(ServerWebExchange exchange, HandlerStrategies strategies);
// Static builder methods // Static builder methods
...@@ -81,9 +81,9 @@ public interface Response<T> { ...@@ -81,9 +81,9 @@ public interface Response<T> {
* @param other the response to copy the status and headers from * @param other the response to copy the status and headers from
* @return the created builder * @return the created builder
*/ */
static BodyBuilder from(Response<?> other) { static BodyBuilder from(ServerResponse<?> other) {
Assert.notNull(other, "'other' must not be null"); Assert.notNull(other, "'other' must not be null");
DefaultResponseBuilder builder = new DefaultResponseBuilder(other.statusCode().value()); DefaultServerResponseBuilder builder = new DefaultServerResponseBuilder(other.statusCode().value());
return builder.headers(other.headers()); return builder.headers(other.headers());
} }
...@@ -95,7 +95,7 @@ public interface Response<T> { ...@@ -95,7 +95,7 @@ public interface Response<T> {
*/ */
static BodyBuilder status(HttpStatus status) { static BodyBuilder status(HttpStatus status) {
Assert.notNull(status, "HttpStatus must not be null"); Assert.notNull(status, "HttpStatus must not be null");
return new DefaultResponseBuilder(status.value()); return new DefaultServerResponseBuilder(status.value());
} }
/** /**
...@@ -105,7 +105,7 @@ public interface Response<T> { ...@@ -105,7 +105,7 @@ public interface Response<T> {
* @return the created builder * @return the created builder
*/ */
static BodyBuilder status(int status) { static BodyBuilder status(int status) {
return new DefaultResponseBuilder(status); return new DefaultServerResponseBuilder(status);
} }
/** /**
...@@ -271,7 +271,7 @@ public interface Response<T> { ...@@ -271,7 +271,7 @@ public interface Response<T> {
* *
* @return the built response * @return the built response
*/ */
Response<Void> build(); ServerResponse<Void> build();
/** /**
* Build the response entity with no body. * Build the response entity with no body.
...@@ -280,7 +280,7 @@ public interface Response<T> { ...@@ -280,7 +280,7 @@ public interface Response<T> {
* @param voidPublisher publisher publisher to indicate when the response should be committed * @param voidPublisher publisher publisher to indicate when the response should be committed
* @return the built response * @return the built response
*/ */
<T extends Publisher<Void>> Response<T> build(T voidPublisher); <T extends Publisher<Void>> ServerResponse<T> build(T voidPublisher);
} }
...@@ -310,23 +310,36 @@ public interface Response<T> { ...@@ -310,23 +310,36 @@ public interface Response<T> {
BodyBuilder contentType(MediaType contentType); BodyBuilder contentType(MediaType contentType);
/** /**
* Set the body with the given {@code supplier} function, and write it with the given * Set the body of the response to the given {@code BodyInserter} and return it.
* {@code writer} function. * @param inserter the {@code BodyInserter} that writes to the response
* @param writer a function that writes the body to the {@code ServerHttpResponse}
* @param supplier a function that returns the body instance
* @param <T> the type contained in the body * @param <T> the type contained in the body
* @return the built response * @return the built response
*/ */
<T> Response<T> body(BiFunction<ServerHttpResponse, BodyInserter.Context, Mono<Void>> writer, <T> ServerResponse<T> body(BodyInserter<T, ? super ServerHttpResponse> inserter);
Supplier<T> supplier);
/** /**
* Set the body of the response to the given {@code BodyInserter} and return it. * Set the body of the response to the given {@code Publisher} and return it. This
* @param inserter the {@code BodyInserter} that writes to the response * convenience method combines {@link #body(BodyInserter)} and
* @param <T> the type contained in the body * {@link BodyInserters#fromPublisher(Publisher, Class)}.
* @return the built response * @param publisher the {@code Publisher} to write to the response
* @param elementClass the class of elements contained in the publisher
* @param <T> the type of the elements contained in the publisher
* @param <S> the type of the {@code Publisher}
* @return the built request
*/
<S extends Publisher<T>, T> ServerResponse<S> body(S publisher, Class<T> elementClass);
/**
* Set the body of the response to the given {@code Publisher} and return it. This
* convenience method combines {@link #body(BodyInserter)} and
* {@link BodyInserters#fromPublisher(Publisher, ResolvableType)}.
* @param publisher the {@code Publisher} to write to the response
* @param elementType the type of elements contained in the publisher
* @param <T> the type of the elements contained in the publisher
* @param <S> the type of the {@code Publisher}
* @return the built request
*/ */
<T> Response<T> body(BodyInserter<T, ? super ServerHttpResponse> inserter); <S extends Publisher<T>, T> ServerResponse<S> body(S publisher, ResolvableType elementType);
/** /**
* Render the template with the given {@code name} using the given {@code modelAttributes}. * Render the template with the given {@code name} using the given {@code modelAttributes}.
...@@ -339,7 +352,7 @@ public interface Response<T> { ...@@ -339,7 +352,7 @@ public interface Response<T> {
* @param modelAttributes the modelAttributes used to render the template * @param modelAttributes the modelAttributes used to render the template
* @return the built response * @return the built response
*/ */
Response<Rendering> render(String name, Object... modelAttributes); ServerResponse<Rendering> render(String name, Object... modelAttributes);
/** /**
* Render the template with the given {@code name} using the given {@code model}. * Render the template with the given {@code name} using the given {@code model}.
...@@ -347,7 +360,7 @@ public interface Response<T> { ...@@ -347,7 +360,7 @@ public interface Response<T> {
* @param model the model used to render the template * @param model the model used to render the template
* @return the built response * @return the built response
*/ */
Response<Rendering> render(String name, Map<String, ?> model); ServerResponse<Rendering> render(String name, Map<String, ?> model);
} }
......
...@@ -24,9 +24,9 @@ import org.springframework.core.MethodParameter; ...@@ -24,9 +24,9 @@ import org.springframework.core.MethodParameter;
import org.springframework.web.reactive.HandlerAdapter; import org.springframework.web.reactive.HandlerAdapter;
import org.springframework.web.reactive.HandlerResult; import org.springframework.web.reactive.HandlerResult;
import org.springframework.web.reactive.function.HandlerFunction; import org.springframework.web.reactive.function.HandlerFunction;
import org.springframework.web.reactive.function.Request;
import org.springframework.web.reactive.function.Response;
import org.springframework.web.reactive.function.RouterFunctions; import org.springframework.web.reactive.function.RouterFunctions;
import org.springframework.web.reactive.function.ServerRequest;
import org.springframework.web.reactive.function.ServerResponse;
import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.ServerWebExchange;
/** /**
...@@ -41,7 +41,7 @@ public class HandlerFunctionAdapter implements HandlerAdapter { ...@@ -41,7 +41,7 @@ public class HandlerFunctionAdapter implements HandlerAdapter {
static { static {
try { try {
Method method = HandlerFunction.class.getMethod("handle", Request.class); Method method = HandlerFunction.class.getMethod("handle", ServerRequest.class);
HANDLER_FUNCTION_RETURN_TYPE = new MethodParameter(method, -1); HANDLER_FUNCTION_RETURN_TYPE = new MethodParameter(method, -1);
} }
catch (NoSuchMethodException ex) { catch (NoSuchMethodException ex) {
...@@ -57,11 +57,12 @@ public class HandlerFunctionAdapter implements HandlerAdapter { ...@@ -57,11 +57,12 @@ public class HandlerFunctionAdapter implements HandlerAdapter {
@Override @Override
public Mono<HandlerResult> handle(ServerWebExchange exchange, Object handler) { public Mono<HandlerResult> handle(ServerWebExchange exchange, Object handler) {
HandlerFunction<?> handlerFunction = (HandlerFunction<?>) handler; HandlerFunction<?> handlerFunction = (HandlerFunction<?>) handler;
Request request = ServerRequest request =
exchange.<Request>getAttribute(RouterFunctions.REQUEST_ATTRIBUTE) exchange.<ServerRequest>getAttribute(RouterFunctions.REQUEST_ATTRIBUTE)
.orElseThrow(() -> new IllegalStateException("Could not find Request in exchange attributes")); .orElseThrow(() -> new IllegalStateException(
"Could not find ServerRequest in exchange attributes"));
Response<?> response = handlerFunction.handle(request); ServerResponse<?> response = handlerFunction.handle(request);
HandlerResult handlerResult = HandlerResult handlerResult =
new HandlerResult(handlerFunction, response, HANDLER_FUNCTION_RETURN_TYPE); new HandlerResult(handlerFunction, response, HANDLER_FUNCTION_RETURN_TYPE);
return Mono.just(handlerResult); return Mono.just(handlerResult);
......
...@@ -32,18 +32,18 @@ import org.springframework.http.codec.BodyExtractor; ...@@ -32,18 +32,18 @@ import org.springframework.http.codec.BodyExtractor;
import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.web.reactive.function.HandlerFunction; import org.springframework.web.reactive.function.HandlerFunction;
import org.springframework.web.reactive.function.Request; import org.springframework.web.reactive.function.ServerRequest;
/** /**
* Implementation of the {@link Request} 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
* {@link HandlerFunction handler function}. All methods default to calling through to the wrapped request. * {@link HandlerFunction handler function}. All methods default to calling through to the wrapped request.
* *
* @author Arjen Poutsma * @author Arjen Poutsma
* @since 5.0 * @since 5.0
*/ */
public class RequestWrapper implements Request { public class ServerRequestWrapper implements ServerRequest {
private final Request request; private final ServerRequest request;
/** /**
...@@ -51,7 +51,7 @@ public class RequestWrapper implements Request { ...@@ -51,7 +51,7 @@ public class RequestWrapper implements Request {
* *
* @param request the request to wrap * @param request the request to wrap
*/ */
public RequestWrapper(Request request) { public ServerRequestWrapper(ServerRequest request) {
Assert.notNull(request, "'request' must not be null"); Assert.notNull(request, "'request' must not be null");
this.request = request; this.request = request;
} }
...@@ -59,7 +59,7 @@ public class RequestWrapper implements Request { ...@@ -59,7 +59,7 @@ public class RequestWrapper implements Request {
/** /**
* Return the wrapped request. * Return the wrapped request.
*/ */
public Request request() { public ServerRequest request() {
return this.request; return this.request;
} }
...@@ -117,7 +117,7 @@ public class RequestWrapper implements Request { ...@@ -117,7 +117,7 @@ public class RequestWrapper implements Request {
* 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.
*/ */
public static class HeadersWrapper implements Request.Headers { public static class HeadersWrapper implements ServerRequest.Headers {
private final Headers headers; private final Headers headers;
......
...@@ -21,31 +21,31 @@ import reactor.core.publisher.Mono; ...@@ -21,31 +21,31 @@ import reactor.core.publisher.Mono;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.web.reactive.HandlerResult; import org.springframework.web.reactive.HandlerResult;
import org.springframework.web.reactive.HandlerResultHandler; import org.springframework.web.reactive.HandlerResultHandler;
import org.springframework.web.reactive.function.Response; import org.springframework.web.reactive.function.ServerResponse;
import org.springframework.web.reactive.function.StrategiesSupplier; import org.springframework.web.reactive.function.HandlerStrategies;
import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.ServerWebExchange;
/** /**
* {@code HandlerResultHandler} implementation that supports {@link Response}s. * {@code HandlerResultHandler} implementation that supports {@link ServerResponse}s.
* *
* @author Arjen Poutsma * @author Arjen Poutsma
* @since 5.0 * @since 5.0
*/ */
public class ResponseResultHandler implements HandlerResultHandler { public class ServerResponseResultHandler implements HandlerResultHandler {
private final StrategiesSupplier strategies; private final HandlerStrategies strategies;
/** /**
* Create a {@code ResponseResultHandler} with default strategies. * Create a {@code ResponseResultHandler} with default strategies.
*/ */
public ResponseResultHandler() { public ServerResponseResultHandler() {
this(StrategiesSupplier.builder().build()); this(HandlerStrategies.builder().build());
} }
/** /**
* Create a {@code ResponseResultHandler} with the given strategies. * Create a {@code ResponseResultHandler} with the given strategies.
*/ */
public ResponseResultHandler(StrategiesSupplier strategies) { public ServerResponseResultHandler(HandlerStrategies strategies) {
Assert.notNull(strategies, "'strategies' must not be null"); Assert.notNull(strategies, "'strategies' must not be null");
this.strategies = strategies; this.strategies = strategies;
} }
...@@ -53,13 +53,13 @@ public class ResponseResultHandler implements HandlerResultHandler { ...@@ -53,13 +53,13 @@ public class ResponseResultHandler implements HandlerResultHandler {
@Override @Override
public boolean supports(HandlerResult result) { public boolean supports(HandlerResult result) {
return result.getReturnValue() return result.getReturnValue()
.filter(o -> o instanceof Response) .filter(o -> o instanceof ServerResponse)
.isPresent(); .isPresent();
} }
@Override @Override
public Mono<Void> handleResult(ServerWebExchange exchange, HandlerResult result) { public Mono<Void> handleResult(ServerWebExchange exchange, HandlerResult result) {
Response<?> response = (Response<?>) result.getReturnValue().orElseThrow( ServerResponse<?> response = (ServerResponse<?>) result.getReturnValue().orElseThrow(
IllegalStateException::new); IllegalStateException::new);
return response.writeTo(exchange, this.strategies); return response.writeTo(exchange, this.strategies);
} }
......
/** /**
* Classes supporting the {@code org.springframework.web.reactive.function} package. * Classes supporting the {@code org.springframework.web.reactive.function} package.
* Contains a {@code HandlerAdapter} that supports {@code HandlerFunction}s, * Contains a {@code HandlerAdapter} that supports {@code HandlerFunction}s,
* a {@code HandlerResultHandler} that supports {@code Response}s, and * a {@code HandlerResultHandler} that supports {@code ServerResponse}s, and
* a {@code Request} wrapper to adapt a request. * a {@code ServerRequest} wrapper to adapt a request.
*/ */
package org.springframework.web.reactive.function.support; package org.springframework.web.reactive.function.support;
\ No newline at end of file
...@@ -57,7 +57,7 @@ import static org.springframework.http.codec.BodyExtractors.toMono; ...@@ -57,7 +57,7 @@ import static org.springframework.http.codec.BodyExtractors.toMono;
/** /**
* @author Arjen Poutsma * @author Arjen Poutsma
*/ */
public class DefaultRequestTests { public class DefaultServerRequestTests {
private ServerHttpRequest mockRequest; private ServerHttpRequest mockRequest;
...@@ -65,9 +65,9 @@ public class DefaultRequestTests { ...@@ -65,9 +65,9 @@ public class DefaultRequestTests {
private ServerWebExchange mockExchange; private ServerWebExchange mockExchange;
private StrategiesSupplier mockStrategiesSupplier; private HandlerStrategies mockHandlerStrategies;
private DefaultRequest defaultRequest; private DefaultServerRequest defaultRequest;
@Before @Before
public void createMocks() { public void createMocks() {
...@@ -77,9 +77,9 @@ public class DefaultRequestTests { ...@@ -77,9 +77,9 @@ public class DefaultRequestTests {
mockExchange = mock(ServerWebExchange.class); mockExchange = mock(ServerWebExchange.class);
when(mockExchange.getRequest()).thenReturn(mockRequest); when(mockExchange.getRequest()).thenReturn(mockRequest);
when(mockExchange.getResponse()).thenReturn(mockResponse); when(mockExchange.getResponse()).thenReturn(mockResponse);
mockStrategiesSupplier = mock(StrategiesSupplier.class); mockHandlerStrategies = mock(HandlerStrategies.class);
defaultRequest = new DefaultRequest(mockExchange, mockStrategiesSupplier); defaultRequest = new DefaultServerRequest(mockExchange, mockHandlerStrategies);
} }
@Test @Test
...@@ -149,7 +149,7 @@ public class DefaultRequestTests { ...@@ -149,7 +149,7 @@ public class DefaultRequestTests {
when(mockRequest.getHeaders()).thenReturn(httpHeaders); when(mockRequest.getHeaders()).thenReturn(httpHeaders);
Request.Headers headers = defaultRequest.headers(); ServerRequest.Headers headers = defaultRequest.headers();
assertEquals(accept, headers.accept()); assertEquals(accept, headers.accept());
assertEquals(acceptCharset, headers.acceptCharset()); assertEquals(acceptCharset, headers.acceptCharset());
assertEquals(OptionalLong.of(contentLength), headers.contentLength()); assertEquals(OptionalLong.of(contentLength), headers.contentLength());
...@@ -171,7 +171,7 @@ public class DefaultRequestTests { ...@@ -171,7 +171,7 @@ public class DefaultRequestTests {
Set<HttpMessageReader<?>> messageReaders = Collections Set<HttpMessageReader<?>> messageReaders = Collections
.singleton(new DecoderHttpMessageReader<String>(new StringDecoder())); .singleton(new DecoderHttpMessageReader<String>(new StringDecoder()));
when(mockStrategiesSupplier.messageReaders()).thenReturn(messageReaders::stream); when(mockHandlerStrategies.messageReaders()).thenReturn(messageReaders::stream);
Mono<String> resultMono = defaultRequest.body(toMono(String.class)); Mono<String> resultMono = defaultRequest.body(toMono(String.class));
assertEquals("foo", resultMono.block()); assertEquals("foo", resultMono.block());
......
...@@ -61,137 +61,137 @@ import static org.mockito.Mockito.when; ...@@ -61,137 +61,137 @@ import static org.mockito.Mockito.when;
/** /**
* @author Arjen Poutsma * @author Arjen Poutsma
*/ */
public class DefaultResponseBuilderTests { public class DefaultServerResponseBuilderTests {
@Test @Test
public void from() throws Exception { public void from() throws Exception {
Response<Void> other = Response.ok().header("foo", "bar").build(); ServerResponse<Void> other = ServerResponse.ok().header("foo", "bar").build();
Response<Void> result = Response.from(other).build(); ServerResponse<Void> result = ServerResponse.from(other).build();
assertEquals(HttpStatus.OK, result.statusCode()); assertEquals(HttpStatus.OK, result.statusCode());
assertEquals("bar", result.headers().getFirst("foo")); assertEquals("bar", result.headers().getFirst("foo"));
} }
@Test @Test
public void status() throws Exception { public void status() throws Exception {
Response<Void> result = Response.status(HttpStatus.CREATED).build(); ServerResponse<Void> result = ServerResponse.status(HttpStatus.CREATED).build();
assertEquals(HttpStatus.CREATED, result.statusCode()); assertEquals(HttpStatus.CREATED, result.statusCode());
} }
@Test @Test
public void statusInt() throws Exception { public void statusInt() throws Exception {
Response<Void> result = Response.status(201).build(); ServerResponse<Void> result = ServerResponse.status(201).build();
assertEquals(HttpStatus.CREATED, result.statusCode()); assertEquals(HttpStatus.CREATED, result.statusCode());
} }
@Test @Test
public void ok() throws Exception { public void ok() throws Exception {
Response<Void> result = Response.ok().build(); ServerResponse<Void> result = ServerResponse.ok().build();
assertEquals(HttpStatus.OK, result.statusCode()); assertEquals(HttpStatus.OK, result.statusCode());
} }
@Test @Test
public void created() throws Exception { public void created() throws Exception {
URI location = URI.create("http://example.com"); URI location = URI.create("http://example.com");
Response<Void> result = Response.created(location).build(); ServerResponse<Void> result = ServerResponse.created(location).build();
assertEquals(HttpStatus.CREATED, result.statusCode()); assertEquals(HttpStatus.CREATED, result.statusCode());
assertEquals(location, result.headers().getLocation()); assertEquals(location, result.headers().getLocation());
} }
@Test @Test
public void accepted() throws Exception { public void accepted() throws Exception {
Response<Void> result = Response.accepted().build(); ServerResponse<Void> result = ServerResponse.accepted().build();
assertEquals(HttpStatus.ACCEPTED, result.statusCode()); assertEquals(HttpStatus.ACCEPTED, result.statusCode());
} }
@Test @Test
public void noContent() throws Exception { public void noContent() throws Exception {
Response<Void> result = Response.noContent().build(); ServerResponse<Void> result = ServerResponse.noContent().build();
assertEquals(HttpStatus.NO_CONTENT, result.statusCode()); assertEquals(HttpStatus.NO_CONTENT, result.statusCode());
} }
@Test @Test
public void badRequest() throws Exception { public void badRequest() throws Exception {
Response<Void> result = Response.badRequest().build(); ServerResponse<Void> result = ServerResponse.badRequest().build();
assertEquals(HttpStatus.BAD_REQUEST, result.statusCode()); assertEquals(HttpStatus.BAD_REQUEST, result.statusCode());
} }
@Test @Test
public void notFound() throws Exception { public void notFound() throws Exception {
Response<Void> result = Response.notFound().build(); ServerResponse<Void> result = ServerResponse.notFound().build();
assertEquals(HttpStatus.NOT_FOUND, result.statusCode()); assertEquals(HttpStatus.NOT_FOUND, result.statusCode());
} }
@Test @Test
public void unprocessableEntity() throws Exception { public void unprocessableEntity() throws Exception {
Response<Void> result = Response.unprocessableEntity().build(); ServerResponse<Void> result = ServerResponse.unprocessableEntity().build();
assertEquals(HttpStatus.UNPROCESSABLE_ENTITY, result.statusCode()); assertEquals(HttpStatus.UNPROCESSABLE_ENTITY, result.statusCode());
} }
@Test @Test
public void allow() throws Exception { public void allow() throws Exception {
Response<Void> result = Response.ok().allow(HttpMethod.GET).build(); ServerResponse<Void> result = ServerResponse.ok().allow(HttpMethod.GET).build();
assertEquals(Collections.singleton(HttpMethod.GET), result.headers().getAllow()); assertEquals(Collections.singleton(HttpMethod.GET), result.headers().getAllow());
} }
@Test @Test
public void contentLength() throws Exception { public void contentLength() throws Exception {
Response<Void> result = Response.ok().contentLength(42).build(); ServerResponse<Void> result = ServerResponse.ok().contentLength(42).build();
assertEquals(42, result.headers().getContentLength()); assertEquals(42, result.headers().getContentLength());
} }
@Test @Test
public void contentType() throws Exception { public void contentType() throws Exception {
Response<Void> result = Response.ok().contentType(MediaType.APPLICATION_JSON).build(); ServerResponse<Void> result = ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).build();
assertEquals(MediaType.APPLICATION_JSON, result.headers().getContentType()); assertEquals(MediaType.APPLICATION_JSON, result.headers().getContentType());
} }
@Test @Test
public void eTag() throws Exception { public void eTag() throws Exception {
Response<Void> result = Response.ok().eTag("foo").build(); ServerResponse<Void> result = ServerResponse.ok().eTag("foo").build();
assertEquals("\"foo\"", result.headers().getETag()); assertEquals("\"foo\"", result.headers().getETag());
} }
@Test @Test
public void lastModified() throws Exception { public void lastModified() throws Exception {
ZonedDateTime now = ZonedDateTime.now(); ZonedDateTime now = ZonedDateTime.now();
Response<Void> result = Response.ok().lastModified(now).build(); ServerResponse<Void> result = ServerResponse.ok().lastModified(now).build();
assertEquals(now.toInstant().toEpochMilli()/1000, result.headers().getLastModified()/1000); assertEquals(now.toInstant().toEpochMilli()/1000, result.headers().getLastModified()/1000);
} }
@Test @Test
public void cacheControlTag() throws Exception { public void cacheControlTag() throws Exception {
Response<Void> result = Response.ok().cacheControl(CacheControl.noCache()).build(); ServerResponse<Void> result = ServerResponse.ok().cacheControl(CacheControl.noCache()).build();
assertEquals("no-cache", result.headers().getCacheControl()); assertEquals("no-cache", result.headers().getCacheControl());
} }
@Test @Test
public void varyBy() throws Exception { public void varyBy() throws Exception {
Response<Void> result = Response.ok().varyBy("foo").build(); ServerResponse<Void> result = ServerResponse.ok().varyBy("foo").build();
assertEquals(Collections.singletonList("foo"), result.headers().getVary()); assertEquals(Collections.singletonList("foo"), result.headers().getVary());
} }
@Test @Test
public void statusCode() throws Exception { public void statusCode() throws Exception {
HttpStatus statusCode = HttpStatus.ACCEPTED; HttpStatus statusCode = HttpStatus.ACCEPTED;
Response<Void> result = Response.status(statusCode).build(); ServerResponse<Void> result = ServerResponse.status(statusCode).build();
assertSame(statusCode, result.statusCode()); assertSame(statusCode, result.statusCode());
} }
@Test @Test
public void headers() throws Exception { public void headers() throws Exception {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
Response<Void> result = Response.ok().headers(headers).build(); ServerResponse<Void> result = ServerResponse.ok().headers(headers).build();
assertEquals(headers, result.headers()); assertEquals(headers, result.headers());
} }
@Test @Test
public void build() throws Exception { public void build() throws Exception {
Response<Void> result = Response.status(201).header("MyKey", "MyValue").build(); ServerResponse<Void> result = ServerResponse.status(201).header("MyKey", "MyValue").build();
ServerWebExchange exchange = mock(ServerWebExchange.class); ServerWebExchange exchange = mock(ServerWebExchange.class);
MockServerHttpResponse response = new MockServerHttpResponse(); MockServerHttpResponse response = new MockServerHttpResponse();
when(exchange.getResponse()).thenReturn(response); when(exchange.getResponse()).thenReturn(response);
StrategiesSupplier strategies = mock(StrategiesSupplier.class); HandlerStrategies strategies = mock(HandlerStrategies.class);
result.writeTo(exchange, strategies).block(); result.writeTo(exchange, strategies).block();
assertEquals(201, response.getStatusCode().value()); assertEquals(201, response.getStatusCode().value());
...@@ -203,12 +203,12 @@ public class DefaultResponseBuilderTests { ...@@ -203,12 +203,12 @@ public class DefaultResponseBuilderTests {
@Test @Test
public void buildVoidPublisher() throws Exception { public void buildVoidPublisher() throws Exception {
Mono<Void> mono = Mono.empty(); Mono<Void> mono = Mono.empty();
Response<Mono<Void>> result = Response.ok().build(mono); ServerResponse<Mono<Void>> result = ServerResponse.ok().build(mono);
ServerWebExchange exchange = mock(ServerWebExchange.class); ServerWebExchange exchange = mock(ServerWebExchange.class);
MockServerHttpResponse response = new MockServerHttpResponse(); MockServerHttpResponse response = new MockServerHttpResponse();
when(exchange.getResponse()).thenReturn(response); when(exchange.getResponse()).thenReturn(response);
StrategiesSupplier strategies = mock(StrategiesSupplier.class); HandlerStrategies strategies = mock(HandlerStrategies.class);
result.writeTo(exchange, strategies).block(); result.writeTo(exchange, strategies).block();
assertNull(response.getBody()); assertNull(response.getBody());
...@@ -227,7 +227,7 @@ public class DefaultResponseBuilderTests { ...@@ -227,7 +227,7 @@ public class DefaultResponseBuilderTests {
return response.writeWith(Mono.just(buffer)); return response.writeWith(Mono.just(buffer));
}; };
Response<String> result = Response.ok().body(writer, supplier); ServerResponse<String> result = ServerResponse.ok().body(BodyInserter.of(writer, supplier));
assertEquals(body, result.body()); assertEquals(body, result.body());
MockServerHttpRequest request = MockServerHttpRequest request =
...@@ -239,7 +239,7 @@ public class DefaultResponseBuilderTests { ...@@ -239,7 +239,7 @@ public class DefaultResponseBuilderTests {
List<HttpMessageWriter<?>> messageWriters = new ArrayList<>(); List<HttpMessageWriter<?>> messageWriters = new ArrayList<>();
messageWriters.add(new EncoderHttpMessageWriter<CharSequence>(new CharSequenceEncoder())); messageWriters.add(new EncoderHttpMessageWriter<CharSequence>(new CharSequenceEncoder()));
StrategiesSupplier strategies = mock(StrategiesSupplier.class); HandlerStrategies strategies = mock(HandlerStrategies.class);
when(strategies.messageWriters()).thenReturn(messageWriters::stream); when(strategies.messageWriters()).thenReturn(messageWriters::stream);
result.writeTo(exchange, strategies).block(); result.writeTo(exchange, strategies).block();
...@@ -249,7 +249,7 @@ public class DefaultResponseBuilderTests { ...@@ -249,7 +249,7 @@ public class DefaultResponseBuilderTests {
@Test @Test
public void render() throws Exception { public void render() throws Exception {
Map<String, Object> model = Collections.singletonMap("foo", "bar"); Map<String, Object> model = Collections.singletonMap("foo", "bar");
Response<Rendering> result = Response.ok().render("view", model); ServerResponse<Rendering> result = ServerResponse.ok().render("view", model);
assertEquals("view", result.body().name()); assertEquals("view", result.body().name());
assertEquals(model, result.body().model()); assertEquals(model, result.body().model());
...@@ -265,7 +265,7 @@ public class DefaultResponseBuilderTests { ...@@ -265,7 +265,7 @@ public class DefaultResponseBuilderTests {
List<ViewResolver> viewResolvers = new ArrayList<>(); List<ViewResolver> viewResolvers = new ArrayList<>();
viewResolvers.add(viewResolver); viewResolvers.add(viewResolver);
StrategiesSupplier mockConfig = mock(StrategiesSupplier.class); HandlerStrategies mockConfig = mock(HandlerStrategies.class);
when(mockConfig.viewResolvers()).thenReturn(viewResolvers::stream); when(mockConfig.viewResolvers()).thenReturn(viewResolvers::stream);
result.writeTo(exchange, mockConfig).block(); result.writeTo(exchange, mockConfig).block();
...@@ -273,11 +273,11 @@ public class DefaultResponseBuilderTests { ...@@ -273,11 +273,11 @@ public class DefaultResponseBuilderTests {
@Test @Test
public void renderObjectArray() throws Exception { public void renderObjectArray() throws Exception {
Response<Rendering> result = ServerResponse<Rendering> result =
Response.ok().render("name", this, Collections.emptyList(), "foo"); ServerResponse.ok().render("name", this, Collections.emptyList(), "foo");
Map<String, Object> model = result.body().model(); Map<String, Object> model = result.body().model();
assertEquals(2, model.size()); assertEquals(2, model.size());
assertEquals(this, model.get("defaultResponseBuilderTests")); assertEquals(this, model.get("defaultServerResponseBuilderTests"));
assertEquals("foo", model.get("string")); assertEquals("foo", model.get("string"));
} }
......
...@@ -45,7 +45,7 @@ import org.springframework.web.reactive.HandlerAdapter; ...@@ -45,7 +45,7 @@ import org.springframework.web.reactive.HandlerAdapter;
import org.springframework.web.reactive.HandlerMapping; import org.springframework.web.reactive.HandlerMapping;
import org.springframework.web.reactive.config.WebReactiveConfigurationSupport; import org.springframework.web.reactive.config.WebReactiveConfigurationSupport;
import org.springframework.web.reactive.function.support.HandlerFunctionAdapter; import org.springframework.web.reactive.function.support.HandlerFunctionAdapter;
import org.springframework.web.reactive.function.support.ResponseResultHandler; import org.springframework.web.reactive.function.support.ServerResponseResultHandler;
import org.springframework.web.reactive.result.view.ViewResolver; import org.springframework.web.reactive.result.view.ViewResolver;
import org.springframework.web.server.adapter.WebHttpHandlerBuilder; import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
...@@ -121,7 +121,7 @@ public class DispatcherHandlerIntegrationTests extends AbstractHttpHandlerIntegr ...@@ -121,7 +121,7 @@ public class DispatcherHandlerIntegrationTests extends AbstractHttpHandlerIntegr
public HandlerMapping handlerMapping(RouterFunction<?> routerFunction, public HandlerMapping handlerMapping(RouterFunction<?> routerFunction,
ApplicationContext applicationContext) { ApplicationContext applicationContext) {
return RouterFunctions.toHandlerMapping(routerFunction, return RouterFunctions.toHandlerMapping(routerFunction,
new StrategiesSupplier() { new HandlerStrategies() {
@Override @Override
public Supplier<Stream<HttpMessageReader<?>>> messageReaders() { public Supplier<Stream<HttpMessageReader<?>>> messageReaders() {
return () -> getMessageReaders().stream(); return () -> getMessageReaders().stream();
...@@ -147,22 +147,22 @@ public class DispatcherHandlerIntegrationTests extends AbstractHttpHandlerIntegr ...@@ -147,22 +147,22 @@ public class DispatcherHandlerIntegrationTests extends AbstractHttpHandlerIntegr
} }
@Bean @Bean
public ResponseResultHandler responseResultHandler() { public ServerResponseResultHandler responseResultHandler() {
return new ResponseResultHandler(); return new ServerResponseResultHandler();
} }
} }
private static class PersonHandler { private static class PersonHandler {
public Response<Publisher<Person>> mono(Request request) { public ServerResponse<Publisher<Person>> mono(ServerRequest request) {
Person person = new Person("John"); Person person = new Person("John");
return Response.ok().body(fromPublisher(Mono.just(person), Person.class)); return ServerResponse.ok().body(fromPublisher(Mono.just(person), Person.class));
} }
public Response<Publisher<Person>> flux(Request request) { public ServerResponse<Publisher<Person>> flux(ServerRequest request) {
Person person1 = new Person("John"); Person person1 = new Person("John");
Person person2 = new Person("Jane"); Person person2 = new Person("Jane");
return Response.ok().body( return ServerResponse.ok().body(
fromPublisher(Flux.just(person1, person2), Person.class)); fromPublisher(Flux.just(person1, person2), Person.class));
} }
......
...@@ -41,11 +41,11 @@ import static org.junit.Assert.*; ...@@ -41,11 +41,11 @@ import static org.junit.Assert.*;
/** /**
* @author Arjen Poutsma * @author Arjen Poutsma
*/ */
public class StrategiesSupplierTests { public class HandlerStrategiesTests {
@Test @Test
public void empty() { public void empty() {
StrategiesSupplier strategies = StrategiesSupplier.empty().build(); HandlerStrategies strategies = HandlerStrategies.empty().build();
assertEquals(Optional.empty(), strategies.messageReaders().get().findFirst()); assertEquals(Optional.empty(), strategies.messageReaders().get().findFirst());
assertEquals(Optional.empty(), strategies.messageWriters().get().findFirst()); assertEquals(Optional.empty(), strategies.messageWriters().get().findFirst());
assertEquals(Optional.empty(), strategies.viewResolvers().get().findFirst()); assertEquals(Optional.empty(), strategies.viewResolvers().get().findFirst());
...@@ -56,7 +56,7 @@ public class StrategiesSupplierTests { ...@@ -56,7 +56,7 @@ public class StrategiesSupplierTests {
HttpMessageReader<?> messageReader = new DummyMessageReader(); HttpMessageReader<?> messageReader = new DummyMessageReader();
HttpMessageWriter<?> messageWriter = new DummyMessageWriter(); HttpMessageWriter<?> messageWriter = new DummyMessageWriter();
StrategiesSupplier strategies = StrategiesSupplier.of( HandlerStrategies strategies = HandlerStrategies.of(
() -> Stream.of(messageReader), () -> Stream.of(messageReader),
() -> Stream.of(messageWriter), () -> Stream.of(messageWriter),
null); null);
...@@ -77,7 +77,7 @@ public class StrategiesSupplierTests { ...@@ -77,7 +77,7 @@ public class StrategiesSupplierTests {
applicationContext.registerSingleton("messageReader", DummyMessageReader.class); applicationContext.registerSingleton("messageReader", DummyMessageReader.class);
applicationContext.refresh(); applicationContext.refresh();
StrategiesSupplier strategies = StrategiesSupplier.of(applicationContext); HandlerStrategies strategies = HandlerStrategies.of(applicationContext);
assertTrue(strategies.messageReaders().get() assertTrue(strategies.messageReaders().get()
.allMatch(r -> r instanceof DummyMessageReader)); .allMatch(r -> r instanceof DummyMessageReader));
assertTrue(strategies.messageWriters().get() assertTrue(strategies.messageWriters().get()
......
...@@ -30,7 +30,7 @@ import org.junit.Test; ...@@ -30,7 +30,7 @@ import org.junit.Test;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRange; import org.springframework.http.HttpRange;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.support.RequestWrapper; import org.springframework.web.reactive.function.support.ServerRequestWrapper;
import static org.junit.Assert.assertSame; import static org.junit.Assert.assertSame;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
...@@ -41,15 +41,15 @@ import static org.mockito.Mockito.when; ...@@ -41,15 +41,15 @@ import static org.mockito.Mockito.when;
*/ */
public class HeadersWrapperTest { public class HeadersWrapperTest {
private Request.Headers mockHeaders; private ServerRequest.Headers mockHeaders;
private RequestWrapper.HeadersWrapper wrapper; private ServerRequestWrapper.HeadersWrapper wrapper;
@Before @Before
public void createWrapper() { public void createWrapper() {
mockHeaders = mock(Request.Headers.class); mockHeaders = mock(ServerRequest.Headers.class);
wrapper = new RequestWrapper.HeadersWrapper(mockHeaders); wrapper = new ServerRequestWrapper.HeadersWrapper(mockHeaders);
} }
@Test @Test
......
...@@ -42,7 +42,7 @@ import org.springframework.util.MultiValueMap; ...@@ -42,7 +42,7 @@ import org.springframework.util.MultiValueMap;
/** /**
* @author Arjen Poutsma * @author Arjen Poutsma
*/ */
public class MockRequest<T> implements Request { public class MockServerRequest<T> implements ServerRequest {
private final HttpMethod method; private final HttpMethod method;
...@@ -58,7 +58,7 @@ public class MockRequest<T> implements Request { ...@@ -58,7 +58,7 @@ public class MockRequest<T> implements Request {
private final Map<String, String> pathVariables; private final Map<String, String> pathVariables;
private MockRequest(HttpMethod method, URI uri, private MockServerRequest(HttpMethod method, URI uri,
MockHeaders headers, T body, Map<String, Object> attributes, MockHeaders headers, T body, Map<String, Object> attributes,
MultiValueMap<String, String> queryParams, MultiValueMap<String, String> queryParams,
Map<String, String> pathVariables) { Map<String, String> pathVariables) {
...@@ -134,9 +134,9 @@ public class MockRequest<T> implements Request { ...@@ -134,9 +134,9 @@ public class MockRequest<T> implements Request {
Builder<T> pathVariables(Map<String, String> pathVariables); Builder<T> pathVariables(Map<String, String> pathVariables);
MockRequest<T> body(T body); MockServerRequest<T> body(T body);
MockRequest<Void> build(); MockServerRequest<Void> build();
} }
...@@ -231,15 +231,15 @@ public class MockRequest<T> implements Request { ...@@ -231,15 +231,15 @@ public class MockRequest<T> implements Request {
} }
@Override @Override
public MockRequest<T> body(T body) { public MockServerRequest<T> body(T body) {
this.body = body; this.body = body;
return new MockRequest<T>(this.method, this.uri, this.headers, this.body, return new MockServerRequest<T>(this.method, this.uri, this.headers, this.body,
this.attributes, this.queryParams, this.pathVariables); this.attributes, this.queryParams, this.pathVariables);
} }
@Override @Override
public MockRequest<Void> build() { public MockServerRequest<Void> build() {
return new MockRequest<Void>(this.method, this.uri, this.headers, null, return new MockServerRequest<Void>(this.method, this.uri, this.headers, null,
this.attributes, this.queryParams, this.pathVariables); this.attributes, this.queryParams, this.pathVariables);
} }
......
...@@ -97,20 +97,20 @@ public class PublisherHandlerFunctionIntegrationTests ...@@ -97,20 +97,20 @@ public class PublisherHandlerFunctionIntegrationTests
private static class PersonHandler { private static class PersonHandler {
public Response<Publisher<Person>> mono(Request request) { public ServerResponse<Publisher<Person>> mono(ServerRequest request) {
Person person = new Person("John"); Person person = new Person("John");
return Response.ok().body(fromPublisher(Mono.just(person), Person.class)); return ServerResponse.ok().body(fromPublisher(Mono.just(person), Person.class));
} }
public Response<Publisher<Person>> postMono(Request request) { public ServerResponse<Publisher<Person>> postMono(ServerRequest request) {
Mono<Person> personMono = request.body(toMono(Person.class)); Mono<Person> personMono = request.body(toMono(Person.class));
return Response.ok().body(fromPublisher(personMono, Person.class)); return ServerResponse.ok().body(fromPublisher(personMono, Person.class));
} }
public Response<Publisher<Person>> flux(Request request) { public ServerResponse<Publisher<Person>> flux(ServerRequest request) {
Person person1 = new Person("John"); Person person1 = new Person("John");
Person person2 = new Person("Jane"); Person person2 = new Person("Jane");
return Response.ok().body( return ServerResponse.ok().body(
fromPublisher(Flux.just(person1, person2), Person.class)); fromPublisher(Flux.just(person1, person2), Person.class));
} }
......
...@@ -32,7 +32,7 @@ public class RequestPredicateTests { ...@@ -32,7 +32,7 @@ public class RequestPredicateTests {
RequestPredicate predicate2 = request -> true; RequestPredicate predicate2 = request -> true;
RequestPredicate predicate3 = request -> false; RequestPredicate predicate3 = request -> false;
MockRequest request = MockRequest.builder().build(); MockServerRequest request = MockServerRequest.builder().build();
assertTrue(predicate1.and(predicate2).test(request)); assertTrue(predicate1.and(predicate2).test(request));
assertTrue(predicate2.and(predicate1).test(request)); assertTrue(predicate2.and(predicate1).test(request));
assertFalse(predicate1.and(predicate3).test(request)); assertFalse(predicate1.and(predicate3).test(request));
...@@ -43,7 +43,7 @@ public class RequestPredicateTests { ...@@ -43,7 +43,7 @@ public class RequestPredicateTests {
RequestPredicate predicate = request -> false; RequestPredicate predicate = request -> false;
RequestPredicate negated = predicate.negate(); RequestPredicate negated = predicate.negate();
MockRequest mockRequest = MockRequest.builder().build(); MockServerRequest mockRequest = MockServerRequest.builder().build();
assertTrue(negated.test(mockRequest)); assertTrue(negated.test(mockRequest));
predicate = request -> true; predicate = request -> true;
...@@ -58,7 +58,7 @@ public class RequestPredicateTests { ...@@ -58,7 +58,7 @@ public class RequestPredicateTests {
RequestPredicate predicate2 = request -> false; RequestPredicate predicate2 = request -> false;
RequestPredicate predicate3 = request -> false; RequestPredicate predicate3 = request -> false;
MockRequest request = MockRequest.builder().build(); MockServerRequest request = MockServerRequest.builder().build();
assertTrue(predicate1.or(predicate2).test(request)); assertTrue(predicate1.or(predicate2).test(request));
assertTrue(predicate2.or(predicate1).test(request)); assertTrue(predicate2.or(predicate1).test(request));
assertFalse(predicate2.or(predicate3).test(request)); assertFalse(predicate2.or(predicate3).test(request));
......
...@@ -35,7 +35,7 @@ public class RequestPredicatesTests { ...@@ -35,7 +35,7 @@ public class RequestPredicatesTests {
@Test @Test
public void all() throws Exception { public void all() throws Exception {
RequestPredicate predicate = RequestPredicates.all(); RequestPredicate predicate = RequestPredicates.all();
MockRequest request = MockRequest.builder().build(); MockServerRequest request = MockServerRequest.builder().build();
assertTrue(predicate.test(request)); assertTrue(predicate.test(request));
} }
...@@ -43,10 +43,10 @@ public class RequestPredicatesTests { ...@@ -43,10 +43,10 @@ public class RequestPredicatesTests {
public void method() throws Exception { public void method() throws Exception {
HttpMethod httpMethod = HttpMethod.GET; HttpMethod httpMethod = HttpMethod.GET;
RequestPredicate predicate = RequestPredicates.method(httpMethod); RequestPredicate predicate = RequestPredicates.method(httpMethod);
MockRequest request = MockRequest.builder().method(httpMethod).build(); MockServerRequest request = MockServerRequest.builder().method(httpMethod).build();
assertTrue(predicate.test(request)); assertTrue(predicate.test(request));
request = MockRequest.builder().method(HttpMethod.POST).build(); request = MockServerRequest.builder().method(HttpMethod.POST).build();
assertFalse(predicate.test(request)); assertFalse(predicate.test(request));
} }
...@@ -55,31 +55,31 @@ public class RequestPredicatesTests { ...@@ -55,31 +55,31 @@ public class RequestPredicatesTests {
URI uri = URI.create("http://localhost/path"); URI uri = URI.create("http://localhost/path");
RequestPredicate predicate = RequestPredicates.GET("/p*"); RequestPredicate predicate = RequestPredicates.GET("/p*");
MockRequest request = MockRequest.builder().method(HttpMethod.GET).uri(uri).build(); MockServerRequest request = MockServerRequest.builder().method(HttpMethod.GET).uri(uri).build();
assertTrue(predicate.test(request)); assertTrue(predicate.test(request));
predicate = RequestPredicates.HEAD("/p*"); predicate = RequestPredicates.HEAD("/p*");
request = MockRequest.builder().method(HttpMethod.HEAD).uri(uri).build(); request = MockServerRequest.builder().method(HttpMethod.HEAD).uri(uri).build();
assertTrue(predicate.test(request)); assertTrue(predicate.test(request));
predicate = RequestPredicates.POST("/p*"); predicate = RequestPredicates.POST("/p*");
request = MockRequest.builder().method(HttpMethod.POST).uri(uri).build(); request = MockServerRequest.builder().method(HttpMethod.POST).uri(uri).build();
assertTrue(predicate.test(request)); assertTrue(predicate.test(request));
predicate = RequestPredicates.PUT("/p*"); predicate = RequestPredicates.PUT("/p*");
request = MockRequest.builder().method(HttpMethod.PUT).uri(uri).build(); request = MockServerRequest.builder().method(HttpMethod.PUT).uri(uri).build();
assertTrue(predicate.test(request)); assertTrue(predicate.test(request));
predicate = RequestPredicates.PATCH("/p*"); predicate = RequestPredicates.PATCH("/p*");
request = MockRequest.builder().method(HttpMethod.PATCH).uri(uri).build(); request = MockServerRequest.builder().method(HttpMethod.PATCH).uri(uri).build();
assertTrue(predicate.test(request)); assertTrue(predicate.test(request));
predicate = RequestPredicates.DELETE("/p*"); predicate = RequestPredicates.DELETE("/p*");
request = MockRequest.builder().method(HttpMethod.DELETE).uri(uri).build(); request = MockServerRequest.builder().method(HttpMethod.DELETE).uri(uri).build();
assertTrue(predicate.test(request)); assertTrue(predicate.test(request));
predicate = RequestPredicates.OPTIONS("/p*"); predicate = RequestPredicates.OPTIONS("/p*");
request = MockRequest.builder().method(HttpMethod.OPTIONS).uri(uri).build(); request = MockServerRequest.builder().method(HttpMethod.OPTIONS).uri(uri).build();
assertTrue(predicate.test(request)); assertTrue(predicate.test(request));
} }
...@@ -88,10 +88,10 @@ public class RequestPredicatesTests { ...@@ -88,10 +88,10 @@ public class RequestPredicatesTests {
public void path() throws Exception { public void path() throws Exception {
URI uri = URI.create("http://localhost/path"); URI uri = URI.create("http://localhost/path");
RequestPredicate predicate = RequestPredicates.path("/p*"); RequestPredicate predicate = RequestPredicates.path("/p*");
MockRequest request = MockRequest.builder().uri(uri).build(); MockServerRequest request = MockServerRequest.builder().uri(uri).build();
assertTrue(predicate.test(request)); assertTrue(predicate.test(request));
request = MockRequest.builder().build(); request = MockServerRequest.builder().build();
assertFalse(predicate.test(request)); assertFalse(predicate.test(request));
} }
...@@ -104,10 +104,10 @@ public class RequestPredicatesTests { ...@@ -104,10 +104,10 @@ public class RequestPredicatesTests {
return headers.header(name).equals( return headers.header(name).equals(
Collections.singletonList(value)); Collections.singletonList(value));
}); });
MockRequest request = MockRequest.builder().header(name, value).build(); MockServerRequest request = MockServerRequest.builder().header(name, value).build();
assertTrue(predicate.test(request)); assertTrue(predicate.test(request));
request = MockRequest.builder().build(); request = MockServerRequest.builder().build();
assertFalse(predicate.test(request)); assertFalse(predicate.test(request));
} }
...@@ -115,10 +115,11 @@ public class RequestPredicatesTests { ...@@ -115,10 +115,11 @@ public class RequestPredicatesTests {
public void contentType() throws Exception { public void contentType() throws Exception {
MediaType json = MediaType.APPLICATION_JSON; MediaType json = MediaType.APPLICATION_JSON;
RequestPredicate predicate = RequestPredicates.contentType(json); RequestPredicate predicate = RequestPredicates.contentType(json);
MockRequest request = MockRequest.builder().header("Content-Type", json.toString()).build(); MockServerRequest
request = MockServerRequest.builder().header("Content-Type", json.toString()).build();
assertTrue(predicate.test(request)); assertTrue(predicate.test(request));
request = MockRequest.builder().build(); request = MockServerRequest.builder().build();
assertFalse(predicate.test(request)); assertFalse(predicate.test(request));
} }
...@@ -126,10 +127,10 @@ public class RequestPredicatesTests { ...@@ -126,10 +127,10 @@ public class RequestPredicatesTests {
public void accept() throws Exception { public void accept() throws Exception {
MediaType json = MediaType.APPLICATION_JSON; MediaType json = MediaType.APPLICATION_JSON;
RequestPredicate predicate = RequestPredicates.accept(json); RequestPredicate predicate = RequestPredicates.accept(json);
MockRequest request = MockRequest.builder().header("Accept", json.toString()).build(); MockServerRequest request = MockServerRequest.builder().header("Accept", json.toString()).build();
assertTrue(predicate.test(request)); assertTrue(predicate.test(request));
request = MockRequest.builder().build(); request = MockServerRequest.builder().build();
assertFalse(predicate.test(request)); assertFalse(predicate.test(request));
} }
......
...@@ -33,14 +33,14 @@ public class RouterFunctionTests { ...@@ -33,14 +33,14 @@ public class RouterFunctionTests {
@Test @Test
public void andSame() throws Exception { public void andSame() throws Exception {
HandlerFunction<Void> handlerFunction = request -> Response.ok().build(); HandlerFunction<Void> handlerFunction = request -> ServerResponse.ok().build();
RouterFunction<Void> routerFunction1 = request -> Optional.empty(); RouterFunction<Void> routerFunction1 = request -> Optional.empty();
RouterFunction<Void> routerFunction2 = request -> Optional.of(handlerFunction); RouterFunction<Void> routerFunction2 = request -> Optional.of(handlerFunction);
RouterFunction<Void> result = routerFunction1.andSame(routerFunction2); RouterFunction<Void> result = routerFunction1.andSame(routerFunction2);
assertNotNull(result); assertNotNull(result);
MockRequest request = MockRequest.builder().build(); MockServerRequest request = MockServerRequest.builder().build();
Optional<HandlerFunction<Void>> resultHandlerFunction = result.route(request); Optional<HandlerFunction<Void>> resultHandlerFunction = result.route(request);
assertTrue(resultHandlerFunction.isPresent()); assertTrue(resultHandlerFunction.isPresent());
assertEquals(handlerFunction, resultHandlerFunction.get()); assertEquals(handlerFunction, resultHandlerFunction.get());
...@@ -48,14 +48,14 @@ public class RouterFunctionTests { ...@@ -48,14 +48,14 @@ public class RouterFunctionTests {
@Test @Test
public void and() throws Exception { public void and() throws Exception {
HandlerFunction<String> handlerFunction = request -> Response.ok().body(fromObject("42")); HandlerFunction<String> handlerFunction = request -> ServerResponse.ok().body(fromObject("42"));
RouterFunction<Void> routerFunction1 = request -> Optional.empty(); RouterFunction<Void> routerFunction1 = request -> Optional.empty();
RouterFunction<String> routerFunction2 = request -> Optional.of(handlerFunction); RouterFunction<String> routerFunction2 = request -> Optional.of(handlerFunction);
RouterFunction<?> result = routerFunction1.and(routerFunction2); RouterFunction<?> result = routerFunction1.and(routerFunction2);
assertNotNull(result); assertNotNull(result);
MockRequest request = MockRequest.builder().build(); MockServerRequest request = MockServerRequest.builder().build();
Optional<? extends HandlerFunction<?>> resultHandlerFunction = result.route(request); Optional<? extends HandlerFunction<?>> resultHandlerFunction = result.route(request);
assertTrue(resultHandlerFunction.isPresent()); assertTrue(resultHandlerFunction.isPresent());
assertEquals(handlerFunction, resultHandlerFunction.get()); assertEquals(handlerFunction, resultHandlerFunction.get());
...@@ -63,21 +63,21 @@ public class RouterFunctionTests { ...@@ -63,21 +63,21 @@ public class RouterFunctionTests {
@Test @Test
public void filter() throws Exception { public void filter() throws Exception {
HandlerFunction<String> handlerFunction = request -> Response.ok().body(fromObject("42")); HandlerFunction<String> handlerFunction = request -> ServerResponse.ok().body(fromObject("42"));
RouterFunction<String> routerFunction = request -> Optional.of(handlerFunction); RouterFunction<String> routerFunction = request -> Optional.of(handlerFunction);
FilterFunction<String, Integer> filterFunction = (request, next) -> { HandlerFilterFunction<String, Integer> filterFunction = (request, next) -> {
Response<String> response = next.handle(request); ServerResponse<String> response = next.handle(request);
int i = Integer.parseInt(response.body()); int i = Integer.parseInt(response.body());
return Response.ok().body(fromObject(i)); return ServerResponse.ok().body(fromObject(i));
}; };
RouterFunction<Integer> result = routerFunction.filter(filterFunction); RouterFunction<Integer> result = routerFunction.filter(filterFunction);
assertNotNull(result); assertNotNull(result);
MockRequest request = MockRequest.builder().build(); MockServerRequest request = MockServerRequest.builder().build();
Optional<? extends HandlerFunction<?>> resultHandlerFunction = result.route(request); Optional<? extends HandlerFunction<?>> resultHandlerFunction = result.route(request);
assertTrue(resultHandlerFunction.isPresent()); assertTrue(resultHandlerFunction.isPresent());
Response<?> resultResponse = resultHandlerFunction.get().handle(request); ServerResponse<?> resultResponse = resultHandlerFunction.get().handle(request);
assertEquals(42, resultResponse.body()); assertEquals(42, resultResponse.body());
} }
......
...@@ -48,9 +48,9 @@ public class RouterFunctionsTests { ...@@ -48,9 +48,9 @@ public class RouterFunctionsTests {
@Test @Test
public void routeMatch() throws Exception { public void routeMatch() throws Exception {
HandlerFunction<Void> handlerFunction = request -> Response.ok().build(); HandlerFunction<Void> handlerFunction = request -> ServerResponse.ok().build();
MockRequest request = MockRequest.builder().build(); MockServerRequest request = MockServerRequest.builder().build();
RequestPredicate requestPredicate = mock(RequestPredicate.class); RequestPredicate requestPredicate = mock(RequestPredicate.class);
when(requestPredicate.test(request)).thenReturn(true); when(requestPredicate.test(request)).thenReturn(true);
...@@ -64,9 +64,9 @@ public class RouterFunctionsTests { ...@@ -64,9 +64,9 @@ public class RouterFunctionsTests {
@Test @Test
public void routeNoMatch() throws Exception { public void routeNoMatch() throws Exception {
HandlerFunction<Void> handlerFunction = request -> Response.ok().build(); HandlerFunction<Void> handlerFunction = request -> ServerResponse.ok().build();
MockRequest request = MockRequest.builder().build(); MockServerRequest request = MockServerRequest.builder().build();
RequestPredicate requestPredicate = mock(RequestPredicate.class); RequestPredicate requestPredicate = mock(RequestPredicate.class);
when(requestPredicate.test(request)).thenReturn(false); when(requestPredicate.test(request)).thenReturn(false);
...@@ -79,10 +79,10 @@ public class RouterFunctionsTests { ...@@ -79,10 +79,10 @@ public class RouterFunctionsTests {
@Test @Test
public void subrouteMatch() throws Exception { public void subrouteMatch() throws Exception {
HandlerFunction<Void> handlerFunction = request -> Response.ok().build(); HandlerFunction<Void> handlerFunction = request -> ServerResponse.ok().build();
RouterFunction<Void> routerFunction = request -> Optional.of(handlerFunction); RouterFunction<Void> routerFunction = request -> Optional.of(handlerFunction);
MockRequest request = MockRequest.builder().build(); MockServerRequest request = MockServerRequest.builder().build();
RequestPredicate requestPredicate = mock(RequestPredicate.class); RequestPredicate requestPredicate = mock(RequestPredicate.class);
when(requestPredicate.test(request)).thenReturn(true); when(requestPredicate.test(request)).thenReturn(true);
...@@ -96,10 +96,10 @@ public class RouterFunctionsTests { ...@@ -96,10 +96,10 @@ public class RouterFunctionsTests {
@Test @Test
public void subrouteNoMatch() throws Exception { public void subrouteNoMatch() throws Exception {
HandlerFunction<Void> handlerFunction = request -> Response.ok().build(); HandlerFunction<Void> handlerFunction = request -> ServerResponse.ok().build();
RouterFunction<Void> routerFunction = request -> Optional.of(handlerFunction); RouterFunction<Void> routerFunction = request -> Optional.of(handlerFunction);
MockRequest request = MockRequest.builder().build(); MockServerRequest request = MockServerRequest.builder().build();
RequestPredicate requestPredicate = mock(RequestPredicate.class); RequestPredicate requestPredicate = mock(RequestPredicate.class);
when(requestPredicate.test(request)).thenReturn(false); when(requestPredicate.test(request)).thenReturn(false);
...@@ -112,7 +112,7 @@ public class RouterFunctionsTests { ...@@ -112,7 +112,7 @@ public class RouterFunctionsTests {
@Test @Test
public void toHttpHandler() throws Exception { public void toHttpHandler() throws Exception {
StrategiesSupplier strategies = mock(StrategiesSupplier.class); HandlerStrategies strategies = mock(HandlerStrategies.class);
when(strategies.messageReaders()).thenReturn( when(strategies.messageReaders()).thenReturn(
() -> Collections.<HttpMessageReader<?>>emptyList().stream()); () -> Collections.<HttpMessageReader<?>>emptyList().stream());
when(strategies.messageWriters()).thenReturn( when(strategies.messageWriters()).thenReturn(
...@@ -120,15 +120,15 @@ public class RouterFunctionsTests { ...@@ -120,15 +120,15 @@ public class RouterFunctionsTests {
when(strategies.viewResolvers()).thenReturn( when(strategies.viewResolvers()).thenReturn(
() -> Collections.<ViewResolver>emptyList().stream()); () -> Collections.<ViewResolver>emptyList().stream());
Request request = mock(Request.class); ServerRequest request = mock(ServerRequest.class);
Response response = mock(Response.class); ServerResponse response = mock(ServerResponse.class);
when(response.writeTo(any(ServerWebExchange.class), eq(strategies))).thenReturn(Mono.empty()); when(response.writeTo(any(ServerWebExchange.class), eq(strategies))).thenReturn(Mono.empty());
HandlerFunction handlerFunction = mock(HandlerFunction.class); HandlerFunction handlerFunction = mock(HandlerFunction.class);
when(handlerFunction.handle(any(Request.class))).thenReturn(response); when(handlerFunction.handle(any(ServerRequest.class))).thenReturn(response);
RouterFunction routerFunction = mock(RouterFunction.class); RouterFunction routerFunction = mock(RouterFunction.class);
when(routerFunction.route(any(Request.class))).thenReturn(Optional.of(handlerFunction)); when(routerFunction.route(any(ServerRequest.class))).thenReturn(Optional.of(handlerFunction));
RequestPredicate requestPredicate = mock(RequestPredicate.class); RequestPredicate requestPredicate = mock(RequestPredicate.class);
when(requestPredicate.test(request)).thenReturn(false); when(requestPredicate.test(request)).thenReturn(false);
......
...@@ -129,25 +129,25 @@ public class SseHandlerFunctionIntegrationTests ...@@ -129,25 +129,25 @@ public class SseHandlerFunctionIntegrationTests
private static class SseHandler { private static class SseHandler {
public Response<Publisher<String>> string(Request request) { public ServerResponse<Publisher<String>> string(ServerRequest request) {
Flux<String> flux = Flux.interval(Duration.ofMillis(100)).map(l -> "foo " + l).take(2); Flux<String> flux = Flux.interval(Duration.ofMillis(100)).map(l -> "foo " + l).take(2);
return Response.ok().body(fromServerSentEvents(flux, String.class)); return ServerResponse.ok().body(fromServerSentEvents(flux, String.class));
} }
public Response<Publisher<Person>> person(Request request) { public ServerResponse<Publisher<Person>> person(ServerRequest request) {
Flux<Person> flux = Flux.interval(Duration.ofMillis(100)) Flux<Person> flux = Flux.interval(Duration.ofMillis(100))
.map(l -> new Person("foo " + l)).take(2); .map(l -> new Person("foo " + l)).take(2);
return Response.ok().body(fromServerSentEvents(flux, Person.class)); return ServerResponse.ok().body(fromServerSentEvents(flux, Person.class));
} }
public Response<Publisher<ServerSentEvent<String>>> sse(Request request) { public ServerResponse<Publisher<ServerSentEvent<String>>> sse(ServerRequest request) {
Flux<ServerSentEvent<String>> flux = Flux.interval(Duration.ofMillis(100)) Flux<ServerSentEvent<String>> flux = Flux.interval(Duration.ofMillis(100))
.map(l -> ServerSentEvent.<String>builder().data("foo") .map(l -> ServerSentEvent.<String>builder().data("foo")
.id(Long.toString(l)) .id(Long.toString(l))
.comment("bar") .comment("bar")
.build()).take(2); .build()).take(2);
return Response.ok().body(fromServerSentEvents(flux)); return ServerResponse.ok().body(fromServerSentEvents(flux));
} }
} }
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.web.reactive.function; package org.springframework.web.reactive.function.support;
import java.net.URI; import java.net.URI;
import java.util.Collections; import java.util.Collections;
...@@ -26,7 +26,7 @@ import org.junit.Before; ...@@ -26,7 +26,7 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.web.reactive.function.support.RequestWrapper; import org.springframework.web.reactive.function.ServerRequest;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame; import static org.junit.Assert.assertSame;
...@@ -36,16 +36,16 @@ import static org.mockito.Mockito.when; ...@@ -36,16 +36,16 @@ import static org.mockito.Mockito.when;
/** /**
* @author Arjen Poutsma * @author Arjen Poutsma
*/ */
public class RequestWrapperTests { public class ServerRequestWrapperTests {
private Request mockRequest; private ServerRequest mockRequest;
private RequestWrapper wrapper; private ServerRequestWrapper wrapper;
@Before @Before
public void createWrapper() { public void createWrapper() {
mockRequest = mock(Request.class); mockRequest = mock(ServerRequest.class);
wrapper = new RequestWrapper(mockRequest); wrapper = new ServerRequestWrapper(mockRequest);
} }
@Test @Test
...@@ -79,7 +79,7 @@ public class RequestWrapperTests { ...@@ -79,7 +79,7 @@ public class RequestWrapperTests {
@Test @Test
public void headers() throws Exception { public void headers() throws Exception {
Request.Headers headers = mock(Request.Headers.class); ServerRequest.Headers headers = mock(ServerRequest.Headers.class);
when(mockRequest.headers()).thenReturn(headers); when(mockRequest.headers()).thenReturn(headers);
assertSame(headers, wrapper.headers()); assertSame(headers, wrapper.headers());
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册