Make ServerHttpMessageReader/Writer more powerful and flexible

This commit makes it possible, in addition to provide hints, to
perform additional operations with the request and the response
at ServerHttpMessageReader/Writer level.

AbstractServerHttpMessageReader/Writer now provide
convenient beforeRead/beforeWrite abstract methods for such need.

Issue: SPR-14557
This commit is contained in:
Sebastien Deleuze
2016-09-20 11:55:19 +02:00
parent 38f3d12e45
commit 857e77eec2
8 changed files with 143 additions and 85 deletions

View File

@@ -28,6 +28,7 @@ import org.springframework.core.ResolvableType;
import org.springframework.http.MediaType;
import org.springframework.http.ReactiveHttpInputMessage;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
/**
* {@link HttpMessageReader} wrapper that implements {@link ServerHttpMessageReader} in order
@@ -46,8 +47,8 @@ public abstract class AbstractServerHttpMessageReader<T> implements ServerHttpMe
}
@Override
public boolean canRead(ResolvableType elementType, MediaType mediaType, Map<String, Object> hints) {
return this.reader.canRead(elementType, mediaType, hints);
public boolean canRead(ResolvableType elementType, MediaType mediaType) {
return this.reader.canRead(elementType, mediaType);
}
@Override
@@ -66,29 +67,42 @@ public abstract class AbstractServerHttpMessageReader<T> implements ServerHttpMe
}
@Override
public final Map<String, Object> resolveReadHints(ResolvableType streamType,
ResolvableType elementType, MediaType mediaType, ServerHttpRequest request) {
public Flux<T> read(ResolvableType streamType, ResolvableType elementType,
ServerHttpRequest request, ServerHttpResponse response, Map<String, Object> hints) {
Map<String, Object> hints = new HashMap<>();
if (this.reader instanceof ServerHttpMessageReader) {
hints.putAll(((ServerHttpMessageReader<T>)this.reader).resolveReadHints(streamType, elementType, mediaType, request));
}
hints.putAll(resolveReadHintsInternal(streamType, elementType, mediaType, request));
return hints;
Map<String, Object> mergedHints = new HashMap<>(hints);
mergedHints.putAll(beforeRead(streamType, elementType, request, response));
return (this.reader instanceof ServerHttpMessageReader ?
((ServerHttpMessageReader<T>)this.reader).read(streamType, elementType, request, response, mergedHints) :
this.read(elementType, request, mergedHints));
}
@Override
public Mono<T> readMono(ResolvableType streamType, ResolvableType elementType,
ServerHttpRequest request, ServerHttpResponse response, Map<String, Object> hints) {
Map<String, Object> mergedHints = new HashMap<>(hints);
mergedHints.putAll(beforeRead(streamType, elementType, request, response));
return (this.reader instanceof ServerHttpMessageReader ?
((ServerHttpMessageReader<T>)this.reader).readMono(streamType, elementType, request, response, mergedHints) :
this.readMono(elementType, request, mergedHints));
}
/**
* Abstract method that returns hints which can be used to customize how the body should be read.
* Invoked from {@link #resolveReadHints}.
* @param streamType the original type used in the method parameter. For annotation
* Invoked before reading the request by
* {@link #read(ResolvableType, ResolvableType, ServerHttpRequest, ServerHttpResponse, Map)}
*
* @param streamType the original type used for the method return value. For annotation
* based controllers, the {@link MethodParameter} is available via {@link ResolvableType#getSource()}.
* @param elementType the stream element type to return
* @param mediaType the media type to read, can be {@code null} if not specified.
* Typically the value of a {@code Content-Type} header.
* @param request the current HTTP request
* @return Additional information about how to read the body
* Can be {@code null}.
* @param elementType the stream element type to process
* @param request the current HTTP request, can be {@code null}
* @param response the current HTTP response, can be {@code null}
* @return Additional information about how to write the body
*/
protected abstract Map<String, Object> resolveReadHintsInternal(ResolvableType streamType,
ResolvableType elementType, MediaType mediaType, ServerHttpRequest request);
protected abstract Map<String, Object> beforeRead(ResolvableType streamType,
ResolvableType elementType, ServerHttpRequest request, ServerHttpResponse response);
}

View File

