|
20 | 20 | import java.util.Collections;
|
21 | 21 | import java.util.List;
|
22 | 22 | import java.util.Map;
|
| 23 | +import java.util.function.Consumer; |
23 | 24 |
|
24 | 25 | import org.springframework.core.SpringProperties;
|
25 | 26 | import org.springframework.core.codec.AbstractDataBufferDecoder;
|
|
46 | 47 | import org.springframework.http.codec.ResourceHttpMessageReader;
|
47 | 48 | import org.springframework.http.codec.ResourceHttpMessageWriter;
|
48 | 49 | import org.springframework.http.codec.ServerSentEventHttpMessageReader;
|
| 50 | +import org.springframework.http.codec.ServerSentEventHttpMessageWriter; |
49 | 51 | import org.springframework.http.codec.json.AbstractJackson2Decoder;
|
50 | 52 | import org.springframework.http.codec.json.Jackson2JsonDecoder;
|
51 | 53 | import org.springframework.http.codec.json.Jackson2JsonEncoder;
|
@@ -139,6 +141,9 @@ class BaseDefaultCodecs implements CodecConfigurer.DefaultCodecs, CodecConfigure
|
139 | 141 | @Nullable
|
140 | 142 | private Encoder<?> kotlinSerializationJsonEncoder;
|
141 | 143 |
|
| 144 | + @Nullable |
| 145 | + private Consumer<Object> codecConsumer; |
| 146 | + |
142 | 147 | @Nullable
|
143 | 148 | private Integer maxInMemorySize;
|
144 | 149 |
|
@@ -196,6 +201,7 @@ protected BaseDefaultCodecs(BaseDefaultCodecs other) {
|
196 | 201 | this.jaxb2Encoder = other.jaxb2Encoder;
|
197 | 202 | this.kotlinSerializationJsonDecoder = other.kotlinSerializationJsonDecoder;
|
198 | 203 | this.kotlinSerializationJsonEncoder = other.kotlinSerializationJsonEncoder;
|
| 204 | + this.codecConsumer = other.codecConsumer; |
199 | 205 | this.maxInMemorySize = other.maxInMemorySize;
|
200 | 206 | this.enableLoggingRequestDetails = other.enableLoggingRequestDetails;
|
201 | 207 | this.registerDefaults = other.registerDefaults;
|
@@ -265,6 +271,14 @@ public void kotlinSerializationJsonEncoder(Encoder<?> encoder) {
|
265 | 271 | initObjectWriters();
|
266 | 272 | }
|
267 | 273 |
|
| 274 | + @Override |
| 275 | + public void configureDefaultCodec(Consumer<Object> codecConsumer) { |
| 276 | + this.codecConsumer = (this.codecConsumer != null ? |
| 277 | + this.codecConsumer.andThen(codecConsumer) : codecConsumer); |
| 278 | + initReaders(); |
| 279 | + initWriters(); |
| 280 | + } |
| 281 | + |
268 | 282 | @Override
|
269 | 283 | public void maxInMemorySize(int byteCount) {
|
270 | 284 | if (!ObjectUtils.nullSafeEquals(this.maxInMemorySize, byteCount)) {
|
@@ -359,6 +373,9 @@ private void initCodec(@Nullable Object codec) {
|
359 | 373 | if (codec instanceof DecoderHttpMessageReader) {
|
360 | 374 | codec = ((DecoderHttpMessageReader) codec).getDecoder();
|
361 | 375 | }
|
| 376 | + else if (codec instanceof EncoderHttpMessageWriter) { |
| 377 | + codec = ((EncoderHttpMessageWriter<?>) codec).getEncoder(); |
| 378 | + } |
362 | 379 |
|
363 | 380 | if (codec == null) {
|
364 | 381 | return;
|
@@ -394,7 +411,6 @@ private void initCodec(@Nullable Object codec) {
|
394 | 411 | }
|
395 | 412 | if (codec instanceof ServerSentEventHttpMessageReader) {
|
396 | 413 | ((ServerSentEventHttpMessageReader) codec).setMaxInMemorySize(size);
|
397 |
| - initCodec(((ServerSentEventHttpMessageReader) codec).getDecoder()); |
398 | 414 | }
|
399 | 415 | if (codec instanceof DefaultPartHttpMessageReader) {
|
400 | 416 | ((DefaultPartHttpMessageReader) codec).setMaxInMemorySize(size);
|
@@ -430,12 +446,23 @@ private void initCodec(@Nullable Object codec) {
|
430 | 446 | }
|
431 | 447 | }
|
432 | 448 |
|
| 449 | + if (this.codecConsumer != null) { |
| 450 | + this.codecConsumer.accept(codec); |
| 451 | + } |
| 452 | + |
| 453 | + // Recurse for nested codecs |
433 | 454 | if (codec instanceof MultipartHttpMessageReader) {
|
434 | 455 | initCodec(((MultipartHttpMessageReader) codec).getPartReader());
|
435 | 456 | }
|
436 | 457 | else if (codec instanceof MultipartHttpMessageWriter) {
|
437 | 458 | initCodec(((MultipartHttpMessageWriter) codec).getFormWriter());
|
438 | 459 | }
|
| 460 | + else if (codec instanceof ServerSentEventHttpMessageReader) { |
| 461 | + initCodec(((ServerSentEventHttpMessageReader) codec).getDecoder()); |
| 462 | + } |
| 463 | + else if (codec instanceof ServerSentEventHttpMessageWriter) { |
| 464 | + initCodec(((ServerSentEventHttpMessageWriter) codec).getEncoder()); |
| 465 | + } |
439 | 466 | }
|
440 | 467 |
|
441 | 468 | /**
|
@@ -521,22 +548,21 @@ protected void initTypedWriters() {
|
521 | 548 | /**
|
522 | 549 | * Return "base" typed writers only, i.e. common to client and server.
|
523 | 550 | */
|
524 |
| - @SuppressWarnings("unchecked") |
525 | 551 | final List<HttpMessageWriter<?>> getBaseTypedWriters() {
|
526 | 552 | if (!this.registerDefaults) {
|
527 | 553 | return Collections.emptyList();
|
528 | 554 | }
|
529 | 555 | List<HttpMessageWriter<?>> writers = new ArrayList<>();
|
530 |
| - writers.add(new EncoderHttpMessageWriter<>(new ByteArrayEncoder())); |
531 |
| - writers.add(new EncoderHttpMessageWriter<>(new ByteBufferEncoder())); |
532 |
| - writers.add(new EncoderHttpMessageWriter<>(new DataBufferEncoder())); |
| 556 | + addCodec(writers, new EncoderHttpMessageWriter<>(new ByteArrayEncoder())); |
| 557 | + addCodec(writers, new EncoderHttpMessageWriter<>(new ByteBufferEncoder())); |
| 558 | + addCodec(writers, new EncoderHttpMessageWriter<>(new DataBufferEncoder())); |
533 | 559 | if (nettyByteBufPresent) {
|
534 |
| - writers.add(new EncoderHttpMessageWriter<>(new NettyByteBufEncoder())); |
| 560 | + addCodec(writers, new EncoderHttpMessageWriter<>(new NettyByteBufEncoder())); |
535 | 561 | }
|
536 |
| - writers.add(new ResourceHttpMessageWriter()); |
537 |
| - writers.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.textPlainOnly())); |
| 562 | + addCodec(writers, new ResourceHttpMessageWriter()); |
| 563 | + addCodec(writers, new EncoderHttpMessageWriter<>(CharSequenceEncoder.textPlainOnly())); |
538 | 564 | if (protobufPresent) {
|
539 |
| - writers.add(new ProtobufHttpMessageWriter(this.protobufEncoder != null ? |
| 565 | + addCodec(writers, new ProtobufHttpMessageWriter(this.protobufEncoder != null ? |
540 | 566 | (ProtobufEncoder) this.protobufEncoder : new ProtobufEncoder()));
|
541 | 567 | }
|
542 | 568 | return writers;
|
@@ -574,17 +600,17 @@ protected void initObjectWriters() {
|
574 | 600 | final List<HttpMessageWriter<?>> getBaseObjectWriters() {
|
575 | 601 | List<HttpMessageWriter<?>> writers = new ArrayList<>();
|
576 | 602 | if (kotlinSerializationJsonPresent) {
|
577 |
| - writers.add(new EncoderHttpMessageWriter<>(getKotlinSerializationJsonEncoder())); |
| 603 | + addCodec(writers, new EncoderHttpMessageWriter<>(getKotlinSerializationJsonEncoder())); |
578 | 604 | }
|
579 | 605 | if (jackson2Present) {
|
580 |
| - writers.add(new EncoderHttpMessageWriter<>(getJackson2JsonEncoder())); |
| 606 | + addCodec(writers, new EncoderHttpMessageWriter<>(getJackson2JsonEncoder())); |
581 | 607 | }
|
582 | 608 | if (jackson2SmilePresent) {
|
583 |
| - writers.add(new EncoderHttpMessageWriter<>(this.jackson2SmileEncoder != null ? |
| 609 | + addCodec(writers, new EncoderHttpMessageWriter<>(this.jackson2SmileEncoder != null ? |
584 | 610 | (Jackson2SmileEncoder) this.jackson2SmileEncoder : new Jackson2SmileEncoder()));
|
585 | 611 | }
|
586 | 612 | if (jaxb2Present && !shouldIgnoreXml) {
|
587 |
| - writers.add(new EncoderHttpMessageWriter<>(this.jaxb2Encoder != null ? |
| 613 | + addCodec(writers, new EncoderHttpMessageWriter<>(this.jaxb2Encoder != null ? |
588 | 614 | (Jaxb2XmlEncoder) this.jaxb2Encoder : new Jaxb2XmlEncoder()));
|
589 | 615 | }
|
590 | 616 | return writers;
|
|
0 commit comments