Skip to content

Commit bd59c7a

Browse files
committed
Polishing contribution
Closes gh-29310
1 parent 997d1b3 commit bd59c7a

File tree

4 files changed

+39
-25
lines changed

4 files changed

+39
-25
lines changed

spring-core/src/test/java/org/springframework/core/io/buffer/DataBufferTests.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,6 @@ void asByteBuffer(DataBufferFactory bufferFactory) {
524524
}
525525

526526
@ParameterizedDataBufferAllocatingTest
527-
@SuppressWarnings("deprecation")
528527
void asByteBufferIndexLength(DataBufferFactory bufferFactory) {
529528
super.bufferFactory = bufferFactory;
530529

@@ -534,12 +533,10 @@ void asByteBufferIndexLength(DataBufferFactory bufferFactory) {
534533
ByteBuffer result = buffer.asByteBuffer(1, 2);
535534
assertThat(result.capacity()).isEqualTo(2);
536535

537-
boolean isNetty5DataBufferFactory = bufferFactory instanceof Netty5DataBufferFactory;
538-
if (isNetty5DataBufferFactory) {
536+
assumeFalse(bufferFactory instanceof Netty5DataBufferFactory, () -> {
539537
DataBufferUtils.release(buffer);
540-
}
541-
542-
assumeFalse(isNetty5DataBufferFactory, "Netty 5 does share the internal buffer");
538+
return "Netty 5 does share the internal buffer";
539+
});
543540

544541
buffer.write((byte) 'c');
545542
assertThat(result.remaining()).isEqualTo(2);

spring-core/src/testFixtures/java/org/springframework/core/testfixture/codec/AbstractDecoderTests.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 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.
@@ -208,12 +208,12 @@ protected <T> void testDecode(Publisher<DataBuffer> input, ResolvableType output
208208
protected void testDecodeError(Publisher<DataBuffer> input, ResolvableType outputType,
209209
@Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
210210

211-
Flux<DataBuffer> buffer = Mono.from(input).concatWith(Flux.error(new InputException()));
211+
Flux<DataBuffer> flux = Mono.from(input).concatWith(Flux.error(new InputException()));
212212
assertThatExceptionOfType(InputException.class).isThrownBy(() ->
213-
this.decoder.decode(buffer, outputType, mimeType, hints)
214-
.doOnNext(o -> {
215-
if (o instanceof Buffer buf) {
216-
buf.close();
213+
this.decoder.decode(flux, outputType, mimeType, hints)
214+
.doOnNext(object -> {
215+
if (object instanceof Buffer buffer) {
216+
buffer.close();
217217
}
218218
})
219219
.blockLast(Duration.ofSeconds(5)));
@@ -234,9 +234,9 @@ protected void testDecodeCancel(Publisher<DataBuffer> input, ResolvableType outp
234234
@Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
235235

236236
Flux<?> result = this.decoder.decode(input, outputType, mimeType, hints)
237-
.doOnNext(o -> {
238-
if (o instanceof Buffer buf) {
239-
buf.close();
237+
.doOnNext(object -> {
238+
if (object instanceof Buffer buffer) {
239+
buffer.close();
240240
}
241241
});
242242
StepVerifier.create(result).expectNextCount(1).thenCancel().verify();

spring-webflux/src/main/java/org/springframework/web/reactive/socket/WebSocketMessage.java

Lines changed: 26 additions & 9 deletions
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.
@@ -24,6 +24,7 @@
2424
import org.springframework.core.io.buffer.Netty5DataBufferFactory;
2525
import org.springframework.lang.Nullable;
2626
import org.springframework.util.Assert;
27+
import org.springframework.util.ClassUtils;
2728
import org.springframework.util.ObjectUtils;
2829

2930
/**
@@ -37,6 +38,10 @@
3738
*/
3839
public class WebSocketMessage {
3940

41+
private static final boolean reactorNetty2Present = ClassUtils.isPresent(
42+
"io.netty5.handler.codec.http.websocketx.WebSocketFrame", WebSocketMessage.class.getClassLoader());
43+
44+
4045
private final Type type;
4146

4247
private final DataBuffer payload;
@@ -129,15 +134,11 @@ public String getPayloadAsText(Charset charset) {
129134
* @see DataBufferUtils#retain(DataBuffer)
130135
*/
131136
public WebSocketMessage retain() {
132-
if (!(this.nativeMessage instanceof io.netty5.handler.codec.http.websocketx.WebSocketFrame frame) ) {
133-
DataBufferUtils.retain(this.payload);
134-
return this;
135-
}
136-
else {
137-
io.netty5.handler.codec.http.websocketx.WebSocketFrame newFrame = frame.send().receive();
138-
DataBuffer newPayload = ((Netty5DataBufferFactory) this.payload.factory()).wrap(newFrame.binaryData());
139-
return new WebSocketMessage(this.type, newPayload, newFrame);
137+
if (reactorNetty2Present) {
138+
return ReactorNetty2Helper.retain(this);
140139
}
140+
DataBufferUtils.retain(this.payload);
141+
return this;
141142
}
142143

143144
/**
@@ -199,4 +200,20 @@ public enum Type {
199200
PONG
200201
}
201202

203+
204+
private static class ReactorNetty2Helper {
205+
206+
static WebSocketMessage retain(WebSocketMessage message) {
207+
if (message.nativeMessage instanceof io.netty5.handler.codec.http.websocketx.WebSocketFrame netty5Frame) {
208+
io.netty5.handler.codec.http.websocketx.WebSocketFrame frame = netty5Frame.send().receive();
209+
DataBuffer payload = ((Netty5DataBufferFactory) message.payload.factory()).wrap(frame.binaryData());
210+
return new WebSocketMessage(message.type, payload, frame);
211+
}
212+
else {
213+
DataBufferUtils.retain(message.payload);
214+
return message;
215+
}
216+
}
217+
}
218+
202219
}

spring-webflux/src/test/java/org/springframework/web/reactive/socket/WebSocketIntegrationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 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.

0 commit comments

Comments
 (0)