private byte[] wrapContent(byte[] message, Schema schema, Map<Utf8, Utf8> metadata) throws Exception { GenericRecord genericRecord = bytesToRecord(message, schema); genericRecord.put(METADATA_MARKER, metadata); return recordToBytes(genericRecord, schema); }
private Optional<Object> select(final Message message) throws IOException { CompiledSchema<Schema> compiledSchema = message.<Schema>getSchema().get(); return select(bytesToRecord(message.getData(), compiledSchema.getSchema())); }
byte[] wrapContent(byte[] message, String id, long timestamp, Schema schema, Map<String, String> externalMetadata) { try { GenericRecord genericRecord = bytesToRecord(message, schema); genericRecord.put(METADATA_MARKER, metadataMap(id, timestamp, externalMetadata)); return recordToBytes(genericRecord, schema); } catch (Exception exception) { throw new WrappingException("Could not wrap avro message", exception); } }
@SuppressWarnings("unchecked") UnwrappedMessageContent unwrapContent(byte[] data, CompiledSchema<Schema> schema) { try { Map<Utf8, Utf8> metadata = (Map<Utf8, Utf8>) bytesToRecord(data, schema.getSchema()).get(METADATA_MARKER); MessageMetadata messageMetadata = getMetadata(metadata); return new UnwrappedMessageContent(messageMetadata, data, schema); } catch (Exception exception) { throw new UnwrappingException("Could not read avro message", exception); } }
private Optional<Object> select(final Message message) throws IOException { CompiledSchema<Schema> compiledSchema = message.<Schema>getSchema().get(); return select(bytesToRecord(message.getData(), compiledSchema.getSchema())); }
@Test @SuppressWarnings("unchecked") public void shouldWrappedMessageContainsMetadata() throws IOException { // when byte[] wrappedMessage = avroMessageContentWrapper.wrapContent(content, id, timestamp, avroUser.getSchema(), Collections.emptyMap()); // then GenericRecord messageWithMetadata = bytesToRecord(wrappedMessage, avroUser.getSchema()); Map<Utf8, Utf8> metadata = (Map<Utf8, Utf8>) messageWithMetadata.get(METADATA_MARKER); assertThat(metadata.get(METADATA_MESSAGE_ID_KEY).toString()).isEqualTo(id); assertThat(valueOf(metadata.get(METADATA_TIMESTAMP_KEY).toString())).isEqualTo(timestamp); assertThat(wrappedMessage).contains(content); }