Clean up conditional use of ChannelOperationsId

Closes gh-29224
This commit is contained in:
rstoyanchev 2022-09-30 13:03:13 +01:00
parent e81d30160c
commit 5d2c9d946a
6 changed files with 36 additions and 105 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2021 the original author or authors. * Copyright 2002-2022 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.
@ -26,6 +26,7 @@ import io.netty.handler.codec.http.cookie.DefaultCookie;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
import reactor.netty.ChannelOperationsId;
import reactor.netty.Connection; import reactor.netty.Connection;
import reactor.netty.NettyInbound; import reactor.netty.NettyInbound;
import reactor.netty.http.client.HttpClientResponse; import reactor.netty.http.client.HttpClientResponse;
@ -37,7 +38,6 @@ import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatusCode; import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseCookie; import org.springframework.http.ResponseCookie;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.util.ClassUtils;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap; import org.springframework.util.MultiValueMap;
@ -53,11 +53,6 @@ import org.springframework.util.ObjectUtils;
*/ */
class ReactorClientHttpResponse implements ClientHttpResponse { class ReactorClientHttpResponse implements ClientHttpResponse {
/** Reactor Netty 1.0.5+. */
static final boolean reactorNettyRequestChannelOperationsIdPresent = ClassUtils.isPresent(
"reactor.netty.ChannelOperationsId", ReactorClientHttpResponse.class.getClassLoader());
private static final Log logger = LogFactory.getLog(ReactorClientHttpResponse.class); private static final Log logger = LogFactory.getLog(ReactorClientHttpResponse.class);
private final HttpClientResponse response; private final HttpClientResponse response;
@ -102,8 +97,8 @@ class ReactorClientHttpResponse implements ClientHttpResponse {
@Override @Override
public String getId() { public String getId() {
String id = null; String id = null;
if (reactorNettyRequestChannelOperationsIdPresent) { if (this.response instanceof ChannelOperationsId operationsId) {
id = ChannelOperationsIdHelper.getId(this.response); id = (logger.isDebugEnabled() ? operationsId.asLongText() : operationsId.asShortText());
} }
if (id == null && this.response instanceof Connection connection) { if (id == null && this.response instanceof Connection connection) {
id = connection.channel().id().asShortText(); id = connection.channel().id().asShortText();
@ -201,16 +196,4 @@ class ReactorClientHttpResponse implements ClientHttpResponse {
"status=" + getRawStatusCode() + '}'; "status=" + getRawStatusCode() + '}';
} }
private static class ChannelOperationsIdHelper {
@Nullable
public static String getId(HttpClientResponse response) {
if (response instanceof reactor.netty.ChannelOperationsId id) {
return (logger.isDebugEnabled() ? id.asLongText() : id.asShortText());
}
return null;
}
}
} }

View File

@ -25,6 +25,7 @@ import io.netty5.handler.codec.http.cookie.DefaultCookie;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
import reactor.netty5.ChannelOperationsId;
import reactor.netty5.Connection; import reactor.netty5.Connection;
import reactor.netty5.NettyInbound; import reactor.netty5.NettyInbound;
import reactor.netty5.http.client.HttpClientResponse; import reactor.netty5.http.client.HttpClientResponse;
@ -82,7 +83,10 @@ class ReactorNetty2ClientHttpResponse implements ClientHttpResponse {
@Override @Override
public String getId() { public String getId() {
String id = ChannelOperationsIdHelper.getId(this.response); String id = null;
if (this.response instanceof ChannelOperationsId operationsId) {
id = (logger.isDebugEnabled() ? operationsId.asLongText() : operationsId.asShortText());
}
if (id == null && this.response instanceof Connection connection) { if (id == null && this.response instanceof Connection connection) {
id = connection.channel().id().asShortText(); id = connection.channel().id().asShortText();
} }
@ -176,16 +180,4 @@ class ReactorNetty2ClientHttpResponse implements ClientHttpResponse {
"status=" + getRawStatusCode() + '}'; "status=" + getRawStatusCode() + '}';
} }
private static class ChannelOperationsIdHelper {
@Nullable
public static String getId(HttpClientResponse response) {
if (response instanceof reactor.netty5.ChannelOperationsId id) {
return (logger.isDebugEnabled() ? id.asLongText() : id.asShortText());
}
return null;
}
}
} }

View File

@ -29,6 +29,7 @@ import io.netty5.handler.codec.http.cookie.Cookie;
import io.netty5.handler.ssl.SslHandler; import io.netty5.handler.ssl.SslHandler;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
import reactor.netty5.ChannelOperationsId;
import reactor.netty5.Connection; import reactor.netty5.Connection;
import reactor.netty5.http.server.HttpServerRequest; import reactor.netty5.http.server.HttpServerRequest;
@ -208,7 +209,10 @@ class ReactorNetty2ServerHttpRequest extends AbstractServerHttpRequest {
@Override @Override
protected String initLogPrefix() { protected String initLogPrefix() {
String id = (ChannelOperationsIdHelper.getId(this.request)); String id = null;
if (this.request instanceof ChannelOperationsId operationsId) {
id = (logger.isDebugEnabled() ? operationsId.asLongText() : operationsId.asShortText());
}
if (id != null) { if (id != null) {
return id; return id;
} }
@ -219,18 +223,4 @@ class ReactorNetty2ServerHttpRequest extends AbstractServerHttpRequest {
return getId(); return getId();
} }
private static class ChannelOperationsIdHelper {
@Nullable
public static String getId(HttpServerRequest request) {
if (request instanceof reactor.netty.ChannelOperationsId) {
return (logger.isDebugEnabled() ?
((reactor.netty.ChannelOperationsId) request).asLongText() :
((reactor.netty.ChannelOperationsId) request).asShortText());
}
return null;
}
}
} }

View File

@ -128,28 +128,16 @@ class ReactorNetty2ServerHttpResponse extends AbstractServerHttpResponse impleme
@Override @Override
protected void touchDataBuffer(DataBuffer buffer) { protected void touchDataBuffer(DataBuffer buffer) {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
if (ChannelOperationsIdHelper.touch(buffer, this.response)) { if (this.response instanceof ChannelOperationsId operationsId) {
return; DataBufferUtils.touch(buffer, "Channel id: " + operationsId.asLongText());
}
else {
this.response.withConnection(connection -> {
ChannelId id = connection.channel().id();
DataBufferUtils.touch(buffer, "Channel id: " + id.asShortText());
});
} }
this.response.withConnection(connection -> {
ChannelId id = connection.channel().id();
DataBufferUtils.touch(buffer, "Channel id: " + id.asShortText());
});
} }
} }
private static class ChannelOperationsIdHelper {
public static boolean touch(DataBuffer dataBuffer, HttpServerResponse response) {
if (response instanceof ChannelOperationsId) {
String id = ((ChannelOperationsId) response).asLongText();
DataBufferUtils.touch(dataBuffer, "Channel id: " + id);
return true;
}
return false;
}
}
} }

