Collape ServerHttpMessage[Reader|Writer]

This commit folds ServerHttpMessage[Reader|Writer] into its parent
HttpMessage[Reader|Writer] with the server methods pre-implemented
by default to be simple pass-through delegates.
This commit is contained in:
Rossen Stoyanchev
2017-03-24 17:36:28 -04:00
parent 4a7218f54f
commit 5414cd0cf8
26 changed files with 186 additions and 245 deletions

View File

@@ -36,7 +36,7 @@ import org.springframework.util.Assert;
/**
* {@code HttpMessageReader} that wraps and delegates to a {@link Decoder}.
*
* <p>Also a {@code ServerHttpMessageReader} that pre-resolves decoding hints
* <p>Also a {@code HttpMessageReader} that pre-resolves decoding hints
* from the extra information available on the server side such as the request
* or controller method parameter annotations.
*
@@ -45,7 +45,7 @@ import org.springframework.util.Assert;
* @author Rossen Stoyanchev
* @since 5.0
*/
public class DecoderHttpMessageReader<T> implements ServerHttpMessageReader<T> {
public class DecoderHttpMessageReader<T> implements HttpMessageReader<T> {
private final Decoder<T> decoder;
@@ -102,7 +102,7 @@ public class DecoderHttpMessageReader<T> implements ServerHttpMessageReader<T> {
}
// ServerHttpMessageReader...
// Server-side only...
@Override
public Flux<T> read(ResolvableType actualType, ResolvableType elementType,

View File

@@ -37,7 +37,7 @@ import org.springframework.util.Assert;
/**
* {@code HttpMessageWriter} that wraps and delegates to a {@link Encoder}.
*
* <p>Also a {@code ServerHttpMessageWriter} that pre-resolves encoding hints
* <p>Also a {@code HttpMessageWriter} that pre-resolves encoding hints
* from the extra information available on the server side such as the request
* or controller method annotations.
*
@@ -46,7 +46,7 @@ import org.springframework.util.Assert;
* @author Rossen Stoyanchev
* @since 5.0
*/
public class EncoderHttpMessageWriter<T> implements ServerHttpMessageWriter<T> {
public class EncoderHttpMessageWriter<T> implements HttpMessageWriter<T> {
private final Encoder<T> encoder;
@@ -136,7 +136,7 @@ public class EncoderHttpMessageWriter<T> implements ServerHttpMessageWriter<T> {
}
// ServerHttpMessageWriter...
// Server side only...
@Override
public Mono<Void> write(Publisher<? extends T> inputStream, ResolvableType actualType,

View File

@@ -22,9 +22,12 @@ import java.util.Map;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.core.MethodParameter;
import org.springframework.core.ResolvableType;
import org.springframework.http.MediaType;
import org.springframework.http.ReactiveHttpInputMessage;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
/**
* Strategy for reading from a {@link ReactiveHttpInputMessage} and decoding
@@ -74,5 +77,44 @@ public interface HttpMessageReader<T> {
*/
Mono<T> readMono(ResolvableType elementType, ReactiveHttpInputMessage message, Map<String, Object> hints);
/**
* Server-side only alternative to
* {@link #read(ResolvableType, ReactiveHttpInputMessage, Map)} with
* additional context available.
*
* @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 decoded stream of elements
*/
default Flux<T> read(ResolvableType actualType, ResolvableType elementType, ServerHttpRequest request,
ServerHttpResponse response, Map<String, Object> hints) {
return read(elementType, request, hints);
}
/**
* Server-side only alternative to
* {@link #readMono(ResolvableType, ReactiveHttpInputMessage, Map)} with
* additional, context available.
*
* @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 decoded stream of elements
*/
default Mono<T> readMono(ResolvableType actualType, ResolvableType elementType, ServerHttpRequest request,
ServerHttpResponse response, Map<String, Object> hints) {
return readMono(elementType, request, hints);
}
}

View File

@@ -22,9 +22,12 @@ import java.util.Map;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Mono;
import org.springframework.core.MethodParameter;
import org.springframework.core.ResolvableType;
import org.springframework.http.MediaType;
import org.springframework.http.ReactiveHttpOutputMessage;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
/**
* Strategy for encoding a stream of objects of type {@code <T>} and writing
@@ -67,4 +70,26 @@ public interface HttpMessageWriter<T> {
MediaType mediaType, ReactiveHttpOutputMessage message, Map<String, Object> hints);
/**
* Server-side only alternative to
* {@link #write(Publisher, ResolvableType, MediaType, ReactiveHttpOutputMessage, Map)}
* with additional context available.
*
* @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
*/
default Mono<Void> write(Publisher<? extends T> inputStream, ResolvableType actualType,
ResolvableType elementType, MediaType mediaType, ServerHttpRequest request,
ServerHttpResponse response, Map<String, Object> hints) {
return write(inputStream, elementType, mediaType, response, hints);
}
}

View File

@@ -53,7 +53,7 @@ import static java.util.Collections.emptyMap;
/**
* {@code HttpMessageWriter} that can write a {@link Resource}.
*
* <p>Also an implementation of {@code ServerHttpMessageWriter} with support
* <p>Also an implementation of {@code HttpMessageWriter} with support
* for writing one or more {@link ResourceRegion}'s based on the HTTP ranges
* specified in the request.
*
@@ -68,7 +68,7 @@ import static java.util.Collections.emptyMap;
* @see ResourceRegionEncoder
* @see HttpRange
*/
public class ResourceHttpMessageWriter implements ServerHttpMessageWriter<Resource> {
public class ResourceHttpMessageWriter implements HttpMessageWriter<Resource> {
private static final ResolvableType REGION_TYPE = ResolvableType.forClass(ResourceRegion.class);
@@ -102,7 +102,7 @@ public class ResourceHttpMessageWriter implements ServerHttpMessageWriter<Resour
}
// HttpMessageWriter (client and server): single Resource
// Client or server: single Resource...
@Override
public Mono<Void> write(Publisher<? extends Resource> inputStream, ResolvableType elementType,
@@ -171,7 +171,7 @@ public class ResourceHttpMessageWriter implements ServerHttpMessageWriter<Resour
}
// ServerHttpMessageWriter (server only): single Resource or sub-regions
// Server-side only: single Resource or sub-regions...
@Override
@SuppressWarnings("unchecked")

View File

@@ -99,10 +99,10 @@ public class ServerCodecConfigurer {
/**
* Prepare a list of HTTP message readers.
*/
public List<ServerHttpMessageReader<?>> getReaders() {
public List<HttpMessageReader<?>> getReaders() {
// Built-in, concrete Java type readers
List<ServerHttpMessageReader<?>> result = new ArrayList<>();
List<HttpMessageReader<?>> result = new ArrayList<>();
this.defaultCodecs.addReaderTo(result, ByteArrayDecoder.class, ByteArrayDecoder::new);
this.defaultCodecs.addReaderTo(result, ByteBufferDecoder.class, ByteBufferDecoder::new);
this.defaultCodecs.addReaderTo(result, DataBufferDecoder.class, DataBufferDecoder::new);
@@ -131,10 +131,10 @@ public class ServerCodecConfigurer {
/**
* Prepare a list of HTTP message writers.
*/
public List<ServerHttpMessageWriter<?>> getWriters() {
public List<HttpMessageWriter<?>> getWriters() {
// Built-in, concrete Java type readers
List<ServerHttpMessageWriter<?>> result = new ArrayList<>();
List<HttpMessageWriter<?>> result = new ArrayList<>();
this.defaultCodecs.addWriterTo(result, ByteArrayEncoder.class, ByteArrayEncoder::new);
this.defaultCodecs.addWriterTo(result, ByteBufferEncoder.class, ByteBufferEncoder::new);
this.defaultCodecs.addWriterTo(result, DataBufferEncoder.class, DataBufferEncoder::new);
@@ -169,9 +169,9 @@ public class ServerCodecConfigurer {
private boolean suppressed = false;
private final Map<Class<?>, ServerHttpMessageReader<?>> readers = new HashMap<>();
private final Map<Class<?>, HttpMessageReader<?>> readers = new HashMap<>();
private final Map<Class<?>, ServerHttpMessageWriter<?>> writers = new HashMap<>();
private final Map<Class<?>, HttpMessageWriter<?>> writers = new HashMap<>();
/**
@@ -197,7 +197,7 @@ public class ServerCodecConfigurer {
* @param encoder the encoder to use
*/
public void sse(Encoder<?> encoder) {
ServerHttpMessageWriter<?> writer = new ServerSentEventHttpMessageWriter(encoder);
HttpMessageWriter<?> writer = new ServerSentEventHttpMessageWriter(encoder);
this.writers.put(ServerSentEventHttpMessageWriter.class, writer);
}
@@ -208,14 +208,14 @@ public class ServerCodecConfigurer {
this.suppressed = suppressed;
}
private <T, D extends Decoder<T>> void addReaderTo(List<ServerHttpMessageReader<?>> result,
private <T, D extends Decoder<T>> void addReaderTo(List<HttpMessageReader<?>> result,
Class<D> key, Supplier<D> fallback) {
addReaderTo(result, () -> findReader(key, fallback));
}
private void addReaderTo(List<ServerHttpMessageReader<?>> result,
Supplier<ServerHttpMessageReader<?>> reader) {
private void addReaderTo(List<HttpMessageReader<?>> result,
Supplier<HttpMessageReader<?>> reader) {
if (!this.suppressed) {
result.add(reader.get());
@@ -230,14 +230,14 @@ public class ServerCodecConfigurer {
}
private <T, E extends Encoder<T>> void addWriterTo(List<ServerHttpMessageWriter<?>> result,
private <T, E extends Encoder<T>> void addWriterTo(List<HttpMessageWriter<?>> result,
Class<E> key, Supplier<E> fallback) {
addWriterTo(result, () -> findWriter(key, fallback));
}
private void addWriterTo(List<ServerHttpMessageWriter<?>> result,
Supplier<ServerHttpMessageWriter<?>> writer) {
private void addWriterTo(List<HttpMessageWriter<?>> result,
Supplier<HttpMessageWriter<?>> writer) {
if (!this.suppressed) {
result.add(writer.get());
@@ -252,25 +252,25 @@ public class ServerCodecConfigurer {
}
private void addStringReaderTextOnlyTo(List<ServerHttpMessageReader<?>> result) {
private void addStringReaderTextOnlyTo(List<HttpMessageReader<?>> result) {
addReaderTo(result, () -> new DecoderHttpMessageReader<>(StringDecoder.textPlainOnly(true)));
}
private void addStringReaderTo(List<ServerHttpMessageReader<?>> result) {
private void addStringReaderTo(List<HttpMessageReader<?>> result) {
addReaderTo(result, () -> new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes(true)));
}
private void addStringWriterTextPlainOnlyTo(List<ServerHttpMessageWriter<?>> result) {
private void addStringWriterTextPlainOnlyTo(List<HttpMessageWriter<?>> result) {
addWriterTo(result, () -> new EncoderHttpMessageWriter<>(CharSequenceEncoder.textPlainOnly()));
}
private void addStringWriterTo(List<ServerHttpMessageWriter<?>> result) {
private void addStringWriterTo(List<HttpMessageWriter<?>> result) {
addWriterTo(result, () -> new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes()));
}
private void addSseWriterTo(List<ServerHttpMessageWriter<?>> result) {
private void addSseWriterTo(List<HttpMessageWriter<?>> result) {
addWriterTo(result, () -> {
ServerHttpMessageWriter<?> writer = this.writers.get(ServerSentEventHttpMessageWriter.class);
HttpMessageWriter<?> writer = this.writers.get(ServerSentEventHttpMessageWriter.class);
if (writer != null) {
return writer;
}
@@ -288,13 +288,13 @@ public class ServerCodecConfigurer {
*/
public static class CustomCodecConfigurer {
private final List<ServerHttpMessageReader<?>> typedReaders = new ArrayList<>();
private final List<HttpMessageReader<?>> typedReaders = new ArrayList<>();
private final List<ServerHttpMessageWriter<?>> typedWriters = new ArrayList<>();
private final List<HttpMessageWriter<?>> typedWriters = new ArrayList<>();
private final List<ServerHttpMessageReader<?>> objectReaders = new ArrayList<>();
private final List<HttpMessageReader<?>> objectReaders = new ArrayList<>();
private final List<ServerHttpMessageWriter<?>> objectWriters = new ArrayList<>();
private final List<HttpMessageWriter<?>> objectWriters = new ArrayList<>();
/**
@@ -314,21 +314,21 @@ public class ServerCodecConfigurer {
}
/**
* Add a custom {@link ServerHttpMessageReader}. For readers of type
* Add a custom {@link HttpMessageReader}. For readers of type
* {@link DecoderHttpMessageReader} consider using the shortcut
* {@link #decoder(Decoder)} instead.
*/
public void reader(ServerHttpMessageReader<?> reader) {
public void reader(HttpMessageReader<?> reader) {
boolean canReadToObject = reader.canRead(ResolvableType.forClass(Object.class), null);
(canReadToObject ? this.objectReaders : this.typedReaders).add(reader);
}
/**
* Add a custom {@link ServerHttpMessageWriter}. For readers of type
* Add a custom {@link HttpMessageWriter}. For readers of type
* {@link EncoderHttpMessageWriter} consider using the shortcut
* {@link #encoder(Encoder)} instead.
*/
public void writer(ServerHttpMessageWriter<?> writer) {
public void writer(HttpMessageWriter<?> writer) {
boolean canWriteObject = writer.canWrite(ResolvableType.forClass(Object.class), null);
(canWriteObject ? this.objectWriters : this.typedWriters).add(writer);
}
@@ -336,19 +336,19 @@ public class ServerCodecConfigurer {
// Internal methods for building a list of custom readers or writers...
private void addTypedReadersTo(List<ServerHttpMessageReader<?>> result) {
private void addTypedReadersTo(List<HttpMessageReader<?>> result) {
result.addAll(this.typedReaders);
}
private void addObjectReadersTo(List<ServerHttpMessageReader<?>> result) {
private void addObjectReadersTo(List<HttpMessageReader<?>> result) {
result.addAll(this.objectReaders);
}
private void addTypedWritersTo(List<ServerHttpMessageWriter<?>> result) {
private void addTypedWritersTo(List<HttpMessageWriter<?>> result) {
result.addAll(this.typedWriters);
}
private void addObjectWritersTo(List<ServerHttpMessageWriter<?>> result) {
private void addObjectWritersTo(List<HttpMessageWriter<?>> result) {
result.addAll(this.objectWriters);
}
}

View File

@@ -1,69 +0,0 @@
/*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.http.codec;
import java.util.Map;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.core.MethodParameter;
import org.springframework.core.ResolvableType;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.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> {
/**
* Decode and read the request body to an object stream.
*
* @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 decoded stream of elements
*/
Flux<T> read(ResolvableType actualType, ResolvableType elementType, ServerHttpRequest request,
ServerHttpResponse response, Map<String, Object> hints);
/**
* Decode and read the request body to a single object.
*
* @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 decoded stream of elements
*/
Mono<T> readMono(ResolvableType actualType, ResolvableType elementType, ServerHttpRequest request,
ServerHttpResponse response, Map<String, Object> hints);
}

View File

@@ -1,57 +0,0 @@
/*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.http.codec;
import java.util.Map;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Mono;
import org.springframework.core.MethodParameter;
import org.springframework.core.ResolvableType;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.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> {
/**
* Encode and write the given object stream to the response.
*
* @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 actualType,
ResolvableType elementType, MediaType mediaType, ServerHttpRequest request,
ServerHttpResponse response, Map<String, Object> hints);
}

View File

@@ -37,14 +37,14 @@ import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
/**
* {@code ServerHttpMessageWriter} for {@code "text/event-stream"} responses.
* {@code HttpMessageWriter} for {@code "text/event-stream"} responses.
*
* @author Sebastien Deleuze
* @author Arjen Poutsma
* @author Rossen Stoyanchev
* @since 5.0
*/
public class ServerSentEventHttpMessageWriter implements ServerHttpMessageWriter<Object> {
public class ServerSentEventHttpMessageWriter implements HttpMessageWriter<Object> {
private static final List<MediaType> WRITABLE_MEDIA_TYPES =
Collections.singletonList(MediaType.TEXT_EVENT_STREAM);