Skip to content

Commit 37a4e84

Browse files
committed
Cache ServerHttpRequest::getMethod in AbstractServerHttpRequest
This commit ensures that the HttpMethod, exposed through ServerHttpRequest::getMethod, is cached in AbstractServerHttpRequest so that potentially expensive HTTP method lookups are only done once. Closes gh-30139
1 parent c27a568 commit 37a4e84

File tree

8 files changed

+62
-62
lines changed

8 files changed

+62
-62
lines changed

spring-test/src/main/java/org/springframework/mock/http/server/reactive/MockServerHttpRequest.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -54,8 +54,6 @@
5454
*/
5555
public final class MockServerHttpRequest extends AbstractServerHttpRequest {
5656

57-
private final HttpMethod httpMethod;
58-
5957
private final MultiValueMap<String, HttpCookie> cookies;
6058

6159
@Nullable
@@ -74,8 +72,7 @@ private MockServerHttpRequest(HttpMethod httpMethod,
7472
@Nullable InetSocketAddress localAddress, @Nullable InetSocketAddress remoteAddress,
7573
@Nullable SslInfo sslInfo, Publisher<? extends DataBuffer> body) {
7674

77-
super(uri, contextPath, headers);
78-
this.httpMethod = httpMethod;
75+
super(httpMethod, uri, contextPath, headers);
7976
this.cookies = cookies;
8077
this.localAddress = localAddress;
8178
this.remoteAddress = remoteAddress;
@@ -84,11 +81,6 @@ private MockServerHttpRequest(HttpMethod httpMethod,
8481
}
8582

8683

87-
@Override
88-
public HttpMethod getMethod() {
89-
return this.httpMethod;
90-
}
91-
9284
@Override
9385
@Nullable
9486
public InetSocketAddress getLocalAddress() {

spring-web/src/main/java/org/springframework/http/server/reactive/AbstractServerHttpRequest.java

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,8 +24,10 @@
2424

2525
import org.springframework.http.HttpCookie;
2626
import org.springframework.http.HttpHeaders;
27+
import org.springframework.http.HttpMethod;
2728
import org.springframework.http.server.RequestPath;
2829
import org.springframework.lang.Nullable;
30+
import org.springframework.util.Assert;
2931
import org.springframework.util.CollectionUtils;
3032
import org.springframework.util.LinkedMultiValueMap;
3133
import org.springframework.util.MultiValueMap;
@@ -49,6 +51,10 @@ public abstract class AbstractServerHttpRequest implements ServerHttpRequest {
4951

5052
private final HttpHeaders headers;
5153

54+
// TODO: remove @Nullable once deprecated constructors have been removed
55+
@Nullable
56+
private final HttpMethod method;
57+
5258
@Nullable
5359
private MultiValueMap<String, String> queryParams;
5460

@@ -71,23 +77,50 @@ public abstract class AbstractServerHttpRequest implements ServerHttpRequest {
7177
* @param contextPath the context path for the request
7278
* @param headers the headers for the request (as {@link MultiValueMap})
7379
* @since 5.3
80+
* @deprecated since 6.0.8, in favor of {@link #AbstractServerHttpRequest(HttpMethod, URI, String, MultiValueMap)}
7481
*/
82+
@Deprecated(since = "6.0.8", forRemoval = true)
7583
public AbstractServerHttpRequest(URI uri, @Nullable String contextPath, MultiValueMap<String, String> headers) {
7684
this.uri = uri;
7785
this.path = RequestPath.parse(uri, contextPath);
7886
this.headers = HttpHeaders.readOnlyHttpHeaders(headers);
87+
this.method = null;
88+
}
89+
90+
/**
91+
* Constructor with the method, URI and headers for the request.
92+
* @param method the HTTP method for the request
93+
* @param uri the URI for the request
94+
* @param contextPath the context path for the request
95+
* @param headers the headers for the request (as {@link MultiValueMap})
96+
* @since 6.0.8
97+
*/
98+
public AbstractServerHttpRequest(HttpMethod method, URI uri, @Nullable String contextPath,
99+
MultiValueMap<String, String> headers) {
100+
101+
Assert.notNull(method, "Method must not be null");
102+
Assert.notNull(uri, "Uri must not be null");
103+
Assert.notNull(headers, "Headers must not be null");
104+
105+
this.method = method;
106+
this.uri = uri;
107+
this.path = RequestPath.parse(uri, contextPath);
108+
this.headers = HttpHeaders.readOnlyHttpHeaders(headers);
79109
}
80110

81111
/**
82112
* Constructor with the URI and headers for the request.
83113
* @param uri the URI for the request
84114
* @param contextPath the context path for the request
85115
* @param headers the headers for the request (as {@link HttpHeaders})
116+
* @deprecated since 6.0.8, in favor of {@link #AbstractServerHttpRequest(HttpMethod, URI, String, MultiValueMap)}
86117
*/
118+
@Deprecated(since = "6.0.8", forRemoval = true)
87119
public AbstractServerHttpRequest(URI uri, @Nullable String contextPath, HttpHeaders headers) {
88120
this.uri = uri;
89121
this.path = RequestPath.parse(uri, contextPath);
90122
this.headers = HttpHeaders.readOnlyHttpHeaders(headers);
123+
this.method = null;
91124
}
92125

93126

@@ -112,6 +145,18 @@ protected String initId() {
112145
return null;
113146
}
114147

148+
@Override
149+
public HttpMethod getMethod() {
150+
// TODO: remove null check once deprecated constructors have been removed
151+
if (this.method != null) {
152+
return this.method;
153+
}
154+
else {
155+
throw new IllegalStateException("No HttpMethod provided in constructor, " +
156+
"and AbstractServerHttpRequest::getMethod not overridden");
157+
}
158+
}
159+
115160
@Override
116161
public URI getURI() {
117162
return this.uri;

spring-web/src/main/java/org/springframework/http/server/reactive/DefaultServerHttpRequestBuilder.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -177,8 +177,6 @@ private URI getUriToUse() {
177177

178178
private static class MutatedServerHttpRequest extends AbstractServerHttpRequest {
179179

180-
private final HttpMethod method;
181-
182180
@Nullable
183181
private final SslInfo sslInfo;
184182

@@ -194,19 +192,13 @@ public MutatedServerHttpRequest(URI uri, @Nullable String contextPath,
194192
HttpMethod method, @Nullable SslInfo sslInfo, @Nullable InetSocketAddress remoteAddress,
195193
HttpHeaders headers, Flux<DataBuffer> body, ServerHttpRequest originalRequest) {
196194

197-
super(uri, contextPath, headers);
198-
this.method = method;
195+
super(method, uri, contextPath, headers);
199196
this.remoteAddress = (remoteAddress != null ? remoteAddress : originalRequest.getRemoteAddress());
200197
this.sslInfo = (sslInfo != null ? sslInfo : originalRequest.getSslInfo());
201198
this.body = body;
202199
this.originalRequest = originalRequest;
203200
}
204201

205-
@Override
206-
public HttpMethod getMethod() {
207-
return this.method;
208-
}
209-
210202
@Override
211203
protected MultiValueMap<String, HttpCookie> initCookies() {
212204
return this.originalRequest.getCookies();

spring-web/src/main/java/org/springframework/http/server/reactive/ReactorNetty2ServerHttpRequest.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,15 @@ class ReactorNetty2ServerHttpRequest extends AbstractServerHttpRequest {
6363

6464
private final Netty5DataBufferFactory bufferFactory;
6565

66-
private final HttpMethod method;
67-
6866

6967
public ReactorNetty2ServerHttpRequest(HttpServerRequest request, Netty5DataBufferFactory bufferFactory)
7068
throws URISyntaxException {
7169

72-
super(initUri(request), "", new Netty5HeadersAdapter(request.requestHeaders()));
70+
super(HttpMethod.valueOf(request.method().name()), initUri(request), "",
71+
new Netty5HeadersAdapter(request.requestHeaders()));
7372
Assert.notNull(bufferFactory, "DataBufferFactory must not be null");
7473
this.request = request;
7574
this.bufferFactory = bufferFactory;
76-
this.method = HttpMethod.valueOf(request.method().name());
7775
}
7876

7977
private static URI initUri(HttpServerRequest request) throws URISyntaxException {
@@ -142,11 +140,6 @@ private static String resolveRequestUri(HttpServerRequest request) {
142140
return uri;
143141
}
144142

145-
@Override
146-
public HttpMethod getMethod() {
147-
return this.method;
148-
}
149-
150143
@Override
151144
protected MultiValueMap<String, HttpCookie> initCookies() {
152145
MultiValueMap<String, HttpCookie> cookies = new LinkedMultiValueMap<>();

spring-web/src/main/java/org/springframework/http/server/reactive/ReactorServerHttpRequest.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,15 @@ class ReactorServerHttpRequest extends AbstractServerHttpRequest {
6161

6262
private final NettyDataBufferFactory bufferFactory;
6363

64-
private final HttpMethod method;
65-
6664

6765
public ReactorServerHttpRequest(HttpServerRequest request, NettyDataBufferFactory bufferFactory)
6866
throws URISyntaxException {
6967

70-
super(initUri(request), "", new NettyHeadersAdapter(request.requestHeaders()));
68+
super(HttpMethod.valueOf(request.method().name()), initUri(request), "",
69+
new NettyHeadersAdapter(request.requestHeaders()));
7170
Assert.notNull(bufferFactory, "DataBufferFactory must not be null");
7271
this.request = request;
7372
this.bufferFactory = bufferFactory;
74-
this.method = HttpMethod.valueOf(request.method().name());
7573
}
7674

7775
private static URI initUri(HttpServerRequest request) throws URISyntaxException {
@@ -112,10 +110,6 @@ private static String resolveRequestUri(HttpServerRequest request) {
112110
return uri;
113111
}
114112

115-
@Override
116-
public HttpMethod getMethod() {
117-
return this.method;
118-
}
119113

120114
@Override
121115
protected MultiValueMap<String, HttpCookie> initCookies() {

spring-web/src/main/java/org/springframework/http/server/reactive/ServletServerHttpRequest.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -90,7 +90,8 @@ public ServletServerHttpRequest(MultiValueMap<String, String> headers, HttpServl
9090
AsyncContext asyncContext, String servletPath, DataBufferFactory bufferFactory, int bufferSize)
9191
throws IOException, URISyntaxException {
9292

93-
super(initUri(request), request.getContextPath() + servletPath, initHeaders(headers, request));
93+
super(HttpMethod.valueOf(request.getMethod()), initUri(request), request.getContextPath() + servletPath,
94+
initHeaders(headers, request));
9495

9596
Assert.notNull(bufferFactory, "'bufferFactory' must not be null");
9697
Assert.isTrue(bufferSize > 0, "'bufferSize' must be greater than 0");
@@ -162,11 +163,6 @@ private static MultiValueMap<String, String> initHeaders(
162163
return (headers != null ? headers : headerValues);
163164
}
164165

165-
@Override
166-
public HttpMethod getMethod() {
167-
return HttpMethod.valueOf(this.request.getMethod());
168-
}
169-
170166
@Override
171167
protected MultiValueMap<String, HttpCookie> initCookies() {
172168
MultiValueMap<String, HttpCookie> httpCookies = new LinkedMultiValueMap<>();

spring-web/src/main/java/org/springframework/http/server/reactive/UndertowServerHttpRequest.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -64,7 +64,8 @@ class UndertowServerHttpRequest extends AbstractServerHttpRequest {
6464
public UndertowServerHttpRequest(HttpServerExchange exchange, DataBufferFactory bufferFactory)
6565
throws URISyntaxException {
6666

67-
super(initUri(exchange), "", new UndertowHeadersAdapter(exchange.getRequestHeaders()));
67+
super(HttpMethod.valueOf(exchange.getRequestMethod().toString()), initUri(exchange), "",
68+
new UndertowHeadersAdapter(exchange.getRequestHeaders()));
6869
this.exchange = exchange;
6970
this.body = new RequestBodyPublisher(exchange, bufferFactory);
7071
this.body.registerListeners(exchange);
@@ -78,11 +79,6 @@ private static URI initUri(HttpServerExchange exchange) throws URISyntaxExceptio
7879
return new URI(requestUriAndQuery);
7980
}
8081

81-
@Override
82-
public HttpMethod getMethod() {
83-
return HttpMethod.valueOf(this.exchange.getRequestMethod().toString());
84-
}
85-
8682
@Override
8783
protected MultiValueMap<String, HttpCookie> initCookies() {
8884
MultiValueMap<String, HttpCookie> cookies = new LinkedMultiValueMap<>();

spring-web/src/testFixtures/java/org/springframework/web/testfixture/http/server/reactive/MockServerHttpRequest.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -55,8 +55,6 @@
5555
*/
5656
public final class MockServerHttpRequest extends AbstractServerHttpRequest {
5757

58-
private final HttpMethod httpMethod;
59-
6058
private final MultiValueMap<String, HttpCookie> cookies;
6159

6260
@Nullable
@@ -75,8 +73,7 @@ private MockServerHttpRequest(HttpMethod httpMethod,
7573
@Nullable InetSocketAddress localAddress, @Nullable InetSocketAddress remoteAddress,
7674
@Nullable SslInfo sslInfo, Publisher<? extends DataBuffer> body) {
7775

78-
super(uri, contextPath, headers);
79-
this.httpMethod = httpMethod;
76+
super(httpMethod, uri, contextPath, headers);
8077
this.cookies = cookies;
8178
this.localAddress = localAddress;
8279
this.remoteAddress = remoteAddress;
@@ -85,11 +82,6 @@ private MockServerHttpRequest(HttpMethod httpMethod,
8582
}
8683

8784

88-
@Override
89-
public HttpMethod getMethod() {
90-
return this.httpMethod;
91-
}
92-
9385
@Override
9486
@Nullable
9587
public InetSocketAddress getLocalAddress() {

0 commit comments

Comments
 (0)