Java Code Examples for com.google.protobuf.Message

The following examples show how to use com.google.protobuf.Message. These examples are extracted from open source projects. 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 Project: parquet-mr   Source File: ProtoInputOutputFormatTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testProto3CustomProtoClass() throws Exception {
  TestProto3.FirstCustomClassMessage.Builder inputMessage;
  inputMessage = TestProto3.FirstCustomClassMessage.newBuilder();
  inputMessage.setString("writtenString");

  Path outputPath = new WriteUsingMR().write(new Message[]{inputMessage.build()});
  ReadUsingMR readUsingMR = new ReadUsingMR();
  String customClass = TestProto3.SecondCustomClassMessage.class.getName();
  ProtoReadSupport.setProtobufClass(readUsingMR.getConfiguration(), customClass);
  List<Message> result = readUsingMR.read(outputPath);

  assertEquals(1, result.size());
  Message msg = result.get(0);
  assertFalse("Class from header returned.",
    msg instanceof TestProto3.FirstCustomClassMessage);
  assertTrue("Custom class was not used",
    msg instanceof TestProto3.SecondCustomClassMessage);

  String stringValue;
  stringValue = ((TestProto3.SecondCustomClassMessage) msg).getString();
  assertEquals("writtenString", stringValue);
}
 
Example 2
@Nonnull
@Override
public <M extends Message> List<Key.Evaluated> evaluateFunction(@Nullable FDBRecord<M> record, @Nullable Message message, @Nonnull Key.Evaluated arguments) {
    SpatialObject spatialObject;
    try {
        spatialObject = parseSpatialObject(arguments);
    } catch (ParseException ex) {
        throw new RecordCoreException(ex);
    }
    if (spatialObject == null) {
        return Collections.singletonList(Key.Evaluated.NULL);
    }
    long[] zs = new long[spatialObject.maxZ()];
    GeophileSpatial.shuffle(space, spatialObject, zs);
    List<Key.Evaluated> result = new ArrayList<>(zs.length);
    for (long z : zs) {
        if (z == Space.Z_NULL) {
            break;
        }
        result.add(Key.Evaluated.scalar(z));
    }
    return result;
}
 
Example 3
Source Project: metastore   Source File: ProtoLanguageFileWriter.java    License: Apache License 2.0 6 votes vote down vote up
private void writeOptionForList(
    Descriptors.FieldDescriptor fieldDescriptor, Object value, int indent, String optionType) {
  if (fieldDescriptor.getFullName().startsWith("google.protobuf." + optionType + "Options")) {
    writer.print(fieldDescriptor.getName());
  } else {
    writer.print("(");
    writer.print(fieldDescriptor.getFullName());
    writer.print(")");
  }
  writer.print(" = ");
  if (fieldDescriptor.getType() == Descriptors.FieldDescriptor.Type.MESSAGE) {
    writeMessageValue((Message) value, indent + 1);
  } else {
    writeValue(fieldDescriptor, value);
  }
}
 
Example 4
@Test
public void testSaveInstanceState() throws Exception {

  runTest(TestRequestHandler.RequestType.SAVE_INSTANCE_STATE,
      new HeronServerTester.SuccessResponseHandler(
          CheckpointManager.SaveInstanceStateResponse.class,
          new HeronServerTester.TestResponseHandler() {
            @Override
            public void handleResponse(HeronClient client, StatusCode status,
                                       Object ctx, Message response) throws Exception {
              verify(statefulStorage).storeCheckpoint(
                  any(CheckpointInfo.class), any(Checkpoint.class));
              assertEquals(CHECKPOINT_ID,
                  ((CheckpointManager.SaveInstanceStateResponse) response).getCheckpointId());
              assertEquals(instance,
                  ((CheckpointManager.SaveInstanceStateResponse) response).getInstance());
            }
          })
  );
}
 
Example 5
Source Project: parquet-mr   Source File: ProtoInputOutputFormatTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testProto3RepeatedInnerMessageClass() throws Exception {
  TestProto3.RepeatedInnerMessage msgEmpty = TestProto3.RepeatedInnerMessage.newBuilder().build();
  TestProto3.RepeatedInnerMessage msgNonEmpty = TestProto3.RepeatedInnerMessage.newBuilder()
    .addRepeatedInnerMessage(TestProto3.InnerMessage.newBuilder().setOne("one").build())
    .addRepeatedInnerMessage(TestProto3.InnerMessage.newBuilder().setTwo("two").build())
    .build();

  Path outputPath = new WriteUsingMR().write(msgEmpty, msgNonEmpty);
  ReadUsingMR readUsingMR = new ReadUsingMR();
  String customClass = TestProto3.RepeatedInnerMessage.class.getName();
  ProtoReadSupport.setProtobufClass(readUsingMR.getConfiguration(), customClass);
  List<Message> result = readUsingMR.read(outputPath);

  assertEquals(2, result.size());
  assertEquals(msgEmpty, result.get(0));
  assertEquals(msgNonEmpty, result.get(1));
}
 
