PageInfoUtils.java
3.45 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.security.user.util.AuthorizableUtil
* com.adobe.granite.xss.XSSAPI
* com.day.cq.replication.ReplicationActionType
* com.day.cq.replication.ReplicationQueue
* com.day.cq.replication.ReplicationQueue$Entry
* com.day.cq.replication.ReplicationStatus
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.JSONObject
*/
package com.day.cq.wcm.core.utils;
import com.adobe.granite.security.user.util.AuthorizableUtil;
import com.adobe.granite.xss.XSSAPI;
import com.day.cq.replication.ReplicationActionType;
import com.day.cq.replication.ReplicationQueue;
import com.day.cq.replication.ReplicationStatus;
import java.util.Calendar;
import java.util.List;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
public class PageInfoUtils {
public static JSONObject getReplicationStateObject(Resource resource, XSSAPI xssAPI) throws JSONException {
ReplicationStatus replicationStatus = (ReplicationStatus)resource.adaptTo(ReplicationStatus.class);
if (replicationStatus != null) {
int maxQueuePos = -1;
for (ReplicationQueue.Entry e : replicationStatus.getPending()) {
if (e.getQueuePosition() <= maxQueuePos) continue;
maxQueuePos = e.getQueuePosition();
}
return PageInfoUtils.getReplicationStateObject(resource, xssAPI, maxQueuePos);
}
return null;
}
private static void writeKey(JSONObject object, String key, Object value) throws JSONException {
object.put(key, value);
}
private static void writeOptionalKey(JSONObject object, String key, Object value) throws JSONException {
if (value != null) {
PageInfoUtils.writeKey(object, key, value);
}
}
private static void writeOptionalDateKey(JSONObject object, String key, Calendar value) throws JSONException {
if (value != null) {
PageInfoUtils.writeKey(object, key, value.getTimeInMillis());
}
}
public static JSONObject getReplicationStateObject(Resource resource, XSSAPI xssAPI, Integer maxQueuePos) throws JSONException {
ReplicationStatus replicationStatus = (ReplicationStatus)resource.adaptTo(ReplicationStatus.class);
if (replicationStatus != null) {
JSONObject replication = new JSONObject();
PageInfoUtils.writeKey(replication, "numQueued", maxQueuePos + 1);
PageInfoUtils.writeOptionalDateKey(replication, "published", replicationStatus.getLastPublished());
String lastPublishedBy = AuthorizableUtil.getFormattedName((ResourceResolver)resource.getResourceResolver(), (String)replicationStatus.getLastPublishedBy());
PageInfoUtils.writeKey(replication, "publishedBy", lastPublishedBy);
PageInfoUtils.writeKey(replication, "publishedBy_xss", lastPublishedBy != null ? xssAPI.filterHTML(lastPublishedBy) : null);
if (replicationStatus.getLastReplicationAction() != null) {
PageInfoUtils.writeOptionalKey(replication, "action", replicationStatus.getLastReplicationAction().name());
}
return replication;
}
return null;
}
}