DTMWorkflowLauncherServlet.java 3.67 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.workflow.WorkflowException
 *  com.day.cq.workflow.WorkflowService
 *  javax.jcr.PathNotFoundException
 *  javax.servlet.ServletException
 *  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.request.RequestPathInfo
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.api.servlets.SlingAllMethodsServlet
 *  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 com.day.cq.workflow.WorkflowException;
import com.day.cq.workflow.WorkflowService;
import java.io.IOException;
import javax.jcr.PathNotFoundException;
import javax.servlet.ServletException;
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.request.RequestPathInfo;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@SlingServlet(methods={"POST"}, resourceTypes={"cq/dtm/components/dynamictagmanagementpage"}, selectors={"staging", "production"}, extensions={"html"})
public class DTMWorkflowLauncherServlet
extends SlingAllMethodsServlet {
    private static final Logger LOG = LoggerFactory.getLogger(DTMWorkflowLauncherServlet.class);
    @Reference
    private WorkflowService workflowService;

    /*
     * Enabled force condition propagation
     * Lifted jumps to return sites
     */
    protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        String[] selectors = request.getRequestPathInfo().getSelectors();
        if (selectors == null || selectors.length != 1) throw new ServletException("Unable to extract the request sling selector.");
        DTMServerType dtmServerType = DTMServerType.valueOf(selectors[0].toUpperCase());
        Resource dtmConfigResource = request.getResource();
        String urlString = (String)((ValueMap)dtmConfigResource.adaptTo(ValueMap.class)).get((Object)((Object)((Object)dtmServerType) + "BundleURL"));
        if (!this.isUrlValid(urlString)) throw new ServletException("The provided DTM Bundle URL is invalid.");
        try {
            DTMConfigurationUtil.triggerDTMWorkflow(this.workflowService, dtmConfigResource, dtmServerType);
            LOG.info("DTM {} bundle download workflow started.", (Object)dtmServerType);
            return;
        }
        catch (WorkflowException e) {
            throw new ServletException((Throwable)e);
        }
        catch (PathNotFoundException e) {
            throw new ServletException((Throwable)e);
        }
    }

    private boolean isUrlValid(String urlString) {
        String URL_REGEX = "^(https?://)(([a-z.-]+).([a-z.]{2,6})|localhost|\\b(?:\\d{1,3}\\.){3}\\d{1,3}\\b)(:[0-9]{1,5})?[\\w .\\-/]*$";
        return urlString.matches(URL_REGEX);
    }

    protected void bindWorkflowService(WorkflowService workflowService) {
        this.workflowService = workflowService;
    }

    protected void unbindWorkflowService(WorkflowService workflowService) {
        if (this.workflowService == workflowService) {
            this.workflowService = null;
        }
    }
}