|
16 | 16 |
|
17 | 17 | package com.jauntsdn.rsocket;
|
18 | 18 |
|
| 19 | +import com.google.protobuf.CodedInputStream; |
19 | 20 | import com.jauntsdn.rsocket.exceptions.ApplicationErrorException;
|
| 21 | +import com.jauntsdn.rsocket.exceptions.SerializationException; |
20 | 22 | import io.netty.buffer.ByteBuf;
|
21 | 23 | import io.netty.buffer.ByteBufAllocator;
|
22 | 24 | import io.netty.buffer.ByteBufUtil;
|
23 | 25 | import io.netty.buffer.Unpooled;
|
24 | 26 | import io.netty.buffer.UnpooledByteBufAllocator;
|
25 | 27 | import io.netty.buffer.UnpooledHeapByteBuf;
|
| 28 | +import java.io.IOException; |
26 | 29 | import java.lang.annotation.ElementType;
|
27 | 30 | import java.lang.annotation.Retention;
|
28 | 31 | import java.lang.annotation.RetentionPolicy;
|
|
31 | 34 | import java.util.ArrayList;
|
32 | 35 | import java.util.List;
|
33 | 36 | import java.util.Objects;
|
| 37 | +import java.util.function.Function; |
| 38 | +import java.util.function.Supplier; |
34 | 39 |
|
35 | 40 | public final class Rpc {
|
36 | 41 |
|
@@ -423,4 +428,59 @@ public static Headers decodeHeaders(ByteBuf metadata) {
|
423 | 428 | return Headers.create(headers);
|
424 | 429 | }
|
425 | 430 | }
|
| 431 | + |
| 432 | + /** |
| 433 | + * Service descriptor used to transcode RPC / Protocol Buffers calls into another representation |
| 434 | + * (e.g. http/json) |
| 435 | + */ |
| 436 | + public static class ServiceDescriptor { |
| 437 | + private final List<Call> serviceCalls; |
| 438 | + |
| 439 | + public ServiceDescriptor(List<Call> serviceCalls) { |
| 440 | + this.serviceCalls = Objects.requireNonNull(serviceCalls, "serviceCalls"); |
| 441 | + } |
| 442 | + |
| 443 | + public final List<Call> serviceCalls() { |
| 444 | + return serviceCalls; |
| 445 | + } |
| 446 | + |
| 447 | + public static class Call { |
| 448 | + final String name; |
| 449 | + final InboundMessageFactory inMessageFactory; |
| 450 | + final OutboundMessageFactory outMessageFactory; |
| 451 | + |
| 452 | + private Call( |
| 453 | + String name, |
| 454 | + InboundMessageFactory inMessageFactory, |
| 455 | + OutboundMessageFactory outMessageFactory) { |
| 456 | + this.name = Objects.requireNonNull(name, "name"); |
| 457 | + this.inMessageFactory = Objects.requireNonNull(inMessageFactory, "inMessageFactory"); |
| 458 | + this.outMessageFactory = Objects.requireNonNull(outMessageFactory, "outMessageFactory"); |
| 459 | + } |
| 460 | + |
| 461 | + public static Call of( |
| 462 | + String name, |
| 463 | + InboundMessageFactory inMessageFactory, |
| 464 | + OutboundMessageFactory outMessageFactory) { |
| 465 | + return new Call(name, inMessageFactory, outMessageFactory); |
| 466 | + } |
| 467 | + } |
| 468 | + |
| 469 | + public interface InboundMessageFactory extends Supplier<com.google.protobuf.Message.Builder> {} |
| 470 | + |
| 471 | + public interface OutboundMessageFactory |
| 472 | + extends Function<CodedInputStream, com.google.protobuf.Message> { |
| 473 | + |
| 474 | + @Override |
| 475 | + default com.google.protobuf.Message apply(CodedInputStream codedInputStream) { |
| 476 | + try { |
| 477 | + return create(codedInputStream); |
| 478 | + } catch (IOException e) { |
| 479 | + throw new SerializationException("Protobuf deserialization error", e); |
| 480 | + } |
| 481 | + } |
| 482 | + |
| 483 | + com.google.protobuf.Message create(CodedInputStream codedInputStream) throws IOException; |
| 484 | + } |
| 485 | + } |
426 | 486 | }
|
0 commit comments