XmlApiAdapter.java
13.7 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.crypto.CryptoSupport
* com.day.cq.wcm.webservicesupport.Configuration
* org.apache.commons.lang.StringUtils
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.analytics.testandtarget.impl;
import com.adobe.granite.crypto.CryptoSupport;
import com.day.cq.analytics.testandtarget.Campaign;
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.ReportType;
import com.day.cq.analytics.testandtarget.Reports;
import com.day.cq.analytics.testandtarget.Resolution;
import com.day.cq.analytics.testandtarget.SaveOfferRequest;
import com.day.cq.analytics.testandtarget.TestandtargetException;
import com.day.cq.analytics.testandtarget.TestandtargetHttpClient;
import com.day.cq.analytics.testandtarget.impl.AbstractApiAdapter;
import com.day.cq.analytics.testandtarget.impl.TestandtargetCampaign;
import com.day.cq.analytics.testandtarget.impl.TestandtargetHttpClientResponseException;
import com.day.cq.analytics.testandtarget.impl.TestandtargetResponse;
import com.day.cq.analytics.testandtarget.impl.serializer.CampaignXmlSerializer;
import com.day.cq.wcm.webservicesupport.Configuration;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class XmlApiAdapter
extends AbstractApiAdapter {
private static final String OPERATION_SAVE_CAMPAIGN = "saveCampaign";
private static final String OPERATION_FOLDER_LIST = "folderList";
private static final String OPERATION_GET_HTML_OFFER = "getHtmlOfferContent";
private static final String OPERATION_SAVE_HTML_OFFER = "saveHtmlOfferContent";
private static final String OPERATION_HTML_OFFER_LIST = "offerList";
private static final String OPERATION_SAVE = "saveWidgetOffer";
private static final String OPERATION_DELETE = "deleteWidgetOffer";
private static final String OPERATION_CAMPAIGN_LIST = "campaignList";
private static final String OPERATION_SET_CAMPAIGN_STATE = "setCampaignState";
private static final String OPERATION_REPORT = "report";
private static final String OPERATION_VIEW_CAMPAIGN = "viewCampaign";
private static final String VERSION = "1";
private final Logger log = LoggerFactory.getLogger(XmlApiAdapter.class);
private final TestandtargetHttpClient client;
final CryptoSupport cryptoSupport;
private CampaignXmlSerializer serializer = new CampaignXmlSerializer();
public XmlApiAdapter(TestandtargetHttpClient client, CryptoSupport cryptoSupport) {
this.client = client;
this.cryptoSupport = cryptoSupport;
}
public void checkCredentials(String clientcode, String email, String password) throws TestandtargetException {
this.listFolders(clientcode, email, password);
}
public void saveCampaign(Configuration configuration, String thirdPartyId, TestandtargetCampaign campaign) throws TestandtargetException {
Map<String, String> queryParameters = this.getInitialParameters(configuration, "saveCampaign");
String campaignXml = this.serializer.serialize(campaign);
queryParameters.put("thirdPartyId", thirdPartyId);
queryParameters.put("campaign", campaignXml);
this.log.debug("Calling T&T API with thirdPartyId {} and campaign XML {}", (Object)thirdPartyId, (Object)campaignXml);
campaign.setCampaignId(this.getSuccessfulResponse(queryParameters).getCampaignId());
this.log.debug("T&T campaign id: {}", (Object)campaign.getCampaignId());
}
public Map<Integer, String> listCampaigns(Configuration configuration, Date before, Date after, String environment, String name, String state, String label) throws TestandtargetException {
DateFormat sdf = this.newDateFormat();
Map<String, String> queryParameter = this.getInitialParameters(configuration, "campaignList");
queryParameter.put("before", before != null ? sdf.format(before) : sdf.format(new Date()));
if (after != null) {
queryParameter.put("after", sdf.format(after));
}
if (StringUtils.isNotEmpty((String)environment)) {
queryParameter.put("environment", environment);
}
if (StringUtils.isNotEmpty((String)name)) {
queryParameter.put("name", name);
}
if (StringUtils.isNotEmpty((String)state)) {
queryParameter.put("state", state);
}
if (StringUtils.isNotEmpty((String)label)) {
queryParameter.put("label", label);
}
TestandtargetResponse response = this.getSuccessfulResponse(queryParameter);
HashMap<Integer, String> campaigns = new HashMap<Integer, String>();
Document doc = response.getDocument();
Element root = response.getRootNode();
if ("campaigns".equals(root.getNodeName()) && doc.getElementsByTagName("campaign").getLength() > 0) {
NodeList nodes = doc.getElementsByTagName("campaign");
for (int i = 0; i < nodes.getLength(); ++i) {
String id = nodes.item(i).getFirstChild().getFirstChild().getNodeValue();
String campaignname = nodes.item(i).getFirstChild().getNextSibling().getFirstChild().getNodeValue();
campaigns.put(Integer.parseInt(id), campaignname);
}
}
return campaigns;
}
public boolean setCampaignState(Configuration configuration, String campaignState, String testAndTargetCampaignId, String thirdPartyCampaignId) throws TestandtargetException {
Map<String, String> queryParameter = this.getInitialParameters(configuration, "setCampaignState");
queryParameter.put("state", campaignState);
if (testAndTargetCampaignId != null) {
queryParameter.put("campaignId", testAndTargetCampaignId);
} else {
queryParameter.put("thirdPartyId", thirdPartyCampaignId);
}
return this.getSuccessfulResponse(queryParameter).isSuccess();
}
public long createHTMLOffer(Configuration configuration, SaveOfferRequest request) throws TestandtargetException {
Map<String, String> queryParameter = this.getInitialParameters(configuration, "saveHtmlOfferContent");
queryParameter.put("version", "1");
queryParameter.put("offerName", request.getName());
queryParameter.put("folderId", "");
queryParameter.put("content", request.getContent());
this.getSuccessfulResponse(queryParameter);
return -1;
}
public void createHTMLOffer(Configuration configuration, String offerName, String folderId, String content) throws TestandtargetException {
Map<String, String> queryParameter = this.getInitialParameters(configuration, "saveHtmlOfferContent");
queryParameter.put("version", "1");
queryParameter.put("offerName", offerName);
queryParameter.put("folderId", folderId);
queryParameter.put("content", content);
this.getSuccessfulResponse(queryParameter);
}
public HTMLOffer getHTMLOffer(Configuration configuration, String offerName) throws TestandtargetException {
Map<String, String> queryParameter = this.getInitialParameters(configuration, "getHtmlOfferContent");
queryParameter.put("version", "1");
queryParameter.put("offerName", offerName);
return this.getSuccessfulResponse(queryParameter).getHTMLOffer();
}
public Collection<Offer> listOffers(Configuration configuration, String folderId) throws TestandtargetException {
Map<String, String> queryParameter = this.getInitialParameters(configuration, "offerList");
queryParameter.put("version", "1");
queryParameter.put("folderId", folderId);
return this.getSuccessfulResponse(queryParameter).getOfferList();
}
public String publishOffer(Configuration configuration, String name, String url, String id) throws TestandtargetException {
return this.widgetOfferOperation(configuration, name, url, id, "saveWidgetOffer");
}
public String unpublishOffer(Configuration configuration, String name, String url, String id) throws TestandtargetException {
return this.widgetOfferOperation(configuration, name, url, id, "deleteWidgetOffer");
}
private String widgetOfferOperation(Configuration configuration, String name, String url, String id, String operation) throws TestandtargetException {
Map<String, String> queryParameter = this.getInitialParameters(configuration, operation);
if (name != null) {
queryParameter.put("name", name);
}
if (url != null) {
queryParameter.put("url", url);
}
queryParameter.put("id", id);
TestandtargetResponse response = new TestandtargetResponse(this.execute(queryParameter));
if (response.isSuccess()) {
String thirdPartyId = response.getThirdPartyId();
if (thirdPartyId != null) {
return thirdPartyId;
}
return null;
}
throw new TestandtargetException(this.getResponseErrorMessage(response));
}
public Folder listFolders(Configuration configuration) throws TestandtargetException {
String clientCode = (String)configuration.getInherited("clientcode", (Object)"");
String email = (String)configuration.getInherited("email", (Object)"");
String password = this.getPassword(configuration);
return this.listFolders(clientCode, email, password);
}
private Folder listFolders(String clientCode, String email, String password) throws TestandtargetException {
HashMap<String, String> queryParameter = new HashMap<String, String>();
queryParameter.put("client", clientCode);
queryParameter.put("email", email);
queryParameter.put("password", password);
queryParameter.put("version", "1");
queryParameter.put("operation", "folderList");
return this.getSuccessfulResponse(queryParameter).getFolderList();
}
public Reports getPerformanceReport(Configuration configuration, ReportType reportType, String campaignId, String thirdPartyCampaignId, Date start, Date end, Resolution resolution) throws TestandtargetException {
DateFormat dateFormat = this.newDateFormat();
Map<String, String> queryParameters = this.getInitialParameters(configuration, "report");
queryParameters.put("type", reportType.getTestandTargetKey());
if (campaignId != null) {
queryParameters.put("campaignId", campaignId);
} else if (thirdPartyCampaignId != null) {
queryParameters.put("thirdPartyCampaignId", thirdPartyCampaignId);
}
if (start != null) {
queryParameters.put("start", dateFormat.format(start));
}
if (end != null) {
queryParameters.put("end", dateFormat.format(end));
}
queryParameters.put("resolution", resolution.getTestandTargetKey());
queryParameters.put("version", "2");
this.log.debug("Getting performance data for campaign with id {}", (Object)campaignId);
return this.getSuccessfulResponse(queryParameters).getReports();
}
@Override
protected CryptoSupport getCryptoSupport() {
return this.cryptoSupport;
}
private String execute(Map<String, String> queryParameter) throws TestandtargetException {
String[] queryKeys = queryParameter.keySet().toArray(new String[0]);
String[] queryValues = queryParameter.values().toArray(new String[0]);
return this.client.execute(queryKeys, queryValues);
}
private String getResponseErrorMessage(TestandtargetResponse response) {
return response.getErrorMessage() != null ? response.getErrorMessage() : "Status code: " + response.getCode();
}
private TestandtargetResponse getSuccessfulResponse(Map<String, String> queryParameter) throws TestandtargetException {
TestandtargetResponse response = new TestandtargetResponse(this.execute(queryParameter));
this.log.debug("Got T&T API response {}", (Object)response.getResponse());
if (!response.isSuccess()) {
throw new TestandtargetException(this.getResponseErrorMessage(response));
}
return response;
}
private DateFormat newDateFormat() {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
dateFormat.setTimeZone(TimeZone.getTimeZone("Etc/GMT"));
return dateFormat;
}
public Campaign getCampaignByThirdPartyId(Configuration configuration, String thirdPartyId) throws TestandtargetException {
Map<String, String> queryParameters = this.getInitialParameters(configuration, "viewCampaign");
queryParameters.put("thirdPartyId", thirdPartyId);
queryParameters.put("version", "1");
try {
TestandtargetResponse response = this.getSuccessfulResponse(queryParameters);
Element rootElement = response.getDocument().getDocumentElement();
NodeList list = rootElement.getElementsByTagName("name");
String campaignName = list.item(0).getNodeValue();
list = rootElement.getElementsByTagName("id");
String campaignId = list.item(0).getNodeValue();
return new Campaign(campaignName, campaignId);
}
catch (TestandtargetHttpClientResponseException e) {
this.log.debug("Campaign with thirdPartyId {} was not found in Target", (Object)thirdPartyId);
return null;
}
}
}