MCMUtil.java
7.18 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.security.Authorizable
* com.day.cq.security.User
* com.day.cq.security.UserManager
* com.day.cq.tagging.Tag
* com.day.cq.wcm.api.Page
* javax.jcr.RepositoryException
* org.apache.jackrabbit.api.security.user.Authorizable
* org.apache.jackrabbit.api.security.user.User
* org.apache.jackrabbit.api.security.user.UserManager
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceUtil
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.commons.json.JSONArray
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.JSONObject
* org.apache.sling.commons.json.io.JSONWriter
*/
package com.day.cq.mcm.api;
import com.day.cq.tagging.Tag;
import com.day.cq.wcm.api.Page;
import java.security.Principal;
import java.util.Map;
import java.util.Set;
import javax.jcr.RepositoryException;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.jackrabbit.api.security.user.User;
import org.apache.jackrabbit.api.security.user.UserManager;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.apache.sling.commons.json.io.JSONWriter;
public class MCMUtil {
public static String removePossibleJcrContent(String path) {
if (path.endsWith("/")) {
path = path.substring(0, path.length() - 1);
}
if (path.endsWith("/jcr:content")) {
path = path.substring(0, path.length() - "/jcr:content".length());
}
return path;
}
public static <T> void writeMapAsJsonObject(JSONWriter out, Map<String, T> m) throws JSONException {
out.object();
for (String key : m.keySet()) {
out.key(key).value(m.get(key));
}
out.endObject();
}
public static void addPossibleTagsToValues(Page aPage, Map<String, Object> values) throws JSONException {
Tag[] tags = aPage.getTags();
if (tags != null && tags.length > 0) {
JSONArray tagsArray = new JSONArray();
for (int i2 = 0; i2 < tags.length; ++i2) {
JSONObject tag = new JSONObject();
tag.put("title", (Object)tags[i2].getTitle());
tag.put("id", (Object)tags[i2].getTagID());
tagsArray.put((Object)tag);
}
values.put("tags", (Object)tagsArray);
}
}
public static void addPossibleSegmentsToValues(ValueMap content, Map<String, Object> values, ResourceResolver resourceResolver) throws JSONException {
if (content.containsKey((Object)"cq:segments")) {
JSONArray segArray = new JSONArray();
if (content.get((Object)"cq:segments") instanceof String) {
JSONObject seg = new JSONObject();
seg.put("title", (Object)MCMUtil.getSegmentName(resourceResolver, content.get((Object)"cq:segments").toString()));
seg.put("path", (Object)content.get((Object)"cq:segments").toString());
segArray.put((Object)seg);
} else {
for (String segment : (String[])content.get("cq:segments", (Object)new String[0])) {
JSONObject seg = new JSONObject();
seg.put("title", (Object)MCMUtil.getSegmentName(resourceResolver, segment));
seg.put("path", (Object)segment);
segArray.put((Object)seg);
}
}
values.put("segments", (Object)segArray);
}
}
public static String getSegmentName(ResourceResolver resolver, String path) {
Resource resource = resolver.getResource(path);
if (resource == null || !resource.getResourceType().equals("cq:Page")) {
return path;
}
Page page = (Page)resource.adaptTo(Page.class);
return page.getTitle();
}
@Deprecated
public static com.day.cq.security.User getUser(ResourceResolver rr, String userId) {
com.day.cq.security.User retval = null;
com.day.cq.security.UserManager userManager = (com.day.cq.security.UserManager)rr.adaptTo(com.day.cq.security.UserManager.class);
if (userManager == null) {
throw new RuntimeException("Could not adapt resource resolver to UserManager.");
}
com.day.cq.security.Authorizable authorizable = userManager.get(userId);
if (authorizable != null && authorizable.isUser()) {
retval = (com.day.cq.security.User)authorizable;
}
return retval;
}
public static User getAuthorizedUser(ResourceResolver rr, String userId) {
User retval = null;
UserManager userManager = (UserManager)rr.adaptTo(UserManager.class);
if (userManager == null) {
throw new RuntimeException("Could not adapt resource resolver to UserManager.");
}
Object authorizable = null;
try {
userManager.getAuthorizable(userId);
}
catch (RepositoryException e) {
throw new RuntimeException((Throwable)e);
}
if (authorizable != null && !authorizable.isGroup()) {
retval = authorizable;
}
return retval;
}
public static String getUserId(ResourceResolver rr, String userId) {
String id = null;
UserManager userManager = (UserManager)rr.adaptTo(UserManager.class);
if (userManager == null) {
throw new RuntimeException("Could not adapt resource resolver to UserManager.");
}
Object authorizable = null;
try {
userManager.getAuthorizable(userId);
}
catch (RepositoryException e) {
throw new RuntimeException((Throwable)e);
}
if (authorizable != null && !authorizable.isGroup()) {
try {
id = authorizable.getPrincipal().getName();
}
catch (RepositoryException e) {
throw new RuntimeException("Could not fetch name for the given UserId : " + userId);
}
}
return id;
}
public static boolean isAnExperience(Resource resource) {
if (resource != null) {
return ResourceUtil.isA((Resource)resource, (String)"cq/personalization/components/teaserpage") || ResourceUtil.isA((Resource)resource, (String)"cq/personalization/components/offerpage") || ResourceUtil.isA((Resource)resource, (String)"mcm/components/newsletter/page") || ResourceUtil.isA((Resource)resource, (String)"mcm/campaign/components/campaign_newsletterpage") || ResourceUtil.isA((Resource)resource, (String)"wcm/designimporter/components/importerpage") || ResourceUtil.isA((Resource)resource, (String)"commerce/components/voucher") || ResourceUtil.isA((Resource)resource, (String)"commerce/components/promotion") || ResourceUtil.isA((Resource)resource, (String)"cq/personalization/components/experiencepage");
}
return false;
}
}