Search in sources :

Example 1 with MessageLite

use of com.google.protobuf.MessageLite in project retrofit by square.

the class ProtoConverterFactory method responseBodyConverter.

@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
    if (!(type instanceof Class<?>)) {
        return null;
    }
    Class<?> c = (Class<?>) type;
    if (!MessageLite.class.isAssignableFrom(c)) {
        return null;
    }
    Parser<MessageLite> parser;
    try {
        Method method = c.getDeclaredMethod("parser");
        // noinspection unchecked
        parser = (Parser<MessageLite>) method.invoke(null);
    } catch (InvocationTargetException e) {
        throw new RuntimeException(e.getCause());
    } catch (NoSuchMethodException | IllegalAccessException ignored) {
        // If the method is missing, fall back to original static field for pre-3.0 support.
        try {
            Field field = c.getDeclaredField("PARSER");
            // noinspection unchecked
            parser = (Parser<MessageLite>) field.get(null);
        } catch (NoSuchFieldException | IllegalAccessException e) {
            throw new IllegalArgumentException("Found a protobuf message but " + c.getName() + " had no parser() method or PARSER field.");
        }
    }
    return new ProtoResponseBodyConverter<>(parser, registry);
}
Also used : Method(java.lang.reflect.Method) MessageLite(com.google.protobuf.MessageLite) InvocationTargetException(java.lang.reflect.InvocationTargetException) Parser(com.google.protobuf.Parser) Field(java.lang.reflect.Field)

Example 2 with MessageLite

use of com.google.protobuf.MessageLite in project ballerina by ballerina-lang.

the class Message method writeTo.

