DTMDeployHookServlet.java 6.02 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.servlet.Servlet
 *  javax.servlet.ServletException
 *  org.apache.felix.scr.annotations.Activate
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.ConfigurationPolicy
 *  org.apache.felix.scr.annotations.Modified
 *  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.SlingHttpServletRequest
 *  org.apache.sling.api.SlingHttpServletResponse
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.servlets.SlingAllMethodsServlet
 *  org.apache.sling.commons.osgi.PropertiesUtil
 *  org.osgi.service.cm.ConfigurationAdmin
 *  org.osgi.service.event.EventAdmin
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.dtm.impl.servlets;

import com.adobe.cq.dtm.impl.constants.DTMServerType;
import com.adobe.cq.dtm.impl.util.DTMConfigurationUtil;
import java.io.IOException;
import java.util.Map;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.ConfigurationPolicy;
import org.apache.felix.scr.annotations.Modified;
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.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.osgi.service.cm.ConfigurationAdmin;
import org.osgi.service.event.EventAdmin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=1, policy=ConfigurationPolicy.REQUIRE, label="Adobe DTM Deploy Hook Configuration", description="The central DTM deploy hook configuration component.")
@Service(value={Servlet.class})
@Properties(value={@Property(name="sling.servlet.resourceTypes", value={"etc/dtm/hook"}, propertyPrivate=1), @Property(name="sling.servlet.selectors", value={"bundle-download"}, propertyPrivate=1), @Property(name="sling.servlet.methods", value={"POST"}, propertyPrivate=1), @Property(name="sling.auth.requirements", value={"-/etc/dtm-hook"}, propertyPrivate=1)})
public class DTMDeployHookServlet
extends SlingAllMethodsServlet {
    private final Logger LOG = LoggerFactory.getLogger(DTMDeployHookServlet.class);
    public static final String DOWNLOAD_SELECTOR = "bundle-download";
    private String[] stagingIpWhitelist;
    private String[] productionIpWhitelist;
    @Reference
    private EventAdmin eventAdmin;
    @Reference
    private ConfigurationAdmin configurationAdmin;
    @Property(cardinality=Integer.MAX_VALUE, label="Staging DTM IP White List", description="List of DTM staging IPs which are allowed to trigger the DTM download workflow trough the DTM deploy hook feature.")
    public static final String DTM_STAGING_IP_WHITELIST = "dtm.staging.ip.whitelist";
    @Property(cardinality=Integer.MAX_VALUE, label="Production DTM IP White List", description="List of DTM production IPs which are allowed to trigger the DTM download workflow trough the DTM deploy hook feature.")
    public static final String DTM_PRODUCTION_IP_WHITELIST = "dtm.production.ip.whitelist";

    protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        String requestIPAddress;
        Resource currentResource = request.getResource();
        String dtmConfigName = currentResource.getParent().getName();
        DTMServerType dtmServerType = DTMServerType.valueOf(currentResource.getName().toUpperCase());
        if (this.isRequestFromAuthorizedHost(dtmServerType, requestIPAddress = request.getRemoteAddr())) {
            DTMConfigurationUtil.sendTriggerWorkflowOSGIEvent(this.eventAdmin, "/etc/cloudservices/dynamictagmanagement/" + dtmConfigName + "/" + "jcr:content", dtmServerType);
        } else {
            this.LOG.error("Request made by unauthorized host: (" + requestIPAddress + ")");
        }
    }

    private boolean isRequestFromAuthorizedHost(DTMServerType dtmServerType, String requestIPAddress) throws ServletException, IOException {
        String[] ipWhiteList;
        String[] arrstring = ipWhiteList = DTMServerType.STAGING.equals((Object)dtmServerType) ? this.stagingIpWhitelist : this.productionIpWhitelist;
        if (ipWhiteList != null) {
            for (String ip : ipWhiteList) {
                if (!requestIPAddress.equals(ip)) continue;
                return true;
            }
        } else {
            this.LOG.error("Unable to find the DTM {} servers IP white list property on the OSGI config.", (Object)dtmServerType);
            throw new ServletException("Unable to check if the host is authorized.");
        }
        return false;
    }

    @Activate
    @Modified
    private void activate(Map<String, Object> props) {
        if (props != null) {
            this.stagingIpWhitelist = PropertiesUtil.toStringArray((Object)props.get("dtm.staging.ip.whitelist"));
            this.productionIpWhitelist = PropertiesUtil.toStringArray((Object)props.get("dtm.production.ip.whitelist"));
        }
    }

    protected void bindEventAdmin(EventAdmin eventAdmin) {
        this.eventAdmin = eventAdmin;
    }

    protected void unbindEventAdmin(EventAdmin eventAdmin) {
        if (this.eventAdmin == eventAdmin) {
            this.eventAdmin = null;
        }
    }

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

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