CodecConfigurer implementation refactoring
Backport of 9d65830133
See gh-24201
This commit is contained in:
@@ -95,6 +95,8 @@ public interface ClientCodecConfigurer extends CodecConfigurer {
|
||||
* <p>By default if this is not set, and Jackson is available, the
|
||||
* {@link #jackson2JsonDecoder} override is used instead. Use this property
|
||||
* if you want to further customize the SSE decoder.
|
||||
* <p>Note that {@link #maxInMemorySize(int)}, if configured, will be
|
||||
* applied to the given decoder.
|
||||
* @param decoder the decoder to use
|
||||
*/
|
||||
void serverSentEventDecoder(Decoder<?> decoder);
|
||||
|
||||
@@ -109,6 +109,8 @@ public interface CodecConfigurer {
|
||||
|
||||
/**
|
||||
* Override the default Jackson JSON {@code Decoder}.
|
||||
* <p>Note that {@link #maxInMemorySize(int)}, if configured, will be
|
||||
* applied to the given decoder.
|
||||
* @param decoder the decoder instance to use
|
||||
* @see org.springframework.http.codec.json.Jackson2JsonDecoder
|
||||
*/
|
||||
@@ -123,6 +125,8 @@ public interface CodecConfigurer {
|
||||
|
||||
/**
|
||||
* Override the default Protobuf {@code Decoder}.
|
||||
* <p>Note that {@link #maxInMemorySize(int)}, if configured, will be
|
||||
* applied to the given decoder.
|
||||
* @param decoder the decoder instance to use
|
||||
* @since 5.1
|
||||
* @see org.springframework.http.codec.protobuf.ProtobufDecoder
|
||||
@@ -140,6 +144,8 @@ public interface CodecConfigurer {
|
||||
|
||||
/**
|
||||
* Override the default JAXB2 {@code Decoder}.
|
||||
* <p>Note that {@link #maxInMemorySize(int)}, if configured, will be
|
||||
* applied to the given decoder.
|
||||
* @param decoder the decoder instance to use
|
||||
* @since 5.1.3
|
||||
* @see org.springframework.http.codec.xml.Jaxb2XmlDecoder
|
||||
@@ -246,7 +252,8 @@ public interface CodecConfigurer {
|
||||
* Whether to log form data at DEBUG level, and headers at TRACE level.
|
||||
* Both may contain sensitive information.
|
||||
*/
|
||||
boolean isEnableLoggingRequestDetails();
|
||||
@Nullable
|
||||
Boolean isEnableLoggingRequestDetails();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -91,6 +91,9 @@ public interface ServerCodecConfigurer extends CodecConfigurer {
|
||||
* MultipartHttpMessageReader} created with an instance of
|
||||
* {@link org.springframework.http.codec.multipart.SynchronossPartHttpMessageReader
|
||||
* SynchronossPartHttpMessageReader}.
|
||||
* <p>Note that {@link #maxInMemorySize(int)} and/or
|
||||
* {@link #enableLoggingRequestDetails(boolean)}, if configured, will be
|
||||
* applied to the given reader, if applicable.
|
||||
* @param reader the message reader to use for multipart requests.
|
||||
* @since 5.1.11
|
||||
*/
|
||||
|
||||
@@ -149,6 +149,16 @@ public class MultipartHttpMessageWriter extends LoggingCodecSupport
|
||||
return Collections.unmodifiableList(this.partWriters);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the configured form writer.
|
||||
* @since 5.1.13
|
||||
*/
|
||||
@Nullable
|
||||
public HttpMessageWriter<MultiValueMap<String, String>> getFormWriter() {
|
||||
return this.formWriter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the character set to use for part headers such as
|
||||
* "Content-Disposition" (and its filename parameter).
|
||||
|
||||
@@ -36,14 +36,19 @@ import org.springframework.http.codec.CodecConfigurer;
|
||||
import org.springframework.http.codec.DecoderHttpMessageReader;
|
||||
import org.springframework.http.codec.EncoderHttpMessageWriter;
|
||||
import org.springframework.http.codec.FormHttpMessageReader;
|
||||
import org.springframework.http.codec.FormHttpMessageWriter;
|
||||
import org.springframework.http.codec.HttpMessageReader;
|
||||
import org.springframework.http.codec.HttpMessageWriter;
|
||||
import org.springframework.http.codec.ResourceHttpMessageWriter;
|
||||
import org.springframework.http.codec.ServerSentEventHttpMessageReader;
|
||||
import org.springframework.http.codec.json.AbstractJackson2Decoder;
|
||||
import org.springframework.http.codec.json.Jackson2JsonDecoder;
|
||||
import org.springframework.http.codec.json.Jackson2JsonEncoder;
|
||||
import org.springframework.http.codec.json.Jackson2SmileDecoder;
|
||||
import org.springframework.http.codec.json.Jackson2SmileEncoder;
|
||||
import org.springframework.http.codec.multipart.MultipartHttpMessageReader;
|
||||
import org.springframework.http.codec.multipart.MultipartHttpMessageWriter;
|
||||
import org.springframework.http.codec.multipart.SynchronossPartHttpMessageReader;
|
||||
import org.springframework.http.codec.protobuf.ProtobufDecoder;
|
||||
import org.springframework.http.codec.protobuf.ProtobufEncoder;
|
||||
import org.springframework.http.codec.protobuf.ProtobufHttpMessageWriter;
|
||||
@@ -69,6 +74,8 @@ class BaseDefaultCodecs implements CodecConfigurer.DefaultCodecs, CodecConfigure
|
||||
|
||||
private static final boolean protobufPresent;
|
||||
|
||||
static final boolean synchronossMultipartPresent;
|
||||
|
||||
static {
|
||||
ClassLoader classLoader = BaseCodecConfigurer.class.getClassLoader();
|
||||
jackson2Present = ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper", classLoader) &&
|
||||
@@ -76,6 +83,7 @@ class BaseDefaultCodecs implements CodecConfigurer.DefaultCodecs, CodecConfigure
|
||||
jackson2SmilePresent = ClassUtils.isPresent("com.fasterxml.jackson.dataformat.smile.SmileFactory", classLoader);
|
||||
jaxb2Present = ClassUtils.isPresent("javax.xml.bind.Binder", classLoader);
|
||||
protobufPresent = ClassUtils.isPresent("com.google.protobuf.Message", classLoader);
|
||||
synchronossMultipartPresent = ClassUtils.isPresent("org.synchronoss.cloud.nio.multipart.NioMultipartParser", classLoader);
|
||||
}
|
||||
|
||||
|
||||
@@ -100,7 +108,8 @@ class BaseDefaultCodecs implements CodecConfigurer.DefaultCodecs, CodecConfigure
|
||||
@Nullable
|
||||
private Integer maxInMemorySize;
|
||||
|
||||
private boolean enableLoggingRequestDetails = false;
|
||||
@Nullable
|
||||
private Boolean enableLoggingRequestDetails;
|
||||
|
||||
private boolean registerDefaults = true;
|
||||
|
||||
@@ -170,7 +179,8 @@ class BaseDefaultCodecs implements CodecConfigurer.DefaultCodecs, CodecConfigure
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEnableLoggingRequestDetails() {
|
||||
@Nullable
|
||||
public Boolean isEnableLoggingRequestDetails() {
|
||||
return this.enableLoggingRequestDetails;
|
||||
}
|
||||
|
||||
@@ -190,48 +200,107 @@ class BaseDefaultCodecs implements CodecConfigurer.DefaultCodecs, CodecConfigure
|
||||
return Collections.emptyList();
|
||||
}
|
||||
List<HttpMessageReader<?>> readers = new ArrayList<>();
|
||||
readers.add(new DecoderHttpMessageReader<>(init(new ByteArrayDecoder())));
|
||||
readers.add(new DecoderHttpMessageReader<>(init(new ByteBufferDecoder())));
|
||||
readers.add(new DecoderHttpMessageReader<>(init(new DataBufferDecoder())));
|
||||
readers.add(new DecoderHttpMessageReader<>(init(new ResourceDecoder())));
|
||||
readers.add(new DecoderHttpMessageReader<>(init(StringDecoder.textPlainOnly())));
|
||||
addCodec(readers, new DecoderHttpMessageReader<>(new ByteArrayDecoder()));
|
||||
addCodec(readers, new DecoderHttpMessageReader<>(new ByteBufferDecoder()));
|
||||
addCodec(readers, new DecoderHttpMessageReader<>(new DataBufferDecoder()));
|
||||
addCodec(readers, new DecoderHttpMessageReader<>(new ResourceDecoder()));
|
||||
addCodec(readers, new DecoderHttpMessageReader<>(StringDecoder.textPlainOnly()));
|
||||
if (protobufPresent) {
|
||||
Decoder<?> decoder = this.protobufDecoder != null ? this.protobufDecoder : init(new ProtobufDecoder());
|
||||
readers.add(new DecoderHttpMessageReader<>(decoder));
|
||||
Decoder<?> decoder = this.protobufDecoder != null ? this.protobufDecoder : new ProtobufDecoder();
|
||||
addCodec(readers, new DecoderHttpMessageReader<>(decoder));
|
||||
}
|
||||
addCodec(readers, new FormHttpMessageReader());
|
||||
|
||||
FormHttpMessageReader formReader = new FormHttpMessageReader();
|
||||
if (this.maxInMemorySize != null) {
|
||||
formReader.setMaxInMemorySize(this.maxInMemorySize);
|
||||
}
|
||||
formReader.setEnableLoggingRequestDetails(this.enableLoggingRequestDetails);
|
||||
readers.add(formReader);
|
||||
|
||||
// client vs server..
|
||||
extendTypedReaders(readers);
|
||||
|
||||
return readers;
|
||||
}
|
||||
|
||||
private <T extends Decoder<?>> T init(T decoder) {
|
||||
if (this.maxInMemorySize != null) {
|
||||
if (decoder instanceof AbstractDataBufferDecoder) {
|
||||
((AbstractDataBufferDecoder<?>) decoder).setMaxInMemorySize(this.maxInMemorySize);
|
||||
/**
|
||||
* Initialize a codec and add it to the List.
|
||||
* @since 5.1.13
|
||||
*/
|
||||
protected <T> void addCodec(List<T> codecs, T codec) {
|
||||
initCodec(codec);
|
||||
codecs.add(codec);
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply {@link #maxInMemorySize()} and {@link #enableLoggingRequestDetails},
|
||||
* if configured by the application, to the given codec , including any
|
||||
* codec it contains.
|
||||
*/
|
||||
private void initCodec(@Nullable Object codec) {
|
||||
|
||||
if (codec instanceof DecoderHttpMessageReader) {
|
||||
codec = ((DecoderHttpMessageReader) codec).getDecoder();
|
||||
}
|
||||
else if (codec instanceof ServerSentEventHttpMessageReader) {
|
||||
codec = ((ServerSentEventHttpMessageReader) codec).getDecoder();
|
||||
}
|
||||
|
||||
if (codec == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Integer size = this.maxInMemorySize;
|
||||
if (size != null) {
|
||||
if (codec instanceof AbstractDataBufferDecoder) {
|
||||
((AbstractDataBufferDecoder<?>) codec).setMaxInMemorySize(size);
|
||||
}
|
||||
if (decoder instanceof ProtobufDecoder) {
|
||||
((ProtobufDecoder) decoder).setMaxMessageSize(this.maxInMemorySize);
|
||||
if (protobufPresent) {
|
||||
if (codec instanceof ProtobufDecoder) {
|
||||
((ProtobufDecoder) codec).setMaxMessageSize(size);
|
||||
}
|
||||
}
|
||||
if (jackson2Present) {
|
||||
if (decoder instanceof AbstractJackson2Decoder) {
|
||||
((AbstractJackson2Decoder) decoder).setMaxInMemorySize(this.maxInMemorySize);
|
||||
if (codec instanceof AbstractJackson2Decoder) {
|
||||
((AbstractJackson2Decoder) codec).setMaxInMemorySize(size);
|
||||
}
|
||||
}
|
||||
if (jaxb2Present) {
|
||||
if (decoder instanceof Jaxb2XmlDecoder) {
|
||||
((Jaxb2XmlDecoder) decoder).setMaxInMemorySize(this.maxInMemorySize);
|
||||
if (codec instanceof Jaxb2XmlDecoder) {
|
||||
((Jaxb2XmlDecoder) codec).setMaxInMemorySize(size);
|
||||
}
|
||||
}
|
||||
if (codec instanceof FormHttpMessageReader) {
|
||||
((FormHttpMessageReader) codec).setMaxInMemorySize(size);
|
||||
}
|
||||
if (synchronossMultipartPresent) {
|
||||
if (codec instanceof SynchronossPartHttpMessageReader) {
|
||||
((SynchronossPartHttpMessageReader) codec).setMaxInMemorySize(size);
|
||||
}
|
||||
}
|
||||
}
|
||||
return decoder;
|
||||
|
||||
Boolean enable = this.enableLoggingRequestDetails;
|
||||
if (enable != null) {
|
||||
if (codec instanceof FormHttpMessageReader) {
|
||||
((FormHttpMessageReader) codec).setEnableLoggingRequestDetails(enable);
|
||||
}
|
||||
if (codec instanceof MultipartHttpMessageReader) {
|
||||
((MultipartHttpMessageReader) codec).setEnableLoggingRequestDetails(enable);
|
||||
}
|
||||
if (synchronossMultipartPresent) {
|
||||
if (codec instanceof SynchronossPartHttpMessageReader) {
|
||||
((SynchronossPartHttpMessageReader) codec).setEnableLoggingRequestDetails(enable);
|
||||
}
|
||||
}
|
||||
if (codec instanceof FormHttpMessageWriter) {
|
||||
((FormHttpMessageWriter) codec).setEnableLoggingRequestDetails(enable);
|
||||
}
|
||||
if (codec instanceof MultipartHttpMessageWriter) {
|
||||
((MultipartHttpMessageWriter) codec).setEnableLoggingRequestDetails(enable);
|
||||
}
|
||||
}
|
||||
|
||||
if (codec instanceof MultipartHttpMessageReader) {
|
||||
initCodec(((MultipartHttpMessageReader) codec).getPartReader());
|
||||
}
|
||||
else if (codec instanceof MultipartHttpMessageWriter) {
|
||||
initCodec(((MultipartHttpMessageWriter) codec).getFormWriter());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -249,16 +318,19 @@ class BaseDefaultCodecs implements CodecConfigurer.DefaultCodecs, CodecConfigure
|
||||
}
|
||||
List<HttpMessageReader<?>> readers = new ArrayList<>();
|
||||
if (jackson2Present) {
|
||||
readers.add(new DecoderHttpMessageReader<>(init(getJackson2JsonDecoder())));
|
||||
addCodec(readers, new DecoderHttpMessageReader<>(getJackson2JsonDecoder()));
|
||||
}
|
||||
if (jackson2SmilePresent) {
|
||||
readers.add(new DecoderHttpMessageReader<>(init(new Jackson2SmileDecoder())));
|
||||
addCodec(readers, new DecoderHttpMessageReader<>(new Jackson2SmileDecoder()));
|
||||
}
|
||||
if (jaxb2Present) {
|
||||
Decoder<?> decoder = this.jaxb2Decoder != null ? this.jaxb2Decoder : init(new Jaxb2XmlDecoder());
|
||||
readers.add(new DecoderHttpMessageReader<>(decoder));
|
||||
Decoder<?> decoder = this.jaxb2Decoder != null ? this.jaxb2Decoder : new Jaxb2XmlDecoder();
|
||||
addCodec(readers, new DecoderHttpMessageReader<>(decoder));
|
||||
}
|
||||
|
||||
// client vs server..
|
||||
extendObjectReaders(readers);
|
||||
|
||||
return readers;
|
||||
}
|
||||
|
||||
@@ -275,9 +347,9 @@ class BaseDefaultCodecs implements CodecConfigurer.DefaultCodecs, CodecConfigure
|
||||
if (!this.registerDefaults) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
List<HttpMessageReader<?>> result = new ArrayList<>();
|
||||
result.add(new DecoderHttpMessageReader<>(init(StringDecoder.allMimeTypes())));
|
||||
return result;
|
||||
List<HttpMessageReader<?>> readers = new ArrayList<>();
|
||||
addCodec(readers, new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes()));
|
||||
return readers;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -364,11 +436,17 @@ class BaseDefaultCodecs implements CodecConfigurer.DefaultCodecs, CodecConfigure
|
||||
// Accessors for use in subclasses...
|
||||
|
||||
protected Decoder<?> getJackson2JsonDecoder() {
|
||||
return (this.jackson2JsonDecoder != null ? this.jackson2JsonDecoder : new Jackson2JsonDecoder());
|
||||
if (this.jackson2JsonDecoder == null) {
|
||||
this.jackson2JsonDecoder = new Jackson2JsonDecoder();
|
||||
}
|
||||
return this.jackson2JsonDecoder;
|
||||
}
|
||||
|
||||
protected Encoder<?> getJackson2JsonEncoder() {
|
||||
return (this.jackson2JsonEncoder != null ? this.jackson2JsonEncoder : new Jackson2JsonEncoder());
|
||||
if (this.jackson2JsonEncoder == null) {
|
||||
this.jackson2JsonEncoder = new Jackson2JsonEncoder();
|
||||
}
|
||||
return this.jackson2JsonEncoder;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -95,24 +95,17 @@ class ClientDefaultCodecsImpl extends BaseDefaultCodecs implements ClientCodecCo
|
||||
|
||||
@Override
|
||||
protected void extendObjectReaders(List<HttpMessageReader<?>> objectReaders) {
|
||||
objectReaders.add(new ServerSentEventHttpMessageReader(getSseDecoder()));
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private Decoder<?> getSseDecoder() {
|
||||
return (this.sseDecoder != null ? this.sseDecoder : jackson2Present ? getJackson2JsonDecoder() : null);
|
||||
Decoder<?> decoder = (this.sseDecoder != null ?
|
||||
this.sseDecoder :
|
||||
jackson2Present ? getJackson2JsonDecoder() : null);
|
||||
|
||||
addCodec(objectReaders, new ServerSentEventHttpMessageReader(decoder));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void extendTypedWriters(List<HttpMessageWriter<?>> typedWriters) {
|
||||
|
||||
FormHttpMessageWriter formWriter = new FormHttpMessageWriter();
|
||||
formWriter.setEnableLoggingRequestDetails(isEnableLoggingRequestDetails());
|
||||
|
||||
MultipartHttpMessageWriter multipartWriter = new MultipartHttpMessageWriter(getPartWriters(), formWriter);
|
||||
multipartWriter.setEnableLoggingRequestDetails(isEnableLoggingRequestDetails());
|
||||
|
||||
typedWriters.add(multipartWriter);
|
||||
addCodec(typedWriters, new MultipartHttpMessageWriter(getPartWriters(), new FormHttpMessageWriter()));
|
||||
}
|
||||
|
||||
private List<HttpMessageWriter<?>> getPartWriters() {
|
||||
|
||||
@@ -25,7 +25,6 @@ import org.springframework.http.codec.ServerSentEventHttpMessageWriter;
|
||||
import org.springframework.http.codec.multipart.MultipartHttpMessageReader;
|
||||
import org.springframework.http.codec.multipart.SynchronossPartHttpMessageReader;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
* Default implementation of {@link ServerCodecConfigurer.ServerDefaultCodecs}.
|
||||
@@ -34,11 +33,6 @@ import org.springframework.util.ClassUtils;
|
||||
*/
|
||||
class ServerDefaultCodecsImpl extends BaseDefaultCodecs implements ServerCodecConfigurer.ServerDefaultCodecs {
|
||||
|
||||
private static final boolean synchronossMultipartPresent =
|
||||
ClassUtils.isPresent("org.synchronoss.cloud.nio.multipart.NioMultipartParser",
|
||||
DefaultServerCodecConfigurer.class.getClassLoader());
|
||||
|
||||
|
||||
@Nullable
|
||||
private HttpMessageReader<?> multipartReader;
|
||||
|
||||
@@ -70,23 +64,13 @@ class ServerDefaultCodecsImpl extends BaseDefaultCodecs implements ServerCodecCo
|
||||
@Override
|
||||
protected void extendTypedReaders(List<HttpMessageReader<?>> typedReaders) {
|
||||
if (this.multipartReader != null) {
|
||||
typedReaders.add(this.multipartReader);
|
||||
addCodec(typedReaders, this.multipartReader);
|
||||
return;
|
||||
}
|
||||
if (synchronossMultipartPresent) {
|
||||
boolean enable = isEnableLoggingRequestDetails();
|
||||
|
||||
SynchronossPartHttpMessageReader partReader = new SynchronossPartHttpMessageReader();
|
||||
Integer size = maxInMemorySize();
|
||||
if (size != null) {
|
||||
partReader.setMaxInMemorySize(size);
|
||||
}
|
||||
partReader.setEnableLoggingRequestDetails(enable);
|
||||
typedReaders.add(partReader);
|
||||
|
||||
MultipartHttpMessageReader reader = new MultipartHttpMessageReader(partReader);
|
||||
reader.setEnableLoggingRequestDetails(enable);
|
||||
typedReaders.add(reader);
|
||||
addCodec(typedReaders, partReader);
|
||||
addCodec(typedReaders, new MultipartHttpMessageReader(partReader));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -22,7 +22,6 @@ import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.junit.Test;
|
||||
import reactor.core.publisher.Flux;
|
||||
@@ -45,6 +44,7 @@ import org.springframework.http.codec.ClientCodecConfigurer;
|
||||
import org.springframework.http.codec.DecoderHttpMessageReader;
|
||||
import org.springframework.http.codec.EncoderHttpMessageWriter;
|
||||
import org.springframework.http.codec.FormHttpMessageReader;
|
||||
import org.springframework.http.codec.FormHttpMessageWriter;
|
||||
import org.springframework.http.codec.HttpMessageReader;
|
||||
import org.springframework.http.codec.HttpMessageWriter;
|
||||
import org.springframework.http.codec.ResourceHttpMessageWriter;
|
||||
@@ -119,12 +119,45 @@ public class ClientCodecConfigurerTests {
|
||||
Jackson2JsonDecoder decoder = new Jackson2JsonDecoder();
|
||||
this.configurer.defaultCodecs().jackson2JsonDecoder(decoder);
|
||||
|
||||
assertSame(decoder, this.configurer.getReaders().stream()
|
||||
.filter(reader -> ServerSentEventHttpMessageReader.class.equals(reader.getClass()))
|
||||
.map(reader -> (ServerSentEventHttpMessageReader) reader)
|
||||
.findFirst()
|
||||
.map(ServerSentEventHttpMessageReader::getDecoder)
|
||||
.filter(e -> e == decoder).orElse(null));
|
||||
List<HttpMessageReader<?>> readers = this.configurer.getReaders();
|
||||
assertSame(decoder, findCodec(readers, ServerSentEventHttpMessageReader.class).getDecoder());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void maxInMemorySize() {
|
||||
int size = 99;
|
||||
this.configurer.defaultCodecs().maxInMemorySize(size);
|
||||
List<HttpMessageReader<?>> readers = this.configurer.getReaders();
|
||||
assertEquals(12, readers.size());
|
||||
assertEquals(size, ((ByteArrayDecoder) getNextDecoder(readers)).getMaxInMemorySize());
|
||||
assertEquals(size, ((ByteBufferDecoder) getNextDecoder(readers)).getMaxInMemorySize());
|
||||
assertEquals(size, ((DataBufferDecoder) getNextDecoder(readers)).getMaxInMemorySize());
|
||||
assertEquals(size, ((ResourceDecoder) getNextDecoder(readers)).getMaxInMemorySize());
|
||||
assertEquals(size, ((StringDecoder) getNextDecoder(readers)).getMaxInMemorySize());
|
||||
assertEquals(size, ((ProtobufDecoder) getNextDecoder(readers)).getMaxMessageSize());
|
||||
assertEquals(size, ((FormHttpMessageReader) nextReader(readers)).getMaxInMemorySize());
|
||||
|
||||
assertEquals(size, ((Jackson2JsonDecoder) getNextDecoder(readers)).getMaxInMemorySize());
|
||||
assertEquals(size, ((Jackson2SmileDecoder) getNextDecoder(readers)).getMaxInMemorySize());
|
||||
assertEquals(size, ((Jaxb2XmlDecoder) getNextDecoder(readers)).getMaxInMemorySize());
|
||||
|
||||
ServerSentEventHttpMessageReader reader = (ServerSentEventHttpMessageReader) nextReader(readers);
|
||||
assertEquals(size, ((Jackson2JsonDecoder) reader.getDecoder()).getMaxInMemorySize());
|
||||
|
||||
assertEquals(size, ((StringDecoder) getNextDecoder(readers)).getMaxInMemorySize());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void enableLoggingRequestDetails() {
|
||||
this.configurer.defaultCodecs().enableLoggingRequestDetails(true);
|
||||
|
||||
List<HttpMessageWriter<?>> writers = this.configurer.getWriters();
|
||||
MultipartHttpMessageWriter multipartWriter = findCodec(writers, MultipartHttpMessageWriter.class);
|
||||
assertTrue(multipartWriter.isEnableLoggingRequestDetails());
|
||||
|
||||
FormHttpMessageWriter formWriter = (FormHttpMessageWriter) multipartWriter.getFormWriter();
|
||||
assertNotNull(formWriter);
|
||||
assertTrue(formWriter.isEnableLoggingRequestDetails());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -138,35 +171,19 @@ public class ClientCodecConfigurerTests {
|
||||
|
||||
// Clone has the customizations
|
||||
|
||||
Decoder<?> sseDecoder = clone.getReaders().stream()
|
||||
.filter(reader -> reader instanceof ServerSentEventHttpMessageReader)
|
||||
.map(reader -> ((ServerSentEventHttpMessageReader) reader).getDecoder())
|
||||
.findFirst()
|
||||
.get();
|
||||
|
||||
List<HttpMessageWriter<?>> multipartWriters = clone.getWriters().stream()
|
||||
.filter(writer -> writer instanceof MultipartHttpMessageWriter)
|
||||
.flatMap(writer -> ((MultipartHttpMessageWriter) writer).getPartWriters().stream())
|
||||
.collect(Collectors.toList());
|
||||
Decoder<?> sseDecoder = findCodec(clone.getReaders(), ServerSentEventHttpMessageReader.class).getDecoder();
|
||||
List<HttpMessageWriter<?>> writers = findCodec(clone.getWriters(), MultipartHttpMessageWriter.class).getPartWriters();
|
||||
|
||||
assertSame(jackson2Decoder, sseDecoder);
|
||||
assertEquals(2, multipartWriters.size());
|
||||
assertEquals(2, writers.size());
|
||||
|
||||
// Original does not have the customizations
|
||||
|
||||
sseDecoder = this.configurer.getReaders().stream()
|
||||
.filter(reader -> reader instanceof ServerSentEventHttpMessageReader)
|
||||
.map(reader -> ((ServerSentEventHttpMessageReader) reader).getDecoder())
|
||||
.findFirst()
|
||||
.get();
|
||||
|
||||
multipartWriters = this.configurer.getWriters().stream()
|
||||
.filter(writer -> writer instanceof MultipartHttpMessageWriter)
|
||||
.flatMap(writer -> ((MultipartHttpMessageWriter) writer).getPartWriters().stream())
|
||||
.collect(Collectors.toList());
|
||||
sseDecoder = findCodec(this.configurer.getReaders(), ServerSentEventHttpMessageReader.class).getDecoder();
|
||||
writers = findCodec(this.configurer.getWriters(), MultipartHttpMessageWriter.class).getPartWriters();
|
||||
|
||||
assertNotSame(jackson2Decoder, sseDecoder);
|
||||
assertEquals(10, multipartWriters.size());
|
||||
assertEquals(10, writers.size());
|
||||
}
|
||||
|
||||
private Decoder<?> getNextDecoder(List<HttpMessageReader<?>> readers) {
|
||||
@@ -175,12 +192,21 @@ public class ClientCodecConfigurerTests {
|
||||
return ((DecoderHttpMessageReader<?>) reader).getDecoder();
|
||||
}
|
||||
|
||||
private HttpMessageReader<?> nextReader(List<HttpMessageReader<?>> readers) {
|
||||
return readers.get(this.index.getAndIncrement());
|
||||
}
|
||||
|
||||
private Encoder<?> getNextEncoder(List<HttpMessageWriter<?>> writers) {
|
||||
HttpMessageWriter<?> writer = writers.get(this.index.getAndIncrement());
|
||||
assertEquals(EncoderHttpMessageWriter.class, writer.getClass());
|
||||
return ((EncoderHttpMessageWriter<?>) writer).getEncoder();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private <T> T findCodec(List<?> codecs, Class<T> type) {
|
||||
return (T) codecs.stream().filter(type::isInstance).findFirst().get();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void assertStringDecoder(Decoder<?> decoder, boolean textOnly) {
|
||||
assertEquals(StringDecoder.class, decoder.getClass());
|
||||
|
||||
@@ -153,6 +153,20 @@ public class ServerCodecConfigurerTests {
|
||||
assertEquals(size, ((StringDecoder) getNextDecoder(readers)).getMaxInMemorySize());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void enableRequestLoggingDetails() {
|
||||
this.configurer.defaultCodecs().enableLoggingRequestDetails(true);
|
||||
|
||||
List<HttpMessageReader<?>> readers = this.configurer.getReaders();
|
||||
assertTrue(findCodec(readers, FormHttpMessageReader.class).isEnableLoggingRequestDetails());
|
||||
|
||||
MultipartHttpMessageReader multipartReader = findCodec(readers, MultipartHttpMessageReader.class);
|
||||
assertTrue(multipartReader.isEnableLoggingRequestDetails());
|
||||
|
||||
SynchronossPartHttpMessageReader reader = (SynchronossPartHttpMessageReader) multipartReader.getPartReader();
|
||||
assertTrue(reader.isEnableLoggingRequestDetails());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cloneConfigurer() {
|
||||
ServerCodecConfigurer clone = this.configurer.clone();
|
||||
@@ -164,37 +178,22 @@ public class ServerCodecConfigurerTests {
|
||||
|
||||
// Clone has the customizations
|
||||
|
||||
HttpMessageReader<?> actualReader = clone.getReaders().stream()
|
||||
.filter(r -> r instanceof MultipartHttpMessageReader)
|
||||
.findFirst()
|
||||
.get();
|
||||
|
||||
Encoder<?> actualEncoder = clone.getWriters().stream()
|
||||
.filter(writer -> writer instanceof ServerSentEventHttpMessageWriter)
|
||||
.map(writer -> ((ServerSentEventHttpMessageWriter) writer).getEncoder())
|
||||
.findFirst()
|
||||
.get();
|
||||
HttpMessageReader<?> actualReader =
|
||||
findCodec(clone.getReaders(), MultipartHttpMessageReader.class);
|
||||
|
||||
ServerSentEventHttpMessageWriter actualWriter =
|
||||
findCodec(clone.getWriters(), ServerSentEventHttpMessageWriter.class);
|
||||
|
||||
assertSame(reader, actualReader);
|
||||
assertSame(encoder, actualEncoder);
|
||||
assertSame(encoder, actualWriter.getEncoder());
|
||||
|
||||
// Original does not have the customizations
|
||||
|
||||
actualReader = this.configurer.getReaders().stream()
|
||||
.filter(r -> r instanceof MultipartHttpMessageReader)
|
||||
.findFirst()
|
||||
.get();
|
||||
|
||||
actualEncoder = this.configurer.getWriters().stream()
|
||||
.filter(writer -> writer instanceof ServerSentEventHttpMessageWriter)
|
||||
.map(writer -> ((ServerSentEventHttpMessageWriter) writer).getEncoder())
|
||||
.findFirst()
|
||||
.get();
|
||||
|
||||
actualReader = findCodec(this.configurer.getReaders(), MultipartHttpMessageReader.class);
|
||||
actualWriter = findCodec(this.configurer.getWriters(), ServerSentEventHttpMessageWriter.class);
|
||||
|
||||
assertNotSame(reader, actualReader);
|
||||
assertNotSame(encoder, actualEncoder);
|
||||
assertNotSame(encoder, actualWriter.getEncoder());
|
||||
}
|
||||
|
||||
private Decoder<?> getNextDecoder(List<HttpMessageReader<?>> readers) {
|
||||
@@ -213,6 +212,11 @@ public class ServerCodecConfigurerTests {
|
||||
return ((EncoderHttpMessageWriter<?>) writer).getEncoder();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private <T> T findCodec(List<?> codecs, Class<T> type) {
|
||||
return (T) codecs.stream().filter(type::isInstance).findFirst().get();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void assertStringDecoder(Decoder<?> decoder, boolean textOnly) {
|
||||
assertEquals(StringDecoder.class, decoder.getClass());
|
||||
|
||||
Reference in New Issue
Block a user