WCMUtils.java
6.16 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.cq.launches.api.Launch
* com.day.cq.tagging.Tag
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.api.PageManager
* com.day.cq.wcm.api.components.Component
* com.day.cq.wcm.api.components.ComponentContext
* com.day.cq.wcm.api.components.ComponentManager
* com.day.cq.wcm.api.designer.Cell
* com.day.cq.wcm.api.designer.Design
* com.day.cq.wcm.api.designer.Designer
* com.day.cq.wcm.api.designer.Style
* javax.jcr.Node
* javax.servlet.ServletRequest
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.request.RequestPathInfo
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ValueMap
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.wcm.commons;
import com.adobe.cq.launches.api.Launch;
import com.day.cq.tagging.Tag;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.components.Component;
import com.day.cq.wcm.api.components.ComponentContext;
import com.day.cq.wcm.api.components.ComponentManager;
import com.day.cq.wcm.api.designer.Cell;
import com.day.cq.wcm.api.designer.Design;
import com.day.cq.wcm.api.designer.Designer;
import com.day.cq.wcm.api.designer.Style;
import java.util.Locale;
import javax.jcr.Node;
import javax.servlet.ServletRequest;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.request.RequestPathInfo;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class WCMUtils {
private static final Logger log = LoggerFactory.getLogger(WCMUtils.class);
public static final String DEBUG_PARAM = "debug";
private WCMUtils() {
}
public static Component getComponent(Resource resource) {
if (resource == null) {
log.debug("Resource to get component from must not be null");
return null;
}
ComponentManager compMgr = (ComponentManager)resource.getResourceResolver().adaptTo(ComponentManager.class);
if (compMgr == null) {
log.warn("Unable to retrieve component manager for {}", (Object)resource);
return null;
}
return compMgr.getComponentOfResource(resource);
}
public static String getInheritedProperty(Page page, ResourceResolver resolver, String name) {
String value = null;
while (value == null && page != null) {
Launch launch;
value = (String)page.getProperties().get(name, String.class);
if (value != null) continue;
Resource res = resolver.getResource(page.getPath() + "/..");
if (res == null) {
page = null;
continue;
}
Page previousPage = page;
page = (Page)res.adaptTo(Page.class);
if (page != null || (launch = (Launch)((Resource)previousPage.adaptTo(Resource.class)).adaptTo(Launch.class)) == null) continue;
page = (Page)launch.getSourceRootResource().adaptTo(Page.class);
}
return value;
}
public static Style getStyle(SlingHttpServletRequest request) {
Design design;
ComponentContext ctx = WCMUtils.getComponentContext((ServletRequest)request);
Resource res = request.getResource();
Designer d = (Designer)request.getResourceResolver().adaptTo(Designer.class);
String designId = "";
String cellPath = "";
String suffix = request.getRequestPathInfo().getSuffix();
if (suffix != null && suffix.length() > 0) {
int idx;
if (!suffix.startsWith("/")) {
suffix = "/" + suffix;
}
if ((idx = suffix.indexOf("/-/")) >= 0) {
designId = suffix.substring(0, idx);
cellPath = suffix.substring(idx + 3);
} else {
designId = suffix;
}
}
if (designId.length() > 0 && d.hasDesign(designId)) {
design = d.getDesign(designId);
} else {
Page page;
Page page2 = page = ctx == null ? null : ctx.getPage();
if (page == null) {
PageManager pMgr = (PageManager)request.getResourceResolver().adaptTo(PageManager.class);
page = pMgr.getContainingPage(res);
}
cellPath = "";
design = d.getDesign(page);
}
if (design == null) {
return null;
}
if (cellPath.length() > 0) {
return design.getStyle(cellPath);
}
if (ctx == null) {
return design.getStyle(res);
}
return design.getStyle(ctx.getCell());
}
public static ComponentContext getComponentContext(ServletRequest request) {
return (ComponentContext)request.getAttribute("com.day.cq.wcm.componentcontext");
}
public static Node getNode(Resource resource) {
if (resource != null) {
return (Node)resource.adaptTo(Node.class);
}
return null;
}
public static String getKeywords(Page page) {
return WCMUtils.getKeywords(page, false);
}
public static String getKeywords(Page page, boolean onlyLocalized) {
StringBuffer keywords = new StringBuffer();
if (page != null) {
Locale locale = page.getLanguage(false);
Tag[] tags = page.getTags();
for (int i = 0; i < tags.length; ++i) {
if (onlyLocalized) {
String title = tags[i].getLocalizedTitle(locale);
if (title == null) continue;
if (keywords.length() > 0) {
keywords.append(", ");
}
keywords.append(title);
continue;
}
if (keywords.length() > 0) {
keywords.append(", ");
}
keywords.append(tags[i].getTitle(locale));
}
}
return keywords.toString();
}
}