ResponsiveInfoProvider.java 4.23 KB
/*
 * 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;
    }
}