Skip to content

Commit b8d75c3

Browse files
committed
Polishing contribution
Closes gh-26674
1 parent d92c74d commit b8d75c3

File tree

2 files changed

+43
-39
lines changed

2 files changed

+43
-39
lines changed

spring-webflux/src/main/java/org/springframework/web/reactive/socket/HandshakeInfo.java

+40-34
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,14 +22,14 @@
2222
import java.util.Collections;
2323
import java.util.Map;
2424

25-
import org.springframework.http.HttpCookie;
26-
import org.springframework.util.CollectionUtils;
27-
import org.springframework.util.MultiValueMap;
2825
import reactor.core.publisher.Mono;
2926

27+
import org.springframework.http.HttpCookie;
3028
import org.springframework.http.HttpHeaders;
3129
import org.springframework.lang.Nullable;
3230
import org.springframework.util.Assert;
31+
import org.springframework.util.CollectionUtils;
32+
import org.springframework.util.MultiValueMap;
3333

3434
/**
3535
* Simple container of information related to the handshake request that started
@@ -41,6 +41,10 @@
4141
*/
4242
public class HandshakeInfo {
4343

44+
private static final MultiValueMap<String, HttpCookie> EMPTY_COOKIES =
45+
CollectionUtils.toMultiValueMap(Collections.emptyMap());
46+
47+
4448
private final URI uri;
4549

4650
private final Mono<Principal> principalMono;
@@ -69,51 +73,51 @@ public class HandshakeInfo {
6973
* @param protocol the negotiated sub-protocol (may be {@code null})
7074
*/
7175
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);
7377
}
7478

75-
7679
/**
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.
8082
* @param uri the endpoint URL
81-
* @param headers request headers for server or response headers or client
83+
* @param headers server request headers
8284
* @param principal the principal for the session
8385
* @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.
8889
* @since 5.1
90+
* @deprecated as of 5.3.5 in favor of
91+
* {@link #HandshakeInfo(URI, HttpHeaders, MultiValueMap, Mono, String, InetSocketAddress, Map, String)}
8992
*/
9093
@Deprecated
9194
public HandshakeInfo(URI uri, HttpHeaders headers, Mono<Principal> principal,
9295
@Nullable String protocol, @Nullable InetSocketAddress remoteAddress,
9396
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);
9599
}
96100

97101
/**
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.
101104
* @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
104107
* @param principal the principal for the session
105108
* @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
111113
*/
112114
public HandshakeInfo(URI uri, HttpHeaders headers, MultiValueMap<String, HttpCookie> cookies,
113115
Mono<Principal> principal, @Nullable String protocol, @Nullable InetSocketAddress remoteAddress,
114116
Map<String, Object> attributes, @Nullable String logPrefix) {
117+
115118
Assert.notNull(uri, "URI is required");
116119
Assert.notNull(headers, "HttpHeaders are required");
120+
Assert.notNull(cookies, "`cookies` are required");
117121
Assert.notNull(principal, "Principal is required");
118122
Assert.notNull(attributes, "'attributes' is required");
119123

@@ -136,22 +140,25 @@ public URI getUri() {
136140
}
137141

138142
/**
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.
141146
*/
142147
public HttpHeaders getHeaders() {
143148
return this.headers;
144149
}
145150

146151
/**
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
148155
*/
149156
public MultiValueMap<String, HttpCookie> getCookies() {
150157
return this.cookies;
151158
}
152159

153160
/**
154-
* Return the principal associated with the handshake HTTP request.
161+
* Return the principal associated with the handshake request, if any.
155162
*/
156163
public Mono<Principal> getPrincipal() {
157164
return this.principalMono;
@@ -168,8 +175,8 @@ public String getSubProtocol() {
168175
}
169176

170177
/**
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}.
173180
* @since 5.1
174181
*/
175182
@Nullable
@@ -178,8 +185,7 @@ public InetSocketAddress getRemoteAddress() {
178185
}
179186

180187
/**
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.
183189
* @since 5.1
184190
*/
185191
public Map<String, Object> getAttributes() {
@@ -199,7 +205,7 @@ public String getLogPrefix() {
199205

200206
@Override
201207
public String toString() {
202-
return "HandshakeInfo[uri=" + this.uri + ", headers=" + this.headers + "]";
208+
return "HandshakeInfo[uri=" + this.uri + "]";
203209
}
204210

205211
}

spring-webflux/src/main/java/org/springframework/web/reactive/socket/server/support/HandshakeWebSocketService.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,16 @@
3030
import reactor.core.publisher.Mono;
3131

3232
import org.springframework.context.Lifecycle;
33+
import org.springframework.http.HttpCookie;
3334
import org.springframework.http.HttpHeaders;
3435
import org.springframework.http.HttpMethod;
35-
import org.springframework.http.HttpCookie;
3636
import org.springframework.http.server.reactive.ServerHttpRequest;
3737
import org.springframework.lang.Nullable;
3838
import org.springframework.util.Assert;
3939
import org.springframework.util.ClassUtils;
40+
import org.springframework.util.MultiValueMap;
4041
import org.springframework.util.ReflectionUtils;
4142
import org.springframework.util.StringUtils;
42-
import org.springframework.util.MultiValueMap;
43-
import org.springframework.util.LinkedMultiValueMap;
4443
import org.springframework.web.reactive.socket.HandshakeInfo;
4544
import org.springframework.web.reactive.socket.WebSocketHandler;
4645
import org.springframework.web.reactive.socket.server.RequestUpgradeStrategy;
@@ -285,8 +284,7 @@ private HandshakeInfo createHandshakeInfo(ServerWebExchange exchange, ServerHttp
285284
// the server implementation once the handshake HTTP exchange is done.
286285
HttpHeaders headers = new HttpHeaders();
287286
headers.addAll(request.getHeaders());
288-
MultiValueMap<String, HttpCookie> cookies = new LinkedMultiValueMap<>();
289-
cookies.addAll(request.getCookies());
287+
MultiValueMap<String, HttpCookie> cookies = request.getCookies();
290288
Mono<Principal> principal = exchange.getPrincipal();
291289
String logPrefix = exchange.getLogPrefix();
292290
InetSocketAddress remoteAddress = request.getRemoteAddress();

0 commit comments

Comments
 (0)