Skip to content

Commit f3d4df2

Browse files
committed
Consistently check available local/remote addresses for non-null
1 parent 9cd9a8e commit f3d4df2

File tree

8 files changed

+63
-53
lines changed

8 files changed

+63
-53
lines changed

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.Collection;
2525
import java.util.List;
2626
import java.util.Locale;
27+
import java.util.Objects;
2728
import java.util.Optional;
2829

2930
import org.reactivestreams.Publisher;
@@ -64,10 +65,10 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
6465
private final MultiValueMap<String, HttpCookie> cookies;
6566

6667
@Nullable
67-
private final InetSocketAddress remoteAddress;
68+
private final InetSocketAddress localAddress;
6869

6970
@Nullable
70-
private final InetSocketAddress localAddress;
71+
private final InetSocketAddress remoteAddress;
7172

7273
@Nullable
7374
private final SslInfo sslInfo;
@@ -98,25 +99,24 @@ public HttpMethod getMethod() {
9899
}
99100

100101
@Override
101-
@SuppressWarnings("ConstantConditions")
102102
public String getMethodValue() {
103-
return (this.httpMethod != null ? this.httpMethod.name() : this.customHttpMethod);
103+
return (this.httpMethod != null ? this.httpMethod.name() : Objects.requireNonNull(this.customHttpMethod));
104104
}
105105

106106
@Override
107107
@Nullable
108-
public InetSocketAddress getRemoteAddress() {
109-
return this.remoteAddress;
110-
}
111-
112-
@Nullable
113-
@Override
114108
public InetSocketAddress getLocalAddress() {
115109
return this.localAddress;
116110
}
117111

112+
@Override
118113
@Nullable
114+
public InetSocketAddress getRemoteAddress() {
115+
return this.remoteAddress;
116+
}
117+
119118
@Override
119+
@Nullable
120120
protected SslInfo initSslInfo() {
121121
return this.sslInfo;
122122
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -211,20 +211,20 @@ protected MultiValueMap<String, HttpCookie> initCookies() {
211211
return this.cookies;
212212
}
213213

214-
@Nullable
215214
@Override
216-
public InetSocketAddress getRemoteAddress() {
217-
return this.originalRequest.getRemoteAddress();
218-
}
219-
220215
@Nullable
221-
@Override
222216
public InetSocketAddress getLocalAddress() {
223217
return this.originalRequest.getLocalAddress();
224218
}
225219

220+
@Override
226221
@Nullable
222+
public InetSocketAddress getRemoteAddress() {
223+
return this.originalRequest.getRemoteAddress();
224+
}
225+
227226
@Override
227+
@Nullable
228228
protected SslInfo initSslInfo() {
229229
return this.sslInfo;
230230
}

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ private static URI resolveBaseUrl(HttpServerRequest request) throws URISyntaxExc
9696
}
9797
else {
9898
InetSocketAddress localAddress = request.hostAddress();
99+
Assert.state(localAddress != null, "No host address available");
99100
return new URI(scheme, null, localAddress.getHostString(),
100101
localAddress.getPort(), null, null, null);
101102
}
@@ -151,13 +152,15 @@ protected MultiValueMap<String, HttpCookie> initCookies() {
151152
}
152153

