PageExporterImpl.java 11.6 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.commons.jcr.JcrUtil
 *  com.day.cq.contentsync.ContentSyncManager
 *  com.day.cq.wcm.api.Page
 *  com.day.cq.wcm.api.WCMException
 *  com.day.cq.wcm.api.designer.Designer
 *  javax.jcr.Node
 *  javax.jcr.NodeIterator
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.servlet.http.HttpServletResponse
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.jackrabbit.api.JackrabbitSession
 *  org.apache.jackrabbit.api.security.principal.PrincipalManager
 *  org.apache.jackrabbit.api.security.user.Authorizable
 *  org.apache.jackrabbit.api.security.user.UserManager
 *  org.apache.jackrabbit.commons.jackrabbit.authorization.AccessControlUtils
 *  org.apache.jackrabbit.util.Text
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceResolverFactory
 *  org.apache.sling.api.resource.ResourceUtil
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.jcr.resource.JcrResourceUtil
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.wcm.contentsync.impl;

import com.day.cq.commons.jcr.JcrUtil;
import com.day.cq.contentsync.ContentSyncManager;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.WCMException;
import com.day.cq.wcm.api.designer.Designer;
import com.day.cq.wcm.contentsync.PageExporter;
import java.io.IOException;
import java.security.Principal;
import java.util.Date;
import java.util.Map;
import java.util.Set;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.servlet.http.HttpServletResponse;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.jackrabbit.api.JackrabbitSession;
import org.apache.jackrabbit.api.security.principal.PrincipalManager;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.jackrabbit.api.security.user.UserManager;
import org.apache.jackrabbit.commons.jackrabbit.authorization.AccessControlUtils;
import org.apache.jackrabbit.util.Text;
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.ResourceResolverFactory;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.jcr.resource.JcrResourceUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(label="Day CQ Page Exporter")
@Service
public class PageExporterImpl
implements PageExporter {
    private static final Logger log = LoggerFactory.getLogger(PageExporterImpl.class);
    private static final String TEMPLATE_PROPERTY = "cq:exportTemplate";
    private static final String DEFAULT_TEMPLATE_PATH = "/etc/contentsync/templates/default";
    private static final String ZIPS_SUFFIX = "-zips";
    @Reference
    private ContentSyncManager contentSyncManager;
    @Reference
    private ResourceResolverFactory resolverFactory;

    @Override
    public void export(Page page, ResourceResolver resolver, HttpServletResponse response) throws WCMException {
        try {
            ResourceResolver serviceResolver = this.resolverFactory.getServiceResourceResolver(null);
            String configPath = "/var/contentsync/tmp" + page.getPath() + "_" + System.currentTimeMillis();
            this.createConfig(page, configPath, resolver, serviceResolver);
            Resource config = serviceResolver.getResource(configPath);
            Session userSession = (Session)resolver.adaptTo(Session.class);
            String zipPath = this.contentSyncManager.getZip(config, null, userSession);
            String zipFileRequestURI = this.getZipFileRequestURI(configPath, zipPath);
            this.contentSyncManager.sendZip(userSession, response, zipFileRequestURI);
            this.cleanup(configPath, zipPath, serviceResolver);
        }
        catch (Exception e) {
            throw new WCMException((Throwable)e);
        }
    }

    @Override
    public void export(Page page, SlingHttpServletRequest request, HttpServletResponse response) throws WCMException {
        try {
            ResourceResolver serviceResolver = this.resolverFactory.getServiceResourceResolver(null);
            String configPath = "/var/contentsync/tmp" + page.getPath() + "_" + System.currentTimeMillis();
            this.createConfig(page, configPath, request.getResourceResolver(), serviceResolver);
            Session serviceSession = (Session)serviceResolver.adaptTo(Session.class);
            if (serviceSession.nodeExists(configPath + "/page")) {
                Node paramNode = JcrResourceUtil.createPath((String)(configPath + "/page/parameters"), (String)"nt:unstructured", (String)"nt:unstructured", (Session)serviceSession, (boolean)false);
                for (Object param : request.getParameterMap().keySet()) {
                    String[] values = request.getParameterValues((String)param);
                    if (values.length == 1) {
                        paramNode.setProperty((String)param, values);
                        continue;
                    }
                    if (values.length <= 1) continue;
                    paramNode.setProperty((String)param, values);
                }
                serviceSession.save();
            }
            Resource config = serviceResolver.getResource(configPath);
            Session userSession = (Session)request.getResourceResolver().adaptTo(Session.class);
            String zipPath = this.contentSyncManager.getZip(config, null, userSession);
            String zipFileRequestURI = this.getZipFileRequestURI(configPath, zipPath);
            this.contentSyncManager.sendZip(userSession, response, zipFileRequestURI);
            this.cleanup(configPath, zipPath, serviceResolver);
        }
        catch (Exception e) {
            throw new WCMException((Throwable)e);
        }
    }

    @Override
    public void export(Page page, ResourceResolver resolver, String targetPath) throws WCMException {
        try {
            ResourceResolver serviceResolver = this.resolverFactory.getServiceResourceResolver(null);
            String configPath = "/var/contentsync/tmp" + page.getPath() + System.currentTimeMillis();
            this.createConfig(page, configPath, resolver, serviceResolver);
            Session session = (Session)resolver.adaptTo(Session.class);
            Resource config = serviceResolver.getResource(configPath);
            String zipPath = this.contentSyncManager.getZip(config, null, session);
            session.move(zipPath, targetPath);
            session.save();
            this.cleanup(configPath, zipPath, serviceResolver);
        }
        catch (Exception e) {
            throw new WCMException((Throwable)e);
        }
    }

    protected void createConfig(Page page, String configPath, ResourceResolver resolver, ResourceResolver serviceResolver) throws RepositoryException, IOException {
        PrincipalManager pm;
        Principal principal;
        Session serviceSession = (Session)serviceResolver.adaptTo(Session.class);
        Designer designer = (Designer)resolver.adaptTo(Designer.class);
        String designPath = designer.getDesignPath(page);
        Node configNode = JcrResourceUtil.createPath((String)configPath, (String)"sling:Folder", (String)"cq:ContentSyncConfig", (Session)serviceSession, (boolean)false);
        configNode.setProperty("personalized", true);
        configNode.setProperty("authorizable", resolver.getUserID());
        Session userSession = (Session)resolver.adaptTo(Session.class);
        JackrabbitSession jrSession = (JackrabbitSession)userSession;
        Authorizable authorizable = jrSession.getUserManager().getAuthorizable(userSession.getUserID());
        if (null != authorizable && (principal = (pm = jrSession.getPrincipalManager()).getPrincipal(authorizable.getPrincipal().getName())) != null) {
            AccessControlUtils.addAccessControlEntry((Session)serviceSession, (String)configPath, (Principal)principal, (String[])new String[]{"{http://www.jcp.org/jcr/1.0}read", "{http://www.jcp.org/jcr/1.0}addChildNodes", "{http://www.jcp.org/jcr/1.0}modifyProperties"}, (boolean)true);
        }
        String templatePath = this.findConfigTemplate(page, (Session)resolver.adaptTo(Session.class));
        log.debug("Content sync configuration template selected: {}", (Object)templatePath);
        Node templateNode = userSession.getNode(templatePath);
        NodeIterator nodes = templateNode.getNodes();
        while (nodes.hasNext()) {
            Node n = nodes.nextNode();
            JcrUtil.copy((Node)n, (Node)configNode, (String)n.getName(), (boolean)true);
        }
        if (configNode.hasNode("design") && designPath != null && designPath.length() > 0) {
            Node designNode = configNode.getNode("design");
            designNode.setProperty("path", designPath);
        }
        if (configNode.hasNode("page")) {
            Node pageNode = configNode.getNode("page");
            pageNode.setProperty("path", page.getPath());
        }
        JackrabbitSession jrServiceSession = (JackrabbitSession)serviceSession;
        jrServiceSession.save();
    }

    protected void cleanup(String configPath, String zipPath, ResourceResolver serviceResolver) throws RepositoryException {
        Session serviceSession = (Session)serviceResolver.adaptTo(Session.class);
        Node configNode = serviceSession.getNode(configPath);
        configNode.remove();
        String zipsCachePath = Text.getRelativeParent((String)zipPath, (int)1);
        String cachePath = zipsCachePath.substring(0, zipsCachePath.lastIndexOf("-zips"));
        serviceSession.getNode(zipsCachePath).remove();
        serviceSession.getNode(cachePath).remove();
        serviceSession.save();
    }

    protected String findConfigTemplate(Page page, Session session) throws RepositoryException {
        String templatePath = null;
        do {
            String path;
            ValueMap props;
            if ((path = (String)(props = ResourceUtil.getValueMap((Resource)page.getContentResource())).get("cq:exportTemplate", String.class)) == null || !session.nodeExists(path)) continue;
            templatePath = path;
            break;
        } while ((page = page.getParent()) != null);
        return templatePath != null ? templatePath : "/etc/contentsync/templates/default";
    }

    protected String getZipFileRequestURI(String configPath, String zipPath) throws RepositoryException {
        String zipFileRequestURI = Text.getRelativeParent((String)configPath, (int)1) + "/" + Text.getName((String)zipPath);
        return zipFileRequestURI;
    }

    protected void bindContentSyncManager(ContentSyncManager contentSyncManager) {
        this.contentSyncManager = contentSyncManager;
    }

    protected void unbindContentSyncManager(ContentSyncManager contentSyncManager) {
        if (this.contentSyncManager == contentSyncManager) {
            this.contentSyncManager = null;
        }
    }

    protected void bindResolverFactory(ResourceResolverFactory resourceResolverFactory) {
        this.resolverFactory = resourceResolverFactory;
    }

    protected void unbindResolverFactory(ResourceResolverFactory resourceResolverFactory) {
        if (this.resolverFactory == resourceResolverFactory) {
            this.resolverFactory = null;
        }
    }
}