View File

@ -29,6 +29,7 @@ import io.netty.handler.codec.http.cookie.Cookie;
import io.netty.handler.ssl.SslHandler; import io.netty.handler.ssl.SslHandler;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
import reactor.netty.ChannelOperationsId;
import reactor.netty.Connection; import reactor.netty.Connection;
import reactor.netty.http.server.HttpServerRequest; import reactor.netty.http.server.HttpServerRequest;
@ -207,7 +208,10 @@ class ReactorServerHttpRequest extends AbstractServerHttpRequest {
@Override @Override
protected String initLogPrefix() { protected String initLogPrefix() {
String id = (ChannelOperationsIdHelper.getId(this.request)); String id = null;
if (this.request instanceof ChannelOperationsId operationsId) {
id = (logger.isDebugEnabled() ? operationsId.asLongText() : operationsId.asShortText());
}
if (id != null) { if (id != null) {
return id; return id;
} }
@ -218,18 +222,4 @@ class ReactorServerHttpRequest extends AbstractServerHttpRequest {
return getId(); return getId();
} }
private static class ChannelOperationsIdHelper {
@Nullable
public static String getId(HttpServerRequest request) {
if (request instanceof reactor.netty.ChannelOperationsId) {
return (logger.isDebugEnabled() ?
((reactor.netty.ChannelOperationsId) request).asLongText() :
((reactor.netty.ChannelOperationsId) request).asShortText());
}
return null;
}
}
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2021 the original author or authors. * Copyright 2002-2022 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.
@ -127,28 +127,16 @@ class ReactorServerHttpResponse extends AbstractServerHttpResponse implements Ze
@Override @Override
protected void touchDataBuffer(DataBuffer buffer) { protected void touchDataBuffer(DataBuffer buffer) {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
if (ChannelOperationsIdHelper.touch(buffer, this.response)) { if (this.response instanceof ChannelOperationsId operationsId) {
return; DataBufferUtils.touch(buffer, "Channel id: " + operationsId.asLongText());
}
else {
this.response.withConnection(connection -> {
ChannelId id = connection.channel().id();
DataBufferUtils.touch(buffer, "Channel id: " + id.asShortText());
});
} }
this.response.withConnection(connection -> {
ChannelId id = connection.channel().id();
DataBufferUtils.touch(buffer, "Channel id: " + id.asShortText());
});
} }
} }
private static class ChannelOperationsIdHelper {
public static boolean touch(DataBuffer dataBuffer, HttpServerResponse response) {
if (response instanceof reactor.netty.ChannelOperationsId) {
String id = ((ChannelOperationsId) response).asLongText();
DataBufferUtils.touch(dataBuffer, "Channel id: " + id);
return true;
}
return false;
}
}
} }