Skip to content

Commit 2f4c39b

Browse files
committed
Add classpath detection for Reactor Netty 2
See gh-28847
1 parent fa1f7f6 commit 2f4c39b

File tree

6 files changed

+35
-32
lines changed

6 files changed

+35
-32
lines changed

spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClientBuilder.java

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -28,6 +28,7 @@
2828
import org.springframework.http.client.reactive.HttpComponentsClientHttpConnector;
2929
import org.springframework.http.client.reactive.JettyClientHttpConnector;
3030
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
31+
import org.springframework.http.client.reactive.ReactorNetty2ClientHttpConnector;
3132
import org.springframework.http.codec.ClientCodecConfigurer;
3233
import org.springframework.lang.Nullable;
3334
import org.springframework.util.Assert;
@@ -51,7 +52,9 @@
5152
*/
5253
class DefaultWebTestClientBuilder implements WebTestClient.Builder {
5354

54-
private static final boolean reactorClientPresent;
55+
private static final boolean reactorNettyClientPresent;
56+
57+
private static final boolean reactorNetty2ClientPresent;
5558

5659
private static final boolean jettyClientPresent;
5760

@@ -61,7 +64,8 @@ class DefaultWebTestClientBuilder implements WebTestClient.Builder {
6164

6265
static {
6366
ClassLoader loader = DefaultWebTestClientBuilder.class.getClassLoader();
64-
reactorClientPresent = ClassUtils.isPresent("reactor.netty.http.client.HttpClient", loader);
67+
reactorNettyClientPresent = ClassUtils.isPresent("reactor.netty.http.client.HttpClient", loader);
68+
reactorNetty2ClientPresent = ClassUtils.isPresent("reactor.netty5.http.client.HttpClient", loader);
6569
jettyClientPresent = ClassUtils.isPresent("org.eclipse.jetty.client.HttpClient", loader);
6670
httpComponentsClientPresent =
6771
ClassUtils.isPresent("org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient", loader) &&
@@ -301,9 +305,12 @@ public WebTestClient build() {
301305
}
302306

303307
private static ClientHttpConnector initConnector() {
304-
if (reactorClientPresent) {
308+
if (reactorNettyClientPresent) {
305309
return new ReactorClientHttpConnector();
306310
}
311+
else if (reactorNetty2ClientPresent) {
312+
return new ReactorNetty2ClientHttpConnector();
313+
}
307314
else if (jettyClientPresent) {
308315
return new JettyClientHttpConnector();
309316
}

spring-web/src/main/java/org/springframework/http/client/reactive/ReactorNetty2ClientHttpResponse.java

+1-10
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import org.springframework.http.HttpStatusCode;
3838
import org.springframework.http.ResponseCookie;
3939
import org.springframework.lang.Nullable;
40-
import org.springframework.util.ClassUtils;
4140
import org.springframework.util.CollectionUtils;
4241
import org.springframework.util.LinkedMultiValueMap;
4342
import org.springframework.util.MultiValueMap;
@@ -54,11 +53,6 @@
5453
*/
5554
class ReactorNetty2ClientHttpResponse implements ClientHttpResponse {
5655

57-
/** Reactor Netty 1.0.5+. */
58-
static final boolean reactorNettyRequestChannelOperationsIdPresent = ClassUtils.isPresent(
59-
"reactor.netty5.ChannelOperationsId", ReactorNetty2ClientHttpResponse.class.getClassLoader());
60-
61-
6256
private static final Log logger = LogFactory.getLog(ReactorNetty2ClientHttpResponse.class);
6357

6458
private final HttpClientResponse response;
@@ -102,10 +96,7 @@ public ReactorNetty2ClientHttpResponse(HttpClientResponse response, NettyInbound
10296

10397
@Override
10498
public String getId() {
105-
String id = null;
106-
if (reactorNettyRequestChannelOperationsIdPresent) {
107-
id = ChannelOperationsIdHelper.getId(this.response);
108-
}
99+
String id = ChannelOperationsIdHelper.getId(this.response);
109100
if (id == null && this.response instanceof Connection connection) {
110101
id = connection.channel().id().asShortText();
111102
}

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

+3-10
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import org.springframework.http.HttpMethod;
4040
import org.springframework.lang.Nullable;
4141
import org.springframework.util.Assert;
42-
import org.springframework.util.ClassUtils;
4342
import org.springframework.util.LinkedMultiValueMap;
4443
import org.springframework.util.MultiValueMap;
4544

@@ -53,10 +52,6 @@
5352
*/
5453
class ReactorNetty2ServerHttpRequest extends AbstractServerHttpRequest {
5554

56-
/** Reactor Netty 1.0.5+. */
57-
static final boolean reactorNettyRequestChannelOperationsIdPresent = ClassUtils.isPresent(
58-
"reactor.netty.ChannelOperationsId", ReactorNetty2ServerHttpRequest.class.getClassLoader());
59-
6055
private static final Log logger = HttpLogging.forLogName(ReactorNetty2ServerHttpRequest.class);
6156

6257

@@ -213,11 +208,9 @@ protected String initId() {
213208

214209
@Override
215210
protected String initLogPrefix() {
216-
if (reactorNettyRequestChannelOperationsIdPresent) {
217-
String id = (ChannelOperationsIdHelper.getId(this.request));
218-
if (id != null) {
219-
return id;
220-
}
211+
String id = (ChannelOperationsIdHelper.getId(this.request));
212+
if (id != null) {
213+
return id;
221214
}
222215
if (this.request instanceof Connection) {
223216
return ((Connection) this.request).channel().id().asShortText() +

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

+2-4
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,8 @@ private Publisher<Buffer> toByteBufs(Publisher<? extends DataBuffer> dataBuffers
128128
@Override
129129
protected void touchDataBuffer(DataBuffer buffer) {
130130
if (logger.isDebugEnabled()) {
131-
if (ReactorNetty2ServerHttpRequest.reactorNettyRequestChannelOperationsIdPresent) {
132-
if (ChannelOperationsIdHelper.touch(buffer, this.response)) {
133-
return;
134-
}
131+
if (ChannelOperationsIdHelper.touch(buffer, this.response)) {
132+
return;
135133
}
136134
this.response.withConnection(connection -> {
137135
ChannelId id = connection.channel().id();

spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClientBuilder.java

+10-3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.springframework.http.client.reactive.JdkClientHttpConnector;
3535
import org.springframework.http.client.reactive.JettyClientHttpConnector;
3636
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
37+
import org.springframework.http.client.reactive.ReactorNetty2ClientHttpConnector;
3738
import org.springframework.http.codec.ClientCodecConfigurer;
3839
import org.springframework.lang.Nullable;
3940
import org.springframework.util.Assert;
@@ -53,15 +54,18 @@
5354
*/
5455
final class DefaultWebClientBuilder implements WebClient.Builder {
5556

56-
private static final boolean reactorClientPresent;
57+
private static final boolean reactorNettyClientPresent;
58+
59+
private static final boolean reactorNetty2ClientPresent;
5760

5861
private static final boolean jettyClientPresent;
5962

6063
private static final boolean httpComponentsClientPresent;
6164

6265
static {
6366
ClassLoader loader = DefaultWebClientBuilder.class.getClassLoader();
64-
reactorClientPresent = ClassUtils.isPresent("reactor.netty.http.client.HttpClient", loader);
67+
reactorNettyClientPresent = ClassUtils.isPresent("reactor.netty.http.client.HttpClient", loader);
68+
reactorNetty2ClientPresent = ClassUtils.isPresent("reactor.netty5.http.client.HttpClient", loader);
6569
jettyClientPresent = ClassUtils.isPresent("org.eclipse.jetty.client.HttpClient", loader);
6670
httpComponentsClientPresent =
6771
ClassUtils.isPresent("org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient", loader) &&
@@ -306,9 +310,12 @@ public WebClient build() {
306310
}
307311

308312
private ClientHttpConnector initConnector() {
309-
if (reactorClientPresent) {
313+
if (reactorNettyClientPresent) {
310314
return new ReactorClientHttpConnector();
311315
}
316+
else if (reactorNetty2ClientPresent) {
317+
return new ReactorNetty2ClientHttpConnector();
318+
}
312319
else if (jettyClientPresent) {
313320
return new JettyClientHttpConnector();
314321
}

spring-webflux/src/main/java/org/springframework/web/reactive/socket/server/support/HandshakeWebSocketService.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -74,12 +74,15 @@ public class HandshakeWebSocketService implements WebSocketService, Lifecycle {
7474

7575
private static final boolean reactorNettyPresent;
7676

77+
private static final boolean reactorNetty2Present;
78+
7779
static {
7880
ClassLoader loader = HandshakeWebSocketService.class.getClassLoader();
7981
tomcatPresent = ClassUtils.isPresent("org.apache.tomcat.websocket.server.WsHttpUpgradeHandler", loader);
8082
jettyPresent = ClassUtils.isPresent("org.eclipse.jetty.websocket.server.JettyWebSocketServerContainer", loader);
8183
undertowPresent = ClassUtils.isPresent("io.undertow.websockets.WebSocketProtocolHandshakeHandler", loader);
8284
reactorNettyPresent = ClassUtils.isPresent("reactor.netty.http.server.HttpServerResponse", loader);
85+
reactorNetty2Present = ClassUtils.isPresent("reactor.netty5.http.server.HttpServerResponse", loader);
8386
}
8487

8588

@@ -126,6 +129,10 @@ else if (reactorNettyPresent) {
126129
// As late as possible (Reactor Netty commonly used for WebClient)
127130
className = "ReactorNettyRequestUpgradeStrategy";
128131
}
132+
else if (reactorNetty2Present) {
133+
// As late as possible (Reactor Netty commonly used for WebClient)
134+
className = "ReactorNetty2RequestUpgradeStrategy";
135+
}
129136
else {
130137
throw new IllegalStateException("No suitable default RequestUpgradeStrategy found");
131138
}

0 commit comments

Comments
 (0)