Example 6
Source Project: twister2   Source File: RRServer.java    License: Apache License 2.0 6 votes vote down vote up
protected TCPMessage sendMessage(Message message, RequestID requestID, SocketChannel channel) {
  byte[] data = message.toByteArray();
  String messageType = message.getDescriptorForType().getFullName();

  // lets serialize the message
  int capacity = requestID.getId().length + data.length + messageType.getBytes().length + 8;
  ByteBuffer buffer = ByteBuffer.allocate(capacity);
  // we send message id, worker id and data
  buffer.put(requestID.getId());
  // pack the name of the message
  ByteUtils.packString(messageType, buffer);
  // pack the worker id
  buffer.putInt(serverID);
  // pack data
  buffer.put(data);

  TCPMessage send = server.send(channel, buffer, capacity, 0);
  if (send != null) {
    pendingSendCount++;
  }
  return send;
}
 
Example 7
Source Project: krpc   Source File: RpcFutureUtils.java    License: Apache License 2.0 6 votes vote down vote up
public void accept(T m) {
    try {
        if( closure != null ) {
            closure.restoreContext();
        }
        action.accept(m);
    }  catch (Throwable e) {

        String traceId = "no_trace_id";
        if( Trace.currentContext() != null && Trace.currentContext().getTrace() != null )
            traceId  = Trace.currentContext().getTrace().getTraceId();
        log.error("accept exception, traceId="+traceId, e);

        if( closure != null ) {
            Message bizErrorMsg = createBizErrorMessage(closure);
            if( bizErrorMsg != null ) {
                closure.done(bizErrorMsg);
            }
        }
        throw e;
    }
}
 
Example 8
Source Project: parquet-mr   Source File: ProtoInputOutputFormatTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testProto3RepeatedIntMessageClassSchemaCompliant() throws Exception {
  TestProto3.RepeatedIntMessage msgEmpty = TestProto3.RepeatedIntMessage.newBuilder().build();
  TestProto3.RepeatedIntMessage msgNonEmpty = TestProto3.RepeatedIntMessage.newBuilder()
    .addRepeatedInt(1).addRepeatedInt(2)
    .build();

  Configuration conf = new Configuration();
  ProtoWriteSupport.setWriteSpecsCompliant(conf, true);

  Path outputPath = new WriteUsingMR(conf).write(msgEmpty, msgNonEmpty);
  ReadUsingMR readUsingMR = new ReadUsingMR();
  String customClass = TestProto3.RepeatedIntMessage.class.getName();
  ProtoReadSupport.setProtobufClass(readUsingMR.getConfiguration(), customClass);
  List<Message> result = readUsingMR.read(outputPath);

  assertEquals(2, result.size());
  assertEquals(msgEmpty, result.get(0));
  assertEquals(msgNonEmpty, result.get(1));
}
 
Example 9
Source Project: parquet-mr   Source File: ProtoInputOutputFormatTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testProto3MapIntMessageClassSchemaCompliant() throws Exception {
  TestProto3.MapIntMessage msgEmpty = TestProto3.MapIntMessage.newBuilder().build();
  TestProto3.MapIntMessage msgNonEmpty = TestProto3.MapIntMessage.newBuilder()
    .putMapInt(1, 123).putMapInt(2, 234)
    .build();

  Configuration conf = new Configuration();
  ProtoWriteSupport.setWriteSpecsCompliant(conf, true);

  Path outputPath = new WriteUsingMR(conf).write(msgEmpty, msgNonEmpty);
  ReadUsingMR readUsingMR = new ReadUsingMR(conf);
  String customClass = TestProto3.MapIntMessage.class.getName();
  ProtoReadSupport.setProtobufClass(readUsingMR.getConfiguration(), customClass);
  List<Message> result = readUsingMR.read(outputPath);

  assertEquals(2, result.size());
  assertEquals(msgEmpty, result.get(0));
  assertEquals(msgNonEmpty, result.get(1));
}
 
