1
1
/*
2
- * Copyright 2002-2018 the original author or authors.
2
+ * Copyright 2002-2021 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
22
22
import java .util .Collections ;
23
23
import java .util .Map ;
24
24
25
- import org .springframework .http .HttpCookie ;
26
- import org .springframework .util .CollectionUtils ;
27
- import org .springframework .util .MultiValueMap ;
28
25
import reactor .core .publisher .Mono ;
29
26
27
+ import org .springframework .http .HttpCookie ;
30
28
import org .springframework .http .HttpHeaders ;
31
29
import org .springframework .lang .Nullable ;
32
30
import org .springframework .util .Assert ;
31
+ import org .springframework .util .CollectionUtils ;
32
+ import org .springframework .util .MultiValueMap ;
33
33
34
34
/**
35
35
* Simple container of information related to the handshake request that started
41
41
*/
42
42
public class HandshakeInfo {
43
43
44
+ private static final MultiValueMap <String , HttpCookie > EMPTY_COOKIES =
45
+ CollectionUtils .toMultiValueMap (Collections .emptyMap ());
46
+
47
+
44
48
private final URI uri ;
45
49
46
50
private final Mono <Principal > principalMono ;
@@ -69,51 +73,51 @@ public class HandshakeInfo {
69
73
* @param protocol the negotiated sub-protocol (may be {@code null})
70
74
*/
71
75
public HandshakeInfo (URI uri , HttpHeaders headers , Mono <Principal > principal , @ Nullable String protocol ) {
72
- this (uri , headers , principal , protocol , null , Collections .emptyMap (), null );
76
+ this (uri , headers , EMPTY_COOKIES , principal , protocol , null , Collections .emptyMap (), null );
73
77
}
74
78
75
-
76
79
/**
77
- * Constructor targetting server-side use with extra information about the
78
- * handshake, the remote address, and a pre-existing log prefix for
79
- * correlation.
80
+ * Constructor targeting server-side use with extra information such as the
81
+ * the remote address, attributes, and a log prefix.
80
82
* @param uri the endpoint URL
81
- * @param headers request headers for server or response headers or client
83
+ * @param headers server request headers
82
84
* @param principal the principal for the session
83
85
* @param protocol the negotiated sub-protocol (may be {@code null})
84
- * @param remoteAddress the remote address where the handshake came from
85
- * @param attributes initial attributes to use for the WebSocket session
86
- * @param logPrefix log prefix used during the handshake for correlating log
87
- * messages, if any.
86
+ * @param remoteAddress the remote address of the client
87
+ * @param attributes initial attributes for the WebSocket session
88
+ * @param logPrefix the log prefix for the handshake request.
88
89
* @since 5.1
90
+ * @deprecated as of 5.3.5 in favor of
91
+ * {@link #HandshakeInfo(URI, HttpHeaders, MultiValueMap, Mono, String, InetSocketAddress, Map, String)}
89
92
*/
90
93
@ Deprecated
91
94
public HandshakeInfo (URI uri , HttpHeaders headers , Mono <Principal > principal ,
92
95
@ Nullable String protocol , @ Nullable InetSocketAddress remoteAddress ,
93
96
Map <String , Object > attributes , @ Nullable String logPrefix ) {
94
- this (uri , headers , CollectionUtils .toMultiValueMap (Collections .emptyMap ()), principal , protocol , remoteAddress , attributes , logPrefix );
97
+
98
+ this (uri , headers , EMPTY_COOKIES , principal , protocol , remoteAddress , attributes , logPrefix );
95
99
}
96
100
97
101
/**
98
- * Constructor targetting server-side use with extra information about the
99
- * handshake, the remote address, and a pre-existing log prefix for
100
- * correlation.
102
+ * Constructor targeting server-side use with extra information such as the
103
+ * cookies, remote address, attributes, and a log prefix.
101
104
* @param uri the endpoint URL
102
- * @param headers request headers for server or response headers or client
103
- * @param cookies request cookies for server
105
+ * @param headers server request headers
106
+ * @param cookies server request cookies
104
107
* @param principal the principal for the session
105
108
* @param protocol the negotiated sub-protocol (may be {@code null})
106
- * @param remoteAddress the remote address where the handshake came from
107
- * @param attributes initial attributes to use for the WebSocket session
108
- * @param logPrefix log prefix used during the handshake for correlating log
109
- * messages, if any.
110
- * @since 5.4
109
+ * @param remoteAddress the remote address of the client
110
+ * @param attributes initial attributes for the WebSocket session
111
+ * @param logPrefix the log prefix for the handshake request.
112
+ * @since 5.3.5
111
113
*/
112
114
public HandshakeInfo (URI uri , HttpHeaders headers , MultiValueMap <String , HttpCookie > cookies ,
113
115
Mono <Principal > principal , @ Nullable String protocol , @ Nullable InetSocketAddress remoteAddress ,
114
116
Map <String , Object > attributes , @ Nullable String logPrefix ) {
117
+
115
118
Assert .notNull (uri , "URI is required" );
116
119
Assert .notNull (headers , "HttpHeaders are required" );
120
+ Assert .notNull (cookies , "`cookies` are required" );
117
121
Assert .notNull (principal , "Principal is required" );
118
122
Assert .notNull (attributes , "'attributes' is required" );
119
123
@@ -136,22 +140,25 @@ public URI getUri() {
136
140
}
137
141
138
142
/**
139
- * Return the handshake HTTP headers. Those are the request headers for a
140
- * server session and the response headers for a client session.
143
+ * Return the HTTP headers from the handshake request, either server request
144
+ * headers for a server session or the client response headers for a client
145
+ * session.
141
146
*/
142
147
public HttpHeaders getHeaders () {
143
148
return this .headers ;
144
149
}
145
150
146
151
/**
147
- * Return the handshake HTTP cookies.
152
+ * For a server session this returns the server request cookies from the
153
+ * handshake request. For a client session, it is an empty map.
154
+ * @since 5.3.5
148
155
*/
149
156
public MultiValueMap <String , HttpCookie > getCookies () {
150
157
return this .cookies ;
151
158
}
152
159
153
160
/**
154
- * Return the principal associated with the handshake HTTP request.
161
+ * Return the principal associated with the handshake request, if any .
155
162
*/
156
163
public Mono <Principal > getPrincipal () {
157
164
return this .principalMono ;
@@ -168,8 +175,8 @@ public String getSubProtocol() {
168
175
}
169
176
170
177
/**
171
- * For a server-side session this is the remote address where the handshake
172
- * request came from.
178
+ * For a server session this is the remote address where the handshake
179
+ * request came from. For a client session, it is {@code null}.
173
180
* @since 5.1
174
181
*/
175
182
@ Nullable
@@ -178,8 +185,7 @@ public InetSocketAddress getRemoteAddress() {
178
185
}
179
186
180
187
/**
181
- * Attributes extracted from the handshake request to be added to the
182
- * WebSocket session.
188
+ * Attributes extracted from the handshake request to add to the session.
183
189
* @since 5.1
184
190
*/
185
191
public Map <String , Object > getAttributes () {
@@ -199,7 +205,7 @@ public String getLogPrefix() {
199
205
200
206
@ Override
201
207
public String toString () {
202
- return "HandshakeInfo[uri=" + this .uri + ", headers=" + this . headers + " ]" ;
208
+ return "HandshakeInfo[uri=" + this .uri + "]" ;
203
209
}
204
210
205
211
}
0 commit comments