001package com.pusher.client.channel;
002
003import com.google.gson.Gson;
004import com.google.gson.GsonBuilder;
005import com.google.gson.JsonElement;
006import com.google.gson.JsonObject;
007import com.google.gson.JsonPrimitive;
008import com.google.gson.annotations.SerializedName;
009
010import java.util.Map;
011
012public class PusherEvent {
013
014    private JsonObject jsonObject = new JsonObject();
015
016    /**
017     * getProperty returns the value associated with the key, or null.
018     * It is recommended that you use the specialized getters in this class instead.
019     *
020     * @param key The key you wish to get.
021     * @return
022     *      The object can be casted as follows:
023     *      - JSON strings - java.lang.String
024     *      - JSON number - java.lang.Double
025     *      - JSON boolean - java.lang.Boolean
026     *      - JSON array - java.util.ArrayList
027     *      - JSON object - java.util.Map
028     *      - JSON null - null
029     */
030    public Object getProperty(String key) {
031        switch(key) {
032            case "user_id":
033                return getUserId();
034            case "channel":
035                return getChannelName();
036            case "data":
037                return jsonObject.get("data");
038            case "event":
039                return getEventName();
040            default:
041                return null;
042        }
043    }
044
045    public String getUserId() {
046        return jsonObject.has("user_id") ? jsonObject.get("user_id").getAsString() : null;
047    }
048
049    public String getChannelName() {
050        return jsonObject.has("channel") ? jsonObject.get("channel").getAsString() : null;
051    }
052
053    public String getEventName() {
054        return jsonObject.has("event") ? jsonObject.get("event").getAsString() : null;
055    }
056
057    public String getData() {
058        JsonElement data = jsonObject.get("data");
059        if (data.isJsonPrimitive()) {
060            return data.getAsString();
061        }
062        final Gson gson = new GsonBuilder().serializeNulls().disableHtmlEscaping().create();
063        return gson.toJson(data);
064    }
065
066    public String toString() {
067        return this.toJson();
068    }
069
070    public PusherEvent(String event, String channel, String userId, String data) {
071        jsonObject.addProperty("event", event);
072        jsonObject.addProperty("channel", channel);
073        jsonObject.addProperty("userId", userId);
074        jsonObject.addProperty("data", data);
075    }
076
077    public PusherEvent(String event, String channel, String userId, Map<String, Object> data) {
078        this(event, channel, userId, new Gson().toJson(data));
079    }
080
081    public PusherEvent(JsonObject jsonObject) {
082        this.jsonObject = jsonObject;
083    }
084
085    public String toJson() {
086        final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
087        return gson.toJson(jsonObject);
088    }
089
090    public static PusherEvent fromJson(String json) {
091        final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
092        return new PusherEvent(gson.fromJson(json, JsonObject.class));
093    }
094}