JobConsumerManagerConfigurationServlet.java 5.93 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.servlet.ServletException
 *  org.apache.felix.scr.annotations.Properties
 *  org.apache.felix.scr.annotations.Property
 *  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.servlets.SlingAllMethodsServlet
 *  org.apache.sling.discovery.DiscoveryService
 *  org.apache.sling.discovery.InstanceDescription
 *  org.apache.sling.discovery.TopologyView
 *  org.osgi.service.cm.Configuration
 *  org.osgi.service.cm.ConfigurationAdmin
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.granite.offloading.impl.servlets;

import com.adobe.granite.offloading.api.InstanceNetworkInfoService;
import com.adobe.granite.offloading.impl.DiscoveryConfigurationCache;
import java.io.IOException;
import java.util.Collection;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Set;
import javax.servlet.ServletException;
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.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.discovery.DiscoveryService;
import org.apache.sling.discovery.InstanceDescription;
import org.apache.sling.discovery.TopologyView;
import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@SlingServlet(methods={"POST"}, paths={"/libs/granite/offloading/config/jobconsumer"})
@Properties(value={@Property(name="sling.auth.requirements", value={"-/libs/granite/offloading/config/jobconsumer"})})
public class JobConsumerManagerConfigurationServlet
extends SlingAllMethodsServlet {
    private static final Logger LOG = LoggerFactory.getLogger(JobConsumerManagerConfigurationServlet.class);
    @Reference
    private ConfigurationAdmin configurationAdmin;
    @Reference
    private DiscoveryService discoveryService;
    @Reference
    private DiscoveryConfigurationCache discoveryConfigurationCache;
    @Reference
    private InstanceNetworkInfoService iins;

    protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        Set<String> allowedHosts = this.discoveryConfigurationCache.getAllowedHosts();
        InstanceDescription localInstance = this.discoveryService.getTopology().getLocalInstance();
        allowedHosts.addAll(this.iins.getListeningAddresses(localInstance));
        if (allowedHosts.contains(request.getRemoteAddr()) || allowedHosts.contains(request.getRemoteHost())) {
            Configuration jobConsumerManagerConfig = this.configurationAdmin.getConfiguration("org.apache.sling.event.impl.jobs.JobConsumerManager");
            if (jobConsumerManagerConfig != null) {
                Hashtable<String, String[]> properties;
                String[] whitelist = request.getParameterValues("job.consumermanager.whitelist");
                String[] blacklist = request.getParameterValues("job.consumermanager.blacklist");
                Boolean persistConfig = Boolean.valueOf(request.getParameter("org.apache.sling.installer.configuration.persist"));
                if (whitelist == null && blacklist == null) {
                    response.sendError(400, "Expected job.consumermanager.blacklist or job.consumermanager.whitelist as a request parameter");
                }
                if ((properties = jobConsumerManagerConfig.getProperties()) == null) {
                    properties = new Hashtable<String, String[]>();
                }
                if (whitelist != null) {
                    properties.put("job.consumermanager.whitelist", whitelist);
                }
                if (blacklist != null) {
                    properties.put("job.consumermanager.blacklist", blacklist);
                }
                properties.put("org.apache.sling.installer.configuration.persist", (String[])persistConfig);
                jobConsumerManagerConfig.update(properties);
            }
        } else {
            response.sendError(404);
            LOG.error("Request made by unauthorized host: (" + request.getRemoteAddr() + ":" + request.getRemoteHost() + ")");
        }
    }

    protected void bindConfigurationAdmin(ConfigurationAdmin configurationAdmin) {
        this.configurationAdmin = configurationAdmin;
    }

    protected void unbindConfigurationAdmin(ConfigurationAdmin configurationAdmin) {
        if (this.configurationAdmin == configurationAdmin) {
            this.configurationAdmin = null;
        }
    }

    protected void bindDiscoveryService(DiscoveryService discoveryService) {
        this.discoveryService = discoveryService;
    }

    protected void unbindDiscoveryService(DiscoveryService discoveryService) {
        if (this.discoveryService == discoveryService) {
            this.discoveryService = null;
        }
    }

    protected void bindDiscoveryConfigurationCache(DiscoveryConfigurationCache discoveryConfigurationCache) {
        this.discoveryConfigurationCache = discoveryConfigurationCache;
    }

    protected void unbindDiscoveryConfigurationCache(DiscoveryConfigurationCache discoveryConfigurationCache) {
        if (this.discoveryConfigurationCache == discoveryConfigurationCache) {
            this.discoveryConfigurationCache = null;
        }
    }

    protected void bindIins(InstanceNetworkInfoService instanceNetworkInfoService) {
        this.iins = instanceNetworkInfoService;
    }

    protected void unbindIins(InstanceNetworkInfoService instanceNetworkInfoService) {
        if (this.iins == instanceNetworkInfoService) {
            this.iins = null;
        }
    }
}