Example 10
Source Project: fuchsia   Source File: ProtobufQueryHandler.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * @see org.apache.cxf.transports.http.QueryHandler#writeResponse(String,.
 * String, org.apache.cxf.service.model.EndpointInfo,
 * java.io.OutputStream)
 */
@SuppressWarnings("unchecked")
public void writeResponse(String fullQueryString, String ctx,
                          EndpointInfo endpoint, OutputStream os) {
    try {
        Class<? extends Message> messageClass = endpoint.getProperty(
                ProtobufServerFactoryBean.PROTOBUF_MESSAGE_CLASS,
                Class.class);
        PrintStream out = new PrintStream(os);
        Descriptor wrapperMessage = ((Descriptor) messageClass.getMethod(
                "getDescriptor").invoke(null));
        new ProtoGenerator().generateProtoFromDescriptor(wrapperMessage
                .getFile(), out, wrapperMessage);
        out.flush();
    } catch (Exception x) {
        throw new RuntimeException(x);
    }
}
 
Example 11
@Test
public void encryptWhenSerializing() throws Exception {
    KeyGenerator keyGen = KeyGenerator.getInstance("AES");
    keyGen.init(128);
    SecretKey key = keyGen.generateKey();
    TransformedRecordSerializer<Message> serializer = TransformedRecordSerializerJCE.newDefaultBuilder()
            .setEncryptWhenSerializing(true)
            .setEncryptionKey(key)
            .build();

    MySimpleRecord mediumRecord = MySimpleRecord.newBuilder().setRecNo(1066L).setStrValueIndexed(SONNET_108).build();
    assertTrue(Bytes.indexOf(mediumRecord.toByteArray(), "brain".getBytes()) >= 0, "should contain clear text");
    byte[] serialized = serialize(serializer, mediumRecord);
    assertEquals(TransformedRecordSerializer.ENCODING_ENCRYPTED, serialized[0]);
    assertFalse(Bytes.indexOf(serialized, "brain".getBytes()) >= 0, "should not contain clear text");
    Message deserialized = deserialize(serializer, Tuple.from(1066L), serialized);
    assertEquals(mediumRecord, deserialized);
}
 
Example 12
Source Project: incubator-heron   Source File: OutgoingTupleCollection.java    License: Apache License 2.0 6 votes vote down vote up
public OutgoingTupleCollection(
    PhysicalPlanHelper helper,
    Communicator<Message> outQueue,
    ReentrantLock lock,
    ComponentMetrics metrics) {
  this.outQueue = outQueue;
  this.helper = helper;
  this.metrics = metrics;
  SystemConfig systemConfig =
      (SystemConfig) SingletonRegistry.INSTANCE.getSingleton(SystemConfig.HERON_SYSTEM_CONFIG);

  this.serializer =
      SerializeDeSerializeHelper.getSerializer(helper.getTopologyContext().getTopologyConfig());

  // Initialize the values in constructor
  this.totalDataEmittedInBytes.set(0);
  this.currentDataTupleSizeInBytes = 0;

  // Read the config values
  this.dataTupleSetCapacity = systemConfig.getInstanceSetDataTupleCapacity();
  this.maxDataTupleSize = systemConfig.getInstanceSetDataTupleSize();
  this.controlTupleSetCapacity = systemConfig.getInstanceSetControlTupleCapacity();
  this.lock = lock;
}
 
Example 13
@Test
public void write() throws IOException {
	MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
	MediaType contentType = ProtobufHttpMessageConverter.PROTOBUF;
	this.converter.write(this.testMsg, contentType, outputMessage);
	assertEquals(contentType, outputMessage.getHeaders().getContentType());
	assertTrue(outputMessage.getBodyAsBytes().length > 0);
	Message result = Msg.parseFrom(outputMessage.getBodyAsBytes());
	assertEquals(this.testMsg, result);

	String messageHeader =
			outputMessage.getHeaders().getFirst(ProtobufHttpMessageConverter.X_PROTOBUF_MESSAGE_HEADER);
	assertEquals("Msg", messageHeader);
	String schemaHeader =
			outputMessage.getHeaders().getFirst(ProtobufHttpMessageConverter.X_PROTOBUF_SCHEMA_HEADER);
	assertEquals("sample.proto", schemaHeader);
}
 
Example 14
Source Project: brpc-java   Source File: ProtobufRpcMethodInfo.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public byte[] inputEncode(Object input) throws IOException {
    if (input instanceof Message) {
        return ((Message) input).toByteArray();
    }
    return null;
}
 
