Add HttpServerRequest-based constructor to RequestData. (#865)
* Add HttpServerRequest-based constructor to RequestData. Add HttpServerResponse-based constructor to ResponseData. * Also get request cookies from headers. * Handle cookie pattern not found. * Fix adding cookie.
This commit is contained in:
committed by
GitHub
parent
28e096573a
commit
3faeb77f55
@@ -17,14 +17,18 @@
|
||||
package org.springframework.cloud.client.loadbalancer;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
import org.springframework.core.style.ToStringCreator;
|
||||
import org.springframework.http.HttpCookie;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpRequest;
|
||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.reactive.function.client.ClientRequest;
|
||||
|
||||
@@ -64,6 +68,31 @@ public class RequestData {
|
||||
this(request.getMethod(), request.getURI(), request.getHeaders(), null, new HashMap<>());
|
||||
}
|
||||
|
||||
public RequestData(ServerHttpRequest request) {
|
||||
this(request.getMethod(), request.getURI(), request.getHeaders(),
|
||||
buildCookies(request.getCookies(), request.getHeaders()), new HashMap<>());
|
||||
}
|
||||
|
||||
private static MultiValueMap<String, String> buildCookies(MultiValueMap<String, HttpCookie> cookies,
|
||||
HttpHeaders headers) {
|
||||
HttpHeaders newCookies = new HttpHeaders();
|
||||
if (cookies != null) {
|
||||
cookies.forEach((key, value) -> value
|
||||
.forEach(cookie -> newCookies.put(cookie.getName(), Collections.singletonList(cookie.getValue()))));
|
||||
}
|
||||
List<String> cookiesFromHeaders = headers.get("cookie");
|
||||
if (cookiesFromHeaders != null) {
|
||||
cookiesFromHeaders.forEach(cookie -> {
|
||||
String[] splitCookie = cookie.split("=");
|
||||
if (splitCookie.length < 2) {
|
||||
return;
|
||||
}
|
||||
newCookies.put(splitCookie[0], Collections.singletonList(splitCookie[1]));
|
||||
});
|
||||
}
|
||||
return newCookies;
|
||||
}
|
||||
|
||||
public HttpMethod getHttpMethod() {
|
||||
return httpMethod;
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ import org.springframework.core.style.ToStringCreator;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseCookie;
|
||||
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.reactive.function.client.ClientResponse;
|
||||
|
||||
@@ -51,10 +52,11 @@ public class ResponseData {
|
||||
}
|
||||
|
||||
public ResponseData(ClientResponse response, RequestData requestData) {
|
||||
httpStatus = response.statusCode();
|
||||
headers = response.headers().asHttpHeaders();
|
||||
cookies = response.cookies();
|
||||
this.requestData = requestData;
|
||||
this(response.statusCode(), response.headers().asHttpHeaders(), response.cookies(), requestData);
|
||||
}
|
||||
|
||||
public ResponseData(ServerHttpResponse response, RequestData requestData) {
|
||||
this(response.getStatusCode(), response.getHeaders(), response.getCookies(), requestData);
|
||||
}
|
||||
|
||||
public HttpStatus getHttpStatus() {
|
||||
|
||||
Reference in New Issue
Block a user