Update Codec API to use Map<String, Object> for hints

Issue: SPR-14557
This commit is contained in:
Sebastien Deleuze
2016-09-12 11:08:01 +02:00
parent b88ed85994
commit b91867cf45
32 changed files with 212 additions and 173 deletions

View File

@@ -18,6 +18,7 @@ package org.springframework.core.codec;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Mono;
@@ -49,7 +50,7 @@ public abstract class AbstractDecoder<T> implements Decoder<T> {
}
@Override
public boolean canDecode(ResolvableType elementType, MimeType mimeType, Object... hints) {
public boolean canDecode(ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) {
if (mimeType == null) {
return true;
}
@@ -58,7 +59,7 @@ public abstract class AbstractDecoder<T> implements Decoder<T> {
@Override
public Mono<T> decodeToMono(Publisher<DataBuffer> inputStream, ResolvableType elementType,
MimeType mimeType, Object... hints) {
MimeType mimeType, Map<String, Object> hints) {
throw new UnsupportedOperationException();
}

View File

@@ -18,6 +18,7 @@ package org.springframework.core.codec;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.springframework.core.ResolvableType;
import org.springframework.util.MimeType;
@@ -45,7 +46,7 @@ public abstract class AbstractEncoder<T> implements Encoder<T> {
}
@Override
public boolean canEncode(ResolvableType elementType, MimeType mimeType, Object... hints) {
public boolean canEncode(ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) {
if (mimeType == null) {
return true;
}

View File

@@ -16,6 +16,8 @@
package org.springframework.core.codec;
import java.util.Map;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
@@ -41,13 +43,13 @@ public abstract class AbstractSingleValueEncoder<T> extends AbstractEncoder<T> {
@Override
public final Flux<DataBuffer> encode(Publisher<? extends T> inputStream, DataBufferFactory bufferFactory,
ResolvableType elementType, MimeType mimeType, Object... hints) {
ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) {
return Flux.from(inputStream).
take(1).
concatMap(t -> {
try {
return encode(t, bufferFactory, elementType, mimeType);
return encode(t, bufferFactory, elementType, mimeType, hints);
}
catch (Exception ex) {
return Flux.error(ex);
@@ -66,6 +68,6 @@ public abstract class AbstractSingleValueEncoder<T> extends AbstractEncoder<T> {
* @throws Exception in case of errors
*/
protected abstract Flux<DataBuffer> encode(T t, DataBufferFactory dataBufferFactory,
ResolvableType type, MimeType mimeType, Object... hints) throws Exception;
ResolvableType type, MimeType mimeType, Map<String, Object> hints) throws Exception;
}

View File

@@ -17,6 +17,7 @@
package org.springframework.core.codec;
import java.nio.ByteBuffer;
import java.util.Map;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
@@ -42,14 +43,14 @@ public class ByteBufferDecoder extends AbstractDecoder<ByteBuffer> {
@Override
public boolean canDecode(ResolvableType elementType, MimeType mimeType, Object... hints) {
public boolean canDecode(ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) {
Class<?> clazz = elementType.getRawClass();
return (super.canDecode(elementType, mimeType, hints) && ByteBuffer.class.isAssignableFrom(clazz));
}
@Override
public Flux<ByteBuffer> decode(Publisher<DataBuffer> inputStream, ResolvableType elementType,
MimeType mimeType, Object... hints) {
MimeType mimeType, Map<String, Object> hints) {
return Flux.from(inputStream).map((dataBuffer) -> {
ByteBuffer copy = ByteBuffer.allocate(dataBuffer.readableByteCount());

View File

@@ -17,6 +17,7 @@
package org.springframework.core.codec;
import java.nio.ByteBuffer;
import java.util.Map;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
@@ -41,7 +42,7 @@ public class ByteBufferEncoder extends AbstractEncoder<ByteBuffer> {
@Override
public boolean canEncode(ResolvableType elementType, MimeType mimeType, Object... hints) {
public boolean canEncode(ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) {
Class<?> clazz = elementType.getRawClass();
return (super.canEncode(elementType, mimeType, hints) && ByteBuffer.class.isAssignableFrom(clazz));
}
@@ -49,7 +50,7 @@ public class ByteBufferEncoder extends AbstractEncoder<ByteBuffer> {
@Override
public Flux<DataBuffer> encode(Publisher<? extends ByteBuffer> inputStream,
DataBufferFactory bufferFactory, ResolvableType elementType, MimeType mimeType,
Object... hints) {
Map<String, Object> hints) {
return Flux.from(inputStream).map(bufferFactory::wrap);
}

View File

@@ -20,6 +20,7 @@ import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
@@ -48,7 +49,7 @@ public class CharSequenceEncoder extends AbstractEncoder<CharSequence> {
@Override
public boolean canEncode(ResolvableType elementType, MimeType mimeType, Object... hints) {
public boolean canEncode(ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) {
Class<?> clazz = elementType.getRawClass();
return (super.canEncode(elementType, mimeType, hints) && CharSequence.class.isAssignableFrom(clazz));
}
@@ -56,7 +57,7 @@ public class CharSequenceEncoder extends AbstractEncoder<CharSequence> {
@Override
public Flux<DataBuffer> encode(Publisher<? extends CharSequence> inputStream,
DataBufferFactory bufferFactory, ResolvableType elementType,
MimeType mimeType, Object... hints) {
MimeType mimeType, Map<String, Object> hints) {
Charset charset;
if (mimeType != null && mimeType.getCharset() != null) {

View File

@@ -16,7 +16,9 @@
package org.springframework.core.codec;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
@@ -42,10 +44,10 @@ public interface Decoder<T> {
* type of the source stream.
* @param elementType the target element type for the output stream
* @param mimeType the mime type associated with the stream to decode
* @param hints additional information about how to do decode, optional
* @param hints additional information about how to do encode
* @return {@code true} if supported, {@code false} otherwise
*/
boolean canDecode(ResolvableType elementType, MimeType mimeType, Object... hints);
boolean canDecode(ResolvableType elementType, MimeType mimeType, Map<String, Object> hints);
/**
* Decode a {@link DataBuffer} input stream into a Flux of {@code T}.
@@ -54,11 +56,11 @@ public interface Decoder<T> {
* this type must have been previously passed to the {@link #canDecode}
* method and it must have returned {@code true}.
* @param mimeType the MIME type associated with the input stream, optional
* @param hints additional information about how to do decode, optional
* @param hints additional information about how to do encode
* @return the output stream with decoded elements
*/
Flux<T> decode(Publisher<DataBuffer> inputStream, ResolvableType elementType,
MimeType mimeType, Object... hints);
MimeType mimeType, Map<String, Object> hints);
/**
* Decode a {@link DataBuffer} input stream into a Mono of {@code T}.
@@ -67,11 +69,11 @@ public interface Decoder<T> {
* this type must have been previously passed to the {@link #canDecode}
* method and it must have returned {@code true}.
* @param mimeType the MIME type associated with the input stream, optional
* @param hints additional information about how to do decode, optional
* @param hints additional information about how to do encode
* @return the output stream with the decoded element
*/
Mono<T> decodeToMono(Publisher<DataBuffer> inputStream, ResolvableType elementType,
MimeType mimeType, Object... hints);
MimeType mimeType, Map<String, Object> hints);
/**
* Return the list of MIME types this decoder supports.

View File

@@ -16,7 +16,9 @@
package org.springframework.core.codec;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
@@ -43,10 +45,10 @@ public interface Encoder<T> {
* type for the output stream.
* @param elementType the type of elements in the source stream
* @param mimeType the MIME type for the output stream
* @param hints additional information about how to do encode, optional
* @param hints additional information about how to do encode
* @return {@code true} if supported, {@code false} otherwise
*/
boolean canEncode(ResolvableType elementType, MimeType mimeType, Object... hints);
boolean canEncode(ResolvableType elementType, MimeType mimeType, Map<String, Object> hints);
/**
* Encode a stream of Objects of type {@code T} into a {@link DataBuffer}
@@ -59,11 +61,11 @@ public interface Encoder<T> {
* this type must have been previously passed to the {@link #canEncode}
* method and it must have returned {@code true}.
* @param mimeType the MIME type for the output stream
* @param hints additional information about how to do encode, optional
* @param hints additional information about how to do encode
* @return the output stream
*/
Flux<DataBuffer> encode(Publisher<? extends T> inputStream, DataBufferFactory bufferFactory,
ResolvableType elementType, MimeType mimeType, Object... hints);
ResolvableType elementType, MimeType mimeType, Map<String, Object> hints);
/**
* Return the list of mime types this encoder supports.

View File

@@ -17,6 +17,7 @@
package org.springframework.core.codec;
import java.io.ByteArrayInputStream;
import java.util.Map;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
@@ -45,7 +46,7 @@ public class ResourceDecoder extends AbstractDecoder<Resource> {
@Override
public boolean canDecode(ResolvableType elementType, MimeType mimeType, Object... hints) {
public boolean canDecode(ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) {
Class<?> clazz = elementType.getRawClass();
return (InputStreamResource.class.equals(clazz) ||
clazz.isAssignableFrom(ByteArrayResource.class)) &&
@@ -54,7 +55,7 @@ public class ResourceDecoder extends AbstractDecoder<Resource> {
@Override
public Flux<Resource> decode(Publisher<DataBuffer> inputStream, ResolvableType elementType,
MimeType mimeType, Object... hints) {
MimeType mimeType, Map<String, Object> hints) {
Class<?> clazz = elementType.getRawClass();

View File

@@ -18,6 +18,7 @@ package org.springframework.core.codec;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import reactor.core.publisher.Flux;
@@ -56,14 +57,14 @@ public class ResourceEncoder extends AbstractSingleValueEncoder<Resource> {
@Override
public boolean canEncode(ResolvableType elementType, MimeType mimeType, Object... hints) {
public boolean canEncode(ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) {
Class<?> clazz = elementType.getRawClass();
return (super.canEncode(elementType, mimeType, hints) && Resource.class.isAssignableFrom(clazz));
}
@Override
protected Flux<DataBuffer> encode(Resource resource, DataBufferFactory dataBufferFactory,
ResolvableType type, MimeType mimeType, Object... hints) throws IOException {
ResolvableType type, MimeType mimeType, Map<String, Object> hints) throws IOException {
InputStream is = resource.getInputStream();
return DataBufferUtils.read(is, dataBufferFactory, bufferSize);

View File

@@ -21,6 +21,7 @@ import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.IntPredicate;
import org.reactivestreams.Publisher;
@@ -77,14 +78,14 @@ public class StringDecoder extends AbstractDecoder<String> {
@Override
public boolean canDecode(ResolvableType elementType, MimeType mimeType, Object... hints) {
public boolean canDecode(ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) {
return (super.canDecode(elementType, mimeType, hints) &&
String.class.equals(elementType.getRawClass()));
}
@Override
public Flux<String> decode(Publisher<DataBuffer> inputStream, ResolvableType elementType,
MimeType mimeType, Object... hints) {
MimeType mimeType, Map<String, Object> hints) {
Flux<DataBuffer> inputFlux = Flux.from(inputStream);
if (this.splitOnNewline) {
@@ -95,7 +96,7 @@ public class StringDecoder extends AbstractDecoder<String> {
@Override
public Mono<String> decodeToMono(Publisher<DataBuffer> inputStream, ResolvableType elementType,
MimeType mimeType, Object... hints) {
MimeType mimeType, Map<String, Object> hints) {
return Flux.from(inputStream)
.reduce(DataBuffer::write)