Example 15
Source Project: apicurio-registry   Source File: ProtoUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static <T> T fromJson(Message.Builder builder, String json, boolean ignoreUnknownFields) throws InvalidProtocolBufferException {
    JsonFormat.Parser parser = JsonFormat.parser();
    if (ignoreUnknownFields) {
        parser = parser.ignoringUnknownFields();
    }
    parser.merge(json, builder);
    //noinspection unchecked
    return (T) builder.build();
}
 
Example 16
Source Project: stateful-functions   Source File: JsonModule.java    License: Apache License 2.0 5 votes vote down vote up
private void configureIngress(Binder binder, Iterable<? extends JsonNode> ingressNode) {
  for (JsonNode ingress : ingressNode) {
    IngressIdentifier<Message> id = ingressId(ingress);
    IngressType type = ingressType(ingress);

    JsonIngressSpec<Message> ingressSpec = new JsonIngressSpec<>(type, id, ingress);
    binder.bindIngress(ingressSpec);
  }
}
 
Example 17
Source Project: fdb-record-layer   Source File: LeaderboardIndexTest.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected Message addScores(Message record, long... scores) {
    TestRecordsLeaderboardProto.FlatLeaderboardRecord.Builder recordBuilder =
            TestRecordsLeaderboardProto.FlatLeaderboardRecord.newBuilder();
    recordBuilder.mergeFrom(record);
    recordBuilder.addAllScores(Longs.asList(scores));
    return recordBuilder.build();
}
 
Example 18
Source Project: sofa-jraft   Source File: RpcRequestProcessor.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void handleRequest(final RpcContext rpcCtx, final T request) {
    try {
        final Message msg = processRequest(request, new RpcRequestClosure(rpcCtx, defaultResp));
        if (msg != null) {
            rpcCtx.sendResponse(msg);
        }
    } catch (final Throwable t) {
        LOG.error("handleRequest {} failed", request, t);
        rpcCtx.sendResponse(RpcFactoryHelper //
            .responseFactory() //
            .newResponse(defaultResp(), -1, "handleRequest internal error") //
            );
    }
}
 
Example 19
Source Project: blog-sample   Source File: ProtoUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static String toStr(Message message) {
   try {
       return JsonFormat.printer()
               .usingTypeRegistry(REGISTRY)
               .includingDefaultValueFields()
               .omittingInsignificantWhitespace()
               .print(message);
   } catch (InvalidProtocolBufferException e) {
       logger.log(Level.WARNING, "ProtoUtils#toStr error", e);
   }
   return "";
}
 
Example 20
Source Project: fdb-record-layer   Source File: JoinedRecordPlan.java    License: Apache License 2.0 5 votes vote down vote up
private FDBSyntheticRecord toSyntheticRecord(@Nonnull EvaluationContext context) {
    final Map<String, FDBStoredRecord<? extends Message>> records = new HashMap<>();
    for (JoinedRecordType.JoinConstituent joinConstituent : joinedRecordType.getConstituents()) {
        records.put(joinConstituent.getName(), (FDBStoredRecord<? extends Message>)context.getBinding(joinConstituent.getName()));
    }
    return FDBSyntheticRecord.of(joinedRecordType, records);
}
 
Example 21
Source Project: twister2   Source File: RRServer.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Send a non-response message to a worker or to the client
 * @param message message
 * @return true if response was accepted
 */
public boolean sendMessage(Message message, int targetID) {

  SocketChannel channel;
  if (targetID == CLIENT_ID) {
    if (clientChannel == null) {
      LOG.severe("Trying to send a message to the client, but it has not connected yet.");
      return false;
    }
    channel = clientChannel;
  } else if (workerChannels.containsValue(targetID)) {
    channel = workerChannels.inverse().get(targetID);
  } else {
    LOG.severe("Trying to send a message to a worker that has not connected yet. workerID: "
        + targetID);
    return false;
  }

  // this is most likely not needed, but just to make sure
  if (channel == null) {
    LOG.log(Level.SEVERE, "Channel is NULL for response");
    return false;
  }

  // since this is not a request/response message, we put the dummy request id
  RequestID dummyRequestID = RequestID.DUMMY_REQUEST_ID;

  TCPMessage tcpMessage = sendMessage(message, dummyRequestID, channel);

  return tcpMessage != null;
}
 
