From ad91b94249c236b5508d36434a847b90312e9ce4 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Fri, 17 Mar 2017 11:05:07 -0400 Subject: [PATCH] Require Encoder & Decoder in Reader & Writer wrappers When CodecHttpMessageConverter was split into DecoderHttpMessageReader and EncoderHttpMessageWriter the null checks were never removed. This commit makes the Encoder and Decoder instances provided to their respective wrappers required. --- .../http/codec/DecoderHttpMessageReader.java | 47 ++++++++++--------- .../http/codec/EncoderHttpMessageWriter.java | 44 ++++++++--------- 2 files changed, 48 insertions(+), 43 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/http/codec/DecoderHttpMessageReader.java b/spring-web/src/main/java/org/springframework/http/codec/DecoderHttpMessageReader.java index 3528522cba..962420b170 100644 --- a/spring-web/src/main/java/org/springframework/http/codec/DecoderHttpMessageReader.java +++ b/spring-web/src/main/java/org/springframework/http/codec/DecoderHttpMessageReader.java @@ -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. @@ -25,12 +25,13 @@ import reactor.core.publisher.Mono; import org.springframework.core.ResolvableType; import org.springframework.core.codec.Decoder; +import org.springframework.http.HttpMessage; import org.springframework.http.MediaType; import org.springframework.http.ReactiveHttpInputMessage; +import org.springframework.util.Assert; /** - * Implementation of the {@link HttpMessageReader} interface that delegates - * to a {@link Decoder}. + * Implementation of {@code HttpMessageReader} delegating to a {@link Decoder}. * * @author Arjen Poutsma * @author Sebastien Deleuze @@ -41,50 +42,54 @@ public class DecoderHttpMessageReader implements HttpMessageReader { private final Decoder decoder; - private final List readableMediaTypes; + private final List mediaTypes; /** - * Create a {@code CodecHttpMessageConverter} with the given {@link Decoder}. - * @param decoder the decoder to use + * Create an instance wrapping the given {@link Decoder}. */ public DecoderHttpMessageReader(Decoder decoder) { + Assert.notNull(decoder, "Decoder is required"); this.decoder = decoder; - this.readableMediaTypes = (decoder != null ? - MediaType.asMediaTypes(decoder.getDecodableMimeTypes()) : Collections.emptyList()); + this.mediaTypes = MediaType.asMediaTypes(decoder.getDecodableMimeTypes()); } - @Override - public boolean canRead(ResolvableType elementType, MediaType mediaType) { - return this.decoder != null && this.decoder.canDecode(elementType, mediaType); + /** + * Return the {@link Decoder} of this reader. + */ + public Decoder getDecoder() { + return this.decoder; } @Override public List getReadableMediaTypes() { - return this.readableMediaTypes; + return this.mediaTypes; } @Override - public Flux read(ResolvableType elementType, ReactiveHttpInputMessage inputMessage, Map hints) { - if (this.decoder == null) { - return Flux.error(new IllegalStateException("No decoder set")); - } + public boolean canRead(ResolvableType elementType, MediaType mediaType) { + return this.decoder.canDecode(elementType, mediaType); + } + + @Override + public Flux read(ResolvableType elementType, ReactiveHttpInputMessage inputMessage, + Map hints) { + MediaType contentType = getContentType(inputMessage); return this.decoder.decode(inputMessage.getBody(), elementType, contentType, hints); } @Override - public Mono readMono(ResolvableType elementType, ReactiveHttpInputMessage inputMessage, Map hints) { - if (this.decoder == null) { - return Mono.error(new IllegalStateException("No decoder set")); - } + public Mono readMono(ResolvableType elementType, ReactiveHttpInputMessage inputMessage, + Map hints) { + MediaType contentType = getContentType(inputMessage); return this.decoder.decodeToMono(inputMessage.getBody(), elementType, contentType, hints); } - private MediaType getContentType(ReactiveHttpInputMessage inputMessage) { + private MediaType getContentType(HttpMessage inputMessage) { MediaType contentType = inputMessage.getHeaders().getContentType(); return (contentType != null ? contentType : MediaType.APPLICATION_OCTET_STREAM); } diff --git a/spring-web/src/main/java/org/springframework/http/codec/EncoderHttpMessageWriter.java b/spring-web/src/main/java/org/springframework/http/codec/EncoderHttpMessageWriter.java index 06c688ad45..8c98c35ccc 100644 --- a/spring-web/src/main/java/org/springframework/http/codec/EncoderHttpMessageWriter.java +++ b/spring-web/src/main/java/org/springframework/http/codec/EncoderHttpMessageWriter.java @@ -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. @@ -16,13 +16,10 @@ package org.springframework.http.codec; -import java.util.Collections; import java.util.List; import java.util.Map; import org.reactivestreams.Publisher; -import static org.springframework.core.codec.AbstractEncoder.FLUSHING_STRATEGY_HINT; -import static org.springframework.core.codec.AbstractEncoder.FlushingStrategy.AFTER_EACH_ELEMENT; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; @@ -33,10 +30,13 @@ import org.springframework.core.io.buffer.DataBufferFactory; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.ReactiveHttpOutputMessage; +import org.springframework.util.Assert; + +import static org.springframework.core.codec.AbstractEncoder.FLUSHING_STRATEGY_HINT; +import static org.springframework.core.codec.AbstractEncoder.FlushingStrategy.AFTER_EACH_ELEMENT; /** - * Implementation of the {@link HttpMessageWriter} interface that delegates - * to an {@link Encoder}. + * Implementation of {@code HttpMessageWriter} delegating to an {@link Encoder}. * * @author Arjen Poutsma * @author Sebastien Deleuze @@ -47,40 +47,42 @@ public class EncoderHttpMessageWriter implements HttpMessageWriter { private final Encoder encoder; - private final List writableMediaTypes; + private final List mediaTypes; /** - * Create a {@code CodecHttpMessageConverter} with the given {@link Encoder}. - * @param encoder the encoder to use + * Create an instance wrapping the given {@link Encoder}. */ public EncoderHttpMessageWriter(Encoder encoder) { + Assert.notNull(encoder, "Encoder is required"); this.encoder = encoder; - this.writableMediaTypes = (encoder != null ? - MediaType.asMediaTypes(encoder.getEncodableMimeTypes()) : Collections.emptyList()); + this.mediaTypes = MediaType.asMediaTypes(encoder.getEncodableMimeTypes()); } - @Override - public boolean canWrite(ResolvableType elementType, MediaType mediaType) { - return this.encoder != null && this.encoder.canEncode(elementType, mediaType); + /** + * Return the {@code Encoder} of this writer. + */ + public Encoder getEncoder() { + return this.encoder; } @Override public List getWritableMediaTypes() { - return this.writableMediaTypes; + return this.mediaTypes; } + @Override + public boolean canWrite(ResolvableType elementType, MediaType mediaType) { + return this.encoder.canEncode(elementType, mediaType); + } + @Override public Mono write(Publisher inputStream, ResolvableType elementType, MediaType mediaType, ReactiveHttpOutputMessage outputMessage, Map hints) { - if (this.encoder == null) { - return Mono.error(new IllegalStateException("No decoder set")); - } - HttpHeaders headers = outputMessage.getHeaders(); if (headers.getContentType() == null) { MediaType contentTypeToUse = mediaType; @@ -119,9 +121,7 @@ public class EncoderHttpMessageWriter implements HttpMessageWriter { * @return the content type, or {@code null} */ protected MediaType getDefaultContentType(ResolvableType elementType) { - return writableMediaTypes.stream() - .filter(MediaType::isConcrete) - .findFirst().orElse(null); + return this.mediaTypes.stream().filter(MediaType::isConcrete).findFirst().orElse(null); } } -- GitLab