|
| 1 | +/* |
| 2 | + * Copyright 2015-Present the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.rsocket.examples.transport.tcp.metadata.routing; |
| 18 | + |
| 19 | +import io.netty.buffer.ByteBuf; |
| 20 | +import io.netty.buffer.ByteBufAllocator; |
| 21 | +import io.netty.buffer.ByteBufUtil; |
| 22 | +import io.netty.buffer.CompositeByteBuf; |
| 23 | +import io.rsocket.RSocket; |
| 24 | +import io.rsocket.SocketAcceptor; |
| 25 | +import io.rsocket.core.RSocketConnector; |
| 26 | +import io.rsocket.core.RSocketServer; |
| 27 | +import io.rsocket.metadata.CompositeMetadata; |
| 28 | +import io.rsocket.metadata.CompositeMetadataCodec; |
| 29 | +import io.rsocket.metadata.RoutingMetadata; |
| 30 | +import io.rsocket.metadata.TaggingMetadataCodec; |
| 31 | +import io.rsocket.metadata.WellKnownMimeType; |
| 32 | +import io.rsocket.transport.netty.client.TcpClientTransport; |
| 33 | +import io.rsocket.transport.netty.server.TcpServerTransport; |
| 34 | +import io.rsocket.util.ByteBufPayload; |
| 35 | +import java.util.Collections; |
| 36 | +import java.util.Objects; |
| 37 | +import org.slf4j.Logger; |
| 38 | +import org.slf4j.LoggerFactory; |
| 39 | +import reactor.core.publisher.Mono; |
| 40 | + |
| 41 | +public class CompositeMetadataExample { |
| 42 | + static final Logger logger = LoggerFactory.getLogger(CompositeMetadataExample.class); |
| 43 | + |
| 44 | + public static void main(String[] args) { |
| 45 | + RSocketServer.create( |
| 46 | + SocketAcceptor.forRequestResponse( |
| 47 | + payload -> { |
| 48 | + final String route = decodeRoute(payload.sliceMetadata()); |
| 49 | + |
| 50 | + logger.info("Received RequestResponse[route={}]", route); |
| 51 | + |
| 52 | + payload.release(); |
| 53 | + |
| 54 | + if ("my.test.route".equals(route)) { |
| 55 | + return Mono.just(ByteBufPayload.create("Hello From My Test Route")); |
| 56 | + } |
| 57 | + |
| 58 | + return Mono.error(new IllegalArgumentException("Route " + route + " not found")); |
| 59 | + })) |
| 60 | + .bindNow(TcpServerTransport.create("localhost", 7000)); |
| 61 | + |
| 62 | + RSocket socket = |
| 63 | + RSocketConnector.create() |
| 64 | + // here we specify that every metadata payload will be encoded using |
| 65 | + // CompositeMetadata layout as specified in the following subspec |
| 66 | + // https://github.com/rsocket/rsocket/blob/master/Extensions/CompositeMetadata.md |
| 67 | + .metadataMimeType(WellKnownMimeType.MESSAGE_RSOCKET_COMPOSITE_METADATA.getString()) |
| 68 | + .connect(TcpClientTransport.create("localhost", 7000)) |
| 69 | + .block(); |
| 70 | + |
| 71 | + final ByteBuf routeMetadata = |
| 72 | + TaggingMetadataCodec.createTaggingContent( |
| 73 | + ByteBufAllocator.DEFAULT, Collections.singletonList("my.test.route")); |
| 74 | + final CompositeByteBuf compositeMetadata = ByteBufAllocator.DEFAULT.compositeBuffer(); |
| 75 | + |
| 76 | + CompositeMetadataCodec.encodeAndAddMetadata( |
| 77 | + compositeMetadata, |
| 78 | + ByteBufAllocator.DEFAULT, |
| 79 | + WellKnownMimeType.MESSAGE_RSOCKET_ROUTING, |
| 80 | + routeMetadata); |
| 81 | + |
| 82 | + socket |
| 83 | + .requestResponse( |
| 84 | + ByteBufPayload.create( |
| 85 | + ByteBufUtil.writeUtf8(ByteBufAllocator.DEFAULT, "HelloWorld"), compositeMetadata)) |
| 86 | + .log() |
| 87 | + .block(); |
| 88 | + } |
| 89 | + |
| 90 | + static String decodeRoute(ByteBuf metadata) { |
| 91 | + final CompositeMetadata compositeMetadata = new CompositeMetadata(metadata, false); |
| 92 | + |
| 93 | + for (CompositeMetadata.Entry metadatum : compositeMetadata) { |
| 94 | + if (Objects.requireNonNull(metadatum.getMimeType()) |
| 95 | + .equals(WellKnownMimeType.MESSAGE_RSOCKET_ROUTING.getString())) { |
| 96 | + return new RoutingMetadata(metadatum.getContent()).iterator().next(); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + return null; |
| 101 | + } |
| 102 | +} |
0 commit comments