MACTenantConfigurationValidationServlet.java 10.3 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.cq.dam.mac.sync.api.SyncAgent
 *  com.adobe.cq.dam.mac.sync.api.SyncAgentFactory
 *  com.adobe.granite.crypto.CryptoException
 *  com.day.cq.i18n.I18n
 *  javax.servlet.ServletException
 *  javax.servlet.http.HttpServletRequest
 *  org.apache.commons.lang.StringUtils
 *  org.apache.felix.scr.annotations.Activate
 *  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.LoginException
 *  org.apache.sling.api.resource.Resource
 *  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.osgi.framework.BundleContext
 *  org.osgi.service.cm.ConfigurationAdmin
 *  org.osgi.service.component.ComponentContext
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.dam.mac.sync.helper.impl.servlets;

import com.adobe.cq.dam.mac.sync.api.SyncAgent;
import com.adobe.cq.dam.mac.sync.api.SyncAgentFactory;
import com.adobe.cq.dam.mac.sync.helper.MacSyncHelper;
import com.adobe.cq.dam.mac.sync.helper.impl.MACSyncClient;
import com.adobe.cq.dam.mac.sync.helper.impl.MACSyncClientServiceTracker;
import com.adobe.cq.dam.mac.sync.helper.impl.MACTenantConfiguration;
import com.adobe.cq.dam.mac.sync.helper.impl.MACTenantConfigurationResolver;
import com.adobe.cq.dam.mac.sync.helper.impl.util.OAuthUtil;
import com.adobe.granite.crypto.CryptoException;
import com.day.cq.i18n.I18n;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Activate;
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.LoginException;
import org.apache.sling.api.resource.Resource;
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.osgi.framework.BundleContext;
import org.osgi.service.cm.ConfigurationAdmin;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@SlingServlet(methods={"POST"}, resourceTypes={"dam/components/marketingcloud/config", "dam/components/mediaportal/config"}, selectors={"validate"}, extensions={"html"})
public class MACTenantConfigurationValidationServlet
extends SlingAllMethodsServlet {
    private static final Logger LOG = LoggerFactory.getLogger(MACTenantConfigurationValidationServlet.class);
    private static final String CLIENT_ID = "clientId";
    private static final String SCOPES = "oauthScopes";
    private static final String TENANT_URL_PARAM = "tenantURL";
    private static final String TENANT_ID = "tenantId";
    private static final String AUDIENCE = "audience";
    private static final String MAC_CONFIG_RESOURCE_TYPE = "dam/components/marketingcloud/config";
    private static final String MP_CONFIG_RESOURCE_TYPE = "dam/components/mediaportal/config";
    private static final String MP_KEY_PAIR_ALIAS = "mpreplication";
    private static final long TIMEOUT = 5000;
    private BundleContext bundleContext;
    @Reference
    private ConfigurationAdmin configurationAdmin = null;
    @Reference
    private SyncAgentFactory agentFactory = null;
    @Reference
    private MacSyncHelper macSyncHelper = null;
    @Reference
    private MACTenantConfigurationResolver mtcr = null;

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        block40 : {
            response.setContentType("application/json");
            String clientId = request.getParameter("clientId");
            String scopes = request.getParameter("oauthScopes");
            String tenantURL = request.getParameter("tenantURL");
            String tenant = request.getParameter("tenantId");
            String audience = request.getParameter("audience");
            JSONObject ret = new JSONObject();
            I18n i18n = new I18n((HttpServletRequest)request);
            boolean error = false;
            String errorMessage = null;
            MACSyncClientServiceTracker st = null;
            try {
                if (StringUtils.isEmpty((String)clientId) || StringUtils.isEmpty((String)scopes) || StringUtils.isEmpty((String)tenantURL) || StringUtils.isEmpty((String)audience)) {
                    LOG.error("Invalid request: one of the required parameters is missing or does not have a value.");
                    error = true;
                    errorMessage = "Invalid request: one of the required parameters is missing or does not have a value.";
                    break block40;
                }
                Resource configRes = request.getResource();
                String pid = null;
                pid = null != configRes && configRes.getResourceType().startsWith("dam/components/mediaportal/config") ? OAuthUtil.updateAccessTokenProvider(this.configurationAdmin, clientId, scopes, audience, "mpreplication") : OAuthUtil.updateAccessTokenProvider(this.configurationAdmin, clientId, scopes, audience);
                ResourceResolver rr = request.getResourceResolver();
                MACTenantConfiguration macConfiguration = this.mtcr.getConfiguration(rr, configRes.getPath());
                this.macSyncHelper.updateReplicationAgentProviderPid(this.agentFactory.getSyncAgent("replication"), rr, macConfiguration, pid);
                st = new MACSyncClientServiceTracker(this.bundleContext, pid);
                st.open();
                MACSyncClient syncClient = (MACSyncClient)st.waitForService(5000);
                if (syncClient != null) {
                    int status = syncClient.validateConnection(clientId, tenantURL, scopes, tenant);
                    if (status == 401) {
                        error = true;
                        errorMessage = i18n.get("Invalid configuration.");
                    }
                    break block40;
                }
                error = true;
                errorMessage = i18n.get("not able to setup mac sync, could not get MACSyncClient Service.");
            }
            catch (MalformedURLException e) {
                LOG.error("Malformed tenant URL: " + tenantURL, (Throwable)e);
                error = true;
                errorMessage = i18n.get("Malformed tenant URL.");
            }
            catch (IOException e) {
                LOG.error("Unable to reach tenant server", (Throwable)e);
                error = true;
                errorMessage = "Unable to reach the tenant server. Is the tenant URL correct?";
            }
            catch (LoginException e) {
                LOG.error("Unable to impersonate user", (Throwable)e);
                error = true;
                errorMessage = "Unable to validate connection";
            }
            catch (CryptoException e) {
                LOG.error("Unable to get access token", (Throwable)e);
                error = true;
                errorMessage = "Unable to get access token";
            }
            catch (URISyntaxException e) {
                LOG.error("Wrong tenant url", (Throwable)e);
                error = true;
                errorMessage = "Error parsing tenant url. Is the tenant URL correct?";
            }
            catch (IllegalArgumentException e) {
                LOG.error("Configuration is not setup correctly", (Throwable)e);
                error = true;
                errorMessage = "Configuration is not setup correctly";
            }
            catch (InterruptedException e) {
                LOG.error("Interrupted while waiting for service", (Throwable)e);
                error = true;
                errorMessage = "Interrupted while waiting for service MACSyncClient";
            }
            finally {
                PrintWriter out = response.getWriter();
                if (error) {
                    try {
                        ret.put("error", (Object)errorMessage);
                    }
                    catch (JSONException e) {
                        LOG.error("JSON exception while building response", (Throwable)e);
                    }
                }
                out.write(ret.toString());
                if (st != null) {
                    st.close();
                }
            }
        }
    }

    @Activate
    private void activate(ComponentContext componentContext) {
        this.bundleContext = componentContext.getBundleContext();
    }

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

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

    protected void bindAgentFactory(SyncAgentFactory syncAgentFactory) {
        this.agentFactory = syncAgentFactory;
    }

    protected void unbindAgentFactory(SyncAgentFactory syncAgentFactory) {
        if (this.agentFactory == syncAgentFactory) {
            this.agentFactory = null;
        }
    }

    protected void bindMacSyncHelper(MacSyncHelper macSyncHelper) {
        this.macSyncHelper = macSyncHelper;
    }

    protected void unbindMacSyncHelper(MacSyncHelper macSyncHelper) {
        if (this.macSyncHelper == macSyncHelper) {
            this.macSyncHelper = null;
        }
    }

    protected void bindMtcr(MACTenantConfigurationResolver mACTenantConfigurationResolver) {
        this.mtcr = mACTenantConfigurationResolver;
    }

    protected void unbindMtcr(MACTenantConfigurationResolver mACTenantConfigurationResolver) {
        if (this.mtcr == mACTenantConfigurationResolver) {
            this.mtcr = null;
        }
    }
}