PublishDataSourceImpl.java
16.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.fasterxml.jackson.core.type.TypeReference
* com.fasterxml.jackson.databind.ObjectMapper
* org.apache.commons.lang3.StringUtils
* org.apache.sling.commons.json.JSONArray
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.JSONObject
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.mobile.dps.impl.ui;
import com.adobe.cq.mobile.dps.DPSArticle;
import com.adobe.cq.mobile.dps.DPSBanner;
import com.adobe.cq.mobile.dps.DPSCollection;
import com.adobe.cq.mobile.dps.DPSException;
import com.adobe.cq.mobile.dps.DPSProject;
import com.adobe.cq.mobile.dps.impl.DPSClient;
import com.adobe.cq.mobile.dps.impl.DPSPageManager;
import com.adobe.cq.mobile.dps.impl.ui.PerfTimer;
import com.adobe.cq.mobile.dps.ui.PublishDataSource;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TimerTask;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class PublishDataSourceImpl
implements PublishDataSource {
private static final Logger LOGGER = LoggerFactory.getLogger(PublishDataSourceImpl.class);
private DPSProject project = null;
private DPSPageManager dpsPageManager = null;
private DPSClient dpsClient = null;
private ObjectMapper jsonMapper;
private final String JSON_KEY_DATA_FIELD = "data";
private final String JSON_KEY_ERROR_FIELD = "errors";
private final String SEARCH_TEXT = "text";
private final String SEARCH_PATH = "rootPath";
private final String ARTICLE_MARKER = "_A";
private final String BANNER_MARKER = "_B";
private final String COLLECTION_MARKER = "_C";
private final String LAYOUT_MARKER = "_L";
private final int CACHE_DURATION = 0;
private static Map<String, JSONObject> dpsCache = new HashMap<String, JSONObject>();
public PublishDataSourceImpl(DPSProject project, DPSPageManager dpsPageManager, DPSClient dpsClient) {
this.project = project;
this.dpsPageManager = dpsPageManager;
this.dpsClient = dpsClient;
this.jsonMapper = new ObjectMapper();
}
@Override
public JSONObject getArticles(Map searchCriteria) throws DPSException {
PerfTimer perfTimer = PerfTimer.startTimer(this, "getArticles");
String cacheKey = (searchCriteria != null ? searchCriteria.toString() : this.project.getId()) + "_A";
JSONObject articles = dpsCache.get(cacheKey);
if (articles == null) {
JSONObject ret = new JSONObject();
try {
JSONArray aemArticles = null;
if (searchCriteria == null || searchCriteria.get("source") == null || searchCriteria.get("source").equals("AEM")) {
aemArticles = this.dpsPageManager.getAEMArticles(this.project);
aemArticles = this.filterEntities(aemArticles, searchCriteria);
ret.put("data", (Object)aemArticles);
}
this.addToCache(cacheKey, ret);
}
catch (DPSException ex) {
this.addErrorToDataFeed(ret, ex);
}
catch (JSONException ex) {
this.addErrorToDataFeed(ret, (Exception)ex);
}
articles = ret;
}
perfTimer.end();
return articles;
}
@Override
public JSONObject getCollection(DPSCollection dpsCollection) throws DPSException {
PerfTimer perfTimer = PerfTimer.startTimer(this, "getCollection");
JSONObject ret = new JSONObject();
if (dpsCollection == null) {
this.addErrorToDataFeed(ret, new DPSException("Illegal parameter (null) in getCollection"));
} else {
try {
JSONObject collection = dpsCache.get(dpsCollection.getName() + "_C");
if (collection == null) {
collection = this.dpsPageManager.getAEMCollection(dpsCollection, false, true, true);
if (collection.has("contentLinks") && collection.get("contentLinks") != null) {
JSONArray dpsContentLinks = collection.getJSONArray("contentLinks");
JSONArray contentDetails = new JSONArray();
collection.put("contentDetails", (Object)contentDetails);
if (dpsContentLinks.length() > 0) {
for (int i = 0; i < dpsContentLinks.length(); ++i) {
Object next = dpsContentLinks.get(i);
if (next instanceof JSONObject) {
JSONObject linkJSON = (JSONObject)next;
String name = linkJSON.getString("name");
String type = linkJSON.getString("type");
if (type.equals("article")) {
DPSArticle dpsChildArticle = this.project.getArticle(name);
if (dpsChildArticle != null) {
contentDetails.put((Object)dpsChildArticle.toJSON());
continue;
}
LOGGER.warn("Article not found " + name);
continue;
}
if (type.equals("banner")) {
DPSBanner dpsChildBanner = this.project.getBanner(name);
if (dpsChildBanner != null) {
contentDetails.put((Object)dpsChildBanner.toJSON());
continue;
}
LOGGER.warn("Banner not found " + name);
continue;
}
if (type.equals("collection")) {
DPSCollection dpsChildCollection = this.project.getCollection(name);
if (dpsChildCollection != null) {
contentDetails.put((Object)dpsChildCollection.toJSON());
continue;
}
LOGGER.warn("Collection not found " + name);
continue;
}
LOGGER.warn("Invalid link type " + type);
continue;
}
LOGGER.warn("Invalid link entry " + next);
}
}
}
this.addToCache(dpsCollection.getName() + "_C", collection);
}
ret.put("data", (Object)collection);
}
catch (DPSException ex) {
this.addErrorToDataFeed(ret, ex);
}
catch (JSONException ex) {
this.addErrorToDataFeed(ret, (Exception)ex);
}
perfTimer.end();
}
return ret;
}
@Override
public JSONObject getCollections(Map searchCriteria) throws DPSException {
PerfTimer perfTimer = PerfTimer.startTimer(this, "getCollections");
String cacheKey = (searchCriteria != null ? searchCriteria.toString() : this.project.getId()) + "_C";
JSONObject collections = dpsCache.get(cacheKey);
if (collections == null) {
boolean flatten = true;
if (searchCriteria != null && searchCriteria.get("flatten") != null && !searchCriteria.get("flatten").equals("true")) {
flatten = false;
}
JSONObject ret = new JSONObject();
try {
JSONArray aemCollections = null;
if (searchCriteria == null || searchCriteria.get("source") == null || searchCriteria.get("source").equals("AEM")) {
aemCollections = this.dpsPageManager.getAEMCollections(this.project, flatten);
aemCollections = this.filterEntities(aemCollections, searchCriteria);
ret.put("data", (Object)aemCollections);
}
this.addToCache(cacheKey, ret);
}
catch (DPSException ex) {
this.addErrorToDataFeed(ret, ex);
}
catch (JSONException ex) {
this.addErrorToDataFeed(ret, (Exception)ex);
}
collections = ret;
}
perfTimer.end();
return collections;
}
@Override
public JSONObject getBanners(Map searchCriteria) throws DPSException {
PerfTimer perfTimer = PerfTimer.startTimer(this, "getBanners");
String cacheKey = (searchCriteria != null ? searchCriteria.toString() : this.project.getId()) + "_B";
JSONObject projectBanners = dpsCache.get(cacheKey);
if (projectBanners == null) {
JSONObject ret = new JSONObject();
try {
JSONArray aemBanners = null;
if (searchCriteria == null || searchCriteria.get("source") == null || searchCriteria.get("source").equals("AEM")) {
aemBanners = this.dpsPageManager.getAEMBanners(this.project);
aemBanners = this.filterEntities(aemBanners, searchCriteria);
ret.put("data", (Object)aemBanners);
}
this.addToCache(cacheKey, ret);
}
catch (DPSException ex) {
this.addErrorToDataFeed(ret, ex);
}
catch (JSONException ex) {
this.addErrorToDataFeed(ret, (Exception)ex);
}
projectBanners = ret;
}
perfTimer.end();
return projectBanners;
}
@Override
public JSONObject getLayouts(Map searchCriteria) throws DPSException {
PerfTimer perfTimer = PerfTimer.startTimer(this, "getLayouts");
String cacheKey = (searchCriteria != null ? searchCriteria.toString() : this.project.getId()) + "_L";
JSONObject projectLayouts = dpsCache.get(cacheKey);
if (projectLayouts == null) {
JSONObject ret = new JSONObject();
try {
JSONArray dpsLayouts = this.dpsClient.getDPSLayouts(this.project, false, true);
ret.put("data", (Object)dpsLayouts);
this.addToCache(cacheKey, ret);
}
catch (DPSException ex) {
this.addErrorToDataFeed(ret, ex);
}
catch (JSONException ex) {
this.addErrorToDataFeed(ret, (Exception)ex);
}
projectLayouts = ret;
}
perfTimer.end();
return projectLayouts;
}
@Override
public Map<String, Object> convertJSONtoMap(JSONArray json) throws DPSException {
PerfTimer perfTimer = PerfTimer.startTimer(this, "convertJSONtoMap");
HashMap<String, Object> convertedData = new HashMap<String, Object>();
try {
if (json != null) {
convertedData.put("data", this.jsonMapper.readValue(json.toString(), (TypeReference)new TypeReference<ArrayList<Object>>(){}));
}
}
catch (IOException ioe) {
throw new DPSException("Failed to convert json to map.", ioe);
}
perfTimer.end();
return convertedData;
}
private void addErrorToDataFeed(JSONObject jsonObject, Exception ex) {
LOGGER.error(ex.getMessage(), (Throwable)ex);
try {
JSONArray errorsJSONArray = new JSONArray();
if (jsonObject.has("errors")) {
errorsJSONArray = jsonObject.getJSONArray("errors");
} else {
jsonObject.put("errors", (Object)errorsJSONArray);
}
JSONObject error = new JSONObject();
error.put("class", ex.getClass());
error.put("message", (Object)ex.getMessage());
if (ex.getCause() != null) {
String cause = ex.getCause().getLocalizedMessage();
if (StringUtils.isEmpty((CharSequence)cause)) {
cause = ex.getCause().getClass().getName();
}
if (StringUtils.isNotEmpty((CharSequence)cause)) {
error.put("cause", (Object)cause);
}
}
errorsJSONArray.put((Object)error);
}
catch (JSONException innerEx) {
LOGGER.error("Failed to add errors to JSONFeed", (Throwable)ex);
}
}
private JSONArray filterEntities(JSONArray entities, Map searchCriteria) {
if (entities == null || searchCriteria == null || searchCriteria.isEmpty()) {
return entities;
}
JSONArray filteredList = new JSONArray();
Set entrySet = searchCriteria.entrySet();
for (int i = 0; i < entities.length(); ++i) {
JSONObject nextArticle;
try {
nextArticle = entities.getJSONObject(i);
}
catch (JSONException jsonEx) {
LOGGER.warn("Error extracting article at index " + i);
nextArticle = null;
}
if (nextArticle == null) continue;
boolean matchedAll = true;
for (Map.Entry<String, String> entry : entrySet) {
if (this.matchesCriteria(nextArticle, entry)) continue;
matchedAll = false;
break;
}
if (!matchedAll) continue;
filteredList.put((Object)nextArticle);
}
return filteredList;
}
private boolean matchesCriteria(JSONObject entity, Map.Entry<String, String> criteria) {
boolean matches;
block10 : {
matches = false;
String key = criteria.getKey();
String value = criteria.getValue();
try {
if (key.equals("text")) {
Iterator it = entity.keys();
while (it.hasNext()) {
String nextKey = (String)it.next();
if (nextKey == null) continue;
try {
String nextPropertyValue = entity.getString(nextKey);
if (!StringUtils.isNotEmpty((CharSequence)nextPropertyValue) || !(matches = nextPropertyValue.contains(value))) continue;
break block10;
}
catch (Exception ex) {
matches = false;
continue;
}
}
break block10;
}
if (key.equals("rootPath")) {
String path;
matches = true;
if (entity.has("url")) {
path = entity.getString("url");
boolean bl = matches = path.equals(value) || path.startsWith(value + "/");
}
if (!matches && entity.has("path")) {
path = entity.getString("path");
matches = path.equals(value) || path.startsWith(value + "/");
}
} else {
matches = true;
}
}
catch (Exception ex) {
matches = false;
}
}
return matches;
}
private void addToCache(String name, JSONObject cacheable) {
}
public static void clearCollectionCache(String entityName) {
dpsCache.remove(entityName);
}
class ClearCollectionCache
extends TimerTask {
private String entityName;
final /* synthetic */ PublishDataSourceImpl this$0;
public ClearCollectionCache(PublishDataSourceImpl publishDataSourceImpl, String entityName) {
this.this$0 = publishDataSourceImpl;
this.entityName = entityName;
}
private ClearCollectionCache(PublishDataSourceImpl publishDataSourceImpl) {
this.this$0 = publishDataSourceImpl;
}
@Override
public void run() {
PublishDataSourceImpl.clearCollectionCache(this.entityName);
}
}
}