TestandtargetJsonResponse.java
3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
* 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);
}
}