TestandtargetResponse.java
11.2 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
* Decompiled with CFR 0_118.
*/
package com.day.cq.analytics.testandtarget.impl;
import com.day.cq.analytics.testandtarget.Conversion;
import com.day.cq.analytics.testandtarget.Folder;
import com.day.cq.analytics.testandtarget.HTMLOffer;
import com.day.cq.analytics.testandtarget.Offer;
import com.day.cq.analytics.testandtarget.Recipe;
import com.day.cq.analytics.testandtarget.Report;
import com.day.cq.analytics.testandtarget.ReportType;
import com.day.cq.analytics.testandtarget.Reports;
import com.day.cq.analytics.testandtarget.Resolution;
import com.day.cq.analytics.testandtarget.Sample;
import com.day.cq.analytics.testandtarget.Step;
import com.day.cq.analytics.testandtarget.TestandtargetException;
import java.io.Reader;
import java.io.StringReader;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Attr;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
public class TestandtargetResponse {
private static final String NODE_ERROR = "error";
private static final String NODE_MESSAGE = "message";
private static final String NODE_OPERATION = "operation";
private static final String NODE_CAUSE = "cause";
private static final String NODE_CODE = "code";
private static final String NODE_THIRDPARTYID = "third-party-id";
private static final String NODE_CAMPAIGN_ID = "id";
private String response;
private Document document;
private Element root;
static final String API_DATE_FORMAT_SHORT = "yyyy-MM-dd";
static final String API_DATE_FORMAT_LONG = "yyyy-MM-dd'T'HH:mm";
public TestandtargetResponse(String response) throws TestandtargetException {
try {
InputSource is = new InputSource(new StringReader(response));
this.response = response;
this.document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is);
this.root = this.document.getDocumentElement();
}
catch (Exception e) {
throw new TestandtargetException(e.getMessage(), e);
}
}
public boolean isSuccess() {
try {
boolean isOperationFail;
boolean bl = isOperationFail = "operation".equals(this.root.getNodeName()) && "FAIL".equals(this.getElement(this.root, "status", true).getTextContent());
if ("error".equals(this.root.getNodeName()) || isOperationFail) {
return false;
}
return true;
}
catch (TestandtargetException e) {
return false;
}
}
public String getCode() {
try {
Element code = this.getElement(this.root, "code", true);
return code.getTextContent();
}
catch (TestandtargetException e) {
return null;
}
}
public String getErrorMessage() {
try {
if ("error".equals(this.root.getNodeName())) {
Element message = this.getElement(this.root, "message", true);
return message.getTextContent();
}
Element cause = this.getElement(this.root, "cause", true);
return cause.getTextContent();
}
catch (TestandtargetException e) {
return null;
}
}
public String getThirdPartyId() {
try {
Element thirdpartid = this.getElement(this.root, "third-party-id", true);
return thirdpartid.getTextContent();
}
catch (TestandtargetException e) {
return null;
}
}
public String getResponse() {
return this.response;
}
public Folder getFolderList() {
try {
return this.getFolder(this.root);
}
catch (TestandtargetException e) {
return null;
}
}
private Folder getFolder(Element parent) throws TestandtargetException {
Element name = this.getElement(parent, "name", true);
Element id = this.getElement(parent, "id", true);
Element folders = this.getElement(parent, "folders", false);
Folder folder = new Folder(name.getTextContent(), id.getTextContent());
if (folders != null) {
NodeList childs = folders.getChildNodes();
for (int i = 0; i < childs.getLength(); ++i) {
Node childNode = childs.item(i);
if (1 != childNode.getNodeType()) continue;
Element child = (Element)childNode;
folder.add(this.getFolder(child));
}
}
return folder;
}
public Collection<Offer> getOfferList() {
ArrayList<Offer> offers = new ArrayList<Offer>();
NodeList offerNodes = this.root.getChildNodes();
for (int i = 0; i < offerNodes.getLength(); ++i) {
Node child = offerNodes.item(i);
if (1 != child.getNodeType()) continue;
try {
Element o = (Element)child;
Element name = this.getElement(o, "name", true);
Element id = this.getElement(o, "id", true);
offers.add(new Offer(name.getTextContent(), id.getTextContent()));
continue;
}
catch (TestandtargetException e) {
// empty catch block
}
}
return offers;
}
public HTMLOffer getHTMLOffer() {
try {
Element htmlOffer = this.getElement(this.root, "htmlOffer", true);
Element script = this.getElement(htmlOffer, "", false);
Element value = this.getElement(htmlOffer, "value", true);
return new HTMLOffer(script.getTextContent(), value.getTextContent());
}
catch (TestandtargetException e) {
return null;
}
}
public Document getDocument() {
return this.document;
}
public Element getRootNode() {
return this.root;
}
public Element getElement(Element parent, String name, boolean required) throws TestandtargetException {
NodeList children = parent.getChildNodes();
Element found = null;
for (int i = 0; i < children.getLength(); ++i) {
Node child = children.item(i);
if (child.getNodeType() != 1 || !name.equals(child.getNodeName())) continue;
found = (Element)child;
}
if (required && found == null) {
throw new TestandtargetException("Element " + name + " not found in " + parent.getNodeName() + ".");
}
return found;
}
public String getAttribute(Element element, String name) throws TestandtargetException {
Attr attribute = element.getAttributeNode(name);
if (attribute != null) {
return attribute.getValue();
}
throw new TestandtargetException("Attribute " + name + " not found in " + element.getNodeName() + ".");
}
public Reports getReports() throws TestandtargetException {
try {
SimpleDateFormat shortDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Reports reports = new Reports();
NodeList reportNodes = this.root.getElementsByTagName("report");
for (int i = 0; i < reportNodes.getLength(); ++i) {
this.parseReport(reports, (Element)reportNodes.item(i), shortDateFormat);
}
return reports;
}
catch (ParseException e) {
throw new TestandtargetException("Failed parsing response", e);
}
}
public String getCampaignId() throws TestandtargetException {
NodeList nodeList = this.root.getElementsByTagName("id");
for (int idx = 0; idx < nodeList.getLength(); ++idx) {
Node currentNode = nodeList.item(idx);
if (!"id".equals(currentNode.getNodeName())) continue;
return currentNode.getTextContent();
}
return "";
}
private void parseReport(Reports reports, Element reportElement, DateFormat shortDateFormat) throws TestandtargetException, ParseException {
String campaignId = this.getAttribute(reportElement, "campaignId");
Date start = shortDateFormat.parse(this.getAttribute(reportElement, "start"));
Date end = shortDateFormat.parse(this.getAttribute(reportElement, "end"));
Resolution resolution = Resolution.fromTestandTargetKey(this.getAttribute(reportElement, "resolution"));
ReportType reportType = ReportType.fromTestandTargetKey(this.getAttribute(reportElement, "type"));
Report report = new Report(campaignId, start, end, resolution, reportType);
reports.addReport(report);
NodeList sampleNodes = reportElement.getElementsByTagName("sample");
for (int i = 0; i < sampleNodes.getLength(); ++i) {
this.parseSample(report, (Element)sampleNodes.item(i), shortDateFormat, new SimpleDateFormat("yyyy-MM-dd'T'HH:mm"));
}
}
private void parseSample(Report report, Element sampleElement, DateFormat shortDateFormat, DateFormat longDateFormat) throws TestandtargetException, ParseException {
Resolution duration = Resolution.fromTestandTargetKey(this.getAttribute(sampleElement, "duration"));
Date start = longDateFormat.parse(this.getAttribute(sampleElement, "start"));
Sample sample = new Sample(start, duration);
report.addSample(sample);
NodeList recipeNodes = sampleElement.getElementsByTagName("recipe");
for (int i = 0; i < recipeNodes.getLength(); ++i) {
this.parseRecipe(sample, (Element)recipeNodes.item(i), shortDateFormat, longDateFormat);
}
}
private void parseRecipe(Sample sample, Element recipeElement, DateFormat shortDateFormat, DateFormat longDateFormat) throws TestandtargetException {
String id = this.getAttribute(recipeElement, "id");
String name = this.getAttribute(recipeElement, "name");
String trafficType = this.getAttribute(recipeElement, "trafficType");
Conversion conversion = this.parseConversion(this.getElement(recipeElement, "conversion", false));
Recipe recipe = new Recipe(id, name, trafficType, conversion);
sample.addRecipe(recipe);
NodeList stepNodes = recipeElement.getElementsByTagName("step");
for (int i = 0; i < stepNodes.getLength(); ++i) {
this.parseStep(recipe, (Element)stepNodes.item(i));
}
}
private Conversion parseConversion(Element recipeElement) throws DOMException, TestandtargetException {
if (recipeElement == null) {
return null;
}
String name = this.getAttribute(recipeElement, "name");
BigDecimal value = new BigDecimal(this.getElement(recipeElement, "count", true).getTextContent());
return new Conversion(name, value);
}
private void parseStep(Recipe recipe, Element stepItem) throws TestandtargetException {
String name = this.getAttribute(stepItem, "name");
BigDecimal value = new BigDecimal(this.getElement(stepItem, "count", true).getTextContent());
recipe.addStep(new Step(name, value));
}
}