DPSManifestXMLUpdateHandler.java 3.29 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.xss.XSSAPI
 *  com.day.cq.commons.jcr.JcrUtil
 *  com.day.cq.contentsync.config.ConfigEntry
 *  com.day.cq.contentsync.handler.ContentUpdateHandler
 *  javax.jcr.Binary
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.Session
 *  javax.jcr.ValueFactory
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.mobile.dps.impl.contentsync;

import com.adobe.cq.mobile.dps.impl.metadata.ManifestXMLGenerator;
import com.adobe.granite.xss.XSSAPI;
import com.day.cq.commons.jcr.JcrUtil;
import com.day.cq.contentsync.config.ConfigEntry;
import com.day.cq.contentsync.handler.ContentUpdateHandler;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Calendar;
import java.util.Map;
import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.Session;
import javax.jcr.ValueFactory;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=0, factory="com.day.cq.contentsync.handler.ContentUpdateHandler/publish-manifest-xml")
public class DPSManifestXMLUpdateHandler
implements ContentUpdateHandler {
    private static final Logger log = LoggerFactory.getLogger(DPSManifestXMLUpdateHandler.class);
    @Reference
    private XSSAPI xssAPI;

    public boolean updateCacheEntry(ConfigEntry configEntry, Long lastUpdated, String configCacheRoot, Session admin, Session session) {
        boolean changed = false;
        try {
            String pagePath = configEntry.getContentPath();
            Node pageNode = session.getNode(pagePath);
            Node cacheFolder = session.getNode(configCacheRoot);
            String cachePath = cacheFolder.getPath() + "/manifest.xml";
            StringWriter buf = new StringWriter();
            ManifestXMLGenerator generator1 = new ManifestXMLGenerator(this.xssAPI);
            generator1.generatePKGPropertiesXMLContent(pageNode.getName() + ".html", new PrintWriter(buf), ManifestXMLGenerator.getPageMetadata(cacheFolder));
            String fileContent = buf.toString();
            JcrUtil.createPath((String)cachePath, (String)"sling:Folder", (String)"nt:file", (Session)admin, (boolean)false);
            Node cacheContentNode = JcrUtil.createPath((String)(cachePath + "/jcr:content"), (String)"nt:resource", (Session)admin);
            cacheContentNode.setProperty("jcr:data", admin.getValueFactory().createBinary((InputStream)new ByteArrayInputStream(fileContent.getBytes())));
            cacheContentNode.setProperty("jcr:lastModified", Calendar.getInstance());
            changed = true;
            admin.save();
        }
        catch (Exception ex) {
            log.error("Unexpected error while updating cache for config: " + configEntry.getPath(), (Throwable)ex);
        }
        return changed;
    }

    protected void bindXssAPI(XSSAPI xSSAPI) {
        this.xssAPI = xSSAPI;
    }

    protected void unbindXssAPI(XSSAPI xSSAPI) {
        if (this.xssAPI == xSSAPI) {
            this.xssAPI = null;
        }
    }
}