SearchPromoteIndexCaller.java 5.16 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.cq.scheduled.exporter.ExportException
 *  com.adobe.cq.scheduled.exporter.Exporter
 *  com.day.cq.wcm.webservicesupport.Configuration
 *  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.ModifiableValueMap
 *  org.apache.sling.api.resource.PersistenceException
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ValueMap
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.searchpromote.feed.impl.job;

import com.adobe.cq.scheduled.exporter.ExportException;
import com.adobe.cq.scheduled.exporter.Exporter;
import com.day.cq.searchpromote.SearchPromoteException;
import com.day.cq.searchpromote.SearchPromoteService;
import com.day.cq.wcm.webservicesupport.Configuration;
import java.util.Iterator;
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.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.ValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(immediate=1, label="Day CQ Search&Promote index caller", description="Calls the remote indexing service from Search&Promote")
@Service
@Properties(value={@Property(name="exporter.type", value={"searchpromote-index-caller"})})
public class SearchPromoteIndexCaller
implements Exporter {
    private final Logger log;
    private static final String CONFIG_PATH_KEY = "configPath";
    private static final String REMOTE_PASS_KEY = "remotectlpass";
    private static final String CONFIG_LAST_REQUEST_STATUS_KEY = "lastRequestResult";
    @Reference
    private SearchPromoteService searchPromoteService;
    private String accountNo;
    private String remoteCtlPassword;
    private String schedulerConfigPath;

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

    public void exportData(Resource config, Resource resource) throws ExportException {
        this.log.debug("Calling the remote indexing service...");
        try {
            ResourceResolver resolver = config.getResourceResolver();
            this.configure(config);
            String responseStatus = this.searchPromoteService.callRemoteIndex(this.accountNo, this.remoteCtlPassword, false);
            this.log.debug("Server responded: {}", (Object)responseStatus);
            this.updateResult(resolver, responseStatus);
        }
        catch (SearchPromoteException e) {
            this.log.error(e.getMessage(), (Throwable)e);
            throw new ExportException((Throwable)e);
        }
        catch (PersistenceException e) {
            this.log.error(e.getMessage(), (Throwable)e);
            throw new ExportException((Throwable)e);
        }
    }

    private void updateResult(ResourceResolver resolver, String responseStatus) throws PersistenceException {
        Resource configurationResource = resolver.getResource(this.schedulerConfigPath + "/jcr:content");
        ModifiableValueMap mvm = (ModifiableValueMap)configurationResource.adaptTo(ModifiableValueMap.class);
        mvm.put((Object)"lastRequestResult", (Object)responseStatus);
        resolver.commit();
    }

    private void configure(Resource config) throws SearchPromoteException {
        ValueMap vm = (ValueMap)config.adaptTo(ValueMap.class);
        String configPath = (String)vm.get("configPath", (Object)"");
        ResourceResolver resolver = config.getResourceResolver();
        Resource configResource = resolver.getResource(configPath);
        Configuration confObject = (Configuration)configResource.getParent().adaptTo(Configuration.class);
        if (confObject == null) {
            this.log.error("Configuration {} not found.", (Object)configPath);
            throw new SearchPromoteException("Configuration " + configPath + " not found.");
        }
        this.accountNo = (String)confObject.get("accountno", (Object)"");
        Iterator confIterator = confObject.listChildren();
        if (confIterator.hasNext()) {
            Configuration schedulerConfig = (Configuration)confIterator.next();
            this.schedulerConfigPath = schedulerConfig.getPath();
            this.remoteCtlPassword = (String)schedulerConfig.get("remotectlpass", (Object)"");
        }
    }

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

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