This commit is contained in:
Rossen Stoyanchev 2016-06-24 18:03:56 -04:00
parent 95751acb33
commit 351e834716
1 changed files with 19 additions and 17 deletions

View File

@ -40,6 +40,7 @@ import org.springframework.http.support.MediaTypeUtils;
* *
* @author Arjen Poutsma * @author Arjen Poutsma
* @author Sebastien Deleuze * @author Sebastien Deleuze
* @author Rossen Stoyanchev
*/ */
public class CodecHttpMessageConverter<T> implements HttpMessageConverter<T> { public class CodecHttpMessageConverter<T> implements HttpMessageConverter<T> {
@ -51,6 +52,7 @@ public class CodecHttpMessageConverter<T> implements HttpMessageConverter<T> {
private final List<MediaType> writableMediaTypes; private final List<MediaType> writableMediaTypes;
/** /**
* Create a {@code CodecHttpMessageConverter} with the given {@link Encoder}. When * Create a {@code CodecHttpMessageConverter} with the given {@link Encoder}. When
* using this constructor, all read-related methods will in {@code false} or an * using this constructor, all read-related methods will in {@code false} or an
@ -89,6 +91,7 @@ public class CodecHttpMessageConverter<T> implements HttpMessageConverter<T> {
Collections.emptyList(); Collections.emptyList();
} }
@Override @Override
public boolean canRead(ResolvableType type, MediaType mediaType) { public boolean canRead(ResolvableType type, MediaType mediaType) {
return this.decoder != null && this.decoder.canDecode(type, mediaType); return this.decoder != null && this.decoder.canDecode(type, mediaType);
@ -109,6 +112,7 @@ public class CodecHttpMessageConverter<T> implements HttpMessageConverter<T> {
return this.writableMediaTypes; return this.writableMediaTypes;
} }
@Override @Override
public Flux<T> read(ResolvableType type, ReactiveHttpInputMessage inputMessage) { public Flux<T> read(ResolvableType type, ReactiveHttpInputMessage inputMessage) {
if (this.decoder == null) { if (this.decoder == null) {
@ -118,16 +122,13 @@ public class CodecHttpMessageConverter<T> implements HttpMessageConverter<T> {
if (contentType == null) { if (contentType == null) {
contentType = MediaType.APPLICATION_OCTET_STREAM; contentType = MediaType.APPLICATION_OCTET_STREAM;
} }
return this.decoder.decode(inputMessage.getBody(), type, contentType);
Flux<DataBuffer> body = inputMessage.getBody();
return this.decoder.decode(body, type, contentType);
} }
@Override @Override
public Mono<Void> write(Publisher<? extends T> inputStream, ResolvableType type, public Mono<Void> write(Publisher<? extends T> inputStream, ResolvableType type,
MediaType contentType, MediaType contentType, ReactiveHttpOutputMessage outputMessage) {
ReactiveHttpOutputMessage outputMessage) {
if (this.encoder == null) { if (this.encoder == null) {
return Mono.error(new IllegalStateException("No decoder set")); return Mono.error(new IllegalStateException("No decoder set"));
} }
@ -139,22 +140,23 @@ public class CodecHttpMessageConverter<T> implements HttpMessageConverter<T> {
} }
headers.setContentType(contentTypeToUse); headers.setContentType(contentTypeToUse);
} }
DataBufferFactory dataBufferFactory = outputMessage.bufferFactory(); DataBufferFactory bufferFactory = outputMessage.bufferFactory();
Flux<DataBuffer> body = Flux<DataBuffer> body = this.encoder.encode(inputStream, bufferFactory, type, contentType);
this.encoder.encode(inputStream, dataBufferFactory, type, contentType);
return outputMessage.writeWith(body); return outputMessage.writeWith(body);
} }
/** /**
* Returns the default content type for the given type. Called when {@link #write} * Return the default content type for the given {@code ResolvableType}.
* is invoked without a specified content type parameter. * Used when {@link #write} is called without a concrete content type.
* <p>By default, this returns a {@link MediaType} created using the first element of *
* the encoder {@link Encoder#getEncodableMimeTypes() encodableMimeTypes} property, if any. * <p>By default returns the first of {@link Encoder#getEncodableMimeTypes()
* Can be overridden in subclasses. * encodableMimeTypes}, if any.
* @param type the type to return the content type for *
* @return the content type, or {@code null} if not known * @param elementType the type of element for encoding
* @return the content type, or {@code null}
*/ */
protected MediaType getDefaultContentType(ResolvableType type) { protected MediaType getDefaultContentType(ResolvableType elementType) {
return (!this.writableMediaTypes.isEmpty() ? this.writableMediaTypes.get(0) : null); return (!this.writableMediaTypes.isEmpty() ? this.writableMediaTypes.get(0) : null);
} }
} }