@@ -28,10 +28,13 @@ import org.springframework.core.ResolvableType;
import org.springframework.http.MediaType;
import org.springframework.http.ReactiveHttpOutputMessage;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
/**
* {@link HttpMessageWriter} wrapper that implements {@link ServerHttpMessageWriter} in order
* to allow providing hints.
* to allow providing hints to the nested {@code writer} or setting the response status, for
* example, by implementing {@link #beforeWrite(ResolvableType, ResolvableType, MediaType, ServerHttpRequest, ServerHttpResponse)}
*
*
* @author Sebastien Deleuze
* @since 5.0
@@ -46,15 +49,8 @@ public abstract class AbstractServerHttpMessageWriter<T> implements ServerHttpMe
}
@Override
public boolean canWrite(ResolvableType elementType, MediaType mediaType, Map<String, Object> hints) {
return this.writer.canWrite(elementType, mediaType, hints);
}
@Override
public Mono<Void> write(Publisher<? extends T> inputStream, ResolvableType elementType,
MediaType mediaType, ReactiveHttpOutputMessage outputMessage, Map<String, Object> hints) {
return this.writer.write(inputStream, elementType, mediaType, outputMessage, hints);
public boolean canWrite(ResolvableType elementType, MediaType mediaType) {
return this.writer.canWrite(elementType, mediaType);
}
@Override
@@ -63,29 +59,37 @@ public abstract class AbstractServerHttpMessageWriter<T> implements ServerHttpMe
}
@Override
public final Map<String, Object> resolveWriteHints(ResolvableType streamType,
ResolvableType elementType, MediaType mediaType, ServerHttpRequest request) {
public Mono<Void> write(Publisher<? extends T> inputStream, ResolvableType elementType,
MediaType mediaType, ReactiveHttpOutputMessage outputMessage, Map<String, Object> hints) {
return this.writer.write(inputStream, elementType, mediaType, outputMessage, hints);
}
Map<String, Object> hints = new HashMap<>();
if (this.writer instanceof ServerHttpMessageWriter) {
hints.putAll(((ServerHttpMessageWriter<T>)this.writer).resolveWriteHints(streamType, elementType, mediaType, request));
}
hints.putAll(resolveWriteHintsInternal(streamType, elementType, mediaType, request));
return hints;
@Override
public Mono<Void> write(Publisher<? extends T> inputStream, ResolvableType streamType, ResolvableType elementType,
MediaType mediaType, ServerHttpRequest request, ServerHttpResponse response, Map<String, Object> hints) {
Map<String, Object> mergedHints = new HashMap<>(hints);
mergedHints.putAll(beforeWrite(streamType, elementType, mediaType, request, response));
return (this.writer instanceof ServerHttpMessageWriter ?
((ServerHttpMessageWriter<T>)this.writer).write(inputStream, streamType, elementType, mediaType, request, response, mergedHints) :
this.writer.write(inputStream, elementType, mediaType, response, mergedHints));
}
/**
* Abstract method that returns hints which can be used to customize how the body should be written.
* Invoked from {@link #resolveWriteHints}.
* Invoked before writing the response by
* {@link #write(Publisher, ResolvableType, ResolvableType, MediaType, ServerHttpRequest, ServerHttpResponse, Map)}.
*
* @param streamType the original type used for the method return value. For annotation
* based controllers, the {@link MethodParameter} is available via {@link ResolvableType#getSource()}.
* Can be {@code null}.
* @param elementType the stream element type to process
* @param mediaType the content type to use when writing. May be {@code null} to
* indicate that the default content type of the converter must be used.
* @param request the current HTTP request
* @param request the current HTTP request, can be {@code null}
* @param response the current HTTP response, can be {@code null}
* @return Additional information about how to write the body
*/
protected abstract Map<String, Object> resolveWriteHintsInternal(ResolvableType streamType,
ResolvableType elementType, MediaType mediaType, ServerHttpRequest request);
protected abstract Map<String, Object> beforeWrite(ResolvableType streamType, ResolvableType elementType,
MediaType mediaType, ServerHttpRequest request, ServerHttpResponse response);
}

View File

