Java Code Examples for io.grpc.Metadata#BinaryMarshaller

The following examples show how to use io.grpc.Metadata#BinaryMarshaller . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MoreMetadata.java    From grpc-java-contrib with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * A metadata marshaller that encodes objects as protobuf according to their proto IDL specification.
 *
 * @param clazz the type to serialize
 * @param <T>
 */
public static <T extends GeneratedMessageV3> Metadata.BinaryMarshaller<T> PROTOBUF_MARSHALLER(Class<T> clazz) {
    try {
        Method defaultInstance = clazz.getMethod("getDefaultInstance");
        GeneratedMessageV3 instance = (GeneratedMessageV3) defaultInstance.invoke(null);

        return new Metadata.BinaryMarshaller<T>() {
            @Override
            public byte[] toBytes(T value) {
                return value.toByteArray();
            }

            @SuppressWarnings("unchecked")
            @Override
            public T parseBytes(byte[] serialized) {
                try {
                    return (T) instance.getParserForType().parseFrom(serialized);
                } catch (InvalidProtocolBufferException ipbe) {
                    throw new IllegalArgumentException(ipbe);
                }
            }
        };
    } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
        throw new IllegalStateException(ex);
    }
}
 
Example 2
Source File: ProtoLiteUtilsTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Test
public void metadataMarshaller_invalid() {
  Metadata.BinaryMarshaller<Type> metadataMarshaller =
      ProtoLiteUtils.metadataMarshaller(Type.getDefaultInstance());
  try {
    metadataMarshaller.parseBytes(new byte[] {-127});
    fail("Expected exception");
  } catch (IllegalArgumentException ex) {
    assertNotNull(((InvalidProtocolBufferException) ex.getCause()).getUnfinishedMessage());
  }
}
 
Example 3
Source File: GrpcUtil.java    From saluki with Apache License 2.0 5 votes vote down vote up
private static Metadata.BinaryMarshaller<String> utf8Marshaller() {
  return new Metadata.BinaryMarshaller<String>() {

    @Override
    public byte[] toBytes(String value) {
      return value.getBytes(Charsets.UTF_8);
    }

    @Override
    public String parseBytes(byte[] serialized) {
      return new String(serialized, Charsets.UTF_8);
    }
  };
}
 
Example 4
Source File: MoreMetadataTest.java    From grpc-java-contrib with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void protobufMarshallerRoundtrip() {
    HelloRequest request = HelloRequest.newBuilder().setName("World").build();

    Metadata.BinaryMarshaller<HelloRequest> marshaller = MoreMetadata.PROTOBUF_MARSHALLER(HelloRequest.class);
    byte[] bytes = marshaller.toBytes(request);
    HelloRequest request2 = marshaller.parseBytes(bytes);

    assertThat(request2).isEqualTo(request);
}
 
Example 5
Source File: ProtoLiteUtilsTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void metadataMarshaller_invalid() {
  Metadata.BinaryMarshaller<Type> metadataMarshaller =
      ProtoLiteUtils.metadataMarshaller(Type.getDefaultInstance());
  try {
    metadataMarshaller.parseBytes(new byte[] {-127});
    fail("Expected exception");
  } catch (IllegalArgumentException ex) {
    assertNotNull(((InvalidProtocolBufferException) ex.getCause()).getUnfinishedMessage());
  }
}
 
Example 6
Source File: ProtoLiteUtilsTest.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
@Test
public void metadataMarshaller_roundtrip() {
  Metadata.BinaryMarshaller<Type> metadataMarshaller =
      ProtoLiteUtils.metadataMarshaller(Type.getDefaultInstance());
  assertEquals(proto, metadataMarshaller.parseBytes(metadataMarshaller.toBytes(proto)));
}
 
Example 7
Source File: ProtoLiteUtilsTest.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
@Test
public void metadataMarshaller_roundtrip() {
  Metadata.BinaryMarshaller<Type> metadataMarshaller =
      ProtoLiteUtils.metadataMarshaller(Type.getDefaultInstance());
  assertEquals(proto, metadataMarshaller.parseBytes(metadataMarshaller.toBytes(proto)));
}
 
Example 8
Source File: ProtoUtils.java    From grpc-nebula-java with Apache License 2.0 2 votes vote down vote up
/**
 * Produce a metadata marshaller for a protobuf type.
 * 
 * @since 1.13.0
 */
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/4477")
public static <T extends Message> Metadata.BinaryMarshaller<T> metadataMarshaller(T instance) {
  return ProtoLiteUtils.metadataMarshaller(instance);
}
 
Example 9
Source File: ProtoLiteUtils.java    From grpc-nebula-java with Apache License 2.0 2 votes vote down vote up
/**
 * Produce a metadata marshaller for a protobuf type.
 *
 * @since 1.0.0
 */
public static <T extends MessageLite> Metadata.BinaryMarshaller<T> metadataMarshaller(
    T defaultInstance) {
  return new MetadataMarshaller<T>(defaultInstance);
}
 
Example 10
Source File: ProtoUtils.java    From grpc-java with Apache License 2.0 2 votes vote down vote up
/**
 * Produce a metadata marshaller for a protobuf type.
 * 
 * @since 1.13.0
 */
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/4477")
public static <T extends Message> Metadata.BinaryMarshaller<T> metadataMarshaller(T instance) {
  return ProtoLiteUtils.metadataMarshaller(instance);
}
 
Example 11
Source File: ProtoLiteUtils.java    From grpc-java with Apache License 2.0 2 votes vote down vote up
/**
 * Produce a metadata marshaller for a protobuf type.
 *
 * @since 1.0.0
 */
public static <T extends MessageLite> Metadata.BinaryMarshaller<T> metadataMarshaller(
    T defaultInstance) {
  return new MetadataMarshaller<>(defaultInstance);
}