17
17
package org .springframework .http .client .reactive ;
18
18
19
19
import java .util .ArrayList ;
20
+ import java .util .Collections ;
21
+ import java .util .LinkedHashMap ;
20
22
import java .util .List ;
23
+ import java .util .Map ;
21
24
import java .util .concurrent .atomic .AtomicReference ;
22
25
import java .util .function .Supplier ;
23
26
@@ -55,6 +58,10 @@ private enum State {NEW, COMMITTING, COMMITTED}
55
58
56
59
private final MultiValueMap <String , HttpCookie > cookies ;
57
60
61
+ private final Map <String , Object > attributes ;
62
+
63
+ private final boolean applyAttributes ;
64
+
58
65
private final AtomicReference <State > state = new AtomicReference <>(State .NEW );
59
66
60
67
private final List <Supplier <? extends Publisher <Void >>> commitActions = new ArrayList <>(4 );
@@ -64,13 +71,19 @@ private enum State {NEW, COMMITTING, COMMITTED}
64
71
65
72
66
73
public AbstractClientHttpRequest () {
67
- this (new HttpHeaders ());
74
+ this (new HttpHeaders (), false );
75
+ }
76
+
77
+ public AbstractClientHttpRequest (boolean applyAttributes ) {
78
+ this (new HttpHeaders (), applyAttributes );
68
79
}
69
80
70
- public AbstractClientHttpRequest (HttpHeaders headers ) {
81
+ public AbstractClientHttpRequest (HttpHeaders headers , boolean applyAttributes ) {
71
82
Assert .notNull (headers , "HttpHeaders must not be null" );
72
83
this .headers = headers ;
73
84
this .cookies = new LinkedMultiValueMap <>();
85
+ this .attributes = new LinkedHashMap <>();
86
+ this .applyAttributes = applyAttributes ;
74
87
}
75
88
76
89
@@ -106,6 +119,14 @@ public MultiValueMap<String, HttpCookie> getCookies() {
106
119
return this .cookies ;
107
120
}
108
121
122
+ @ Override
123
+ public Map <String , Object > getAttributes () {
124
+ if (State .COMMITTED .equals (this .state .get ())) {
125
+ return Collections .unmodifiableMap (this .attributes );
126
+ }
127
+ return this .attributes ;
128
+ }
129
+
109
130
@ Override
110
131
public void beforeCommit (Supplier <? extends Mono <Void >> action ) {
111
132
Assert .notNull (action , "Action must not be null" );
@@ -140,6 +161,9 @@ protected Mono<Void> doCommit(@Nullable Supplier<? extends Publisher<Void>> writ
140
161
Mono .fromRunnable (() -> {
141
162
applyHeaders ();
142
163
applyCookies ();
164
+ if (this .applyAttributes ) {
165
+ applyAttributes ();
166
+ }
143
167
this .state .set (State .COMMITTED );
144
168
}));
145
169
@@ -168,4 +192,10 @@ protected Mono<Void> doCommit(@Nullable Supplier<? extends Publisher<Void>> writ
168
192
*/
169
193
protected abstract void applyCookies ();
170
194
195
+ /**
196
+ * Add additional attributes from {@link #getAttributes()} to the underlying request.
197
+ * This method is called once only.
198
+ */
199
+ protected abstract void applyAttributes ();
200
+
171
201
}
0 commit comments