RemoteIndexCallServlet.java 5.47 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.wcm.webservicesupport.Configuration
 *  com.day.cq.wcm.webservicesupport.ConfigurationManager
 *  com.day.cq.wcm.webservicesupport.ConfigurationManagerFactory
 *  javax.servlet.ServletException
 *  org.apache.commons.lang3.StringUtils
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.sling.SlingServlet
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.SlingHttpServletResponse
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.servlets.SlingAllMethodsServlet
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.JSONObject
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.searchpromote.feed.impl.servlet;

import com.day.cq.searchpromote.SearchPromoteException;
import com.day.cq.searchpromote.SearchPromoteService;
import com.day.cq.wcm.webservicesupport.Configuration;
import com.day.cq.wcm.webservicesupport.ConfigurationManager;
import com.day.cq.wcm.webservicesupport.ConfigurationManagerFactory;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import org.apache.commons.lang3.StringUtils;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@SlingServlet(name="com.day.cq.searchpromote.feed.impl.servlet.RemoteIndexCallServlet", description="Calls the remote index retrieval for Search&Promote", paths={"/libs/cq/searchpromote/remoteindex"}, methods={"POST"})
public class RemoteIndexCallServlet
extends SlingAllMethodsServlet {
    private final Logger log;
    @Reference
    private SearchPromoteService searchPromoteService;
    @Reference
    private ConfigurationManagerFactory configManagerFactory;
    private static final String INDEX_ALL_PARAM_KEY = "fullfeed";
    private static final String SP_ACCOUNT_PARAM_KEY = "sp_account";
    private static final String INDEX_PASS_PARAM_KEY = "index_password";
    private static final String BAD_REQUEST_ERR = "Some of the required parameters are missing (sp_account or index_password)";

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

    protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        String configPath = request.getParameter("configPath");
        Configuration confObject = this.configManagerFactory.getConfigurationManager(request.getResourceResolver()).getConfiguration(configPath);
        String indexPass = (String)confObject.get("remotectlpass", (Object)"");
        String spAccount = (String)confObject.getInherited("accountno", (Object)"");
        String fullIndex = request.getParameter("fullfeed");
        boolean fullFeedRequested = StringUtils.isNotEmpty((CharSequence)fullIndex) && Boolean.TRUE.toString().equals(fullIndex);
        this.log.debug("Calling remote index for account {} (password {})", (Object)spAccount, (Object)indexPass);
        if (StringUtils.isEmpty((CharSequence)spAccount) || StringUtils.isEmpty((CharSequence)indexPass)) {
            this.log.error("Some of the required parameters are missing (sp_account or index_password)");
            response.sendError(400, "Some of the required parameters are missing (sp_account or index_password)");
        }
        try {
            String responseStatus = this.searchPromoteService.callRemoteIndex(spAccount, indexPass, fullFeedRequested);
            this.log.debug("Indexing requested, response status was {}", (Object)responseStatus);
            this.sendResponse(responseStatus, response);
        }
        catch (SearchPromoteException e) {
            this.log.error(e.getMessage(), (Throwable)e);
            response.sendError(500, e.getMessage());
        }
        catch (JSONException e) {
            response.sendError(500, e.getMessage());
        }
    }

    private void sendResponse(String responseCode, SlingHttpServletResponse response) throws JSONException, IOException {
        JSONObject jsonObject = new JSONObject();
        jsonObject.append("status_code", (Object)responseCode);
        response.setContentType("application/json");
        response.setStatus(200);
        PrintWriter out = response.getWriter();
        out.write(jsonObject.toString());
    }

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

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

    protected void bindConfigManagerFactory(ConfigurationManagerFactory configurationManagerFactory) {
        this.configManagerFactory = configurationManagerFactory;
    }

    protected void unbindConfigManagerFactory(ConfigurationManagerFactory configurationManagerFactory) {
        if (this.configManagerFactory == configurationManagerFactory) {
            this.configManagerFactory = null;
        }
    }
}