Skip to content

Commit

Permalink
Polishing contribution
Browse files Browse the repository at this point in the history
Closes gh-26674
  • Loading branch information
rstoyanchev committed Mar 15, 2021
1 parent d92c74d commit b8d75c3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -22,14 +22,14 @@
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
Expand All @@ -41,6 +41,10 @@
*/
public class HandshakeInfo {

private static final MultiValueMap<String, HttpCookie> EMPTY_COOKIES =
CollectionUtils.toMultiValueMap(Collections.emptyMap());


private final URI uri;

private final Mono<Principal> principalMono;
Expand Down Expand Up @@ -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");

Expand All @@ -136,22 +140,25 @@ public URI getUri() {
}

/**
* 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;
Expand All @@ -168,8 +175,8 @@ public String getSubProtocol() {
}

/**
* 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
Expand All @@ -178,8 +185,7 @@ public InetSocketAddress getRemoteAddress() {
}

/**
* 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() {
Expand All @@ -199,7 +205,7 @@ public String getLogPrefix() {

@Override
public String toString() {
return "HandshakeInfo[uri=" + this.uri + ", headers=" + this.headers + "]";
return "HandshakeInfo[uri=" + this.uri + "]";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,16 @@
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;
Expand Down Expand Up @@ -285,8 +284,7 @@ private HandshakeInfo createHandshakeInfo(ServerWebExchange exchange, ServerHttp
// 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();
Expand Down

0 comments on commit b8d75c3

Please sign in to comment.