public GrpcMessageMarshaller(ByteBufAllocator alloc, SerializationFormat serializationFormat, MethodDescriptor<I, O> method, @Nullable MessageMarshaller jsonMarshaller, boolean unsafeWrapDeserializedBuffer) { this.alloc = requireNonNull(alloc, "alloc"); this.serializationFormat = requireNonNull(serializationFormat, "serializationFormat"); this.method = requireNonNull(method, "method"); this.unsafeWrapDeserializedBuffer = unsafeWrapDeserializedBuffer; checkArgument(!GrpcSerializationFormats.isJson(serializationFormat) || jsonMarshaller != null, "jsonMarshaller must be non-null when serializationFormat is JSON."); this.jsonMarshaller = jsonMarshaller; requestType = marshallerType(method.getRequestMarshaller()); responseType = marshallerType(method.getResponseMarshaller()); }
/** * Returns a {@link MessageMarshaller} with the request/response {@link Message}s of all the {@code methods} * registered. */ public static MessageMarshaller jsonMarshaller(List<MethodDescriptor<?, ?>> methods) { final MessageMarshaller.Builder builder = MessageMarshaller.builder() .omittingInsignificantWhitespace(true) .ignoringUnknownFields(true); for (MethodDescriptor<?, ?> method : methods) { marshallerPrototype(method.getRequestMarshaller()).ifPresent(builder::register); marshallerPrototype(method.getResponseMarshaller()).ifPresent(builder::register); } return builder.build(); }
public I deserializeRequest(ByteBufOrStream message) throws IOException { InputStream messageStream = message.stream(); if (message.buf() != null) { try { switch (requestType) { case PROTOBUF: final PrototypeMarshaller<I> marshaller = (PrototypeMarshaller<I>) method.getRequestMarshaller(); // PrototypeMarshaller<I>.getMessagePrototype will always parse to I @SuppressWarnings("unchecked") final I msg = (I) deserializeProto(message.buf(), (Message) marshaller.getMessagePrototype()); return msg; default: // Fallback to using the method's stream marshaller. messageStream = new ByteBufInputStream(message.buf().retain(), true); break; } } finally { if (!unsafeWrapDeserializedBuffer) { message.buf().release(); } } } try (InputStream msg = messageStream) { return method.parseRequest(msg); } }
log.trace("Intercepting method {} : req marshaller = {}, resp marshaller = {}", method.getFullMethodName(), method.getRequestMarshaller(), method.getResponseMarshaller());
public GrpcMessageMarshaller(ByteBufAllocator alloc, SerializationFormat serializationFormat, MethodDescriptor<I, O> method, @Nullable MessageMarshaller jsonMarshaller, boolean unsafeWrapDeserializedBuffer) { this.alloc = requireNonNull(alloc, "alloc"); this.serializationFormat = requireNonNull(serializationFormat, "serializationFormat"); this.method = requireNonNull(method, "method"); this.unsafeWrapDeserializedBuffer = unsafeWrapDeserializedBuffer; checkArgument(!GrpcSerializationFormats.isJson(serializationFormat) || jsonMarshaller != null, "jsonMarshaller must be non-null when serializationFormat is JSON."); this.jsonMarshaller = jsonMarshaller; requestType = marshallerType(method.getRequestMarshaller()); responseType = marshallerType(method.getResponseMarshaller()); }
@Override public void sendMessage(Object message) { MethodDescriptor.Marshaller<RequestT> requestMarshaller = methodDescriptor.getRequestMarshaller(); MethodDescriptor.Marshaller<ResponseT> responseMarshaller = methodDescriptor.getResponseMarshaller();
/** * Returns a {@link MessageMarshaller} with the request/response {@link Message}s of all the {@code methods} * registered. */ public static MessageMarshaller jsonMarshaller(List<MethodDescriptor<?, ?>> methods) { final MessageMarshaller.Builder builder = MessageMarshaller.builder() .omittingInsignificantWhitespace(true) .ignoringUnknownFields(true); for (MethodDescriptor<?, ?> method : methods) { marshallerPrototype(method.getRequestMarshaller()).ifPresent(builder::register); marshallerPrototype(method.getResponseMarshaller()).ifPresent(builder::register); } return builder.build(); }
public I deserializeRequest(ByteBufOrStream message) throws IOException { InputStream messageStream = message.stream(); if (message.buf() != null) { try { switch (requestType) { case PROTOBUF: final PrototypeMarshaller<I> marshaller = (PrototypeMarshaller<I>) method.getRequestMarshaller(); // PrototypeMarshaller<I>.getMessagePrototype will always parse to I @SuppressWarnings("unchecked") final I msg = (I) deserializeProto(message.buf(), (Message) marshaller.getMessagePrototype()); return msg; default: // Fallback to using the method's stream marshaller. messageStream = new ByteBufInputStream(message.buf().retain(), true); break; } } finally { if (!unsafeWrapDeserializedBuffer) { message.buf().release(); } } } try (InputStream msg = messageStream) { return method.parseRequest(msg); } }