ResponsiveInfoProvider.java
4.23 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.api.PageInfoProvider
* com.day.cq.wcm.api.TemplatedResource
* org.apache.commons.lang.StringUtils
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.JSONObject
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.wcm.core.impl;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageInfoProvider;
import com.day.cq.wcm.api.TemplatedResource;
import java.util.Iterator;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=0)
@Service(value={PageInfoProvider.class})
public class ResponsiveInfoProvider
implements PageInfoProvider {
private static final Logger log = LoggerFactory.getLogger(ResponsiveInfoProvider.class);
private final String NN_BREAKPOINTS = "breakpoints";
public void updatePageInfo(SlingHttpServletRequest request, JSONObject info, Resource resource) throws JSONException {
Resource breakpointResource;
Page page = (Page)resource.adaptTo(Page.class);
Resource jcrContentOriginal = page.getContentResource();
TemplatedResource jcrContentTemplated = (TemplatedResource)jcrContentOriginal.adaptTo(TemplatedResource.class);
TemplatedResource jcrContent = jcrContentTemplated != null ? jcrContentTemplated : jcrContentOriginal;
Resource responsiveResource = jcrContent.getChild("cq:responsive");
JSONObject responsive = new JSONObject();
if (responsiveResource == null && resource != null) {
responsiveResource = this.getInheritedResource(page, resource.getResourceResolver(), "cq:responsive");
}
if (responsiveResource != null && (breakpointResource = responsiveResource.getChild("breakpoints")) != null) {
JSONObject breakpoints = new JSONObject();
Iterator breakpointIt = breakpointResource.listChildren();
while (breakpointIt.hasNext()) {
Resource bp = (Resource)breakpointIt.next();
ValueMap prop = (ValueMap)bp.adaptTo(ValueMap.class);
int width = (Integer)prop.get("width", (Object)0);
String title = (String)prop.get("title", String.class);
if (width <= 0) continue;
JSONObject bpObj = new JSONObject();
bpObj.put("width", width);
if (title != null) {
bpObj.put("title", (Object)title);
}
breakpoints.put(bp.getName(), (Object)bpObj);
}
responsive.put("breakpoints", (Object)breakpoints);
}
info.put("responsive", (Object)responsive);
}
private Resource getInheritedResource(Page page, ResourceResolver resolver, String name) {
String value = null;
Resource childRes = null;
if (StringUtils.isEmpty((String)name)) {
return null;
}
while (value == null && childRes == null && page != null) {
Resource contentRes;
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 = (Page)res.adaptTo(Page.class);
if (page == null || (contentRes = page.getContentResource()) == null) continue;
childRes = contentRes.getChild(name);
}
return childRes;
}
}