Browse Source

Consistently check available local/remote addresses for non-null

pull/25758/head
Juergen Hoeller 6 years ago
parent
commit
f3d4df2fd4
  1. 20
      spring-test/src/main/java/org/springframework/mock/http/server/reactive/MockServerHttpRequest.java
  2. 14
      spring-web/src/main/java/org/springframework/http/server/reactive/DefaultServerHttpRequestBuilder.java
  3. 11
      spring-web/src/main/java/org/springframework/http/server/reactive/ReactorServerHttpRequest.java
  4. 12
      spring-web/src/main/java/org/springframework/http/server/reactive/ServerHttpRequest.java
  5. 14
      spring-web/src/main/java/org/springframework/http/server/reactive/ServerHttpRequestDecorator.java
  6. 13
      spring-web/src/main/java/org/springframework/http/server/reactive/ServletServerHttpRequest.java
  7. 12
      spring-web/src/main/java/org/springframework/http/server/reactive/UndertowServerHttpRequest.java
  8. 20
      spring-web/src/testFixtures/java/org/springframework/web/testfixture/http/server/reactive/MockServerHttpRequest.java

20
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.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,25 +99,24 @@ 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 @Override
@Nullable @Nullable
public InetSocketAddress getRemoteAddress() {
return this.remoteAddress;
}
@Nullable
@Override
public InetSocketAddress getLocalAddress() { public InetSocketAddress getLocalAddress() {
return this.localAddress; return this.localAddress;
} }
@Override
@Nullable @Nullable
public InetSocketAddress getRemoteAddress() {
return this.remoteAddress;
}
@Override @Override
@Nullable
protected SslInfo initSslInfo() { protected SslInfo initSslInfo() {
return this.sslInfo; return this.sslInfo;
} }

14
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"); * 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();
} }
@Override
@Nullable @Nullable
public InetSocketAddress getRemoteAddress() {
return this.originalRequest.getRemoteAddress();
}
@Override @Override
@Nullable
protected SslInfo initSslInfo() { protected SslInfo initSslInfo() {
return this.sslInfo; return this.sslInfo;
} }

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

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

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

14
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"); * 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
public InetSocketAddress getRemoteAddress() { @Nullable
return getDelegate().getRemoteAddress();
}
@Override
public InetSocketAddress getLocalAddress() { public InetSocketAddress getLocalAddress() {
return getDelegate().getLocalAddress(); return getDelegate().getLocalAddress();
} }
@Override
@Nullable @Nullable
public InetSocketAddress getRemoteAddress() {
return getDelegate().getRemoteAddress();
}
@Override @Override
@Nullable
public SslInfo getSslInfo() { public SslInfo getSslInfo() {
return getDelegate().getSslInfo(); return getDelegate().getSslInfo();
} }

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

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

20
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.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,25 +99,24 @@ 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 @Override
@Nullable @Nullable
public InetSocketAddress getRemoteAddress() {
return this.remoteAddress;
}
@Nullable
@Override
public InetSocketAddress getLocalAddress() { public InetSocketAddress getLocalAddress() {
return this.localAddress; return this.localAddress;
} }
@Override
@Nullable @Nullable
public InetSocketAddress getRemoteAddress() {
return this.remoteAddress;
}
@Override @Override
@Nullable
protected SslInfo initSslInfo() { protected SslInfo initSslInfo() {
return this.sslInfo; return this.sslInfo;
} }

Loading…
Cancel
Save