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

com.pubnub.api.workers.SubscribeMessageWorker Maven / Gradle / Ivy

Go to download

PubNub is a cross-platform client-to-client (1:1 and 1:many) push service in the cloud, capable of broadcasting real-time messages to millions of web and mobile clients simultaneously, in less than a quarter second!

There is a newer version: 4.6.5
Show newest version
// Generated by delombok at Mon Dec 12 08:48:14 PST 2016
package com.pubnub.api.workers;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.pubnub.api.PubNub;
import com.pubnub.api.PubNubException;
import com.pubnub.api.PubNubUtil;
import com.pubnub.api.enums.PNOperationType;
import com.pubnub.api.enums.PNStatusCategory;
import com.pubnub.api.managers.ListenerManager;
import com.pubnub.api.models.consumer.PNErrorData;
import com.pubnub.api.models.consumer.PNStatus;
import com.pubnub.api.models.consumer.pubsub.PNMessageResult;
import com.pubnub.api.models.consumer.pubsub.PNPresenceEventResult;
import com.pubnub.api.models.server.PresenceEnvelope;
import com.pubnub.api.models.server.PublishMetaData;
import com.pubnub.api.models.server.SubscribeMessage;
import com.pubnub.api.vendor.Crypto;
import java.io.IOException;
import java.util.concurrent.LinkedBlockingQueue;

public class SubscribeMessageWorker implements Runnable {
    @java.lang.SuppressWarnings("all")
    @javax.annotation.Generated("lombok")
    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(SubscribeMessageWorker.class);
    private PubNub pubnub;
    private ListenerManager listenerManager;
    private LinkedBlockingQueue queue;
    private ObjectMapper mapper;
    private boolean isRunning;

    public SubscribeMessageWorker(PubNub pubnubInstance, ListenerManager listenerManagerInstance, LinkedBlockingQueue queueInstance) {
        this.pubnub = pubnubInstance;
        this.listenerManager = listenerManagerInstance;
        this.mapper = new ObjectMapper();
        this.queue = queueInstance;
    }

    @Override
    public void run() {
        takeMessage();
    }

    private void takeMessage() {
        this.isRunning = true;
        while (this.isRunning) {
            try {
                this.processIncomingPayload(this.queue.take());
            } catch (InterruptedException e) {
                this.isRunning = false;
                log.warn("take message interrupted", e);
            }
        }
    }

    private JsonNode processMessage(JsonNode input) {
        // if we do not have a crypto key, there is no way to process the node; let's return.
        if (pubnub.getConfiguration().getCipherKey() == null) {
            return input;
        }
        Crypto crypto = new Crypto(pubnub.getConfiguration().getCipherKey());
        String inputText;
        String outputText;
        JsonNode outputObject;
        if (input.isObject() && input.has("pn_other")) {
            inputText = input.get("pn_other").asText();
        } else {
            inputText = input.asText();
        }
        try {
            outputText = crypto.decrypt(inputText);
        } catch (PubNubException e) {
            PNStatus pnStatus = PNStatus.builder().error(true).errorData(new PNErrorData(e.getMessage(), e)).operation(PNOperationType.PNSubscribeOperation).category(PNStatusCategory.PNDecryptionErrorCategory).build();
            listenerManager.announce(pnStatus);
            return null;
        }
        try {
            outputObject = mapper.readValue(outputText, JsonNode.class);
        } catch (IOException e) {
            PNStatus pnStatus = PNStatus.builder().error(true).errorData(new PNErrorData(e.getMessage(), e)).operation(PNOperationType.PNSubscribeOperation).category(PNStatusCategory.PNMalformedResponseCategory).build();
            listenerManager.announce(pnStatus);
            return null;
        }
        // inject the decoded response into the payload
        if (input.isObject() && input.has("pn_other")) {
            ObjectNode objectNode = (ObjectNode) input;
            objectNode.set("pn_other", outputObject);
            outputObject = objectNode;
        }
        return outputObject;
    }

    private void processIncomingPayload(SubscribeMessage message) {
        String channel = message.getChannel();
        String subscriptionMatch = message.getSubscriptionMatch();
        PublishMetaData publishMetaData = message.getPublishMetaData();
        if (channel != null && channel.equals(subscriptionMatch)) {
            subscriptionMatch = null;
        }
        if (message.getChannel().endsWith("-pnpres")) {
            PresenceEnvelope presencePayload = mapper.convertValue(message.getPayload(), PresenceEnvelope.class);
            String strippedPresenceChannel = null;
            String strippedPresenceSubscription = null;
            if (channel != null) {
                strippedPresenceChannel = PubNubUtil.replaceLast(channel, "-pnpres", "");
            }
            if (subscriptionMatch != null) {
                strippedPresenceSubscription = PubNubUtil.replaceLast(subscriptionMatch, "-pnpres", "");
            }
            PNPresenceEventResult pnPresenceEventResult = 
            // deprecated
            // deprecated
            PNPresenceEventResult.builder().event(presencePayload.getAction()).actualChannel((subscriptionMatch != null) ? channel : null).subscribedChannel(subscriptionMatch != null ? subscriptionMatch : channel).channel(strippedPresenceChannel).subscription(strippedPresenceSubscription).state(presencePayload.getData()).timetoken(publishMetaData.getPublishTimetoken()).occupancy(presencePayload.getOccupancy()).uuid(presencePayload.getUuid()).timestamp(presencePayload.getTimestamp()).build();
            listenerManager.announce(pnPresenceEventResult);
        } else {
            JsonNode extractedMessage = processMessage(message.getPayload());
            if (extractedMessage == null) {
                log.debug("unable to parse payload on #processIncomingMessages");
            }
            PNMessageResult pnMessageResult = 
            // deprecated
            // deprecated
            PNMessageResult.builder().message(extractedMessage).actualChannel((subscriptionMatch != null) ? channel : null).subscribedChannel(subscriptionMatch != null ? subscriptionMatch : channel).channel(channel).subscription(subscriptionMatch).timetoken(publishMetaData.getPublishTimetoken()).publisher(message.getIssuingClientId()).build();
            listenerManager.announce(pnMessageResult);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy