PublishSearchPromoteConfigHandler.java 6.64 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.felix.scr.annotations.Activate
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Properties
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.resource.LoginException
 *  org.apache.sling.api.resource.ModifiableValueMap
 *  org.apache.sling.api.resource.PersistenceException
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceResolverFactory
 *  org.apache.sling.commons.osgi.PropertiesUtil
 *  org.osgi.service.component.ComponentContext
 *  org.osgi.service.component.ComponentException
 *  org.osgi.service.event.Event
 *  org.osgi.service.event.EventHandler
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.searchpromote.impl;

import com.day.cq.searchpromote.SearchPromoteException;
import com.day.cq.searchpromote.SearchPromoteService;
import com.day.cq.searchpromote.impl.SearchPromoteEnvironment;
import java.util.Dictionary;
import java.util.Map;
import org.apache.felix.scr.annotations.Activate;
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.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.PersistenceException;
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.commons.osgi.PropertiesUtil;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.ComponentException;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Service(value={EventHandler.class})
@Component(metatype=1, label="AEM 6 - Search&Promote integration publish configuration handler", description="Handler that updates the S&P cloud configuration when this configuration is pushed to publish")
@Properties(value={@Property(name="event.topics", value={"org/apache/sling/api/resource/Resource/ADDED", "org/apache/sling/api/resource/Resource/CHANGED"}, propertyPrivate=1), @Property(name="event.filter", value={"(&(path=/etc/cloudservices/search-promote/*/jcr:content)(!(event.application=*)))"}, propertyPrivate=1)})
public class PublishSearchPromoteConfigHandler
implements EventHandler {
    private final Logger log;
    private static final boolean DEFAULT_ENABLED = false;
    @Property(boolValue={0}, label="Enabled", description="Enables the configuration handler. It must be disabled on author instances.")
    private static final String PROPERTY_ENABLED = "cq.searchpromote.confighandler.enabled";
    @Reference
    private SearchPromoteService searchPromoteService;
    @Reference
    private ResourceResolverFactory resolverFactory;
    private boolean enabled;

    public PublishSearchPromoteConfigHandler() {
        this.log = LoggerFactory.getLogger(this.getClass());
    }

    @Activate
    public void activate(ComponentContext componentContext) throws ComponentException {
        this.log.debug("Activating Search&Promote publish configuration handler...");
        this.enabled = PropertiesUtil.toBoolean(componentContext.getProperties().get("cq.searchpromote.confighandler.enabled"), (boolean)false);
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public void handleEvent(Event event) {
        block10 : {
            if (!this.enabled) {
                return;
            }
            String resourcePath = (String)event.getProperty("path");
            this.log.debug("Detected {} at {}", (Object)event.getTopic(), (Object)resourcePath);
            ResourceResolver resolver = null;
            try {
                resolver = this.getServiceResolver();
                Resource resource = resolver.getResource(resourcePath);
                if (resource != null) {
                    ModifiableValueMap props = (ModifiableValueMap)resource.adaptTo(ModifiableValueMap.class);
                    try {
                        this.log.debug("Retrieving search form from LIVE environment...");
                        String formXml = this.searchPromoteService.getSearchForm((String)props.get("memberid", (Object)""), (String)props.get("accountno", (Object)""), SearchPromoteEnvironment.LIVE);
                        props.put((Object)"searchformxml", (Object)formXml);
                        this.log.debug("Form retrieved. Persisting changes...");
                        resolver.commit();
                        this.log.debug("Changes saved in {}", (Object)resourcePath);
                    }
                    catch (SearchPromoteException e) {
                        this.log.error(e.getMessage(), (Throwable)e);
                    }
                    catch (PersistenceException e) {
                        this.log.error(e.getMessage(), (Throwable)e);
                    }
                    break block10;
                }
                this.log.error("Searchpromote Service User could not access resource {}", (Object)resourcePath);
            }
            catch (LoginException ex) {
                this.log.error(ex.getMessage(), (Throwable)ex);
            }
            finally {
                this.closeResolver(resolver);
            }
        }
    }

    private ResourceResolver getServiceResolver() throws LoginException {
        return this.resolverFactory.getServiceResourceResolver(null);
    }

    private void closeResolver(ResourceResolver resolver) {
        if (resolver != null) {
            resolver.close();
        }
    }

    protected void bindSearchPromoteService(SearchPromoteService searchPromoteService) {
        this.searchPromoteService = searchPromoteService;
    }

    protected void unbindSearchPromoteService(SearchPromoteService searchPromoteService) {
        if (this.searchPromoteService == searchPromoteService) {
            this.searchPromoteService = null;
        }
    }

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

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