Support ByteBuf as a metadata value

This allows encoding of metadata values externally via some
existing API for encoding RSocket metadata, rather than relying on
registered Encoders.

Also remove explicit checks for DataBuffer since those are supported
through the DataBufferEncoder (registered by default).

See gh-23513
This commit is contained in:
Rossen Stoyanchev
2019-09-02 13:09:09 +01:00
parent 71f3498a26
commit b144c72937
4 changed files with 36 additions and 34 deletions

View File

@@ -37,7 +37,6 @@ import org.springframework.core.codec.Decoder;
import org.springframework.core.io.buffer.NettyDataBuffer;
import org.springframework.core.io.buffer.NettyDataBufferFactory;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.MimeType;
/**

View File

@@ -215,9 +215,6 @@ final class DefaultRSocketRequester implements RSocketRequester {
@SuppressWarnings("unchecked")
private <T> DataBuffer encodeData(T value, ResolvableType elementType, @Nullable Encoder<?> encoder) {
if (value instanceof DataBuffer) {
return (DataBuffer) value;
}
if (encoder == null) {
elementType = ResolvableType.forInstance(value);
encoder = strategies.encoder(elementType, dataMimeType);

View File

@@ -167,11 +167,14 @@ final class MetadataEncoder {
CompositeMetadataFlyweight.encodeAndAddMetadata(composite, this.allocator,
WellKnownMimeType.MESSAGE_RSOCKET_ROUTING, encodeRoute());
}
this.metadata.forEach((value, mimeType) ->
CompositeMetadataFlyweight.encodeAndAddMetadata(composite, this.allocator,
mimeType.toString(), PayloadUtils.asByteBuf(encodeEntry(value, mimeType))));
this.metadata.forEach((value, mimeType) -> {
ByteBuf metadata = (value instanceof ByteBuf ?
(ByteBuf) value : PayloadUtils.asByteBuf(encodeEntry(value, mimeType)));
CompositeMetadataFlyweight.encodeAndAddMetadata(
composite, this.allocator, mimeType.toString(), metadata);
});
return asDataBuffer(composite);
}
}
catch (Throwable ex) {
composite.release();
throw ex;
@@ -179,7 +182,8 @@ final class MetadataEncoder {
}
else if (this.route != null) {
Assert.isTrue(this.metadata.isEmpty(), "Composite metadata required for route and other entries");
return this.metadataMimeType.toString().equals(WellKnownMimeType.MESSAGE_RSOCKET_ROUTING.getString()) ?
String routingMimeType = WellKnownMimeType.MESSAGE_RSOCKET_ROUTING.getString();
return this.metadataMimeType.toString().equals(routingMimeType) ?
asDataBuffer(encodeRoute()) :
encodeEntry(this.route, this.metadataMimeType);
}
@@ -202,8 +206,8 @@ final class MetadataEncoder {
@SuppressWarnings("unchecked")
private <T> DataBuffer encodeEntry(Object metadata, MimeType mimeType) {
if (metadata instanceof DataBuffer) {
return (DataBuffer) metadata;
if (metadata instanceof ByteBuf) {
return asDataBuffer((ByteBuf) metadata);
}
ResolvableType type = ResolvableType.forInstance(metadata);
Encoder<T> encoder = this.strategies.encoder(type, mimeType);