Search in sources :

Example 1 with RecordWithEncryptionContext

use of org.apache.pulsar.functions.source.RecordWithEncryptionContext in project pulsar by apache.

the class Utils method serializeRecordToJson.

/**
 * Serializes sink-record into json format. It encodes encryption-keys, encryption-param and payload in base64
 * format so, it can be sent in json.
 *
 * @param record
 * @return
 */
public static String serializeRecordToJson(Record<byte[]> record) {
    checkNotNull(record, "record can't be null");
    JsonObject result = new JsonObject();
    result.addProperty(PAYLOAD_FIELD, getEncoder().encodeToString(record.getValue()));
    if (record.getProperties() != null) {
        JsonObject properties = new JsonObject();
        record.getProperties().entrySet().forEach(e -> properties.addProperty(e.getKey(), e.getValue()));
        result.add(PROPERTIES_FIELD, properties);
    }
    Optional<EncryptionContext> optEncryptionCtx = (record instanceof RecordWithEncryptionContext) ? ((RecordWithEncryptionContext<byte[]>) record).getEncryptionCtx() : Optional.empty();
    if (optEncryptionCtx.isPresent()) {
        EncryptionContext encryptionCtx = optEncryptionCtx.get();
        JsonObject encryptionCtxJson = new JsonObject();
        JsonObject keyBase64Map = new JsonObject();
        JsonObject keyMetadataMap = new JsonObject();
        encryptionCtx.getKeys().entrySet().forEach(entry -> {
            keyBase64Map.addProperty(entry.getKey(), getEncoder().encodeToString(entry.getValue().getKeyValue()));
            Map<String, String> keyMetadata = entry.getValue().getMetadata();
            if (keyMetadata != null && !keyMetadata.isEmpty()) {
                JsonObject metadata = new JsonObject();
                entry.getValue().getMetadata().entrySet().forEach(m -> metadata.addProperty(m.getKey(), m.getValue()));
                keyMetadataMap.add(entry.getKey(), metadata);
            }
        });
        encryptionCtxJson.add(KEY_MAP_FIELD, keyBase64Map);
        encryptionCtxJson.add(KEY_METADATA_MAP_FIELD, keyMetadataMap);
        encryptionCtxJson.addProperty(ENCRYPTION_PARAM_FIELD, getEncoder().encodeToString(encryptionCtx.getParam()));
        encryptionCtxJson.addProperty(ALGO_FIELD, encryptionCtx.getAlgorithm());
        if (encryptionCtx.getCompressionType() != null) {
            encryptionCtxJson.addProperty(COMPRESSION_TYPE_FIELD, encryptionCtx.getCompressionType().name());
            encryptionCtxJson.addProperty(UNCPRESSED_MSG_SIZE_FIELD, encryptionCtx.getUncompressedMessageSize());
        }
        if (encryptionCtx.getBatchSize().isPresent()) {
            encryptionCtxJson.addProperty(BATCH_SIZE_FIELD, encryptionCtx.getBatchSize().get());
        }
        result.add(ENCRYPTION_CTX_FIELD, encryptionCtxJson);
    }
    return result.toString();
}
Also used : EncryptionContext(org.apache.pulsar.common.api.EncryptionContext) RecordWithEncryptionContext(org.apache.pulsar.functions.source.RecordWithEncryptionContext) JsonObject(com.google.gson.JsonObject) RecordWithEncryptionContext(org.apache.pulsar.functions.source.RecordWithEncryptionContext)

Example 2 with RecordWithEncryptionContext

use of org.apache.pulsar.functions.source.RecordWithEncryptionContext in project pulsar by apache.

the class Utils method serializeRecordToFlatBuffer.

public static ByteBuffer serializeRecordToFlatBuffer(FlatBufferBuilder builder, Record<byte[]> record) {
    checkNotNull(record, "record-context can't be null");
    Optional<EncryptionContext> encryptionCtx = (record instanceof RecordWithEncryptionContext) ? ((RecordWithEncryptionContext<byte[]>) record).getEncryptionCtx() : Optional.empty();
    Map<String, String> properties = record.getProperties();
    int encryptionCtxOffset = -1;
    int propertiesOffset = -1;
    if (properties != null && !properties.isEmpty()) {
        int[] propertiesOffsetArray = new int[properties.size()];
        int i = 0;
        for (Entry<String, String> property : properties.entrySet()) {
            propertiesOffsetArray[i++] = KeyValue.createKeyValue(builder, builder.createString(property.getKey()), builder.createString(property.getValue()));
        }
        propertiesOffset = Message.createPropertiesVector(builder, propertiesOffsetArray);
    }
    if (encryptionCtx.isPresent()) {
        encryptionCtxOffset = createEncryptionCtxOffset(builder, encryptionCtx);
    }
    int payloadOffset = Message.createPayloadVector(builder, record.getValue());
    Message.startMessage(builder);
    Message.addPayload(builder, payloadOffset);
    if (encryptionCtxOffset != -1) {
        Message.addEncryptionCtx(builder, encryptionCtxOffset);
    }
    if (propertiesOffset != -1) {
        Message.addProperties(builder, propertiesOffset);
    }
    int endMessage = Message.endMessage(builder);
    builder.finish(endMessage);
    ByteBuffer bb = builder.dataBuffer();
    // to avoid copying of data, use same byte[] wrapped by ByteBuffer. But, ByteBuffer.array() returns entire array
    // so, it requires to read from offset:
    // builder.sizedByteArray()=>copies buffer: sizedByteArray(space, bb.capacity() - space)
    int space = bb.capacity() - builder.offset();
    return ByteBuffer.wrap(bb.array(), space, bb.capacity() - space);
}
Also used : EncryptionContext(org.apache.pulsar.common.api.EncryptionContext) RecordWithEncryptionContext(org.apache.pulsar.functions.source.RecordWithEncryptionContext) RecordWithEncryptionContext(org.apache.pulsar.functions.source.RecordWithEncryptionContext) ByteBuffer(java.nio.ByteBuffer)

Aggregations

EncryptionContext (org.apache.pulsar.common.api.EncryptionContext)2 RecordWithEncryptionContext (org.apache.pulsar.functions.source.RecordWithEncryptionContext)2 JsonObject (com.google.gson.JsonObject)1 ByteBuffer (java.nio.ByteBuffer)1