提交 b8d75c31 编写于 作者: R Rossen Stoyanchev

Polishing contribution

Closes gh-26674
上级 d92c74d9
/*
* 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<String, HttpCookie> EMPTY_COOKIES =
CollectionUtils.toMultiValueMap(Collections.emptyMap());
private final URI uri;
private final Mono<Principal> 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> 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> principal,
@Nullable String protocol, @Nullable InetSocketAddress remoteAddress,
Map<String, Object> 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<String, HttpCookie> cookies,
Mono<Principal> principal, @Nullable String protocol, @Nullable InetSocketAddress remoteAddress,
Map<String, Object> 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<String, HttpCookie> 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<Principal> 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<String, Object> 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 + "]";
}
}
......@@ -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<String, HttpCookie> cookies = new LinkedMultiValueMap<>();
cookies.addAll(request.getCookies());
MultiValueMap<String, HttpCookie> cookies = request.getCookies();
Mono<Principal> principal = exchange.getPrincipal();
String logPrefix = exchange.getLogPrefix();
InetSocketAddress remoteAddress = request.getRemoteAddress();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册