@@ -75,6 +75,8 @@ protected NativeMessageHeaderAccessor(@Nullable Message<?> message) {
75
75
@ SuppressWarnings ("unchecked" )
76
76
Map <String , List <String >> map = (Map <String , List <String >>) getHeader (NATIVE_HEADERS );
77
77
if (map != null ) {
78
+ // setHeader checks for equality but we need copy of native headers
79
+ setHeader (NATIVE_HEADERS , null );
78
80
setHeader (NATIVE_HEADERS , new LinkedMultiValueMap <>(map ));
79
81
}
80
82
}
@@ -103,38 +105,28 @@ public void setImmutable() {
103
105
if (isMutable ()) {
104
106
Map <String , List <String >> map = getNativeHeaders ();
105
107
if (map != null ) {
108
+ // setHeader checks for equality but we need immutable wrapper
109
+ setHeader (NATIVE_HEADERS , null );
106
110
setHeader (NATIVE_HEADERS , Collections .unmodifiableMap (map ));
107
111
}
108
112
super .setImmutable ();
109
113
}
110
114
}
111
115
112
116
@ Override
113
- public void setHeader (String name , @ Nullable Object value ) {
114
- if (name .equalsIgnoreCase (NATIVE_HEADERS )) {
115
- // Force removal since setHeader checks for equality
116
- super .setHeader (NATIVE_HEADERS , null );
117
+ public void copyHeaders (@ Nullable Map <String , ?> headersToCopy ) {
118
+ if (headersToCopy == null ) {
119
+ return ;
117
120
}
118
- super .setHeader (name , value );
119
- }
120
121
121
- @ Override
122
- @ SuppressWarnings ("unchecked" )
123
- public void copyHeaders (@ Nullable Map <String , ?> headersToCopy ) {
124
- if (headersToCopy != null ) {
125
- Map <String , List <String >> nativeHeaders = getNativeHeaders ();
126
- Map <String , List <String >> map = (Map <String , List <String >>) headersToCopy .get (NATIVE_HEADERS );
127
- if (map != null ) {
128
- if (nativeHeaders != null ) {
129
- nativeHeaders .putAll (map );
130
- }
131
- else {
132
- nativeHeaders = new LinkedMultiValueMap <>(map );
133
- }
134
- }
135
- super .copyHeaders (headersToCopy );
136
- setHeader (NATIVE_HEADERS , nativeHeaders );
122
+ @ SuppressWarnings ("unchecked" )
123
+ Map <String , List <String >> map = (Map <String , List <String >>) headersToCopy .get (NATIVE_HEADERS );
124
+ if (map != null && map != getNativeHeaders ()) {
125
+ map .forEach (this ::setNativeHeaderValues );
137
126
}
127
+
128
+ // setHeader checks for equality, native headers should be equal by now
129
+ super .copyHeaders (headersToCopy );
138
130
}
139
131
140
132
/**
@@ -201,6 +193,30 @@ public void setNativeHeader(String name, @Nullable String value) {
201
193
}
202
194
}
203
195
196
+ /**
197
+ * Variant of {@link #addNativeHeader(String, String)} for all values.
198
+ * @since 5.2.12
199
+ */
200
+ public void setNativeHeaderValues (String name , @ Nullable List <String > values ) {
201
+ Assert .state (isMutable (), "Already immutable" );
202
+ Map <String , List <String >> map = getNativeHeaders ();
203
+ if (values == null ) {
204
+ if (map != null && map .get (name ) != null ) {
205
+ setModified (true );
206
+ map .remove (name );
207
+ }
208
+ return ;
209
+ }
210
+ if (map == null ) {
211
+ map = new LinkedMultiValueMap <>(3 );
212
+ setHeader (NATIVE_HEADERS , map );
213
+ }
214
+ if (!ObjectUtils .nullSafeEquals (values , getHeader (name ))) {
215
+ setModified (true );
216
+ map .put (name , new ArrayList <>(values ));
217
+ }
218
+ }
219
+
204
220
/**
205
221
* Add the specified native header value to existing values.
206
222
* <p>In order for this to work, the accessor must be {@link #isMutable()
0 commit comments