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

io.elastic.sailor.impl.DataCallback Maven / Gradle / Ivy

There is a newer version: 4.0.3
Show newest version
package io.elastic.sailor.impl;

import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;
import com.google.inject.name.Named;
import io.elastic.api.JSON;
import io.elastic.api.Message;
import io.elastic.sailor.Constants;
import io.elastic.sailor.ExecutionContext;
import io.elastic.sailor.MessagePublisher;
import io.elastic.sailor.MessageResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.json.JsonObject;

public class DataCallback extends CountingCallbackImpl {

    private static final Logger logger = LoggerFactory.getLogger(DataCallback.class);

    private MessagePublisher messagePublisher;
    private CryptoServiceImpl cipher;
    private ExecutionContext executionContext;
    private MessageResolver messageResolver;
    private String routingKey;
    private boolean emitLightweightMessage;

    @Inject
    public DataCallback(
            @Assisted ExecutionContext executionContext,
            MessagePublisher messagePublisher,
            CryptoServiceImpl cipher,
            MessageResolver messageResolver,
            @Named(Constants.ENV_VAR_DATA_ROUTING_KEY) String routingKey,
            @Named(Constants.ENV_VAR_EMIT_LIGHTWEIGHT_MESSAGE) boolean emitLightweightMessage) {
        this.executionContext = executionContext;
        this.messagePublisher = messagePublisher;
        this.cipher = cipher;
        this.messageResolver = messageResolver;
        this.routingKey = routingKey;
        this.emitLightweightMessage = emitLightweightMessage;
    }

    @Override
    public void receiveData(Object data) {
        logger.info("Step produced data to be published");

        // payload
        final Message message = (Message) data;

        JsonObject messageAsJson = executionContext.createPublisheableMessage(message);

        if (emitLightweightMessage) {
            messageAsJson = messageResolver.externalize(messageAsJson);
        }

        // encrypt
        byte[] encryptedPayload = cipher.encrypt(JSON.stringify(messageAsJson), MessageEncoding.BASE64);

        messagePublisher.publish(routingKey, encryptedPayload, executionContext.buildAmqpProperties(message.getId()));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy