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

com.urbanairship.connect.client.model.responses.EventAdapter Maven / Gradle / Ivy

There is a newer version: 5.4.2
Show newest version
/*
Copyright 2015 Urban Airship and Contributors
*/

package com.urbanairship.connect.client.model.responses;

import com.google.common.collect.ImmutableBiMap;
import com.google.common.collect.ImmutableList;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.urbanairship.connect.client.model.EventType;
import com.urbanairship.connect.client.model.GsonUtil;
import com.urbanairship.connect.client.model.responses.region.RegionEvent;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;

import java.lang.reflect.Type;

public class EventAdapter implements JsonDeserializer {

    private static final ImmutableBiMap typeMap = ImmutableBiMap.builder()
            .put(EventType.CLOSE, CloseEvent.class)
            .put(EventType.CUSTOM, CustomEvent.class)
            .put(EventType.LOCATION, LocationEvent.class)
            .put(EventType.OPEN, OpenEvent.class)
            .put(EventType.SEND, SendEvent.class)
            .put(EventType.TAG_CHANGE, TagChange.class)
            .put(EventType.UNINSTALL, UninstallEvent.class)
            .put(EventType.FIRST_OPEN, FirstOpenEvent.class)
            .put(EventType.REGION, RegionEvent.class)
            .put(EventType.RICH_READ, RichReadEvent.class)
            .put(EventType.RICH_DELETE, RichDeleteEvent.class)
            .put(EventType.RICH_DELIVERY, RichDeliveryEvent.class)
            .put(EventType.PUSH_BODY, PushBody.class)
            .put(EventType.IN_APP_MESSAGE_DISPLAY, InAppMessageDisplayEvent.class)
            .put(EventType.IN_APP_MESSAGE_EXPIRATION, InAppMessageExpirationEvent.class)
            .put(EventType.IN_APP_MESSAGE_RESOLUTION, InAppMessageResolutionEvent.class)
            .put(EventType.CONTROL, ControlEvent.class)
            .put(EventType.SCREEN_VIEWED, ScreenViewedEvent.class)
        .build();

    private static final ImmutableList emptyEventBodyTypes = ImmutableList.builder()
        .add(EventType.UNINSTALL)
        .add(EventType.FIRST_OPEN)
        .build();

    @Override
    public Event deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
        JsonObject eventJson = json.getAsJsonObject();
        Event.Builder eventBuilder = Event.newBuilder();

        // Parse Event ID
        JsonElement eventId = eventJson.get(Event.EVENT_ID_KEY);
        if (eventId == null || StringUtils.isEmpty(eventId.getAsString())) {
            throw new JsonParseException("Unable to parse event with missing identifier");
        }
        eventBuilder.setIdentifier(eventId.getAsString());


        // Parse offset
        JsonElement offset = eventJson.get(Event.OFFSET_KEY);
        if (offset == null) {
            throw new JsonParseException("Unable to parse event with missing offset");
        }
        eventBuilder.setOffset(offset.getAsString());

        // Parse Event Type
        JsonElement type = eventJson.get(Event.TYPE_KEY);
        if (type == null || StringUtils.isEmpty(type.getAsString())) {
            throw new JsonParseException("Unable to parse event with missing type");
        }

        EventType eventType;
        try {
            eventType = EventType.valueOf(type.getAsString());
            if (!typeMap.containsKey(eventType)) {
                throw new JsonParseException(String.format("Unable to create event of type: %s", type.getAsString()));
            }
        } catch (IllegalArgumentException e) {
            throw new JsonParseException(String.format("Unable to create event of type: %s", type.getAsString()), e);
        }
        eventBuilder.setEventType(eventType);

        // Parse device info
        JsonElement deviceInfo = eventJson.get(Event.DEVICE_INFO_KEY);
        if (deviceInfo != null) {
            eventBuilder.setDeviceInfo(DeviceInfo.parseJSON(deviceInfo.toString()));
        }

        // Parse event body
        EventBody eventBody;
        if (emptyEventBodyTypes.contains(eventType)) {
            eventBody = GsonUtil.getGson().fromJson("{}", typeMap.get(eventType));
        } else {
            JsonElement eventBodyJson = eventJson.get(Event.EVENT_BODY_KEY);
            if (eventBodyJson == null) {
                throw new JsonParseException("Unable to parse event with missing event body");
            }
            eventBody = GsonUtil.getGson().fromJson(eventBodyJson.toString(), typeMap.get(eventType));
        }
        eventBuilder.setEventBody(eventBody);

        JsonElement occurredJson = eventJson.get(Event.OCCURRED_KEY);
        if (occurredJson == null) {
            throw new JsonParseException("Unable to parse event with missing occurred field");
        }

        JsonElement processedJson = eventJson.get(Event.PROCESSED_KEY);
        if (processedJson == null) {
            throw new JsonParseException("Unable to parse event with missing processed field");
        }

        DateTimeFormatter fmt = ISODateTimeFormat.dateTimeParser().withZoneUTC();
        // validate occurrence string
        DateTime occurred = fmt.parseDateTime(occurredJson.getAsString());
        // validate processed string
        DateTime processed = fmt.parseDateTime(processedJson.getAsString());

        eventBuilder.setOccurred(occurred);
        eventBuilder.setProcessed(processed);

        return eventBuilder.build();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy