All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.cloudevents.gpubsub.impl.PubsubWithRoutingKeyMessageWriterImpl Maven / Gradle / Ivy

There is a newer version: 2.0.33
Show newest version
package io.cloudevents.gpubsub.impl;

import com.google.protobuf.ByteString;
import com.google.pubsub.v1.PubsubMessage;

import io.cloudevents.CloudEventData;
import io.cloudevents.SpecVersion;
import io.cloudevents.core.format.EventFormat;
import io.cloudevents.core.message.MessageWriter;
import io.cloudevents.rw.CloudEventContextWriter;
import io.cloudevents.rw.CloudEventRWException;
import io.cloudevents.rw.CloudEventWriter;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class PubsubWithRoutingKeyMessageWriterImpl implements MessageWriter, PubsubMessage>, CloudEventWriter {
    private final PubsubMessage.Builder message;

    @Override
    public PubsubMessage setEvent(EventFormat format, byte[] value) throws CloudEventRWException {
        message.putAttributes(PubsubHeaders.CONTENT_TYPE, format.serializedContentType());
        message.setData(ByteString.copyFrom(value));
        return message.build();
    }
    @Override
    public PubsubMessage end(CloudEventData data) throws CloudEventRWException {
        message.setData(ByteString.copyFrom(data.toBytes()));
        return message.build();
    }
    @Override
    public PubsubMessage end() throws CloudEventRWException {
        return message.build();
    }
    @Override
    public CloudEventContextWriter withContextAttribute(String name, String value) throws CloudEventRWException {
        message.putAttributes(PubsubHeaders.headerMapper(name), value);
        return this;
    }
    @Override
    public CloudEventWriter create(SpecVersion version) throws CloudEventRWException {
        message.putAttributes(PubsubHeaders.SPEC_VERSION, SpecVersion.V1.toString());
        return this;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy