@@ -480,28 +480,37 @@ public <T> T getTransient(String key) {
480
480
* @param value the header value
481
481
*/
482
482
public void addResponseHeader (final String key , final String value ) {
483
- addResponseHeader (key , value , v -> v );
483
+ updateResponseHeader (key , value , v -> v , false );
484
484
}
485
485
486
486
/**
487
- * Remove the {@code value} for the specified {@code key}.
487
+ * Update the {@code value} for the specified {@code key}
488
488
*
489
489
* @param key the header name
490
+ * @param value the header value
490
491
*/
491
- public void removeResponseHeader (final String key ) {
492
- threadLocal . get (). responseHeaders . remove ( key );
492
+ public void updateResponseHeader (final String key , final String value ) {
493
+ updateResponseHeader ( key , value , v -> v , true );
493
494
}
494
495
495
496
/**
496
- * Add the {@code value} for the specified {@code key} with the specified {@code uniqueValue} used for de-duplication. Any duplicate
497
+ * Update the {@code value} for the specified {@code key} with the specified {@code uniqueValue} used for de-duplication. Any duplicate
497
498
* {@code value} after applying {@code uniqueValue} is ignored.
498
499
*
499
500
* @param key the header name
500
501
* @param value the header value
501
502
* @param uniqueValue the function that produces de-duplication values
502
- */
503
- public void addResponseHeader (final String key , final String value , final Function <String , String > uniqueValue ) {
504
- threadLocal .set (threadLocal .get ().putResponse (key , value , uniqueValue , maxWarningHeaderCount , maxWarningHeaderSize ));
503
+ * @param replaceExistingKey whether to replace the existing header if it already exists
504
+ */
505
+ public void updateResponseHeader (
506
+ final String key ,
507
+ final String value ,
508
+ final Function <String , String > uniqueValue ,
509
+ final boolean replaceExistingKey
510
+ ) {
511
+ threadLocal .set (
512
+ threadLocal .get ().putResponse (key , value , uniqueValue , maxWarningHeaderCount , maxWarningHeaderSize , replaceExistingKey )
513
+ );
505
514
}
506
515
507
516
/**
@@ -726,7 +735,8 @@ private ThreadContextStruct putResponse(
726
735
final String value ,
727
736
final Function <String , String > uniqueValue ,
728
737
final int maxWarningHeaderCount ,
729
- final long maxWarningHeaderSize
738
+ final long maxWarningHeaderSize ,
739
+ final boolean replaceExistingKey
730
740
) {
731
741
assert value != null ;
732
742
long newWarningHeaderSize = warningHeadersSize ;
@@ -768,8 +778,13 @@ private ThreadContextStruct putResponse(
768
778
if (existingValues .contains (uniqueValue .apply (value ))) {
769
779
return this ;
770
780
}
771
- // preserve insertion order
772
- final Set <String > newValues = Stream .concat (existingValues .stream (), Stream .of (value )).collect (LINKED_HASH_SET_COLLECTOR );
781
+ Set <String > newValues ;
782
+ if (replaceExistingKey ) {
783
+ newValues = Stream .of (value ).collect (LINKED_HASH_SET_COLLECTOR );
784
+ } else {
785
+ // preserve insertion order
786
+ newValues = Stream .concat (existingValues .stream (), Stream .of (value )).collect (LINKED_HASH_SET_COLLECTOR );
787
+ }
773
788
newResponseHeaders = new HashMap <>(responseHeaders );
774
789
newResponseHeaders .put (key , Collections .unmodifiableSet (newValues ));
775
790
} else {
0 commit comments