@@ -480,28 +480,37 @@ public <T> T getTransient(String key) {
480480 * @param value the header value
481481 */
482482 public void addResponseHeader (final String key , final String value ) {
483- addResponseHeader (key , value , v -> v );
483+ updateResponseHeader (key , value , v -> v , false );
484484 }
485485
486486 /**
487- * Remove the {@code value} for the specified {@code key}.
487+ * Update the {@code value} for the specified {@code key}
488488 *
489489 * @param key the header name
490+ * @param value the header value
490491 */
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 );
493494 }
494495
495496 /**
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
497498 * {@code value} after applying {@code uniqueValue} is ignored.
498499 *
499500 * @param key the header name
500501 * @param value the header value
501502 * @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+ );
505514 }
506515
507516 /**
@@ -726,7 +735,8 @@ private ThreadContextStruct putResponse(
726735 final String value ,
727736 final Function <String , String > uniqueValue ,
728737 final int maxWarningHeaderCount ,
729- final long maxWarningHeaderSize
738+ final long maxWarningHeaderSize ,
739+ final boolean replaceExistingKey
730740 ) {
731741 assert value != null ;
732742 long newWarningHeaderSize = warningHeadersSize ;
@@ -768,8 +778,13 @@ private ThreadContextStruct putResponse(
768778 if (existingValues .contains (uniqueValue .apply (value ))) {
769779 return this ;
770780 }
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+ }
773788 newResponseHeaders = new HashMap <>(responseHeaders );
774789 newResponseHeaders .put (key , Collections .unmodifiableSet (newValues ));
775790 } else {
0 commit comments