Polish
This commit is contained in:
@@ -16,7 +16,6 @@
|
||||
|
||||
package org.springframework.core.codec;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
|
||||
package org.springframework.core.codec;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
@@ -162,7 +162,7 @@ public class RouterTests {
|
||||
private static class DummyMessageWriter implements HttpMessageWriter<Object> {
|
||||
|
||||
@Override
|
||||
public boolean canWrite(ResolvableType type, MediaType mediaType, Map<String, Object> hints) {
|
||||
public boolean canWrite(ResolvableType elementType, MediaType mediaType, Map<String, Object> hints) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -172,8 +172,8 @@ public class RouterTests {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Void> write(Publisher<?> inputStream, ResolvableType type,
|
||||
MediaType contentType,
|
||||
public Mono<Void> write(Publisher<?> inputStream, ResolvableType elementType,
|
||||
MediaType mediaType,
|
||||
ReactiveHttpOutputMessage outputMessage,
|
||||
Map<String, Object> hints) {
|
||||
return Mono.empty();
|
||||
@@ -183,7 +183,7 @@ public class RouterTests {
|
||||
private static class DummyMessageReader implements HttpMessageReader<Object> {
|
||||
|
||||
@Override
|
||||
public boolean canRead(ResolvableType type, MediaType mediaType, Map<String, Object> hints) {
|
||||
public boolean canRead(ResolvableType elementType, MediaType mediaType, Map<String, Object> hints) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -193,13 +193,13 @@ public class RouterTests {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Flux<Object> read(ResolvableType type, ReactiveHttpInputMessage inputMessage,
|
||||
public Flux<Object> read(ResolvableType elementType, ReactiveHttpInputMessage inputMessage,
|
||||
Map<String, Object> hints) {
|
||||
return Flux.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Object> readMono(ResolvableType type, ReactiveHttpInputMessage inputMessage,
|
||||
public Mono<Object> readMono(ResolvableType elementType, ReactiveHttpInputMessage inputMessage,
|
||||
Map<String, Object> hints) {
|
||||
return Mono.empty();
|
||||
}
|
||||
|
||||
@@ -56,8 +56,8 @@ public class DecoderHttpMessageReader<T> implements HttpMessageReader<T> {
|
||||
|
||||
|
||||
@Override
|
||||
public boolean canRead(ResolvableType type, MediaType mediaType, Map<String, Object> hints) {
|
||||
return this.decoder != null && this.decoder.canDecode(type, mediaType, hints);
|
||||
public boolean canRead(ResolvableType elementType, MediaType mediaType, Map<String, Object> hints) {
|
||||
return this.decoder != null && this.decoder.canDecode(elementType, mediaType, hints);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -67,21 +67,21 @@ public class DecoderHttpMessageReader<T> implements HttpMessageReader<T> {
|
||||
|
||||
|
||||
@Override
|
||||
public Flux<T> read(ResolvableType type, ReactiveHttpInputMessage inputMessage, Map<String, Object> hints) {
|
||||
public Flux<T> read(ResolvableType elementType, ReactiveHttpInputMessage inputMessage, Map<String, Object> hints) {
|
||||
if (this.decoder == null) {
|
||||
return Flux.error(new IllegalStateException("No decoder set"));
|
||||
}
|
||||
MediaType contentType = getContentType(inputMessage);
|
||||
return this.decoder.decode(inputMessage.getBody(), type, contentType, hints);
|
||||
return this.decoder.decode(inputMessage.getBody(), elementType, contentType, hints);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<T> readMono(ResolvableType type, ReactiveHttpInputMessage inputMessage, Map<String, Object> hints) {
|
||||
public Mono<T> readMono(ResolvableType elementType, ReactiveHttpInputMessage inputMessage, Map<String, Object> hints) {
|
||||
if (this.decoder == null) {
|
||||
return Mono.error(new IllegalStateException("No decoder set"));
|
||||
}
|
||||
MediaType contentType = getContentType(inputMessage);
|
||||
return this.decoder.decodeToMono(inputMessage.getBody(), type, contentType, hints);
|
||||
return this.decoder.decodeToMono(inputMessage.getBody(), elementType, contentType, hints);
|
||||
}
|
||||
|
||||
private MediaType getContentType(ReactiveHttpInputMessage inputMessage) {
|
||||
|
||||
@@ -60,8 +60,8 @@ public class EncoderHttpMessageWriter<T> implements HttpMessageWriter<T> {
|
||||
|
||||
|
||||
@Override
|
||||
public boolean canWrite(ResolvableType type, MediaType mediaType, Map<String, Object> hints) {
|
||||
return this.encoder != null && this.encoder.canEncode(type, mediaType, hints);
|
||||
public boolean canWrite(ResolvableType elementType, MediaType mediaType, Map<String, Object> hints) {
|
||||
return this.encoder != null && this.encoder.canEncode(elementType, mediaType, hints);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -71,8 +71,8 @@ public class EncoderHttpMessageWriter<T> implements HttpMessageWriter<T> {
|
||||
|
||||
|
||||
@Override
|
||||
public Mono<Void> write(Publisher<? extends T> inputStream, ResolvableType type,
|
||||
MediaType contentType, ReactiveHttpOutputMessage outputMessage,
|
||||
public Mono<Void> write(Publisher<? extends T> inputStream, ResolvableType elementType,
|
||||
MediaType mediaType, ReactiveHttpOutputMessage outputMessage,
|
||||
Map<String, Object> hints) {
|
||||
|
||||
if (this.encoder == null) {
|
||||
@@ -81,19 +81,19 @@ public class EncoderHttpMessageWriter<T> implements HttpMessageWriter<T> {
|
||||
|
||||
HttpHeaders headers = outputMessage.getHeaders();
|
||||
if (headers.getContentType() == null) {
|
||||
MediaType contentTypeToUse = contentType;
|
||||
if (contentType == null || contentType.isWildcardType() || contentType.isWildcardSubtype()) {
|
||||
contentTypeToUse = getDefaultContentType(type);
|
||||
MediaType contentTypeToUse = mediaType;
|
||||
if (mediaType == null || mediaType.isWildcardType() || mediaType.isWildcardSubtype()) {
|
||||
contentTypeToUse = getDefaultContentType(elementType);
|
||||
}
|
||||
else if (MediaType.APPLICATION_OCTET_STREAM.equals(contentType)) {
|
||||
MediaType mediaType = getDefaultContentType(type);
|
||||
contentTypeToUse = (mediaType != null ? mediaType : contentTypeToUse);
|
||||
else if (MediaType.APPLICATION_OCTET_STREAM.equals(mediaType)) {
|
||||
MediaType contentType = getDefaultContentType(elementType);
|
||||
contentTypeToUse = (contentType != null ? contentType : contentTypeToUse);
|
||||
}
|
||||
if (contentTypeToUse != null) {
|
||||
if (contentTypeToUse.getCharset() == null) {
|
||||
MediaType mediaType = getDefaultContentType(type);
|
||||
if (mediaType != null && mediaType.getCharset() != null) {
|
||||
contentTypeToUse = new MediaType(contentTypeToUse, mediaType.getCharset());
|
||||
MediaType contentType = getDefaultContentType(elementType);
|
||||
if (contentType != null && contentType.getCharset() != null) {
|
||||
contentTypeToUse = new MediaType(contentTypeToUse, contentType.getCharset());
|
||||
}
|
||||
}
|
||||
headers.setContentType(contentTypeToUse);
|
||||
@@ -101,7 +101,7 @@ public class EncoderHttpMessageWriter<T> implements HttpMessageWriter<T> {
|
||||
}
|
||||
|
||||
DataBufferFactory bufferFactory = outputMessage.bufferFactory();
|
||||
Flux<DataBuffer> body = this.encoder.encode(inputStream, bufferFactory, type, contentType, hints);
|
||||
Flux<DataBuffer> body = this.encoder.encode(inputStream, bufferFactory, elementType, mediaType, hints);
|
||||
return outputMessage.writeWith(body);
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
|
||||
package org.springframework.http.codec;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -40,35 +39,35 @@ public interface HttpMessageReader<T> {
|
||||
|
||||
/**
|
||||
* Indicates whether the given class can be read by this converter.
|
||||
* @param type the type to test for readability
|
||||
* @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.
|
||||
* @param hints additional information about how to do read
|
||||
* @return {@code true} if readable; {@code false} otherwise
|
||||
*/
|
||||
boolean canRead(ResolvableType type, MediaType mediaType, Map<String, Object> hints);
|
||||
boolean canRead(ResolvableType elementType, MediaType mediaType, Map<String, Object> hints);
|
||||
|
||||
/**
|
||||
* Read a {@link Flux} of the given type form the given input message, and returns it.
|
||||
* @param type the type of object to return. This type must have previously been
|
||||
* @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 do read
|
||||
* @return the converted {@link Flux} of elements
|
||||
*/
|
||||
Flux<T> read(ResolvableType type, ReactiveHttpInputMessage inputMessage, Map<String, Object> hints);
|
||||
Flux<T> read(ResolvableType elementType, ReactiveHttpInputMessage inputMessage, Map<String, Object> hints);
|
||||
|
||||
/**
|
||||
* Read a {@link Mono} of the given type form the given input message, and returns it.
|
||||
* @param type the type of object to return. This type must have previously been
|
||||
* @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 do read
|
||||
* @return the converted {@link Mono} of object
|
||||
*/
|
||||
Mono<T> readMono(ResolvableType type, ReactiveHttpInputMessage inputMessage, Map<String, Object> hints);
|
||||
Mono<T> readMono(ResolvableType elementType, ReactiveHttpInputMessage inputMessage, Map<String, Object> hints);
|
||||
|
||||
/**
|
||||
* Return the list of {@link MediaType} objects that can be read by this converter.
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
|
||||
package org.springframework.http.codec;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -40,26 +39,29 @@ public interface HttpMessageWriter<T> {
|
||||
|
||||
/**
|
||||
* Indicates whether the given class can be written by this converter.
|
||||
* @param type the class to test for writability
|
||||
* @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.
|
||||
* @param hints additional information about how to do write
|
||||
* @param hints additional information about how to write
|
||||
* @return {@code true} if writable; {@code false} otherwise
|
||||
*/
|
||||
boolean canWrite(ResolvableType type, MediaType mediaType, Map<String, Object> hints);
|
||||
boolean canWrite(ResolvableType elementType, MediaType mediaType, Map<String, Object> hints);
|
||||
|
||||
/**
|
||||
* Write an given object to the given output message.
|
||||
* @param inputStream the input stream to write
|
||||
* @param type the stream element type to process.
|
||||
* @param contentType 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 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 do write
|
||||
* @param hints additional information about how to write
|
||||
* @return the converted {@link Mono} of object
|
||||
*/
|
||||
Mono<Void> write(Publisher<? extends T> inputStream, ResolvableType type,
|
||||
MediaType contentType, ReactiveHttpOutputMessage outputMessage, Map<String, Object> hints);
|
||||
Mono<Void> write(Publisher<? extends T> inputStream, ResolvableType elementType,
|
||||
MediaType mediaType, ReactiveHttpOutputMessage outputMessage, Map<String, Object> hints);
|
||||
|
||||
/**
|
||||
* Return the list of {@link MediaType} objects that can be written by this converter.
|
||||
|
||||
@@ -59,26 +59,26 @@ public class ResourceHttpMessageWriter extends EncoderHttpMessageWriter<Resource
|
||||
|
||||
|
||||
@Override
|
||||
public Mono<Void> write(Publisher<? extends Resource> inputStream, ResolvableType type,
|
||||
MediaType contentType, ReactiveHttpOutputMessage outputMessage, Map<String, Object> hints) {
|
||||
public Mono<Void> write(Publisher<? extends Resource> inputStream, ResolvableType elementType,
|
||||
MediaType mediaType, ReactiveHttpOutputMessage outputMessage, Map<String, Object> hints) {
|
||||
|
||||
return Mono.from(Flux.from(inputStream).
|
||||
take(1).
|
||||
concatMap(resource -> {
|
||||
HttpHeaders headers = outputMessage.getHeaders();
|
||||
addHeaders(headers, resource, contentType);
|
||||
return writeContent(resource, type, outputMessage, hints);
|
||||
addHeaders(headers, resource, mediaType);
|
||||
return writeContent(resource, elementType, outputMessage, hints);
|
||||
}));
|
||||
}
|
||||
|
||||
protected void addHeaders(HttpHeaders headers, Resource resource, MediaType contentType) {
|
||||
protected void addHeaders(HttpHeaders headers, Resource resource, MediaType mediaType) {
|
||||
if (headers.getContentType() == null) {
|
||||
if (contentType == null || !contentType.isConcrete() ||
|
||||
MediaType.APPLICATION_OCTET_STREAM.equals(contentType)) {
|
||||
contentType = Optional.ofNullable(MediaTypeFactory.getMediaType(resource)).
|
||||
if (mediaType == null || !mediaType.isConcrete() ||
|
||||
MediaType.APPLICATION_OCTET_STREAM.equals(mediaType)) {
|
||||
mediaType = Optional.ofNullable(MediaTypeFactory.getMediaType(resource)).
|
||||
orElse(MediaType.APPLICATION_OCTET_STREAM);
|
||||
}
|
||||
headers.setContentType(contentType);
|
||||
headers.setContentType(mediaType);
|
||||
}
|
||||
if (headers.getContentLength() < 0) {
|
||||
contentLength(resource).ifPresent(headers::setContentLength);
|
||||
|
||||
@@ -62,7 +62,7 @@ public class ServerSentEventHttpMessageWriter implements HttpMessageWriter<Objec
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canWrite(ResolvableType type, MediaType mediaType, Map<String, Object> hints) {
|
||||
public boolean canWrite(ResolvableType elementType, MediaType mediaType, Map<String, Object> hints) {
|
||||
return mediaType == null || TEXT_EVENT_STREAM.isCompatibleWith(mediaType);
|
||||
}
|
||||
|
||||
@@ -72,13 +72,13 @@ public class ServerSentEventHttpMessageWriter implements HttpMessageWriter<Objec
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Void> write(Publisher<?> inputStream, ResolvableType type, MediaType contentType,
|
||||
public Mono<Void> write(Publisher<?> inputStream, ResolvableType elementType, MediaType mediaType,
|
||||
ReactiveHttpOutputMessage outputMessage, Map<String, Object> hints) {
|
||||
|
||||
outputMessage.getHeaders().setContentType(TEXT_EVENT_STREAM);
|
||||
|
||||
DataBufferFactory bufferFactory = outputMessage.bufferFactory();
|
||||
Flux<Publisher<DataBuffer>> body = encode(inputStream, bufferFactory, type, hints);
|
||||
Flux<Publisher<DataBuffer>> body = encode(inputStream, bufferFactory, elementType, hints);
|
||||
|
||||
return outputMessage.writeAndFlushWith(body);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user