下面列出了org.apache.commons.lang.NotImplementedException#com.google.protobuf.Message 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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);
}
@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;
}
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);
}
}
@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());
}
})
);
}
@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));
}
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;
}
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;
}
}
@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));
}
@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));
}
/**
* @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);
}
}
@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);
}
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;
}
@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);
}
@Override
public byte[] inputEncode(Object input) throws IOException {
if (input instanceof Message) {
return ((Message) input).toByteArray();
}
return null;
}
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();
}
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);
}
}
@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();
}
@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") //
);
}
}
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 "";
}
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);
}
/**
* 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;
}
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);
}
@Override
public Message toProtoMessage() {
return getPaymentAccountPayloadBuilder()
.setOKPayAccountPayload(protobuf.OKPayAccountPayload.newBuilder()
.setAccountNr(accountNr))
.build();
}
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);
}
@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;
}
@Override
public void print(Message message, OutputStream output, Charset cs)
throws IOException {
OutputStreamWriter writer = new OutputStreamWriter(output, cs);
print(message, writer);
writer.flush();
}
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();
}
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;
}
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);
}
}
/**
* {@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);
}