|
| 1 | +package com.pubnub.api.endpoints; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.JsonNode; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 6 | +import com.pubnub.api.PubNub; |
| 7 | +import com.pubnub.api.PubNubException; |
| 8 | +import com.pubnub.api.PubNubUtil; |
| 9 | +import com.pubnub.api.builder.PubNubErrorBuilder; |
| 10 | +import com.pubnub.api.enums.PNOperationType; |
| 11 | +import com.pubnub.api.models.server.HistoryForChannelsEnvelope; |
| 12 | +import com.pubnub.api.models.server.HistoryForChannelsItem; |
| 13 | +import com.pubnub.api.vendor.Crypto; |
| 14 | +import lombok.Setter; |
| 15 | +import lombok.experimental.Accessors; |
| 16 | +import retrofit2.Call; |
| 17 | +import retrofit2.Response; |
| 18 | +import retrofit2.Retrofit; |
| 19 | +import retrofit2.http.GET; |
| 20 | +import retrofit2.http.Path; |
| 21 | +import retrofit2.http.QueryMap; |
| 22 | + |
| 23 | +import java.io.IOException; |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Map; |
| 27 | + |
| 28 | +@Accessors(chain = true, fluent = true) |
| 29 | +public class HistoryForChannels extends Endpoint<HistoryForChannelsEnvelope, HistoryForChannelsEnvelope> { |
| 30 | + private static final int MAX_CHANNELS = 100; |
| 31 | + private static final int MAX_MESSAGES = 25; |
| 32 | + @Setter |
| 33 | + private List<String> channels; |
| 34 | + @Setter |
| 35 | + private Integer maximumPerChannel; |
| 36 | + @Setter |
| 37 | + private Long start; |
| 38 | + @Setter |
| 39 | + private Long end; |
| 40 | + |
| 41 | + public HistoryForChannels(PubNub pubnub, Retrofit retrofit) { |
| 42 | + super(pubnub, retrofit); |
| 43 | + channels = new ArrayList<>(); |
| 44 | + maximumPerChannel = 1; |
| 45 | + } |
| 46 | + |
| 47 | + private interface HistoryForChannelsService { |
| 48 | + @GET("v3/history/sub-key/{subKey}/channel/{channels}") |
| 49 | + Call<HistoryForChannelsEnvelope> fetchHistoryForChannels(@Path("subKey") String subKey, |
| 50 | + @Path("channels") String channels, |
| 51 | + @QueryMap Map<String, String> options); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + protected void validateParams() throws PubNubException { |
| 56 | + if (channels == null || channels.size() == 0) { |
| 57 | + throw PubNubException.builder().pubnubError(PubNubErrorBuilder.PNERROBJ_CHANNEL_MISSING).build(); |
| 58 | + } |
| 59 | + if (maximumPerChannel != null && maximumPerChannel > MAX_MESSAGES) { |
| 60 | + maximumPerChannel = MAX_MESSAGES; |
| 61 | + } else if (maximumPerChannel == null) { |
| 62 | + maximumPerChannel = 1; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + protected Call<HistoryForChannelsEnvelope> doWork(Map<String, String> params) { |
| 68 | + |
| 69 | + HistoryForChannelsService service = this.getRetrofit().create(HistoryForChannelsService.class); |
| 70 | + |
| 71 | + params.put("max", String.valueOf(maximumPerChannel)); |
| 72 | + |
| 73 | + if (start != null) { |
| 74 | + params.put("start", Long.toString(start).toLowerCase()); |
| 75 | + } |
| 76 | + if (end != null) { |
| 77 | + params.put("end", Long.toString(end).toLowerCase()); |
| 78 | + } |
| 79 | + |
| 80 | + return service.fetchHistoryForChannels(this.getPubnub().getConfiguration().getSubscribeKey(), PubNubUtil.joinString(channels, ","), params); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + protected HistoryForChannelsEnvelope createResponse(Response<HistoryForChannelsEnvelope> input) throws PubNubException { |
| 85 | + if (input.body() == null) { |
| 86 | + throw PubNubException.builder().pubnubError(PubNubErrorBuilder.PNERROBJ_PARSING_ERROR).build(); |
| 87 | + } |
| 88 | + |
| 89 | + HistoryForChannelsEnvelope envelope = input.body(); |
| 90 | + Map<String, List<HistoryForChannelsItem>> channelsList = envelope.getChannels(); |
| 91 | + |
| 92 | + for (Map.Entry<String, List<HistoryForChannelsItem>> entry : channelsList.entrySet()) { |
| 93 | + for (HistoryForChannelsItem item: entry.getValue()) { |
| 94 | + JsonNode message = processMessage(item.getMessage()); |
| 95 | + item.setMessage(message); |
| 96 | + } |
| 97 | + |
| 98 | + } |
| 99 | + |
| 100 | + return envelope; |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + protected PNOperationType getOperationType() { |
| 105 | + return PNOperationType.PNHistoryOperation; |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + protected boolean isAuthRequired() { |
| 110 | + return true; |
| 111 | + } |
| 112 | + |
| 113 | + private JsonNode processMessage(JsonNode message) throws PubNubException { |
| 114 | + // if we do not have a crypto key, there is no way to process the node; let's return. |
| 115 | + if (this.getPubnub().getConfiguration().getCipherKey() == null) { |
| 116 | + return message; |
| 117 | + } |
| 118 | + |
| 119 | + Crypto crypto = new Crypto(this.getPubnub().getConfiguration().getCipherKey()); |
| 120 | + ObjectMapper mapper = new ObjectMapper(); |
| 121 | + String inputText; |
| 122 | + String outputText; |
| 123 | + JsonNode outputObject; |
| 124 | + |
| 125 | + if (message.isObject() && message.has("message")) { |
| 126 | + inputText = message.get("message").asText(); |
| 127 | + } else { |
| 128 | + inputText = message.asText(); |
| 129 | + } |
| 130 | + |
| 131 | + outputText = crypto.decrypt(inputText); |
| 132 | + |
| 133 | + try { |
| 134 | + outputObject = mapper.readValue(outputText, JsonNode.class); |
| 135 | + } catch (IOException e) { |
| 136 | + throw PubNubException.builder().pubnubError(PubNubErrorBuilder.PNERROBJ_PARSING_ERROR).errormsg(e.getMessage()).build(); |
| 137 | + } |
| 138 | + |
| 139 | + // inject the decoded resposne into the payload |
| 140 | + if (message.isObject() && message.has("message")) { |
| 141 | + ObjectNode objectNode = (ObjectNode) message; |
| 142 | + objectNode.set("message", outputObject); |
| 143 | + outputObject = objectNode; |
| 144 | + } |
| 145 | + |
| 146 | + return outputObject; |
| 147 | + } |
| 148 | + |
| 149 | +} |
| 150 | + |
0 commit comments