AnnotationsInfoProvider.java 2.6 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.wcm.api.Page
 *  com.day.cq.wcm.api.PageInfoProvider
 *  javax.jcr.Node
 *  javax.jcr.NodeIterator
 *  javax.jcr.RepositoryException
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Properties
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.commons.json.JSONArray
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.JSONObject
 */
package com.day.cq.wcm.core.impl;

import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageInfoProvider;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.RepositoryException;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;

@Component(metatype=0)
@Properties(value={@Property(name="service.description", value={"Export content paths with annotations"})})
@Service
public class AnnotationsInfoProvider
implements PageInfoProvider {
    public void traverse(Node node, JSONArray annotations) throws JSONException {
        try {
            NodeIterator nodes = node.getNodes();
            while (nodes.hasNext()) {
                Node n = nodes.nextNode();
                if (n.getName().equals("cq:annotations")) {
                    try {
                        if (!n.getNodes().hasNext()) continue;
                        annotations.put((Object)node.getPath());
                    }
                    catch (RepositoryException re) {}
                    continue;
                }
                this.traverse(n, annotations);
            }
        }
        catch (RepositoryException re) {
            // empty catch block
        }
    }

    public void updatePageInfo(SlingHttpServletRequest request, JSONObject info, Resource resource) throws JSONException {
        Page page = (Page)resource.adaptTo(Page.class);
        Resource contentResource = page.getContentResource();
        JSONArray annotations = new JSONArray();
        info.put("annotations", (Object)annotations);
        this.traverse((Node)contentResource.adaptTo(Node.class), annotations);
    }
}