From f3d4df2fd4e9d2c015ced1f5c749ce43391db017 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Sat, 6 Jun 2020 15:15:16 +0200 Subject: [PATCH] Consistently check available local/remote addresses for non-null --- .../reactive/MockServerHttpRequest.java | 20 +++++++++---------- .../DefaultServerHttpRequestBuilder.java | 14 ++++++------- .../reactive/ReactorServerHttpRequest.java | 11 ++++++---- .../server/reactive/ServerHttpRequest.java | 12 +++++------ .../reactive/ServerHttpRequestDecorator.java | 14 +++++++------ .../reactive/ServletServerHttpRequest.java | 13 +++++++----- .../reactive/UndertowServerHttpRequest.java | 12 ++++++----- .../reactive/MockServerHttpRequest.java | 20 +++++++++---------- 8 files changed, 63 insertions(+), 53 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/mock/http/server/reactive/MockServerHttpRequest.java b/spring-test/src/main/java/org/springframework/mock/http/server/reactive/MockServerHttpRequest.java index 90eeb86054d..2488bdfc036 100644 --- a/spring-test/src/main/java/org/springframework/mock/http/server/reactive/MockServerHttpRequest.java +++ b/spring-test/src/main/java/org/springframework/mock/http/server/reactive/MockServerHttpRequest.java @@ -24,6 +24,7 @@ import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.Locale; +import java.util.Objects; import java.util.Optional; import org.reactivestreams.Publisher; @@ -64,10 +65,10 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest { private final MultiValueMap cookies; @Nullable - private final InetSocketAddress remoteAddress; + private final InetSocketAddress localAddress; @Nullable - private final InetSocketAddress localAddress; + private final InetSocketAddress remoteAddress; @Nullable private final SslInfo sslInfo; @@ -98,25 +99,24 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest { } @Override - @SuppressWarnings("ConstantConditions") public String getMethodValue() { - return (this.httpMethod != null ? this.httpMethod.name() : this.customHttpMethod); + return (this.httpMethod != null ? this.httpMethod.name() : Objects.requireNonNull(this.customHttpMethod)); } @Override @Nullable - public InetSocketAddress getRemoteAddress() { - return this.remoteAddress; - } - - @Nullable - @Override public InetSocketAddress getLocalAddress() { return this.localAddress; } + @Override @Nullable + public InetSocketAddress getRemoteAddress() { + return this.remoteAddress; + } + @Override + @Nullable protected SslInfo initSslInfo() { return this.sslInfo; } diff --git a/spring-web/src/main/java/org/springframework/http/server/reactive/DefaultServerHttpRequestBuilder.java b/spring-web/src/main/java/org/springframework/http/server/reactive/DefaultServerHttpRequestBuilder.java index ce5a3d2b373..bc05a9da1a8 100644 --- a/spring-web/src/main/java/org/springframework/http/server/reactive/DefaultServerHttpRequestBuilder.java +++ b/spring-web/src/main/java/org/springframework/http/server/reactive/DefaultServerHttpRequestBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -211,20 +211,20 @@ class DefaultServerHttpRequestBuilder implements ServerHttpRequest.Builder { return this.cookies; } - @Nullable @Override - public InetSocketAddress getRemoteAddress() { - return this.originalRequest.getRemoteAddress(); - } - @Nullable - @Override public InetSocketAddress getLocalAddress() { return this.originalRequest.getLocalAddress(); } + @Override @Nullable + public InetSocketAddress getRemoteAddress() { + return this.originalRequest.getRemoteAddress(); + } + @Override + @Nullable protected SslInfo initSslInfo() { return this.sslInfo; } diff --git a/spring-web/src/main/java/org/springframework/http/server/reactive/ReactorServerHttpRequest.java b/spring-web/src/main/java/org/springframework/http/server/reactive/ReactorServerHttpRequest.java index 9cd57c08c09..21e019fed4b 100644 --- a/spring-web/src/main/java/org/springframework/http/server/reactive/ReactorServerHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/server/reactive/ReactorServerHttpRequest.java @@ -96,6 +96,7 @@ class ReactorServerHttpRequest extends AbstractServerHttpRequest { } else { InetSocketAddress localAddress = request.hostAddress(); + Assert.state(localAddress != null, "No host address available"); return new URI(scheme, null, localAddress.getHostString(), localAddress.getPort(), null, null, null); } @@ -151,13 +152,15 @@ class ReactorServerHttpRequest extends AbstractServerHttpRequest { } @Override - public InetSocketAddress getRemoteAddress() { - return this.request.remoteAddress(); + @Nullable + public InetSocketAddress getLocalAddress() { + return this.request.hostAddress(); } @Override - public InetSocketAddress getLocalAddress() { - return this.request.hostAddress(); + @Nullable + public InetSocketAddress getRemoteAddress() { + return this.request.remoteAddress(); } @Override diff --git a/spring-web/src/main/java/org/springframework/http/server/reactive/ServerHttpRequest.java b/spring-web/src/main/java/org/springframework/http/server/reactive/ServerHttpRequest.java index fd866d13503..a7673c3f472 100644 --- a/spring-web/src/main/java/org/springframework/http/server/reactive/ServerHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/server/reactive/ServerHttpRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -65,19 +65,19 @@ public interface ServerHttpRequest extends HttpRequest, ReactiveHttpInputMessage MultiValueMap getCookies(); /** - * Return the remote address where this request is connected to, if available. + * Return the local address the request was accepted on, if available. + * @since 5.2.3 */ @Nullable - default InetSocketAddress getRemoteAddress() { + default InetSocketAddress getLocalAddress() { return null; } /** - * Return the local address the request was accepted on, if available. - * 5.2.3 + * Return the remote address where this request is connected to, if available. */ @Nullable - default InetSocketAddress getLocalAddress() { + default InetSocketAddress getRemoteAddress() { return null; } diff --git a/spring-web/src/main/java/org/springframework/http/server/reactive/ServerHttpRequestDecorator.java b/spring-web/src/main/java/org/springframework/http/server/reactive/ServerHttpRequestDecorator.java index a526108d463..b75e3f12fb1 100644 --- a/spring-web/src/main/java/org/springframework/http/server/reactive/ServerHttpRequestDecorator.java +++ b/spring-web/src/main/java/org/springframework/http/server/reactive/ServerHttpRequestDecorator.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -97,17 +97,19 @@ public class ServerHttpRequestDecorator implements ServerHttpRequest { } @Override - public InetSocketAddress getRemoteAddress() { - return getDelegate().getRemoteAddress(); - } - - @Override + @Nullable public InetSocketAddress getLocalAddress() { return getDelegate().getLocalAddress(); } + @Override @Nullable + public InetSocketAddress getRemoteAddress() { + return getDelegate().getRemoteAddress(); + } + @Override + @Nullable public SslInfo getSslInfo() { return getDelegate().getSslInfo(); } diff --git a/spring-web/src/main/java/org/springframework/http/server/reactive/ServletServerHttpRequest.java b/spring-web/src/main/java/org/springframework/http/server/reactive/ServletServerHttpRequest.java index 6ea6e9404d6..e235826d67d 100644 --- a/spring-web/src/main/java/org/springframework/http/server/reactive/ServletServerHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/server/reactive/ServletServerHttpRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -42,6 +42,7 @@ import org.springframework.core.io.buffer.DefaultDataBufferFactory; import org.springframework.http.HttpCookie; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; +import org.springframework.lang.NonNull; import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.LinkedCaseInsensitiveMap; @@ -174,13 +175,15 @@ class ServletServerHttpRequest extends AbstractServerHttpRequest { } @Override - public InetSocketAddress getRemoteAddress() { - return new InetSocketAddress(this.request.getRemoteHost(), this.request.getRemotePort()); + @NonNull + public InetSocketAddress getLocalAddress() { + return new InetSocketAddress(this.request.getLocalAddr(), this.request.getLocalPort()); } @Override - public InetSocketAddress getLocalAddress() { - return new InetSocketAddress(this.request.getLocalAddr(), this.request.getLocalPort()); + @NonNull + public InetSocketAddress getRemoteAddress() { + return new InetSocketAddress(this.request.getRemoteHost(), this.request.getRemotePort()); } @Override diff --git a/spring-web/src/main/java/org/springframework/http/server/reactive/UndertowServerHttpRequest.java b/spring-web/src/main/java/org/springframework/http/server/reactive/UndertowServerHttpRequest.java index 11b4418951f..6f6c4257ece 100644 --- a/spring-web/src/main/java/org/springframework/http/server/reactive/UndertowServerHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/server/reactive/UndertowServerHttpRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -98,13 +98,15 @@ class UndertowServerHttpRequest extends AbstractServerHttpRequest { } @Override - public InetSocketAddress getRemoteAddress() { - return this.exchange.getSourceAddress(); + @Nullable + public InetSocketAddress getLocalAddress() { + return this.exchange.getDestinationAddress(); } @Override - public InetSocketAddress getLocalAddress() { - return this.exchange.getDestinationAddress(); + @Nullable + public InetSocketAddress getRemoteAddress() { + return this.exchange.getSourceAddress(); } @Nullable diff --git a/spring-web/src/testFixtures/java/org/springframework/web/testfixture/http/server/reactive/MockServerHttpRequest.java b/spring-web/src/testFixtures/java/org/springframework/web/testfixture/http/server/reactive/MockServerHttpRequest.java index 9a468cb9937..4f0ab79882b 100644 --- a/spring-web/src/testFixtures/java/org/springframework/web/testfixture/http/server/reactive/MockServerHttpRequest.java +++ b/spring-web/src/testFixtures/java/org/springframework/web/testfixture/http/server/reactive/MockServerHttpRequest.java @@ -24,6 +24,7 @@ import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.Locale; +import java.util.Objects; import java.util.Optional; import org.reactivestreams.Publisher; @@ -64,10 +65,10 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest { private final MultiValueMap cookies; @Nullable - private final InetSocketAddress remoteAddress; + private final InetSocketAddress localAddress; @Nullable - private final InetSocketAddress localAddress; + private final InetSocketAddress remoteAddress; @Nullable private final SslInfo sslInfo; @@ -98,25 +99,24 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest { } @Override - @SuppressWarnings("ConstantConditions") public String getMethodValue() { - return (this.httpMethod != null ? this.httpMethod.name() : this.customHttpMethod); + return (this.httpMethod != null ? this.httpMethod.name() : Objects.requireNonNull(this.customHttpMethod)); } @Override @Nullable - public InetSocketAddress getRemoteAddress() { - return this.remoteAddress; - } - - @Nullable - @Override public InetSocketAddress getLocalAddress() { return this.localAddress; } + @Override @Nullable + public InetSocketAddress getRemoteAddress() { + return this.remoteAddress; + } + @Override + @Nullable protected SslInfo initSslInfo() { return this.sslInfo; }