153154
@Override
154-
public InetSocketAddress getRemoteAddress() {
155-
return this.request.remoteAddress();
155+
@Nullable
156+
public InetSocketAddress getLocalAddress() {
157+
return this.request.hostAddress();
156158
}
157159

158160
@Override
159-
public InetSocketAddress getLocalAddress() {
160-
return this.request.hostAddress();
161+
@Nullable
162+
public InetSocketAddress getRemoteAddress() {
163+
return this.request.remoteAddress();
161164
}
162165

163166
@Override

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -65,19 +65,19 @@ public interface ServerHttpRequest extends HttpRequest, ReactiveHttpInputMessage
6565
MultiValueMap<String, HttpCookie> getCookies();
6666

6767
/**
68-
* Return the remote address where this request is connected to, if available.
68+
* Return the local address the request was accepted on, if available.
69+
* @since 5.2.3
6970
*/
7071
@Nullable
71-
default InetSocketAddress getRemoteAddress() {
72+
default InetSocketAddress getLocalAddress() {
7273
return null;
7374
}
7475

7576
/**
76-
* Return the local address the request was accepted on, if available.
77-
* 5.2.3
77+
* Return the remote address where this request is connected to, if available.
7878
*/
7979
@Nullable
80-
default InetSocketAddress getLocalAddress() {
80+
default InetSocketAddress getRemoteAddress() {
8181
return null;
8282
}
8383

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -97,17 +97,19 @@ public MultiValueMap<String, HttpCookie> getCookies() {
9797
}
9898

9999
@Override
100-
public InetSocketAddress getRemoteAddress() {
101-
return getDelegate().getRemoteAddress();
102-
}
103-
104-
@Override
100+
@Nullable
105101
public InetSocketAddress getLocalAddress() {
106102
return getDelegate().getLocalAddress();
107103
}
108104

105+
@Override
109106
@Nullable
107+
public InetSocketAddress getRemoteAddress() {
108+
return getDelegate().getRemoteAddress();
109+
}
110+
110111
@Override
112+
@Nullable
111113
public SslInfo getSslInfo() {
112114
return getDelegate().getSslInfo();
113115
}

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -42,6 +42,7 @@
4242
import org.springframework.http.HttpCookie;
4343
import org.springframework.http.HttpHeaders;
4444
import org.springframework.http.MediaType;
45+
import org.springframework.lang.NonNull;
4546
import org.springframework.lang.Nullable;
4647
import org.springframework.util.Assert;
4748
import org.springframework.util.LinkedCaseInsensitiveMap;
@@ -174,13 +175,15 @@ protected MultiValueMap<String, HttpCookie> initCookies() {
174175
}
175176

176177
@Override
177-
public InetSocketAddress getRemoteAddress() {
178-
return new InetSocketAddress(this.request.getRemoteHost(), this.request.getRemotePort());
178+
@NonNull
179+
public InetSocketAddress getLocalAddress() {
180+
return new InetSocketAddress(this.request.getLocalAddr(), this.request.getLocalPort());
179181
}
180182

181183
@Override
182-
public InetSocketAddress getLocalAddress() {
183-
return new InetSocketAddress(this.request.getLocalAddr(), this.request.getLocalPort());
184+
@NonNull
185+
public InetSocketAddress getRemoteAddress() {
186+
return new InetSocketAddress(this.request.getRemoteHost(), this.request.getRemotePort());
184187
}
185188

186189
@Override

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -98,13 +98,15 @@ protected MultiValueMap<String, HttpCookie> initCookies() {
9898
}
9999

100100
@Override
101-
public InetSocketAddress getRemoteAddress() {
102-
return this.exchange.getSourceAddress();
101+
@Nullable
102+
public InetSocketAddress getLocalAddress() {
103+
return this.exchange.getDestinationAddress();
103104
}
104105

105106
@Override
106-
public InetSocketAddress getLocalAddress() {
107-
return this.exchange.getDestinationAddress();
107+
@Nullable
108+
public InetSocketAddress getRemoteAddress() {
109+
return this.exchange.getSourceAddress();
108110
}
109111

110112
@Nullable

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.Collection;
2525
import java.util.List;
2626
import java.util.Locale;
27+
import java.util.Objects;
2728
import java.util.Optional;
2829

2930
import org.reactivestreams.Publisher;
@@ -64,10 +65,10 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
6465
private final MultiValueMap<String, HttpCookie> cookies;
6566

6667
@Nullable
67-
private final InetSocketAddress remoteAddress;
68+
private final InetSocketAddress localAddress;
6869

6970
@Nullable
70-
private final InetSocketAddress localAddress;
71+
private final InetSocketAddress remoteAddress;
7172

7273
@Nullable
7374
private final SslInfo sslInfo;
@@ -98,25 +99,24 @@ public HttpMethod getMethod() {
9899
}
99100

100101
@Override
101-
@SuppressWarnings("ConstantConditions")
102102
public String getMethodValue() {
103-
return (this.httpMethod != null ? this.httpMethod.name() : this.customHttpMethod);
103+
return (this.httpMethod != null ? this.httpMethod.name() : Objects.requireNonNull(this.customHttpMethod));
104104
}
105105

106106
@Override
107107
@Nullable
108-
public InetSocketAddress getRemoteAddress() {
109-
return this.remoteAddress;
110-
}
111-
112-
@Nullable
113-
@Override
114108
public InetSocketAddress getLocalAddress() {
115109
return this.localAddress;
116110
}
117111

112+
@Override
118113
@Nullable
114+
public InetSocketAddress getRemoteAddress() {
115+
return this.remoteAddress;
116+
}
117+
119118
@Override
119+
@Nullable
120120
protected SslInfo initSslInfo() {
121121
return this.sslInfo;
122122
}

0 commit comments

Comments
 (0)