Polish Javadoc of [Server]HttpMessage[Reader|Writer]

This commit is contained in:
Rossen Stoyanchev 2017-03-21 10:57:21 -04:00
parent 54013a0920
commit 124cdb5c58
15 changed files with 136 additions and 141 deletions

View File

@ -81,19 +81,19 @@ public class DecoderHttpMessageReader<T> implements ServerHttpMessageReader<T> {
}
@Override
public Flux<T> read(ResolvableType elementType, ReactiveHttpInputMessage inputMessage,
public Flux<T> read(ResolvableType elementType, ReactiveHttpInputMessage message,
Map<String, Object> hints) {
MediaType contentType = getContentType(inputMessage);
return this.decoder.decode(inputMessage.getBody(), elementType, contentType, hints);
MediaType contentType = getContentType(message);
return this.decoder.decode(message.getBody(), elementType, contentType, hints);
}
@Override
public Mono<T> readMono(ResolvableType elementType, ReactiveHttpInputMessage inputMessage,
public Mono<T> readMono(ResolvableType elementType, ReactiveHttpInputMessage message,
Map<String, Object> hints) {
MediaType contentType = getContentType(inputMessage);
return this.decoder.decodeToMono(inputMessage.getBody(), elementType, contentType, hints);
MediaType contentType = getContentType(message);
return this.decoder.decodeToMono(message.getBody(), elementType, contentType, hints);
}
private MediaType getContentType(HttpMessage inputMessage) {
@ -105,22 +105,22 @@ public class DecoderHttpMessageReader<T> implements ServerHttpMessageReader<T> {
// ServerHttpMessageReader...
@Override
public Flux<T> read(ResolvableType streamType, ResolvableType elementType,
public Flux<T> read(ResolvableType actualType, ResolvableType elementType,
ServerHttpRequest request, ServerHttpResponse response, Map<String, Object> hints) {
Map<String, Object> allHints = new HashMap<>(4);
allHints.putAll(getReadHints(streamType, elementType, request, response));
allHints.putAll(getReadHints(actualType, elementType, request, response));
allHints.putAll(hints);
return read(elementType, request, allHints);
}
@Override
public Mono<T> readMono(ResolvableType streamType, ResolvableType elementType,
public Mono<T> readMono(ResolvableType actualType, ResolvableType elementType,
ServerHttpRequest request, ServerHttpResponse response, Map<String, Object> hints) {
Map<String, Object> allHints = new HashMap<>(4);
allHints.putAll(getReadHints(streamType, elementType, request, response));
allHints.putAll(getReadHints(actualType, elementType, request, response));
allHints.putAll(hints);
return readMono(elementType, request, allHints);

View File

@ -120,10 +120,10 @@ public class EncoderHttpMessageWriter<T> implements ServerHttpMessageWriter<T> {
@Override
public Mono<Void> write(Publisher<? extends T> inputStream, ResolvableType elementType,
MediaType mediaType, ReactiveHttpOutputMessage outputMessage,
MediaType mediaType, ReactiveHttpOutputMessage message,
Map<String, Object> hints) {
HttpHeaders headers = outputMessage.getHeaders();
HttpHeaders headers = message.getHeaders();
if (headers.getContentType() == null) {
MediaType fallback = this.defaultMediaType;
@ -135,11 +135,11 @@ public class EncoderHttpMessageWriter<T> implements ServerHttpMessageWriter<T> {
}
Flux<DataBuffer> body = this.encoder.encode(inputStream,
outputMessage.bufferFactory(), elementType, headers.getContentType(), hints);
message.bufferFactory(), elementType, headers.getContentType(), hints);
return isStreamingMediaType(headers.getContentType()) ?
outputMessage.writeAndFlushWith(body.map(Flux::just)) :
outputMessage.writeWith(body);
message.writeAndFlushWith(body.map(Flux::just)) :
message.writeWith(body);
}
private static boolean useFallback(MediaType main, MediaType fallback) {
@ -162,12 +162,12 @@ public class EncoderHttpMessageWriter<T> implements ServerHttpMessageWriter<T> {
// ServerHttpMessageWriter...
@Override
public Mono<Void> write(Publisher<? extends T> inputStream, ResolvableType streamType,
public Mono<Void> write(Publisher<? extends T> inputStream, ResolvableType actualType,
ResolvableType elementType, MediaType mediaType, ServerHttpRequest request,
ServerHttpResponse response, Map<String, Object> hints) {
Map<String, Object> allHints = new HashMap<>();
allHints.putAll(getWriteHints(streamType, elementType, mediaType, request, response));
allHints.putAll(getWriteHints(actualType, elementType, mediaType, request, response));
allHints.putAll(hints);
return write(inputStream, elementType, mediaType, response, allHints);

View File

@ -83,19 +83,19 @@ public class FormHttpMessageReader implements HttpMessageReader<MultiValueMap<St
@Override
public Flux<MultiValueMap<String, String>> read(ResolvableType elementType,
ReactiveHttpInputMessage inputMessage, Map<String, Object> hints) {
ReactiveHttpInputMessage message, Map<String, Object> hints) {
return Flux.from(readMono(elementType, inputMessage, hints));
return Flux.from(readMono(elementType, message, hints));
}
@Override
public Mono<MultiValueMap<String, String>> readMono(ResolvableType elementType,
ReactiveHttpInputMessage inputMessage, Map<String, Object> hints) {
ReactiveHttpInputMessage message, Map<String, Object> hints) {
MediaType contentType = inputMessage.getHeaders().getContentType();
MediaType contentType = message.getHeaders().getContentType();
Charset charset = getMediaTypeCharset(contentType);
return inputMessage.getBody()
return message.getBody()
.reduce(DataBuffer::write)
.map(buffer -> {
CharBuffer charBuffer = charset.decode(buffer.asByteBuffer());

View File

@ -82,13 +82,13 @@ public class FormHttpMessageWriter implements HttpMessageWriter<MultiValueMap<St
@Override
public Mono<Void> write(Publisher<? extends MultiValueMap<String, String>> inputStream,
ResolvableType elementType, MediaType mediaType, ReactiveHttpOutputMessage outputMessage,
ResolvableType elementType, MediaType mediaType, ReactiveHttpOutputMessage message,
Map<String, Object> hints) {
MediaType contentType = outputMessage.getHeaders().getContentType();
MediaType contentType = message.getHeaders().getContentType();
if (contentType == null) {
contentType = MediaType.APPLICATION_FORM_URLENCODED;
outputMessage.getHeaders().setContentType(contentType);
message.getHeaders().setContentType(contentType);
}
Charset charset = getMediaTypeCharset(contentType);
@ -99,9 +99,9 @@ public class FormHttpMessageWriter implements HttpMessageWriter<MultiValueMap<St
.map(form -> generateForm(form, charset))
.then(value -> {
ByteBuffer byteBuffer = charset.encode(value);
DataBuffer buffer = outputMessage.bufferFactory().wrap(byteBuffer);
outputMessage.getHeaders().setContentLength(byteBuffer.remaining());
return outputMessage.writeWith(Mono.just(buffer));
DataBuffer buffer = message.bufferFactory().wrap(byteBuffer);
message.getHeaders().setContentLength(byteBuffer.remaining());
return message.writeWith(Mono.just(buffer));
});
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -27,8 +27,10 @@ import org.springframework.http.MediaType;
import org.springframework.http.ReactiveHttpInputMessage;
/**
* Strategy interface that specifies a reader that can convert from the HTTP
* request body from a stream of bytes to Objects.
* Strategy for reading from a {@link ReactiveHttpInputMessage} and decoding
* the stream of bytes to Objects of type {@code <T>}.
*
* @param <T> the type of objects in the decoded output stream
*
* @author Rossen Stoyanchev
* @author Arjen Poutsma
@ -38,40 +40,39 @@ import org.springframework.http.ReactiveHttpInputMessage;
public interface HttpMessageReader<T> {
/**
* Indicates whether the given class can be read by this converter.
* @param elementType the stream element type to test for readability
* @param mediaType the media type to read, can be {@code null} if not specified.
* Typically the value of a {@code Content-Type} header.
* @return {@code true} if readable; {@code false} otherwise
* Return the {@link MediaType}'s that this reader supports.
*/
List<MediaType> getReadableMediaTypes();
/**
* Whether the given object type is supported by this reader.
* @param elementType the type of object to check
* @param mediaType the media type for the read, possibly {@code null}
* @return {@code true} if readable, {@code false} otherwise
*/
boolean canRead(ResolvableType elementType, MediaType mediaType);
/**
* Read a {@link Flux} of the given type form the given input message, and returns it.
* @param elementType the stream element type to return. This type must have previously been
* passed to the {@link #canRead canRead} method of this interface, which must have
* returned {@code true}.
* @param inputMessage the HTTP input message to read from
* @param hints additional information about how to read the body
* @return the converted {@link Flux} of elements
* Read from the input message and encode to a stream of objects.
*
* @param elementType the type of objects in the stream which must have been
* previously checked via {@link #canRead(ResolvableType, MediaType)}
* @param message the message to read from
* @param hints additional information about how to read and decode the input
* @return the decoded stream of elements
*/
Flux<T> read(ResolvableType elementType, ReactiveHttpInputMessage inputMessage, Map<String, Object> hints);
Flux<T> read(ResolvableType elementType, ReactiveHttpInputMessage message, Map<String, Object> hints);
/**
* Read a {@link Mono} of the given type form the given input message, and returns it.
* @param elementType the stream element type to return. This type must have previously been
* passed to the {@link #canRead canRead} method of this interface, which must have
* returned {@code true}.
* @param inputMessage the HTTP input message to read from
* @param hints additional information about how to read the body
* @return the converted {@link Mono} of object
* Read from the input message and encode to a single object.
*
* @param elementType the type of objects in the stream which must have been
* previously checked via {@link #canRead(ResolvableType, MediaType)}
* @param message the message to read from
* @param hints additional information about how to read and decode the input
* @return the decoded object
*/
Mono<T> readMono(ResolvableType elementType, ReactiveHttpInputMessage inputMessage, Map<String, Object> hints);
Mono<T> readMono(ResolvableType elementType, ReactiveHttpInputMessage message, Map<String, Object> hints);
/**
* Return the list of {@link MediaType} objects that can be read by this converter.
* @return the list of supported readable media types
*/
List<MediaType> getReadableMediaTypes();
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -27,8 +27,10 @@ import org.springframework.http.MediaType;
import org.springframework.http.ReactiveHttpOutputMessage;
/**
* Strategy interface that specifies a converter that can convert a stream of
* Objects to a stream of bytes to be written to the HTTP response body.
* Strategy for encoding a stream of objects of type {@code <T>} and writing
* the encoded stream of bytes to an {@link ReactiveHttpOutputMessage}.
*
* @param <T> the type of objects in the input stream
*
* @author Rossen Stoyanchev
* @author Arjen Poutsma
@ -38,34 +40,31 @@ import org.springframework.http.ReactiveHttpOutputMessage;
public interface HttpMessageWriter<T> {
/**
* Indicates whether the given class can be written by this converter.
* @param elementType the stream element type to test for writability
* @param mediaType the media type to write, can be {@code null} if not specified.
* Typically the value of an {@code Accept} header.
* @return {@code true} if writable; {@code false} otherwise
* Return the {@link MediaType}'s that this writer supports.
*/
List<MediaType> getWritableMediaTypes();
/**
* Whether the given object type is supported by this writer.
* @param elementType the type of object to check
* @param mediaType the media type for the write, possibly {@code null}
* @return {@code true} if writable, {@code false} otherwise
*/
boolean canWrite(ResolvableType elementType, MediaType mediaType);
/**
* Write an given object to the given output message.
* @param inputStream the input stream to write
* @param elementType the stream element type to process. This type must have previously
* been passed to the {@link #canWrite} canWrite} method of this interface, which must
* have returned {@code true}.
* @param mediaType the content type to use when writing, typically the value of an
* {@code Accept} header. May be {@code null} to indicate that the default content
* type of the converter must be used.
* @param outputMessage the message to write to
* @param hints additional information about how to write
* @return a {@link Mono} that indicates completion or error
* Write an given stream of object to the output message.
* @param inputStream the objects to write
* @param elementType the type of objects in the stream which must have been
* previously checked via {@link #canWrite(ResolvableType, MediaType)}
* @param mediaType the content type for the write, possibly {@code null} to
* indicate that the default content type of the writer must be used.
* @param message the message to write to
* @param hints additional information about how to encode and write
* @return indicates completion or error
*/
Mono<Void> write(Publisher<? extends T> inputStream, ResolvableType elementType,
MediaType mediaType, ReactiveHttpOutputMessage outputMessage, Map<String, Object> hints);
MediaType mediaType, ReactiveHttpOutputMessage message, Map<String, Object> hints);
/**
* Return the list of {@link MediaType} objects that can be written by this converter.
* @return the list of supported readable media types
*/
List<MediaType> getWritableMediaTypes();
}

View File

@ -176,7 +176,7 @@ public class ResourceHttpMessageWriter implements ServerHttpMessageWriter<Resour
@Override
@SuppressWarnings("unchecked")
public Mono<Void> write(Publisher<? extends Resource> inputStream, ResolvableType streamType,
public Mono<Void> write(Publisher<? extends Resource> inputStream, ResolvableType actualType,
ResolvableType elementType, MediaType mediaType, ServerHttpRequest request,
ServerHttpResponse response, Map<String, Object> hints) {

View File

@ -31,7 +31,6 @@ import org.springframework.http.server.reactive.ServerHttpResponse;
*/
public interface ServerHttpDecoder<T> extends Decoder<T> {
/**
* Get decoding hints based on the server request or annotations on the
* target controller method parameter.

View File

@ -33,7 +33,6 @@ import org.springframework.http.server.reactive.ServerHttpResponse;
*/
public interface ServerHttpEncoder<T> extends Encoder<T> {
/**
* Get decoding hints based on the server request or annotations on the
* target controller method parameter.

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -27,46 +27,43 @@ import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
/**
* Server oriented {@link HttpMessageReader} that allows to resolve hints using annotations or
* perform additional operation using {@link ServerHttpRequest} or {@link ServerHttpResponse}.
* An extension of {@code HttpMessageReader} for decoding and reading the
* request body with extra information available on the server side.
*
* @author Sebastien Deleuze
* @author Rossen Stoyanchev
* @since 5.0
*/
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.
* Decode and read the request body to an object stream.
*
* 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 actualType the actual type of the target method parameter; for
* annotated controllers, the {@link MethodParameter} can be accessed via
* {@link ResolvableType#getSource()}.
* @param elementType the type of Objects in the output stream
* @param request the current request
* @param response the current response
* @param hints additional information about how to read the body
* @return the converted {@link Flux} of elements
* @return the decoded stream of elements
*/
Flux<T> read(ResolvableType streamType, ResolvableType elementType, ServerHttpRequest request,
Flux<T> read(ResolvableType actualType, 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.
* Decode and read the request body to a single object.
*
* 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 actualType the actual type of the target method parameter; for
* annotated controllers, the {@link MethodParameter} can be accessed via
* {@link ResolvableType#getSource()}.
* @param elementType the type of Objects in the output stream
* @param request the current request
* @param response the current response
* @param hints additional information about how to read the body
* @return the converted {@link Mono} of object
* @return the decoded stream of elements
*/
Mono<T> readMono(ResolvableType streamType, ResolvableType elementType, ServerHttpRequest request,
Mono<T> readMono(ResolvableType actualType, ResolvableType elementType, ServerHttpRequest request,
ServerHttpResponse response, Map<String, Object> hints);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -28,29 +28,29 @@ 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
* perform additional operation using {@link ServerHttpRequest} or {@link ServerHttpResponse}.
* An extension of {@code HttpMessageWriter} for encoding and writing the
* response body with extra information available on the server side.
*
* @author Sebastien Deleuze
* @author Rossen Stoyanchev
* @since 5.0
*/
public interface ServerHttpMessageWriter<T> extends HttpMessageWriter<T> {
/**
* 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.
* Encode and write the given object stream to the response.
*
* @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 response the current HTTP response
* @return a {@link Mono} that indicates completion or error
* @param actualType the actual return type of the method that returned the
* value; for annotated controllers, the {@link MethodParameter} can be
* accessed via {@link ResolvableType#getSource()}.
* @param elementType the type of Objects in the input stream
* @param mediaType the content type to use, possibly {@code null} indicating
* the default content type of the writer should be used.
* @param request the current request
* @param response the current response
* @return a {@link Mono} that indicates completion of writing or error
*/
Mono<Void> write(Publisher<? extends T> inputStream, ResolvableType streamType,
Mono<Void> write(Publisher<? extends T> inputStream, ResolvableType actualType,
ResolvableType elementType, MediaType mediaType, ServerHttpRequest request,
ServerHttpResponse response, Map<String, Object> hints);

View File

@ -87,13 +87,13 @@ public class ServerSentEventHttpMessageReader implements HttpMessageReader<Objec
@Override
public Flux<Object> read(ResolvableType elementType, ReactiveHttpInputMessage inputMessage,
public Flux<Object> read(ResolvableType elementType, ReactiveHttpInputMessage message,
Map<String, Object> hints) {
boolean hasSseWrapper = ServerSentEvent.class.isAssignableFrom(elementType.getRawClass());
ResolvableType dataType = (hasSseWrapper ? elementType.getGeneric(0) : elementType);
return Flux.from(inputMessage.getBody())
return Flux.from(message.getBody())
.concatMap(ServerSentEventHttpMessageReader::splitOnNewline)
.map(buffer -> {
CharBuffer charBuffer = StandardCharsets.UTF_8.decode(buffer.asByteBuffer());
@ -178,13 +178,13 @@ public class ServerSentEventHttpMessageReader implements HttpMessageReader<Objec
}
@Override
public Mono<Object> readMono(ResolvableType elementType, ReactiveHttpInputMessage inputMessage,
public Mono<Object> readMono(ResolvableType elementType, ReactiveHttpInputMessage message,
Map<String, Object> hints) {
// Let's give StringDecoder a chance since SSE is ordered ahead of it
if (String.class.equals(elementType.getRawClass())) {
Flux<DataBuffer> body = inputMessage.getBody();
Flux<DataBuffer> body = message.getBody();
return stringDecoder.decodeToMono(body, elementType, null, null).cast(Object.class);
}

View File

@ -87,14 +87,14 @@ public class ServerSentEventHttpMessageWriter implements ServerHttpMessageWriter
@Override
public Mono<Void> write(Publisher<?> inputStream, ResolvableType elementType, MediaType mediaType,
ReactiveHttpOutputMessage outputMessage, Map<String, Object> hints) {
ReactiveHttpOutputMessage message, Map<String, Object> hints) {
outputMessage.getHeaders().setContentType(MediaType.TEXT_EVENT_STREAM);
message.getHeaders().setContentType(MediaType.TEXT_EVENT_STREAM);
DataBufferFactory bufferFactory = outputMessage.bufferFactory();
DataBufferFactory bufferFactory = message.bufferFactory();
Flux<Publisher<DataBuffer>> body = encode(inputStream, bufferFactory, elementType, hints);
return outputMessage.writeAndFlushWith(body);
return message.writeAndFlushWith(body);
}
private Flux<Publisher<DataBuffer>> encode(Publisher<?> inputStream, DataBufferFactory bufferFactory,
@ -164,14 +164,14 @@ public class ServerSentEventHttpMessageWriter implements ServerHttpMessageWriter
}
@Override
public Mono<Void> write(Publisher<?> inputStream, ResolvableType streamType, ResolvableType elementType,
public Mono<Void> write(Publisher<?> inputStream, ResolvableType actualType, ResolvableType elementType,
MediaType mediaType, ServerHttpRequest request, ServerHttpResponse response,
Map<String, Object> hints) {
Map<String, Object> allHints = this.dataEncoders.stream()
.filter(encoder -> encoder instanceof ServerHttpEncoder)
.map(encoder -> (ServerHttpEncoder<?>) encoder)
.map(encoder -> encoder.getEncodeHints(streamType, elementType, mediaType, request, response))
.map(encoder -> encoder.getEncodeHints(actualType, elementType, mediaType, request, response))
.reduce(new HashMap<>(), (t, u) -> {
t.putAll(u);
return t;

View File

@ -97,7 +97,7 @@ public class ExchangeStrategiesTests {
@Override
public Mono<Void> write(Publisher<?> inputStream, ResolvableType type,
MediaType contentType,
ReactiveHttpOutputMessage outputMessage,
ReactiveHttpOutputMessage message,
Map<String, Object> hints) {
return Mono.empty();
}
@ -117,13 +117,13 @@ public class ExchangeStrategiesTests {
}
@Override
public Flux<Object> read(ResolvableType type, ReactiveHttpInputMessage inputMessage,
public Flux<Object> read(ResolvableType type, ReactiveHttpInputMessage message,
Map<String, Object> hints) {
return Flux.empty();
}
@Override
public Mono<Object> readMono(ResolvableType type, ReactiveHttpInputMessage inputMessage,
public Mono<Object> readMono(ResolvableType type, ReactiveHttpInputMessage message,
Map<String, Object> hints) {
return Mono.empty();
}

View File

@ -99,7 +99,7 @@ public class HandlerStrategiesTests {
@Override
public Mono<Void> write(Publisher<?> inputStream, ResolvableType type,
MediaType contentType,
ReactiveHttpOutputMessage outputMessage,
ReactiveHttpOutputMessage message,
Map<String, Object> hints) {
return Mono.empty();
}
@ -119,13 +119,13 @@ public class HandlerStrategiesTests {
}
@Override
public Flux<Object> read(ResolvableType type, ReactiveHttpInputMessage inputMessage,
public Flux<Object> read(ResolvableType type, ReactiveHttpInputMessage message,
Map<String, Object> hints) {
return Flux.empty();
}
@Override
public Mono<Object> readMono(ResolvableType type, ReactiveHttpInputMessage inputMessage,
public Mono<Object> readMono(ResolvableType type, ReactiveHttpInputMessage message,
Map<String, Object> hints) {
return Mono.empty();
}