diff --git a/src/main/java/am/ik/rsocket/Args.java b/src/main/java/am/ik/rsocket/Args.java index 0acce17..b32761d 100644 --- a/src/main/java/am/ik/rsocket/Args.java +++ b/src/main/java/am/ik/rsocket/Args.java @@ -342,6 +342,13 @@ static ByteBuf addCompositeMetadata(ByteBuf metadata, String mimeType) { return composite; } + static ByteBuf addCompositeMetadata(ByteBuf metadata, WellKnownMimeType mimeType) { + final ByteBufAllocator allocator = new PooledByteBufAllocator(true); + final CompositeByteBuf composite = allocator.compositeBuffer(); + CompositeMetadataCodec.encodeAndAddMetadata(composite, allocator, mimeType, metadata); + return composite; + } + public Optional setupPayload() { final Optional payload = this.setupData() .map(data -> this.setupMetadata() @@ -378,10 +385,7 @@ else if (metadataList.size() != mimeTypeList.size()) { if (metadataList.isEmpty()) { return Tuples.of(DEFAULT_METADATA_MIME_TYPE, Unpooled.buffer()); } - // use composite metadata if setup payload exists - if (metadataList.size() == 1 && !this.setupPayload().isPresent()) { - return Tuples.of(mimeTypeList.get(0), metadataList.get(0)); - } + // Always use composite metadata final CompositeByteBuf compositeByteBuf = Unpooled.compositeBuffer(); final ByteBufAllocator allocator = new PooledByteBufAllocator(true); final Iterator mimeTypeIterator = mimeTypeList.iterator(); diff --git a/src/main/java/am/ik/rsocket/Rsc.java b/src/main/java/am/ik/rsocket/Rsc.java index b6e4522..5b26f56 100644 --- a/src/main/java/am/ik/rsocket/Rsc.java +++ b/src/main/java/am/ik/rsocket/Rsc.java @@ -63,6 +63,9 @@ public static void main(String[] a) throws Exception { args.printHelp(System.out); System.exit(2); } + if (args.debug()) { + configureDebugLevel("io.rsocket.FrameLogger"); + } if (args.secure() && System.getenv("JAVA_HOME") != null) { final File javaHome = new File(System.getenv("JAVA_HOME")); if (System.getProperty("java.library.path") == null @@ -100,9 +103,6 @@ public static void main(String[] a) throws Exception { } static Flux run(Args args) { - if (args.debug()) { - configureDebugLevel("io.rsocket.FrameLogger"); - } args.log().ifPresent(Rsc::configureDebugLevel); final ClientTransport clientTransport = args.clientTransport(); final RSocketConnector connector = RSocketConnector.create(); diff --git a/src/test/java/am/ik/rsocket/ArgsTest.java b/src/test/java/am/ik/rsocket/ArgsTest.java index bb498ff..211e349 100644 --- a/src/test/java/am/ik/rsocket/ArgsTest.java +++ b/src/test/java/am/ik/rsocket/ArgsTest.java @@ -27,6 +27,7 @@ import io.netty.buffer.ByteBufAllocator; import io.netty.buffer.Unpooled; import io.rsocket.metadata.TracingMetadataCodec.Flags; +import io.rsocket.metadata.WellKnownMimeType; import io.rsocket.transport.ClientTransport; import io.rsocket.transport.netty.client.TcpClientTransport; import io.rsocket.transport.netty.client.WebsocketClientTransport; @@ -108,8 +109,8 @@ void resumeEnabledWithDuration() { void route() { final Args args = new Args(new String[] { "tcp://localhost:8080", "-r", "locate.aircrafts.for" }); final Tuple2 metadata = args.composeMetadata(); - assertThat(metadata.getT1()).isEqualTo("message/x.rsocket.routing.v0"); - assertThat(metadata.getT2()).isEqualTo(new Route("locate.aircrafts.for").toMetadata(ByteBufAllocator.DEFAULT)); + assertThat(metadata.getT1()).isEqualTo("message/x.rsocket.composite-metadata.v0"); + assertThat(metadata.getT2()).isEqualTo(addCompositeMetadata(new Route("locate.aircrafts.for").toMetadata(ByteBufAllocator.DEFAULT), WellKnownMimeType.MESSAGE_RSOCKET_ROUTING)); } @Test @@ -124,8 +125,8 @@ void metadataDefault() { void metadata() { final Args args = new Args("tcp://localhost:8080 -m {\"foo\":\"bar\"}"); final Tuple2 metadata = args.composeMetadata(); - assertThat(metadata.getT1()).isEqualTo(DEFAULT_METADATA_MIME_TYPE); - assertThat(metadata.getT2().toString(UTF_8)).isEqualTo("{\"foo\":\"bar\"}"); + assertThat(metadata.getT1()).isEqualTo("message/x.rsocket.composite-metadata.v0"); + assertThat(metadata.getT2()).isEqualTo(addCompositeMetadata(Unpooled.wrappedBuffer("{\"foo\":\"bar\"}".getBytes()), WellKnownMimeType.APPLICATION_JSON)); } @Test @@ -133,8 +134,8 @@ void metadataSingle() { final Args args = new Args(new String[] { "tcp://localhost:8080", "--metadataMimeType", "application/vnd.spring.rsocket.metadata+json", "-m", "{\"route\":\"locate.aircrafts.for\"}" }); final Tuple2 metadata = args.composeMetadata(); - assertThat(metadata.getT1()).isEqualTo("application/vnd.spring.rsocket.metadata+json"); - assertThat(metadata.getT2().toString(UTF_8)).isEqualTo("{\"route\":\"locate.aircrafts.for\"}"); + assertThat(metadata.getT1()).isEqualTo("message/x.rsocket.composite-metadata.v0"); + assertThat(metadata.getT2()).isEqualTo(addCompositeMetadata(Unpooled.wrappedBuffer("{\"route\":\"locate.aircrafts.for\"}".getBytes()), "application/vnd.spring.rsocket.metadata+json")); } @Test @@ -262,16 +263,16 @@ void setupMetadataBasicAuth() { void metadataAuthSimple() { final Args args = new Args("tcp://localhost:8080 --authSimple user:password"); final Tuple2 metadata = args.composeMetadata(); - assertThat(metadata.getT1()).isEqualTo("message/x.rsocket.authentication.v0"); - assertThat(metadata.getT2()).isEqualTo(new SimpleAuthentication("user", "password").toMetadata(ByteBufAllocator.DEFAULT)); + assertThat(metadata.getT1()).isEqualTo("message/x.rsocket.composite-metadata.v0"); + assertThat(metadata.getT2()).isEqualTo(addCompositeMetadata(new SimpleAuthentication("user", "password").toMetadata(ByteBufAllocator.DEFAULT), WellKnownMimeType.MESSAGE_RSOCKET_AUTHENTICATION)); } @Test void metadataAuthBearer() { final Args args = new Args("tcp://localhost:8080 --authBearer TOKEN"); final Tuple2 metadata = args.composeMetadata(); - assertThat(metadata.getT1()).isEqualTo("message/x.rsocket.authentication.v0"); - assertThat(metadata.getT2()).isEqualTo(new BearerAuthentication("TOKEN").toMetadata(ByteBufAllocator.DEFAULT)); + assertThat(metadata.getT1()).isEqualTo("message/x.rsocket.composite-metadata.v0"); + assertThat(metadata.getT2()).isEqualTo(addCompositeMetadata(new BearerAuthentication("TOKEN").toMetadata(ByteBufAllocator.DEFAULT), WellKnownMimeType.MESSAGE_RSOCKET_AUTHENTICATION)); } @Test