|
1 | 1 | package com.pubnub.api.managers; |
2 | 2 |
|
| 3 | +import com.fasterxml.jackson.databind.JsonNode; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
3 | 5 | import com.pubnub.api.callbacks.SubscribeCallback; |
4 | 6 | import com.pubnub.api.callbacks.PNCallback; |
| 7 | +import com.pubnub.api.core.Crypto; |
5 | 8 | import com.pubnub.api.core.Pubnub; |
| 9 | +import com.pubnub.api.core.PubnubError; |
| 10 | +import com.pubnub.api.core.PubnubException; |
6 | 11 | import com.pubnub.api.core.enums.PNOperationType; |
| 12 | +import com.pubnub.api.core.enums.PNStatusCategory; |
7 | 13 | import com.pubnub.api.core.enums.SubscriptionType; |
8 | 14 | import com.pubnub.api.core.models.*; |
9 | 15 | import com.pubnub.api.core.models.consumer_facing.*; |
|
15 | 21 | import lombok.extern.slf4j.Slf4j; |
16 | 22 | import retrofit2.Call; |
17 | 23 |
|
| 24 | +import java.io.IOException; |
18 | 25 | import java.util.*; |
19 | 26 |
|
20 | 27 | @Slf4j |
@@ -161,10 +168,10 @@ public void onResponse(SubscribeEnvelope result, PNErrorStatus status) { |
161 | 168 | }); |
162 | 169 | } |
163 | 170 |
|
164 | | - private void performHeartbeatLoop(){ |
| 171 | + private void performHeartbeatLoop() { |
165 | 172 | log.debug("performingHeartbeatLoop"); |
166 | 173 |
|
167 | | - if (heartbeatCall != null && heartbeatCall.isCanceled() == false && heartbeatCall.isExecuted() == false) { |
| 174 | + if (heartbeatCall != null && !heartbeatCall.isCanceled() && !heartbeatCall.isExecuted()) { |
168 | 175 | heartbeatCall.cancel(); |
169 | 176 | } |
170 | 177 |
|
@@ -229,28 +236,73 @@ private void processIncomingMessages(List<SubscribeMessage> messages) { |
229 | 236 | pnPresenceEventResult.setData(pnPresenceEventData); |
230 | 237 | pnPresenceEventResult.setOperation(PNOperationType.PNSubscribeOperation); |
231 | 238 |
|
232 | | - for (SubscribeCallback subscribeCallback: listeners) { |
233 | | - subscribeCallback.presence(this.pubnub, pnPresenceEventResult); |
234 | | - } |
| 239 | + announce(pnPresenceEventResult); |
235 | 240 | } else { |
| 241 | + Object extractedMessage = null; |
| 242 | + |
| 243 | + try { |
| 244 | + extractedMessage = processMessage(message.getPayload()); |
| 245 | + } catch (PubnubException e) { |
| 246 | + PNStatus pnStatus = new PNStatus(); |
| 247 | + pnStatus.setError(true); |
| 248 | + pnStatus.setOperation(PNOperationType.PNSubscribeOperation); |
| 249 | + pnStatus.setCategory(PNStatusCategory.PNDecryptionErrorCategory); |
| 250 | + |
| 251 | + announce(pnStatus); |
| 252 | + } |
| 253 | + |
236 | 254 | PNMessageResult pnMessageResult = new PNMessageResult(); |
237 | 255 | PNMessageData pnMessageData = new PNMessageData(); |
238 | 256 |
|
239 | 257 | pnMessageResult.setOperation(PNOperationType.PNSubscribeOperation); |
240 | | - pnMessageData.setMessage(message.getPayload()); |
| 258 | + pnMessageData.setMessage(extractedMessage); |
241 | 259 |
|
242 | 260 | pnMessageData.setActualChannel((subscriptionMatch != null) ? channel : null); |
243 | 261 | pnMessageData.setSubscribedChannel(subscriptionMatch != null ? subscriptionMatch : channel); |
244 | 262 | pnMessageData.setTimetoken(timetoken); |
245 | 263 |
|
246 | 264 | pnMessageResult.setData(pnMessageData); |
247 | 265 |
|
248 | | - for (SubscribeCallback subscribeCallback: listeners) { |
249 | | - subscribeCallback.message(this.pubnub, pnMessageResult); |
250 | | - } |
251 | | - |
| 266 | + announce(pnMessageResult); |
252 | 267 | } |
253 | 268 | } |
254 | 269 | } |
255 | 270 |
|
| 271 | + private Object processMessage(Object input) throws PubnubException { |
| 272 | + if (pubnub.getConfiguration().getCipherKey() == null) { |
| 273 | + return input; |
| 274 | + } |
| 275 | + |
| 276 | + Crypto crypto = new Crypto(pubnub.getConfiguration().getCipherKey()); |
| 277 | + String outputText = crypto.decrypt(input.toString()); |
| 278 | + |
| 279 | + ObjectMapper mapper = new ObjectMapper(); |
| 280 | + Object outputObject; |
| 281 | + try { |
| 282 | + outputObject = mapper.readValue(outputText, JsonNode.class); |
| 283 | + } catch (IOException e) { |
| 284 | + throw new PubnubException(PubnubError.PNERROBJ_PARSING_ERROR); |
| 285 | + } |
| 286 | + |
| 287 | + return outputObject; |
| 288 | + } |
| 289 | + |
| 290 | + private void announce(PNStatus status) { |
| 291 | + for (SubscribeCallback subscribeCallback: listeners) { |
| 292 | + subscribeCallback.status(this.pubnub, status); |
| 293 | + } |
| 294 | + } |
| 295 | + |
| 296 | + private void announce(PNMessageResult message) { |
| 297 | + for (SubscribeCallback subscribeCallback: listeners) { |
| 298 | + subscribeCallback.message(this.pubnub, message); |
| 299 | + } |
| 300 | + } |
| 301 | + |
| 302 | + private void announce(PNPresenceEventResult presence) { |
| 303 | + for (SubscribeCallback subscribeCallback: listeners) { |
| 304 | + subscribeCallback.presence(this.pubnub, presence); |
| 305 | + } |
| 306 | + } |
| 307 | + |
256 | 308 | } |
0 commit comments