Polish ServerHttpRequest
Consistent initialization of HttpHeaders and the cookies map. - allow sub-classes to create instance - make unmodifiable
This commit is contained in:
@@ -93,8 +93,7 @@ public abstract class AbstractServerHttpRequest implements ServerHttpRequest {
|
||||
@Override
|
||||
public HttpHeaders getHeaders() {
|
||||
if (this.headers == null) {
|
||||
this.headers = new HttpHeaders();
|
||||
initHeaders(this.headers);
|
||||
this.headers = HttpHeaders.readOnlyHttpHeaders(initHeaders());
|
||||
}
|
||||
return this.headers;
|
||||
}
|
||||
@@ -102,15 +101,13 @@ public abstract class AbstractServerHttpRequest implements ServerHttpRequest {
|
||||
/**
|
||||
* Initialize the headers from the underlying request. Invoked lazily on the
|
||||
* first call to {@link #getHeaders()} and then cached.
|
||||
* @param headers the map to add headers to
|
||||
*/
|
||||
protected abstract void initHeaders(HttpHeaders headers);
|
||||
protected abstract HttpHeaders initHeaders();
|
||||
|
||||
@Override
|
||||
public MultiValueMap<String, HttpCookie> getCookies() {
|
||||
if (this.cookies == null) {
|
||||
this.cookies = new LinkedMultiValueMap<>();
|
||||
initCookies(this.cookies);
|
||||
this.cookies = CollectionUtils.unmodifiableMultiValueMap(initCookies());
|
||||
}
|
||||
return this.cookies;
|
||||
}
|
||||
@@ -118,8 +115,7 @@ public abstract class AbstractServerHttpRequest implements ServerHttpRequest {
|
||||
/**
|
||||
* Initialize the cookies from the underlying request. Invoked lazily on the
|
||||
* first access to cookies via {@link #getHeaders()} and then cached.
|
||||
* @param cookies the map to add cookies to
|
||||
*/
|
||||
protected abstract void initCookies(MultiValueMap<String, HttpCookie> cookies);
|
||||
protected abstract MultiValueMap<String, HttpCookie> initCookies();
|
||||
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ import org.springframework.http.HttpCookie;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
|
||||
/**
|
||||
@@ -66,20 +67,24 @@ public class ReactorServerHttpRequest extends AbstractServerHttpRequest {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initHeaders(HttpHeaders headers) {
|
||||
protected HttpHeaders initHeaders() {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
for (String name : this.channel.headers().names()) {
|
||||
headers.put(name, this.channel.headers().getAll(name));
|
||||
}
|
||||
return headers;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initCookies(MultiValueMap<String, HttpCookie> cookies) {
|
||||
protected MultiValueMap<String, HttpCookie> initCookies() {
|
||||
MultiValueMap<String, HttpCookie> cookies = new LinkedMultiValueMap<>();
|
||||
for (CharSequence name : this.channel.cookies().keySet()) {
|
||||
for (Cookie cookie : this.channel.cookies().get(name)) {
|
||||
HttpCookie httpCookie = new HttpCookie(name.toString(), cookie.value());
|
||||
cookies.add(name.toString(), httpCookie);
|
||||
}
|
||||
}
|
||||
return cookies;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -32,6 +32,7 @@ import org.springframework.http.HttpCookie;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
|
||||
/**
|
||||
@@ -70,20 +71,24 @@ public class RxNettyServerHttpRequest extends AbstractServerHttpRequest {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initHeaders(HttpHeaders headers) {
|
||||
protected HttpHeaders initHeaders() {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
for (String name : this.request.getHeaderNames()) {
|
||||
headers.put(name, this.request.getAllHeaderValues(name));
|
||||
}
|
||||
return headers;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initCookies(MultiValueMap<String, HttpCookie> cookies) {
|
||||
protected MultiValueMap<String, HttpCookie> initCookies() {
|
||||
MultiValueMap<String, HttpCookie> cookies = new LinkedMultiValueMap<>();
|
||||
for (String name : this.request.getCookies().keySet()) {
|
||||
for (Cookie cookie : this.request.getCookies().get(name)) {
|
||||
HttpCookie httpCookie = new HttpCookie(name, cookie.value());
|
||||
cookies.add(name, httpCookie);
|
||||
}
|
||||
}
|
||||
return cookies;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -24,8 +24,6 @@ import java.util.Map;
|
||||
import javax.servlet.http.Cookie;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.reactivestreams.Publisher;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
@@ -36,6 +34,7 @@ import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.LinkedCaseInsensitiveMap;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
@@ -46,8 +45,6 @@ import org.springframework.util.StringUtils;
|
||||
*/
|
||||
public class ServletServerHttpRequest extends AbstractServerHttpRequest {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(ServletServerHttpRequest.class);
|
||||
|
||||
private final HttpServletRequest request;
|
||||
|
||||
private final Flux<DataBuffer> requestBodyPublisher;
|
||||
@@ -81,7 +78,8 @@ public class ServletServerHttpRequest extends AbstractServerHttpRequest {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initHeaders(HttpHeaders headers) {
|
||||
protected HttpHeaders initHeaders() {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
for (Enumeration<?> names = getServletRequest().getHeaderNames(); names.hasMoreElements(); ) {
|
||||
String name = (String) names.nextElement();
|
||||
for (Enumeration<?> values = getServletRequest().getHeaders(name); values.hasMoreElements(); ) {
|
||||
@@ -112,10 +110,12 @@ public class ServletServerHttpRequest extends AbstractServerHttpRequest {
|
||||
headers.setContentLength(contentLength);
|
||||
}
|
||||
}
|
||||
return headers;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initCookies(MultiValueMap<String, HttpCookie> httpCookies) {
|
||||
protected MultiValueMap<String, HttpCookie> initCookies() {
|
||||
MultiValueMap<String, HttpCookie> httpCookies = new LinkedMultiValueMap<>();
|
||||
Cookie[] cookies = this.request.getCookies();
|
||||
if (cookies != null) {
|
||||
for (Cookie cookie : cookies) {
|
||||
@@ -124,6 +124,7 @@ public class ServletServerHttpRequest extends AbstractServerHttpRequest {
|
||||
httpCookies.add(name, httpCookie);
|
||||
}
|
||||
}
|
||||
return httpCookies;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -30,6 +30,7 @@ import org.springframework.http.HttpCookie;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
|
||||
/**
|
||||
@@ -70,19 +71,23 @@ public class UndertowServerHttpRequest extends AbstractServerHttpRequest {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initHeaders(HttpHeaders headers) {
|
||||
protected HttpHeaders initHeaders() {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
for (HeaderValues values : this.getUndertowExchange().getRequestHeaders()) {
|
||||
headers.put(values.getHeaderName().toString(), values);
|
||||
}
|
||||
return headers;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initCookies(MultiValueMap<String, HttpCookie> cookies) {
|
||||
protected MultiValueMap<String, HttpCookie> initCookies() {
|
||||
MultiValueMap<String, HttpCookie> cookies = new LinkedMultiValueMap<>();
|
||||
for (String name : this.exchange.getRequestCookies().keySet()) {
|
||||
Cookie cookie = this.exchange.getRequestCookies().get(name);
|
||||
HttpCookie httpCookie = new HttpCookie(name, cookie.getValue());
|
||||
cookies.add(name, httpCookie);
|
||||
}
|
||||
return cookies;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user