public void writeTo(com.google.protobuf.CodedOutputStream output) throws java.io.IOException {
    Descriptors.Descriptor messageDescriptor = getDescriptor();
    for (Descriptors.FieldDescriptor fieldDescriptor : messageDescriptor.getFields()) {
        if (fields.containsKey(fieldDescriptor.getName())) {
            switch(fieldDescriptor.getType().toProto().getNumber()) {
                case DescriptorProtos.FieldDescriptorProto.Type.TYPE_DOUBLE_VALUE:
                    {
                        Object msgObject = fields.get(fieldDescriptor.getName());
                        if (MessageUtils.isArray(msgObject)) {
                            Double[] messages = (Double[]) msgObject;
                            for (Double message : messages) {
                                output.writeDouble(fieldDescriptor.getNumber(), message);
                            }
                        } else {
                            output.writeDouble(fieldDescriptor.getNumber(), (Double) msgObject);
                        }
                        break;
                    }
                case DescriptorProtos.FieldDescriptorProto.Type.TYPE_FLOAT_VALUE:
                    {
                        Object msgObject = fields.get(fieldDescriptor.getName());
                        if (MessageUtils.isArray(msgObject)) {
                            Float[] messages = (Float[]) msgObject;
                            for (Float message : messages) {
                                output.writeFloat(fieldDescriptor.getNumber(), message);
                            }
                        } else {
                            output.writeFloat(fieldDescriptor.getNumber(), (Float) msgObject);
                        }
                        break;
                    }
                case DescriptorProtos.FieldDescriptorProto.Type.TYPE_INT64_VALUE:
                    {
                        Object msgObject = fields.get(fieldDescriptor.getName());
                        if (MessageUtils.isArray(msgObject)) {
                            Long[] messages = (Long[]) msgObject;
                            for (Long message : messages) {
                                output.writeInt64(fieldDescriptor.getNumber(), message);
                            }
                        } else {
                            output.writeInt64(fieldDescriptor.getNumber(), (Long) msgObject);
                        }
                        break;
                    }
                case DescriptorProtos.FieldDescriptorProto.Type.TYPE_UINT64_VALUE:
                    {
                        Object msgObject = fields.get(fieldDescriptor.getName());
                        if (MessageUtils.isArray(msgObject)) {
                            Long[] messages = (Long[]) msgObject;
                            for (Long message : messages) {
                                output.writeUInt64(fieldDescriptor.getNumber(), message);
                            }
                        } else {
                            output.writeUInt64(fieldDescriptor.getNumber(), (Long) msgObject);
                        }
                        break;
                    }
                case DescriptorProtos.FieldDescriptorProto.Type.TYPE_INT32_VALUE:
                    {
                        Object msgObject = fields.get(fieldDescriptor.getName());
                        if (MessageUtils.isArray(msgObject)) {
                            Integer[] messages = (Integer[]) msgObject;
                            for (Integer message : messages) {
                                output.writeInt32(fieldDescriptor.getNumber(), message);
                            }
                        } else {
                            output.writeInt32(fieldDescriptor.getNumber(), (Integer) msgObject);
                        }
                        break;
                    }
                case DescriptorProtos.FieldDescriptorProto.Type.TYPE_FIXED64_VALUE:
                    {
                        Object msgObject = fields.get(fieldDescriptor.getName());
                        if (MessageUtils.isArray(msgObject)) {
                            Long[] messages = (Long[]) msgObject;
                            for (Long message : messages) {
                                output.writeFixed64(fieldDescriptor.getNumber(), message);
                            }
                        } else {
                            output.writeFixed64(fieldDescriptor.getNumber(), (Long) msgObject);
                        }
                        break;
                    }
                case DescriptorProtos.FieldDescriptorProto.Type.TYPE_FIXED32_VALUE:
                    {
                        Object msgObject = fields.get(fieldDescriptor.getName());
                        if (MessageUtils.isArray(msgObject)) {
                            Integer[] messages = (Integer[]) msgObject;
                            for (Integer message : messages) {
                                output.writeFixed32(fieldDescriptor.getNumber(), message);
                            }
                        } else {
                            output.writeFixed32(fieldDescriptor.getNumber(), (Integer) msgObject);
                        }
                        break;
                    }
                case DescriptorProtos.FieldDescriptorProto.Type.TYPE_BOOL_VALUE:
                    {
                        Object msgObject = fields.get(fieldDescriptor.getName());
                        if (MessageUtils.isArray(msgObject)) {
                            Boolean[] messages = (Boolean[]) msgObject;
                            for (Boolean message : messages) {
                                output.writeBool(fieldDescriptor.getNumber(), message);
                            }
                        } else {
                            output.writeBool(fieldDescriptor.getNumber(), (Boolean) msgObject);
                        }
                        break;
                    }
                case DescriptorProtos.FieldDescriptorProto.Type.TYPE_STRING_VALUE:
                    {
                        Object msgObject = fields.get(fieldDescriptor.getName());
                        if (MessageUtils.isArray(msgObject)) {
                            String[] messages = (String[]) msgObject;
                            for (String message : messages) {
                                output.writeString(fieldDescriptor.getNumber(), message);
                            }
                        } else {
                            com.google.protobuf.GeneratedMessageV3.writeString(output, fieldDescriptor.getNumber(), msgObject);
                        }
                        break;
                    }
                case DescriptorProtos.FieldDescriptorProto.Type.TYPE_MESSAGE_VALUE:
                    {
                        Object msgObject = fields.get(fieldDescriptor.getName());
                        if (MessageUtils.isArray(msgObject)) {
                            Object[] messages = (Object[]) msgObject;
                            for (Object message : messages) {
                                output.writeMessage(fieldDescriptor.getNumber(), (MessageLite) message);
                            }
                        } else {
                            output.writeMessage(fieldDescriptor.getNumber(), (MessageLite) msgObject);
                        }
                        break;
                    }
                default:
                    {
                        throw new UnsupportedFieldTypeException("Error while writing output stream. Field " + "type is not supported : " + fieldDescriptor.getType());
                    }
            }
        }
    }
    unknownFields.writeTo(output);
}
Also used : UnsupportedFieldTypeException(org.ballerinalang.net.grpc.exception.UnsupportedFieldTypeException) Descriptors(com.google.protobuf.Descriptors) MessageLite(com.google.protobuf.MessageLite)

Example 3 with MessageLite

use of com.google.protobuf.MessageLite in project tink by google.

the class Ed25519PrivateKeyManagerTest method testBasic.

@Test
public void testBasic() throws Exception {
    Ed25519PrivateKeyManager manager = new Ed25519PrivateKeyManager();
    KeyTemplate template = SignatureKeyTemplates.ED25519;
    MessageLite key = manager.newKey(template);
    assertTrue(key instanceof Ed25519PrivateKey);
    Ed25519PrivateKey keyProto = (Ed25519PrivateKey) key;
    assertEquals(32, keyProto.getKeyValue().size());
    PublicKeySign signer = manager.getPrimitive(key);
    assertTrue(signer instanceof Ed25519Sign);
    byte[] message = Random.randBytes(20);
    byte[] signature = signer.sign(message);
    assertEquals(64, signature.length);
    Ed25519PublicKeyManager publicKeyManager = new Ed25519PublicKeyManager();
    PublicKeyVerify verifier = publicKeyManager.getPrimitive(keyProto.getPublicKey());
    assertTrue(verifier instanceof Ed25519Verify);
    try {
        verifier.verify(signature, message);
    } catch (GeneralSecurityException e) {
        fail("Do not expect GeneralSecurityException: " + e);
    }
}
Also used : Ed25519PrivateKey(com.google.crypto.tink.proto.Ed25519PrivateKey) Ed25519Verify(com.google.crypto.tink.subtle.Ed25519Verify) GeneralSecurityException(java.security.GeneralSecurityException) PublicKeyVerify(com.google.crypto.tink.PublicKeyVerify) Ed25519Sign(com.google.crypto.tink.subtle.Ed25519Sign) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) MessageLite(com.google.protobuf.MessageLite) PublicKeySign(com.google.crypto.tink.PublicKeySign) Test(org.junit.Test)

