Skip to content

Commit 8b7167b

Browse files
committed
Fix a NullPointerException when InetAddress is not resolved.
1 parent 7beb640 commit 8b7167b

File tree

2 files changed

+109
-3
lines changed

2 files changed

+109
-3
lines changed

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/web/trace/reactive/ServerWebExchangeTraceableRequest.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 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.
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.boot.actuate.web.trace.reactive;
1818

19+
import java.net.InetAddress;
20+
import java.net.InetSocketAddress;
1921
import java.net.URI;
2022
import java.util.LinkedHashMap;
2123
import java.util.List;
@@ -45,8 +47,7 @@ class ServerWebExchangeTraceableRequest implements TraceableRequest {
4547
this.method = request.getMethodValue();
4648
this.headers = request.getHeaders();
4749
this.uri = request.getURI();
48-
this.remoteAddress = (request.getRemoteAddress() != null)
49-
? request.getRemoteAddress().getAddress().toString() : null;
50+
this.remoteAddress = getRemoteAddress(request);
5051
}
5152

5253
@Override
@@ -69,4 +70,10 @@ public String getRemoteAddress() {
6970
return this.remoteAddress;
7071
}
7172

73+
private static String getRemoteAddress(ServerHttpRequest request) {
74+
InetSocketAddress remoteAddress = request.getRemoteAddress();
75+
InetAddress address = (remoteAddress != null) ? remoteAddress.getAddress() : null;
76+
return (address != null) ? address.toString() : null;
77+
}
78+
7279
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright 2012-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.boot.actuate.web.trace.reactive;
17+
18+
import java.net.InetSocketAddress;
19+
import java.net.URI;
20+
import java.util.Collections;
21+
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
25+
import org.springframework.http.HttpHeaders;
26+
import org.springframework.http.server.reactive.ServerHttpRequest;
27+
import org.springframework.web.server.ServerWebExchange;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
import static org.assertj.core.api.Assertions.entry;
31+
import static org.mockito.Mockito.doReturn;
32+
import static org.mockito.Mockito.mock;
33+
34+
/**
35+
* Tests for {@link ServerWebExchangeTraceableRequest}.
36+
*
37+
* @author Dmytro Nosan
38+
*/
39+
public class ServerWebExchangeTraceableRequestTests {
40+
41+
private final ServerWebExchange serverExchange = mock(ServerWebExchange.class);
42+
43+
private final ServerHttpRequest serverRequest = mock(ServerHttpRequest.class);
44+
45+
@Before
46+
public void setUp() {
47+
doReturn(this.serverRequest).when(this.serverExchange).getRequest();
48+
}
49+
50+
@Test
51+
public void getMethod() {
52+
String method = "POST";
53+
doReturn(method).when(this.serverRequest).getMethodValue();
54+
ServerWebExchangeTraceableRequest traceableRequest = new ServerWebExchangeTraceableRequest(
55+
this.serverExchange);
56+
assertThat(traceableRequest.getMethod()).isSameAs(method);
57+
}
58+
59+
@Test
60+
public void getUri() {
61+
URI uri = URI.create("http://localhost:8080/");
62+
doReturn(uri).when(this.serverRequest).getURI();
63+
ServerWebExchangeTraceableRequest traceableRequest = new ServerWebExchangeTraceableRequest(
64+
this.serverExchange);
65+
assertThat(traceableRequest.getUri()).isSameAs(uri);
66+
}
67+
68+
@Test
69+
public void getHeaders() {
70+
HttpHeaders httpHeaders = new HttpHeaders();
71+
httpHeaders.add("name", "value");
72+
doReturn(httpHeaders).when(this.serverRequest).getHeaders();
73+
ServerWebExchangeTraceableRequest traceableRequest = new ServerWebExchangeTraceableRequest(
74+
this.serverExchange);
75+
assertThat(traceableRequest.getHeaders())
76+
.containsOnly(entry("name", Collections.singletonList("value")));
77+
}
78+
79+
@Test
80+
public void getRemoteAddress() {
81+
InetSocketAddress socketAddress = InetSocketAddress.createUnresolved("", 0);
82+
doReturn(socketAddress).when(this.serverRequest).getRemoteAddress();
83+
ServerWebExchangeTraceableRequest traceableRequest = new ServerWebExchangeTraceableRequest(
84+
this.serverExchange);
85+
assertThat(traceableRequest.getRemoteAddress()).isNull();
86+
87+
}
88+
89+
@Test
90+
public void getUnresolvedRemoteAddress() {
91+
InetSocketAddress socketAddress = new InetSocketAddress(0);
92+
doReturn(socketAddress).when(this.serverRequest).getRemoteAddress();
93+
ServerWebExchangeTraceableRequest traceableRequest = new ServerWebExchangeTraceableRequest(
94+
this.serverExchange);
95+
assertThat(traceableRequest.getRemoteAddress())
96+
.isEqualTo(socketAddress.getAddress().toString());
97+
}
98+
99+
}

0 commit comments

Comments
 (0)