UGCUtil.java
5.59 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.commons.jcr.JcrUtil
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.api.PageManager
* com.day.cq.wcm.api.WCMException
* com.day.text.Text
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.commons.lang.StringUtils
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceUtil
*/
package com.day.cq.wcm.commons;
import com.day.cq.commons.jcr.JcrUtil;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.WCMException;
import com.day.text.Text;
import java.util.StringTokenizer;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
public class UGCUtil {
public static String resourceToUGCPath(Resource resource) {
StringBuilder path = new StringBuilder("/content/usergenerated");
path.append(UGCUtil.getPagePath(resource));
path.append("/").append("jcr:content");
path.append("/").append(UGCUtil.getIdFromResource(resource));
return path.toString();
}
public static String UGCToResourcePath(Resource resource) {
String pagePath = StringUtils.substringAfter((String)resource.getPath(), (String)"/content/usergenerated");
return resource.getResourceResolver().map(pagePath);
}
public static String getPagePath(Resource resource) {
return StringUtils.substringBefore((String)resource.getPath(), (String)"/jcr:content");
}
public static String prepareUserGeneratedContent(ResourceResolver resolver, String pagePath) throws WCMException {
String ugcPagePath = "/content/usergenerated";
StringTokenizer pathElems = new StringTokenizer(pagePath, "/");
while (pathElems.hasMoreTokens()) {
if (resolver.getResource(ugcPagePath = ugcPagePath + "/" + pathElems.nextToken()) != null) continue;
if (ugcPagePath.equals("/content/usergenerated/content")) {
UGCUtil.createNode(resolver, ugcPagePath, "sling:Folder");
UGCUtil.save(resolver);
continue;
}
UGCUtil.createPage(resolver, ugcPagePath, null, null, null, null);
}
return ugcPagePath;
}
public static String getIdFromResource(Resource resource) {
return StringUtils.substringBefore((String)ResourceUtil.getName((Resource)resource), (String)".");
}
protected static void save(ResourceResolver resolver) throws WCMException, IllegalArgumentException {
Session session = (Session)resolver.adaptTo(Session.class);
if (session == null) {
throw new IllegalArgumentException("resolver must be adaptable to session");
}
try {
session.save();
}
catch (RepositoryException re) {
throw new WCMException("failed to save changes", (Throwable)re);
}
}
protected static Page createPage(ResourceResolver resolver, String path, String template, String resourceTypeProp, String resourceType, String title) throws WCMException {
String parentPath = Text.getRelativeParent((String)path, (int)1);
String name = Text.getName((String)path);
return UGCUtil.createPage(resolver, parentPath, name, template, resourceTypeProp, resourceType, title);
}
protected static Page createPage(ResourceResolver resolver, String parentPath, String name, String template, String resourceTypeProp, String resourceType, String title) throws WCMException {
RepositoryException e;
try {
if (resolver.getResource(parentPath) == null) {
UGCUtil.createNode(resolver, parentPath, "nt:unstructured");
UGCUtil.save(resolver);
}
if (!JcrUtil.isValidName((String)name)) {
name = JcrUtil.createValidName((String)name, (String[])JcrUtil.HYPHEN_LABEL_CHAR_MAPPING);
}
Page page = ((PageManager)resolver.adaptTo(PageManager.class)).create(parentPath, name, template, title);
if (StringUtils.isNotEmpty((String)resourceTypeProp)) {
((Node)page.getContentResource().adaptTo(Node.class)).setProperty(resourceTypeProp, resourceType);
}
return page;
}
catch (RepositoryException re) {
e = re;
}
catch (WCMException wcme) {
e = wcme;
}
throw new WCMException("failed to create page", (Throwable)e);
}
protected static Node createNode(ResourceResolver resolver, String path, String nodeType) throws WCMException {
if (path.startsWith("/")) {
path = path.substring(1);
}
try {
Node root = ((Session)resolver.adaptTo(Session.class)).getRootNode();
StringTokenizer names = new StringTokenizer(Text.getRelativeParent((String)path, (int)1), "/");
while (names.hasMoreTokens()) {
String name = names.nextToken();
if (!root.hasNode(name)) {
root.addNode(name);
}
root = root.getNode(name);
}
return root.addNode(Text.getName((String)path), nodeType);
}
catch (Exception e) {
throw new WCMException("failed to create node", (Throwable)e);
}
}
}