Example 22
private TypeInformation<?> typeInformation(Class<?> valueType) {
  if (Message.class.isAssignableFrom(valueType)) {
    Class<Message> message = (Class<Message>) valueType;
    return new ProtobufTypeInformation<>(message);
  }
  if (com.ververica.statefun.flink.core.message.Message.class.isAssignableFrom(valueType)) {
    return new MessageTypeInformation(messageFactoryType);
  }
  // TODO: we may want to restrict the allowed typeInfo here to theses that respect shcema
  // evaluation.
  return TypeInformation.of(valueType);
}
 
Example 23
@Override
public Message toProtoMessage() {
    return getPaymentAccountPayloadBuilder()
            .setOKPayAccountPayload(protobuf.OKPayAccountPayload.newBuilder()
                    .setAccountNr(accountNr))
            .build();
}
 
Example 24
Source Project: jigsaw-payment   Source File: JsonJacksonFormat.java    License: Apache License 2.0 5 votes vote down vote up
protected void printMessage(Message message, JsonGenerator generator) throws IOException {

        for (Iterator<Map.Entry<FieldDescriptor, Object>> iter = message.getAllFields().entrySet().iterator(); iter.hasNext();) {
            Map.Entry<FieldDescriptor, Object> field = iter.next();
            printField(field.getKey(), field.getValue(), generator);
        }
        printUnknownFields(message.getUnknownFields(), generator);
    }
 
Example 25
@SuppressWarnings("unchecked")
private <T extends Message> ProtobufDeserializer<T, ?> getDeserializer(Class<T> messageType) {
  ProtobufDeserializer<?, ?> deserializer = deserializerCache.get(messageType);
  if (deserializer == null) {
    ProtobufDeserializer<T, ?> newDeserializer = new MessageDeserializer<>(messageType, config);
    ProtobufDeserializer<?, ?> previousDeserializer = deserializerCache.putIfAbsent(messageType, newDeserializer);
    deserializer = previousDeserializer == null ? newDeserializer : previousDeserializer;
  }

  return (ProtobufDeserializer<T, ?>) deserializer;
}
 
Example 26
Source Project: incubator-tajo   Source File: AbstractCharBasedFormatter.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void print(Message message, OutputStream output, Charset cs)
		throws IOException {
	OutputStreamWriter writer = new OutputStreamWriter(output, cs);
	print(message, writer);
	writer.flush();
}
 
Example 27
Source Project: curiostack   Source File: ProtoTruthMessageDifferencer.java    License: MIT License 5 votes vote down vote up
private static Message orDefaultForType(
    @NullableDecl Message input, @NullableDecl Message other) {
  if (input != null) {
    return input;
  }
  checkNotNull(other, "One of input or other must not be null.");
  return other.getDefaultInstanceForType();
}
 
Example 28
Source Project: curiostack   Source File: ListenableFutureAssert.java    License: MIT License 5 votes vote down vote up
public ListenableFutureAssert<ACTUAL> completesWithValue(ACTUAL value) {
  if (value instanceof Message) {
    Message resolved = (Message) getUnchecked(actual);
    assertThat(resolved).isEqualTo(value);
  }
  assertThat(getUnchecked(actual)).isEqualTo(value);
  return this;
}
 
Example 29
Source Project: brpc-java   Source File: ProtobufUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static Message parseFrom(InputStream inputStream, Class clazz) {
    try {
        Method method = clazz.getMethod("getDefaultInstance");
        Message proto = (Message) method.invoke(null);
        proto = proto.newBuilderForType().mergeFrom(inputStream).build();
        return proto;
    } catch (Exception ex) {
        String errorMsg = String.format("parse proto failed, msg=%s", ex.getMessage());
        log.error(errorMsg);
        throw new RuntimeException(errorMsg);
    }
}
 
Example 30
Source Project: protobuf-converter   Source File: DefaultMapperImpl.java    License: MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public <T extends Message.Builder> MappingResult mapToProtobufField(final FieldResolver fieldResolver, final
Object domain, final T
		protobufBuilder) throws MappingException {
	Object domainFieldValue = getFieldValue(FieldUtils.createDomainGetterName(fieldResolver), domain);
	if (FieldUtils.isComplexType(fieldResolver.getField())) {
		return new MappingResult(MappingResult.Result.NESTED_MAPPING, domainFieldValue, protobufBuilder);
	}
	if (FieldUtils.isCollectionType(fieldResolver.getField())) {
		return new MappingResult(MappingResult.Result.COLLECTION_MAPPING, domainFieldValue, protobufBuilder);
	}
	return new MappingResult(MappingResult.Result.MAPPED, domainFieldValue, protobufBuilder);
}