@@ -23,9 +23,9 @@ import com.fasterxml.jackson.annotation.JsonView;
import org.springframework.core.MethodParameter;
import org.springframework.core.ResolvableType;
import org.springframework.http.MediaType;
import org.springframework.http.codec.json.AbstractJackson2Codec;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
/**
* {@link ServerHttpMessageReader} that resolves those annotation or request based Jackson 2 hints:
@@ -44,8 +44,8 @@ public class Jackson2ServerHttpMessageReader extends AbstractServerHttpMessageRe
}
@Override
protected Map<String, Object> resolveReadHintsInternal(ResolvableType streamType,
ResolvableType elementType, MediaType mediaType, ServerHttpRequest request) {
protected Map<String, Object> beforeRead(ResolvableType streamType,
ResolvableType elementType, ServerHttpRequest request, ServerHttpResponse response) {
Object source = streamType.getSource();
MethodParameter parameter = (source instanceof MethodParameter ? (MethodParameter)source : null);

View File

@@ -16,16 +16,20 @@
package org.springframework.http.codec;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonView;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Mono;
import org.springframework.core.MethodParameter;
import org.springframework.core.ResolvableType;
import org.springframework.http.MediaType;
import org.springframework.http.codec.json.AbstractJackson2Codec;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
/**
* {@link ServerHttpMessageWriter} that resolves those annotation or request based Jackson 2 hints:
@@ -44,9 +48,10 @@ public class Jackson2ServerHttpMessageWriter extends AbstractServerHttpMessageWr
}
@Override
protected Map<String, Object> resolveWriteHintsInternal(ResolvableType streamType,
ResolvableType elementType, MediaType mediaType, ServerHttpRequest request) {
protected Map<String, Object> beforeWrite(ResolvableType streamType, ResolvableType elementType,
MediaType mediaType, ServerHttpRequest request, ServerHttpResponse response) {
Map<String, Object> hints = new HashMap<>();
Object source = streamType.getSource();
MethodParameter returnValue = (source instanceof MethodParameter ? (MethodParameter)source : null);
if (returnValue != null) {
@@ -57,10 +62,10 @@ public class Jackson2ServerHttpMessageWriter extends AbstractServerHttpMessageWr
throw new IllegalArgumentException(
"@JsonView only supported for write hints with exactly 1 class argument: " + returnValue);
}
return Collections.singletonMap(AbstractJackson2Codec.JSON_VIEW_HINT, classes[0]);
hints.put(AbstractJackson2Codec.JSON_VIEW_HINT, classes[0]);
}
}
return Collections.emptyMap();
return hints;
}
}

View File

@@ -18,14 +18,17 @@ package org.springframework.http.codec;
import java.util.Map;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.core.MethodParameter;
import org.springframework.core.ResolvableType;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
/**
* Server and annotation based controller specific {@link HttpMessageReader} that allows to
* resolve hints using annotations or request based information.
* Server oriented {@link HttpMessageReader} that allows to resolve hints using annotations or
* perform additional operation using {@link ServerHttpRequest} or {@link ServerHttpResponse}.
*
* @author Sebastien Deleuze
* @since 5.0
@@ -33,16 +36,37 @@ import org.springframework.http.server.reactive.ServerHttpRequest;
public interface ServerHttpMessageReader<T> extends HttpMessageReader<T> {
/**
* Read a {@link Flux} of the given type form the given input message with additional server related
* parameters which could be used to create some hints or set the response status for example.
*
* Return hints that can be used to customize how the body should be read
* @param streamType the original type used in the method parameter. For annotation
* based controllers, the {@link MethodParameter} is available via {@link ResolvableType#getSource()}.
* @param elementType the stream element type to return
* @param mediaType the media type to read, can be {@code null} if not specified.
* Typically the value of a {@code Content-Type} header.
* @param request the current HTTP request
* @return Additional information about how to read the body
* @param response the current HTTP response
* @param hints additional information about how to read the body
* @return the converted {@link Flux} of elements
*/
Map<String, Object> resolveReadHints(ResolvableType streamType, ResolvableType elementType,
MediaType mediaType, ServerHttpRequest request);
Flux<T> read(ResolvableType streamType, ResolvableType elementType, ServerHttpRequest request,
ServerHttpResponse response, Map<String, Object> hints);
/**
* Read a {@link Mono} of the given type form the given input message with additional server related
* parameters which could be used to create some hints or set the response status for example.
*
* Return hints that can be used to customize how the body should be read
* @param streamType the original type used in the method parameter. For annotation
* based controllers, the {@link MethodParameter} is available via {@link ResolvableType#getSource()}.
* @param elementType the stream element type to return
* Typically the value of a {@code Content-Type} header.
* @param request the current HTTP request
* @param response the current HTTP response
* @param hints additional information about how to read the body
* @return the converted {@link Mono} of object
*/
Mono<T> readMono(ResolvableType streamType, ResolvableType elementType, ServerHttpRequest request,
ServerHttpResponse response, Map<String, Object> hints);
}

View File

@@ -18,14 +18,18 @@ package org.springframework.http.codec;
import java.util.Map;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Mono;
import org.springframework.core.MethodParameter;
import org.springframework.core.ResolvableType;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
/**
* Server oriented {@link HttpMessageWriter} that allows to resolve hints using annotations or
* request based information.
* perform additional operation using {@link ServerHttpRequest} or {@link ServerHttpResponse}.
*
* @author Sebastien Deleuze
* @since 5.0
@@ -33,16 +37,21 @@ import org.springframework.http.server.reactive.ServerHttpRequest;
public interface ServerHttpMessageWriter<T> extends HttpMessageWriter<T> {
/**
* Return hints that can be used to customize how the body should be written
* Write a given object to the given output message with additional server related
* parameters which could be used to create some hints or set the response status for example.
*
* @param streamType the original type used for the method return value. For annotation
* based controllers, the {@link MethodParameter} is available via {@link ResolvableType#getSource()}.
* Can be {@code null}.
* @param elementType the stream element type to process
* @param mediaType the content type to use when writing. May be {@code null} to
* indicate that the default content type of the converter must be used.
* @param request the current HTTP request
* @return Additional information about how to write the body
* @param request the current HTTP request, can be {@code null}
* @param response the current HTTP response, can be {@code null}
* @return a {@link Mono} that indicates completion or error
*/
Map<String, Object> resolveWriteHints(ResolvableType streamType, ResolvableType elementType,
MediaType mediaType, ServerHttpRequest request);
Mono<Void> write(Publisher<? extends T> inputStream, ResolvableType streamType,
ResolvableType elementType, MediaType mediaType, ServerHttpRequest request,
ServerHttpResponse response, Map<String, Object> hints);
}