TestandtargetJsonResponse.java 3.49 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.google.gson.ExclusionStrategy
 *  com.google.gson.FieldAttributes
 *  com.google.gson.Gson
 *  com.google.gson.GsonBuilder
 *  com.google.gson.JsonArray
 *  com.google.gson.JsonElement
 *  com.google.gson.JsonObject
 *  com.google.gson.JsonParser
 *  com.google.gson.JsonSyntaxException
 *  com.google.gson.TypeAdapterFactory
 */
package com.day.cq.analytics.testandtarget.impl;

import com.day.cq.analytics.testandtarget.Segment;
import com.day.cq.analytics.testandtarget.TestandtargetException;
import com.day.cq.analytics.testandtarget.ViewOfferResponse;
import com.day.cq.analytics.testandtarget.impl.ISO8601DateTypeAdapter;
import com.day.cq.analytics.testandtarget.impl.serializer.TntJsonSerializer;
import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import com.google.gson.TypeAdapterFactory;

public class TestandtargetJsonResponse {
    private final String rawJson;

    public TestandtargetJsonResponse(String rawJson) {
        this.rawJson = rawJson;
    }

    public <T> T getPayload(Class<T> payloadClass) throws TestandtargetException {
        try {
            JsonParser parser = new JsonParser();
            JsonElement element = parser.parse(this.rawJson);
            JsonElement parseElement = null;
            boolean isRangedResponse = false;
            if (element.isJsonObject()) {
                JsonObject maybeError = element.getAsJsonObject();
                if (maybeError.has("status") || maybeError.has("message")) {
                    throw new TestandtargetException("Status " + (Object)maybeError.get("status") + " : " + (Object)maybeError.get("message"));
                }
                isRangedResponse = maybeError.has("total");
                if (isRangedResponse) {
                    parseElement = maybeError.has("segments") ? maybeError.get("segments") : new JsonArray();
                }
            }
            if (payloadClass == Segment[].class) {
                return (T)this.extractSegments(parseElement);
            }
            if (payloadClass == ViewOfferResponse.class) {
                return (T)this.extractOffer();
            }
        }
        catch (JsonSyntaxException e) {
            throw new TestandtargetException((Throwable)e);
        }
        throw new IllegalArgumentException("Unable to extract payload of type " + payloadClass.getName());
    }

    private Segment[] extractSegments(JsonElement parseElement) throws JsonSyntaxException {
        String parseJsonStr = parseElement == null ? this.rawJson : parseElement.toString();
        return (Segment[])new GsonBuilder().registerTypeAdapterFactory(ISO8601DateTypeAdapter.FACTORY).setExclusionStrategies(new ExclusionStrategy[]{new ExclusionStrategy(){

            public boolean shouldSkipField(FieldAttributes f) {
                return f.getName().equals("segmentRule");
            }

            public boolean shouldSkipClass(Class<?> clazz) {
                return false;
            }
        }}).create().fromJson(parseJsonStr, Segment[].class);
    }

    private ViewOfferResponse extractOffer() throws JsonSyntaxException {
        return new TntJsonSerializer<ViewOfferResponse>().deserialize(this.rawJson, ViewOfferResponse.class);
    }

}