Consistently check available local/remote addresses for non-null

This commit is contained in:
Juergen Hoeller
2020-06-06 15:15:16 +02:00
parent 9cd9a8e86b
commit f3d4df2fd4
8 changed files with 64 additions and 54 deletions

View File

@@ -24,6 +24,7 @@ import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import org.reactivestreams.Publisher; import org.reactivestreams.Publisher;
@@ -64,10 +65,10 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
private final MultiValueMap<String, HttpCookie> cookies; private final MultiValueMap<String, HttpCookie> cookies;
@Nullable @Nullable
private final InetSocketAddress remoteAddress; private final InetSocketAddress localAddress;
@Nullable @Nullable
private final InetSocketAddress localAddress; private final InetSocketAddress remoteAddress;
@Nullable @Nullable
private final SslInfo sslInfo; private final SslInfo sslInfo;
@@ -98,9 +99,14 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
} }
@Override @Override
@SuppressWarnings("ConstantConditions")
public String getMethodValue() { 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 getLocalAddress() {
return this.localAddress;
} }
@Override @Override
@@ -109,14 +115,8 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
return this.remoteAddress; return this.remoteAddress;
} }
@Nullable
@Override @Override
public InetSocketAddress getLocalAddress() {
return this.localAddress;
}
@Nullable @Nullable
@Override
protected SslInfo initSslInfo() { protected SslInfo initSslInfo() {
return this.sslInfo; return this.sslInfo;
} }

View File

@@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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; return this.cookies;
} }
@Nullable
@Override @Override
public InetSocketAddress getRemoteAddress() {
return this.originalRequest.getRemoteAddress();
}
@Nullable @Nullable
@Override
public InetSocketAddress getLocalAddress() { public InetSocketAddress getLocalAddress() {
return this.originalRequest.getLocalAddress(); return this.originalRequest.getLocalAddress();
} }
@Nullable
@Override @Override
@Nullable
public InetSocketAddress getRemoteAddress() {
return this.originalRequest.getRemoteAddress();
}
@Override
@Nullable
protected SslInfo initSslInfo() { protected SslInfo initSslInfo() {
return this.sslInfo; return this.sslInfo;
} }

View File

@@ -96,6 +96,7 @@ class ReactorServerHttpRequest extends AbstractServerHttpRequest {
} }
else { else {
InetSocketAddress localAddress = request.hostAddress(); InetSocketAddress localAddress = request.hostAddress();
Assert.state(localAddress != null, "No host address available");
return new URI(scheme, null, localAddress.getHostString(), return new URI(scheme, null, localAddress.getHostString(),
localAddress.getPort(), null, null, null); localAddress.getPort(), null, null, null);
} }
@@ -151,13 +152,15 @@ class ReactorServerHttpRequest extends AbstractServerHttpRequest {
} }
@Override @Override
public InetSocketAddress getRemoteAddress() { @Nullable
return this.request.remoteAddress(); public InetSocketAddress getLocalAddress() {
return this.request.hostAddress();
} }
@Override @Override
public InetSocketAddress getLocalAddress() { @Nullable
return this.request.hostAddress(); public InetSocketAddress getRemoteAddress() {
return this.request.remoteAddress();
} }
@Override @Override

View File

@@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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<String, HttpCookie> getCookies(); MultiValueMap<String, HttpCookie> 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 @Nullable
default InetSocketAddress getRemoteAddress() { default InetSocketAddress getLocalAddress() {
return null; return null;
} }
/** /**
* Return the local address the request was accepted on, if available. * Return the remote address where this request is connected to, if available.
* 5.2.3
*/ */
@Nullable @Nullable
default InetSocketAddress getLocalAddress() { default InetSocketAddress getRemoteAddress() {
return null; return null;
} }

View File

@@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -97,17 +97,19 @@ public class ServerHttpRequestDecorator implements ServerHttpRequest {
} }
@Override @Override
@Nullable
public InetSocketAddress getLocalAddress() {
return getDelegate().getLocalAddress();
}
@Override
@Nullable
public InetSocketAddress getRemoteAddress() { public InetSocketAddress getRemoteAddress() {
return getDelegate().getRemoteAddress(); return getDelegate().getRemoteAddress();
} }
@Override @Override
public InetSocketAddress getLocalAddress() {
return getDelegate().getLocalAddress();
}
@Nullable @Nullable
@Override
public SslInfo getSslInfo() { public SslInfo getSslInfo() {
return getDelegate().getSslInfo(); return getDelegate().getSslInfo();
} }

View File

@@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.HttpCookie;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.LinkedCaseInsensitiveMap; import org.springframework.util.LinkedCaseInsensitiveMap;
@@ -174,13 +175,15 @@ class ServletServerHttpRequest extends AbstractServerHttpRequest {
} }
@Override @Override
public InetSocketAddress getRemoteAddress() { @NonNull
return new InetSocketAddress(this.request.getRemoteHost(), this.request.getRemotePort()); public InetSocketAddress getLocalAddress() {
return new InetSocketAddress(this.request.getLocalAddr(), this.request.getLocalPort());
} }
@Override @Override
public InetSocketAddress getLocalAddress() { @NonNull
return new InetSocketAddress(this.request.getLocalAddr(), this.request.getLocalPort()); public InetSocketAddress getRemoteAddress() {
return new InetSocketAddress(this.request.getRemoteHost(), this.request.getRemotePort());
} }
@Override @Override

View File

@@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -98,13 +98,15 @@ class UndertowServerHttpRequest extends AbstractServerHttpRequest {
} }
@Override @Override
public InetSocketAddress getRemoteAddress() { @Nullable
return this.exchange.getSourceAddress(); public InetSocketAddress getLocalAddress() {
return this.exchange.getDestinationAddress();
} }
@Override @Override
public InetSocketAddress getLocalAddress() { @Nullable
return this.exchange.getDestinationAddress(); public InetSocketAddress getRemoteAddress() {
return this.exchange.getSourceAddress();
} }
@Nullable @Nullable

View File

@@ -24,6 +24,7 @@ import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import org.reactivestreams.Publisher; import org.reactivestreams.Publisher;
@@ -64,10 +65,10 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
private final MultiValueMap<String, HttpCookie> cookies; private final MultiValueMap<String, HttpCookie> cookies;
@Nullable @Nullable
private final InetSocketAddress remoteAddress; private final InetSocketAddress localAddress;
@Nullable @Nullable
private final InetSocketAddress localAddress; private final InetSocketAddress remoteAddress;
@Nullable @Nullable
private final SslInfo sslInfo; private final SslInfo sslInfo;
@@ -98,9 +99,14 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
} }
@Override @Override
@SuppressWarnings("ConstantConditions")
public String getMethodValue() { 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 getLocalAddress() {
return this.localAddress;
} }
@Override @Override
@@ -109,14 +115,8 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
return this.remoteAddress; return this.remoteAddress;
} }
@Nullable
@Override @Override
public InetSocketAddress getLocalAddress() {
return this.localAddress;
}
@Nullable @Nullable
@Override
protected SslInfo initSslInfo() { protected SslInfo initSslInfo() {
return this.sslInfo; return this.sslInfo;
} }