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.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<String, HttpCookie> 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,9 +99,14 @@ 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 getLocalAddress() {
return this.localAddress;
}
@Override
@ -109,14 +115,8 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
return this.remoteAddress;
}
@Nullable
@Override
public InetSocketAddress getLocalAddress() {
return this.localAddress;
}
@Nullable
@Override
protected SslInfo initSslInfo() {
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");
* 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();
}
@Nullable
@Override
@Nullable
public InetSocketAddress getRemoteAddress() {
return this.originalRequest.getRemoteAddress();
}
@Override
@Nullable
protected SslInfo initSslInfo() {
return this.sslInfo;
}

View File

@ -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

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");
* 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();
/**
* 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;
}

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

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");
* 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

View File

@ -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<String, HttpCookie> 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,9 +99,14 @@ 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 getLocalAddress() {
return this.localAddress;
}
@Override
@ -109,14 +115,8 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
return this.remoteAddress;
}
@Nullable
@Override
public InetSocketAddress getLocalAddress() {
return this.localAddress;
}
@Nullable
@Override
protected SslInfo initSslInfo() {
return this.sslInfo;
}