From b8d75c3139d6b8e89498742099bac7eac99e09bc Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Mon, 15 Mar 2021 18:07:13 +0000 Subject: [PATCH] Polishing contribution Closes gh-26674 --- .../web/reactive/socket/HandshakeInfo.java | 74 ++++++++++--------- .../support/HandshakeWebSocketService.java | 8 +- 2 files changed, 43 insertions(+), 39 deletions(-) diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/socket/HandshakeInfo.java b/spring-webflux/src/main/java/org/springframework/web/reactive/socket/HandshakeInfo.java index b7314fa691..657a20408c 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/socket/HandshakeInfo.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/socket/HandshakeInfo.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,14 +22,14 @@ import java.security.Principal; import java.util.Collections; import java.util.Map; -import org.springframework.http.HttpCookie; -import org.springframework.util.CollectionUtils; -import org.springframework.util.MultiValueMap; import reactor.core.publisher.Mono; +import org.springframework.http.HttpCookie; import org.springframework.http.HttpHeaders; import org.springframework.lang.Nullable; import org.springframework.util.Assert; +import org.springframework.util.CollectionUtils; +import org.springframework.util.MultiValueMap; /** * Simple container of information related to the handshake request that started @@ -41,6 +41,10 @@ import org.springframework.util.Assert; */ public class HandshakeInfo { + private static final MultiValueMap EMPTY_COOKIES = + CollectionUtils.toMultiValueMap(Collections.emptyMap()); + + private final URI uri; private final Mono principalMono; @@ -69,51 +73,51 @@ public class HandshakeInfo { * @param protocol the negotiated sub-protocol (may be {@code null}) */ public HandshakeInfo(URI uri, HttpHeaders headers, Mono principal, @Nullable String protocol) { - this(uri, headers, principal, protocol, null, Collections.emptyMap(), null); + this(uri, headers, EMPTY_COOKIES, principal, protocol, null, Collections.emptyMap(), null); } - /** - * Constructor targetting server-side use with extra information about the - * handshake, the remote address, and a pre-existing log prefix for - * correlation. + * Constructor targeting server-side use with extra information such as the + * the remote address, attributes, and a log prefix. * @param uri the endpoint URL - * @param headers request headers for server or response headers or client + * @param headers server request headers * @param principal the principal for the session * @param protocol the negotiated sub-protocol (may be {@code null}) - * @param remoteAddress the remote address where the handshake came from - * @param attributes initial attributes to use for the WebSocket session - * @param logPrefix log prefix used during the handshake for correlating log - * messages, if any. + * @param remoteAddress the remote address of the client + * @param attributes initial attributes for the WebSocket session + * @param logPrefix the log prefix for the handshake request. * @since 5.1 + * @deprecated as of 5.3.5 in favor of + * {@link #HandshakeInfo(URI, HttpHeaders, MultiValueMap, Mono, String, InetSocketAddress, Map, String)} */ @Deprecated public HandshakeInfo(URI uri, HttpHeaders headers, Mono principal, @Nullable String protocol, @Nullable InetSocketAddress remoteAddress, Map attributes, @Nullable String logPrefix) { - this(uri, headers, CollectionUtils.toMultiValueMap(Collections.emptyMap()), principal, protocol, remoteAddress, attributes, logPrefix); + + this(uri, headers, EMPTY_COOKIES, principal, protocol, remoteAddress, attributes, logPrefix); } /** - * Constructor targetting server-side use with extra information about the - * handshake, the remote address, and a pre-existing log prefix for - * correlation. + * Constructor targeting server-side use with extra information such as the + * cookies, remote address, attributes, and a log prefix. * @param uri the endpoint URL - * @param headers request headers for server or response headers or client - * @param cookies request cookies for server + * @param headers server request headers + * @param cookies server request cookies * @param principal the principal for the session * @param protocol the negotiated sub-protocol (may be {@code null}) - * @param remoteAddress the remote address where the handshake came from - * @param attributes initial attributes to use for the WebSocket session - * @param logPrefix log prefix used during the handshake for correlating log - * messages, if any. - * @since 5.4 + * @param remoteAddress the remote address of the client + * @param attributes initial attributes for the WebSocket session + * @param logPrefix the log prefix for the handshake request. + * @since 5.3.5 */ public HandshakeInfo(URI uri, HttpHeaders headers, MultiValueMap cookies, Mono principal, @Nullable String protocol, @Nullable InetSocketAddress remoteAddress, Map attributes, @Nullable String logPrefix) { + Assert.notNull(uri, "URI is required"); Assert.notNull(headers, "HttpHeaders are required"); + Assert.notNull(cookies, "`cookies` are required"); Assert.notNull(principal, "Principal is required"); Assert.notNull(attributes, "'attributes' is required"); @@ -136,22 +140,25 @@ public class HandshakeInfo { } /** - * Return the handshake HTTP headers. Those are the request headers for a - * server session and the response headers for a client session. + * Return the HTTP headers from the handshake request, either server request + * headers for a server session or the client response headers for a client + * session. */ public HttpHeaders getHeaders() { return this.headers; } /** - * Return the handshake HTTP cookies. + * For a server session this returns the server request cookies from the + * handshake request. For a client session, it is an empty map. + * @since 5.3.5 */ public MultiValueMap getCookies() { return this.cookies; } /** - * Return the principal associated with the handshake HTTP request. + * Return the principal associated with the handshake request, if any. */ public Mono getPrincipal() { return this.principalMono; @@ -168,8 +175,8 @@ public class HandshakeInfo { } /** - * For a server-side session this is the remote address where the handshake - * request came from. + * For a server session this is the remote address where the handshake + * request came from. For a client session, it is {@code null}. * @since 5.1 */ @Nullable @@ -178,8 +185,7 @@ public class HandshakeInfo { } /** - * Attributes extracted from the handshake request to be added to the - * WebSocket session. + * Attributes extracted from the handshake request to add to the session. * @since 5.1 */ public Map getAttributes() { @@ -199,7 +205,7 @@ public class HandshakeInfo { @Override public String toString() { - return "HandshakeInfo[uri=" + this.uri + ", headers=" + this.headers + "]"; + return "HandshakeInfo[uri=" + this.uri + "]"; } } diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/socket/server/support/HandshakeWebSocketService.java b/spring-webflux/src/main/java/org/springframework/web/reactive/socket/server/support/HandshakeWebSocketService.java index 45549d5f11..9255dd6ea7 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/socket/server/support/HandshakeWebSocketService.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/socket/server/support/HandshakeWebSocketService.java @@ -30,17 +30,16 @@ import org.apache.commons.logging.LogFactory; import reactor.core.publisher.Mono; import org.springframework.context.Lifecycle; +import org.springframework.http.HttpCookie; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; -import org.springframework.http.HttpCookie; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; +import org.springframework.util.MultiValueMap; import org.springframework.util.ReflectionUtils; import org.springframework.util.StringUtils; -import org.springframework.util.MultiValueMap; -import org.springframework.util.LinkedMultiValueMap; import org.springframework.web.reactive.socket.HandshakeInfo; import org.springframework.web.reactive.socket.WebSocketHandler; import org.springframework.web.reactive.socket.server.RequestUpgradeStrategy; @@ -285,8 +284,7 @@ public class HandshakeWebSocketService implements WebSocketService, Lifecycle { // the server implementation once the handshake HTTP exchange is done. HttpHeaders headers = new HttpHeaders(); headers.addAll(request.getHeaders()); - MultiValueMap cookies = new LinkedMultiValueMap<>(); - cookies.addAll(request.getCookies()); + MultiValueMap cookies = request.getCookies(); Mono principal = exchange.getPrincipal(); String logPrefix = exchange.getLogPrefix(); InetSocketAddress remoteAddress = request.getRemoteAddress(); -- GitLab