NotificationsConfigHandler.java 5.4 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.commons.jcr.JcrUtil
 *  com.day.cq.contentsync.config.ConfigEntry
 *  com.day.cq.contentsync.handler.AbstractSlingResourceUpdateHandler
 *  javax.jcr.Binary
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.jcr.ValueFactory
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Service
 *  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.apache.sling.jcr.resource.JcrResourceResolverFactory
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.mobile.platform.impl.contentsync.handler;

import com.adobe.cq.mobile.platform.MobileResource;
import com.adobe.cq.mobile.platform.MobileResourceLocator;
import com.adobe.cq.mobile.platform.MobileResourceType;
import com.day.cq.commons.jcr.JcrUtil;
import com.day.cq.contentsync.config.ConfigEntry;
import com.day.cq.contentsync.handler.AbstractSlingResourceUpdateHandler;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.ValueFactory;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
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.apache.sling.jcr.resource.JcrResourceResolverFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(factory="com.day.cq.contentsync.handler.ContentUpdateHandler/notificationsconfig", inherit=1)
@Service
public class NotificationsConfigHandler
extends AbstractSlingResourceUpdateHandler {
    private static final Logger LOGGER = LoggerFactory.getLogger(NotificationsConfigHandler.class);
    private static final String NOTIFICATIONS_CONFIG_FILENAME = "pge-notifications-config.json";
    private static final String PN_EXCLUDE_PROPERTIES = "excludeProperties";

    public boolean updateCacheEntry(ConfigEntry configEntry, Long lastUpdated, String configCacheRoot, Session adminSession, Session userSession) {
        configCacheRoot = this.getConfigCacheRoot(configEntry, configCacheRoot);
        try {
            ResourceResolver resourceResolver = this.resolverFactory.getResourceResolver(adminSession);
            Resource resource = resourceResolver.getResource(this.getResolvedContentPath(configEntry));
            MobileResourceLocator mobileResMgr = (MobileResourceLocator)resource.getResourceResolver().adaptTo(MobileResourceLocator.class);
            MobileResource groupResource = mobileResMgr.findAncestorResourceByType(resource, MobileResourceType.GROUP.getType(), new String[0]);
            Iterable<MobileResource> appShell = mobileResMgr.findResourcesByType((Resource)groupResource.adaptTo(Resource.class), MobileResourceType.INSTANCE.getType());
            MobileResource shellMobileResource = appShell.iterator().next();
            ValueMap properties = shellMobileResource.getProperties();
            JSONObject notificationConfigJSON = new JSONObject();
            ValueMap vm = shellMobileResource.getProperties();
            if (vm.containsKey((Object)"notificationConfig")) {
                List<String> excludes = null;
                String[] excludeProperties = configEntry.getValues("excludeProperties");
                if (excludeProperties != null) {
                    excludes = Arrays.asList(excludeProperties);
                }
                for (String propertyName : properties.keySet()) {
                    if (!propertyName.startsWith("rps/")) continue;
                    String rawPropertyName = propertyName.substring("rps".length() + 1);
                    if (excludes != null && excludes.contains(rawPropertyName)) continue;
                    notificationConfigJSON.put(rawPropertyName, properties.get(propertyName, String.class));
                }
            }
            this.addListingFile(notificationConfigJSON, configCacheRoot + "/" + "pge-notifications-config.json", adminSession);
        }
        catch (Exception ex) {
            LOGGER.error("Unexpected error while updating cache for config: " + configEntry.getPath(), (Throwable)ex);
        }
        return false;
    }

    private void addListingFile(JSONObject jsonData, String cachePath, Session adminSession) throws RepositoryException, JSONException {
        JcrUtil.createPath((String)cachePath, (String)"sling:Folder", (String)"nt:file", (Session)adminSession, (boolean)false);
        Node cacheContentNode = JcrUtil.createPath((String)(cachePath + "/" + "jcr:content"), (String)"nt:resource", (Session)adminSession);
        Calendar calTS = Calendar.getInstance();
        cacheContentNode.setProperty("jcr:data", adminSession.getValueFactory().createBinary((InputStream)new ByteArrayInputStream(jsonData.toString(3).getBytes())));
        cacheContentNode.setProperty("jcr:lastModified", calTS);
        adminSession.save();
    }
}