Example 4 with MessageLite

use of com.google.protobuf.MessageLite in project j2objc by google.

the class CompatibilityTest method testMessageLiteToBuilderAndMergeFrom.

public void testMessageLiteToBuilderAndMergeFrom() throws Exception {
    TypicalData input = TypicalData.newBuilder().setMyInt(123).build();
    MessageLite msg = TypicalData.getDefaultInstance();
    // mergeFrom(byte[], ExtensionRegistryLite)
    MessageLite.Builder builder = msg.toBuilder();
    builder.mergeFrom(input.toByteString().toByteArray(), ExtensionRegistry.getEmptyRegistry());
    assertEquals(123, ((TypicalData) builder.build()).getMyInt());
    // mergeFrom(byte[])
    builder = msg.toBuilder();
    builder.mergeFrom(input.toByteString().toByteArray());
    assertEquals(123, ((TypicalData) builder.build()).getMyInt());
    // mergeFrom(ByteString, ExtensionRegistryLite)
    builder = msg.toBuilder();
    builder.mergeFrom(input.toByteString(), ExtensionRegistry.getEmptyRegistry());
    assertEquals(123, ((TypicalData) builder.build()).getMyInt());
    // mergeFrom(ByteString)
    builder = msg.toBuilder();
    builder.mergeFrom(input.toByteString());
    assertEquals(123, ((TypicalData) builder.build()).getMyInt());
}
Also used : TypicalData(protos.TypicalData) MessageLite(com.google.protobuf.MessageLite)

Example 5 with MessageLite

use of com.google.protobuf.MessageLite in project j2objc by google.

the class CompatibilityTest method testMessageLiteInterface.

public void testMessageLiteInterface() throws Exception {
    ExtensionRegistryLite registry = ExtensionRegistryLite.newInstance();
    TypicalData data = TypicalData.newBuilder().build();
    MessageLite messageLite = data;
    MessageLite.Builder builderLite = messageLite.newBuilderForType();
    messageLite.writeTo(new ByteArrayOutputStream());
    messageLite.writeDelimitedTo(new ByteArrayOutputStream());
    builderLite.mergeFrom(new ByteArrayInputStream(new byte[0]));
    builderLite.mergeFrom(new ByteArrayInputStream(new byte[0]), registry);
    builderLite.mergeDelimitedFrom(new ByteArrayInputStream(new byte[0]));
    builderLite.mergeDelimitedFrom(new ByteArrayInputStream(new byte[0]), registry);
    assertEquals(0, messageLite.getSerializedSize());
}
Also used : TypicalData(protos.TypicalData) ByteArrayInputStream(java.io.ByteArrayInputStream) ExtensionRegistryLite(com.google.protobuf.ExtensionRegistryLite) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MessageLite(com.google.protobuf.MessageLite)

Aggregations

MessageLite (com.google.protobuf.MessageLite)17 PublicKeySign (com.google.crypto.tink.PublicKeySign)2 PublicKeyVerify (com.google.crypto.tink.PublicKeyVerify)2 Ed25519PrivateKey (com.google.crypto.tink.proto.Ed25519PrivateKey)2 KeyTemplate (com.google.crypto.tink.proto.KeyTemplate)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 GeneralSecurityException (java.security.GeneralSecurityException)2 Test (org.junit.Test)2 TypicalData (protos.TypicalData)2 Ed25519Sign (com.google.crypto.tink.subtle.Ed25519Sign)1 Ed25519Verify (com.google.crypto.tink.subtle.Ed25519Verify)1 Descriptors (com.google.protobuf.Descriptors)1 ExtensionRegistryLite (com.google.protobuf.ExtensionRegistryLite)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 Parser (com.google.protobuf.Parser)1 Deadline (io.grpc.Deadline)1 ForwardingClientCallListener (io.grpc.ForwardingClientCallListener)1 Metadata (io.grpc.Metadata)1 Status (io.grpc.Status)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1