From fc52ddcd973e08118028599a177ddb79e98b93cd Mon Sep 17 00:00:00 2001 From: Sebastien Deleuze Date: Tue, 13 Sep 2016 14:57:29 +0200 Subject: [PATCH] Polish --- .../springframework/core/codec/Decoder.java | 1 - .../springframework/core/codec/Encoder.java | 1 - .../web/reactive/function/RouterTests.java | 12 ++++---- .../http/codec/DecoderHttpMessageReader.java | 12 ++++---- .../http/codec/EncoderHttpMessageWriter.java | 28 +++++++++---------- .../http/codec/HttpMessageReader.java | 13 ++++----- .../http/codec/HttpMessageWriter.java | 22 ++++++++------- .../http/codec/ResourceHttpMessageWriter.java | 18 ++++++------ .../ServerSentEventHttpMessageWriter.java | 6 ++-- 9 files changed, 56 insertions(+), 57 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/codec/Decoder.java b/spring-core/src/main/java/org/springframework/core/codec/Decoder.java index cf88dfa6644..3559e0b3971 100644 --- a/spring-core/src/main/java/org/springframework/core/codec/Decoder.java +++ b/spring-core/src/main/java/org/springframework/core/codec/Decoder.java @@ -16,7 +16,6 @@ package org.springframework.core.codec; -import java.util.Collections; import java.util.List; import java.util.Map; diff --git a/spring-core/src/main/java/org/springframework/core/codec/Encoder.java b/spring-core/src/main/java/org/springframework/core/codec/Encoder.java index 49ae35ac2a4..9e8e4765bc0 100644 --- a/spring-core/src/main/java/org/springframework/core/codec/Encoder.java +++ b/spring-core/src/main/java/org/springframework/core/codec/Encoder.java @@ -16,7 +16,6 @@ package org.springframework.core.codec; -import java.util.Collections; import java.util.List; import java.util.Map; diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/function/RouterTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/function/RouterTests.java index 74bfb38a4ce..f6a8b88b9e2 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/function/RouterTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/function/RouterTests.java @@ -162,7 +162,7 @@ public class RouterTests { private static class DummyMessageWriter implements HttpMessageWriter { @Override - public boolean canWrite(ResolvableType type, MediaType mediaType, Map hints) { + public boolean canWrite(ResolvableType elementType, MediaType mediaType, Map hints) { return false; } @@ -172,8 +172,8 @@ public class RouterTests { } @Override - public Mono write(Publisher inputStream, ResolvableType type, - MediaType contentType, + public Mono write(Publisher inputStream, ResolvableType elementType, + MediaType mediaType, ReactiveHttpOutputMessage outputMessage, Map hints) { return Mono.empty(); @@ -183,7 +183,7 @@ public class RouterTests { private static class DummyMessageReader implements HttpMessageReader { @Override - public boolean canRead(ResolvableType type, MediaType mediaType, Map hints) { + public boolean canRead(ResolvableType elementType, MediaType mediaType, Map hints) { return false; } @@ -193,13 +193,13 @@ public class RouterTests { } @Override - public Flux read(ResolvableType type, ReactiveHttpInputMessage inputMessage, + public Flux read(ResolvableType elementType, ReactiveHttpInputMessage inputMessage, Map hints) { return Flux.empty(); } @Override - public Mono readMono(ResolvableType type, ReactiveHttpInputMessage inputMessage, + public Mono readMono(ResolvableType elementType, ReactiveHttpInputMessage inputMessage, Map hints) { return Mono.empty(); } 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 38be3457282..0db32a137a3 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 @@ -56,8 +56,8 @@ public class DecoderHttpMessageReader implements HttpMessageReader { @Override - public boolean canRead(ResolvableType type, MediaType mediaType, Map hints) { - return this.decoder != null && this.decoder.canDecode(type, mediaType, hints); + public boolean canRead(ResolvableType elementType, MediaType mediaType, Map hints) { + return this.decoder != null && this.decoder.canDecode(elementType, mediaType, hints); } @Override @@ -67,21 +67,21 @@ public class DecoderHttpMessageReader implements HttpMessageReader { @Override - public Flux read(ResolvableType type, ReactiveHttpInputMessage inputMessage, Map hints) { + public Flux read(ResolvableType elementType, ReactiveHttpInputMessage inputMessage, Map 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 readMono(ResolvableType type, ReactiveHttpInputMessage inputMessage, Map hints) { + public Mono readMono(ResolvableType elementType, ReactiveHttpInputMessage inputMessage, Map 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) { 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 2ffce181af2..55c8099cb7c 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 @@ -60,8 +60,8 @@ public class EncoderHttpMessageWriter implements HttpMessageWriter { @Override - public boolean canWrite(ResolvableType type, MediaType mediaType, Map hints) { - return this.encoder != null && this.encoder.canEncode(type, mediaType, hints); + public boolean canWrite(ResolvableType elementType, MediaType mediaType, Map hints) { + return this.encoder != null && this.encoder.canEncode(elementType, mediaType, hints); } @Override @@ -71,8 +71,8 @@ public class EncoderHttpMessageWriter implements HttpMessageWriter { @Override - public Mono write(Publisher inputStream, ResolvableType type, - MediaType contentType, ReactiveHttpOutputMessage outputMessage, + public Mono write(Publisher inputStream, ResolvableType elementType, + MediaType mediaType, ReactiveHttpOutputMessage outputMessage, Map hints) { if (this.encoder == null) { @@ -81,19 +81,19 @@ public class EncoderHttpMessageWriter implements HttpMessageWriter { 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 implements HttpMessageWriter { } DataBufferFactory bufferFactory = outputMessage.bufferFactory(); - Flux body = this.encoder.encode(inputStream, bufferFactory, type, contentType, hints); + Flux body = this.encoder.encode(inputStream, bufferFactory, elementType, mediaType, hints); return outputMessage.writeWith(body); } diff --git a/spring-web/src/main/java/org/springframework/http/codec/HttpMessageReader.java b/spring-web/src/main/java/org/springframework/http/codec/HttpMessageReader.java index 7fb7e76a186..73a44e6adb9 100644 --- a/spring-web/src/main/java/org/springframework/http/codec/HttpMessageReader.java +++ b/spring-web/src/main/java/org/springframework/http/codec/HttpMessageReader.java @@ -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 { /** * 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 hints); + boolean canRead(ResolvableType elementType, MediaType mediaType, Map 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 read(ResolvableType type, ReactiveHttpInputMessage inputMessage, Map hints); + Flux read(ResolvableType elementType, ReactiveHttpInputMessage inputMessage, Map 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 readMono(ResolvableType type, ReactiveHttpInputMessage inputMessage, Map hints); + Mono readMono(ResolvableType elementType, ReactiveHttpInputMessage inputMessage, Map hints); /** * Return the list of {@link MediaType} objects that can be read by this converter. diff --git a/spring-web/src/main/java/org/springframework/http/codec/HttpMessageWriter.java b/spring-web/src/main/java/org/springframework/http/codec/HttpMessageWriter.java index 900c2af85aa..15538df4ad8 100644 --- a/spring-web/src/main/java/org/springframework/http/codec/HttpMessageWriter.java +++ b/spring-web/src/main/java/org/springframework/http/codec/HttpMessageWriter.java @@ -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 { /** * 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 hints); + boolean canWrite(ResolvableType elementType, MediaType mediaType, Map 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 write(Publisher inputStream, ResolvableType type, - MediaType contentType, ReactiveHttpOutputMessage outputMessage, Map hints); + Mono write(Publisher inputStream, ResolvableType elementType, + MediaType mediaType, ReactiveHttpOutputMessage outputMessage, Map hints); /** * Return the list of {@link MediaType} objects that can be written by this converter. diff --git a/spring-web/src/main/java/org/springframework/http/codec/ResourceHttpMessageWriter.java b/spring-web/src/main/java/org/springframework/http/codec/ResourceHttpMessageWriter.java index a488cbf2a0b..0541c24a7c5 100644 --- a/spring-web/src/main/java/org/springframework/http/codec/ResourceHttpMessageWriter.java +++ b/spring-web/src/main/java/org/springframework/http/codec/ResourceHttpMessageWriter.java @@ -59,26 +59,26 @@ public class ResourceHttpMessageWriter extends EncoderHttpMessageWriter write(Publisher inputStream, ResolvableType type, - MediaType contentType, ReactiveHttpOutputMessage outputMessage, Map hints) { + public Mono write(Publisher inputStream, ResolvableType elementType, + MediaType mediaType, ReactiveHttpOutputMessage outputMessage, Map 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); diff --git a/spring-web/src/main/java/org/springframework/http/codec/ServerSentEventHttpMessageWriter.java b/spring-web/src/main/java/org/springframework/http/codec/ServerSentEventHttpMessageWriter.java index d54679d0740..fe9e66ead87 100644 --- a/spring-web/src/main/java/org/springframework/http/codec/ServerSentEventHttpMessageWriter.java +++ b/spring-web/src/main/java/org/springframework/http/codec/ServerSentEventHttpMessageWriter.java @@ -62,7 +62,7 @@ public class ServerSentEventHttpMessageWriter implements HttpMessageWriter hints) { + public boolean canWrite(ResolvableType elementType, MediaType mediaType, Map hints) { return mediaType == null || TEXT_EVENT_STREAM.isCompatibleWith(mediaType); } @@ -72,13 +72,13 @@ public class ServerSentEventHttpMessageWriter implements HttpMessageWriter write(Publisher inputStream, ResolvableType type, MediaType contentType, + public Mono write(Publisher inputStream, ResolvableType elementType, MediaType mediaType, ReactiveHttpOutputMessage outputMessage, Map hints) { outputMessage.getHeaders().setContentType(TEXT_EVENT_STREAM); DataBufferFactory bufferFactory = outputMessage.bufferFactory(); - Flux> body = encode(inputStream, bufferFactory, type, hints); + Flux> body = encode(inputStream, bufferFactory, elementType, hints); return outputMessage.writeAndFlushWith(body); }