DPSConfigActionsGetServlet.java 9.69 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.dps.client.authorization.AuthorizationDao
 *  com.adobe.dps.client.authorization.Project
 *  com.adobe.dps.client.producer.utils.AccessToken
 *  com.adobe.dps.client.utils.Config
 *  com.day.cq.i18n.I18n
 *  com.day.cq.wcm.api.Page
 *  com.day.cq.wcm.webservicesupport.Configuration
 *  com.day.cq.wcm.webservicesupport.ConfigurationManager
 *  javax.servlet.ServletException
 *  javax.servlet.http.HttpServletRequest
 *  org.apache.commons.lang3.StringUtils
 *  org.apache.felix.scr.annotations.Component
 *  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.adapter.AdapterManager
 *  org.apache.sling.api.request.RequestPathInfo
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.servlets.SlingAllMethodsServlet
 *  org.apache.sling.commons.json.JSONArray
 *  org.apache.sling.commons.json.JSONObject
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.mobile.dps.impl.servlets;

import com.adobe.cq.mobile.dps.DPSException;
import com.adobe.cq.mobile.dps.impl.DPSConnection;
import com.adobe.cq.mobile.dps.impl.service.AdobeDPSClient;
import com.adobe.cq.mobile.dps.impl.service.DPSConnectionAdapterFactory;
import com.adobe.cq.mobile.dps.impl.utils.DPSUtil;
import com.adobe.dps.client.authorization.AuthorizationDao;
import com.adobe.dps.client.authorization.Project;
import com.adobe.dps.client.producer.utils.AccessToken;
import com.adobe.dps.client.utils.Config;
import com.day.cq.i18n.I18n;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.webservicesupport.Configuration;
import com.day.cq.wcm.webservicesupport.ConfigurationManager;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.apache.felix.scr.annotations.Component;
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.adapter.AdapterManager;
import org.apache.sling.api.request.RequestPathInfo;
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.JSONArray;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Property(name="service.description", value={"Experience Manager Mobile Config Action GET Servlet"})
@Component(metatype=0)
@SlingServlet(methods={"GET"}, resourceTypes={"cq:Page"}, selectors={"getDPSProjects", "validateConnect"}, extensions={"json"}, generateComponent=0)
public class DPSConfigActionsGetServlet
extends SlingAllMethodsServlet {
    public static final String ACTION_GET_PROJECTS = "getDPSProjects";
    public static final String ACTION_GET_CONNECTION = "validateConnect";
    private static final Logger LOGGER = LoggerFactory.getLogger(DPSConfigActionsGetServlet.class);
    private static final String KEY_ERROR = "error";
    private static final String KEY_STATUS = "status";
    private static final String KEY_DURATION = "duration";
    private static final String STATUS_VALID = "valid";
    private static final String STATUS_INVALID = "invalid";
    @Reference
    AdapterManager adapterManager;
    @Reference
    DPSConnectionAdapterFactory dpsConnectionAdapterFactory;
    @Reference
    private AdobeDPSClient adobePublishClient;
    private I18n i18n;
    private static final String PARAM_DEVICE_ID = "deviceId";
    private static final String PARAM_DEVICE_TOKEN = "deviceToken";

    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        String[] selectors = request.getRequestPathInfo().getSelectors();
        String action = selectors[0];
        this.i18n = new I18n((HttpServletRequest)request);
        try {
            JSONObject jsonResponse;
            Resource resource = request.getResource();
            ConfigurationManager configurationManager = (ConfigurationManager)resource.getResourceResolver().adaptTo(ConfigurationManager.class);
            Page page = (Page)resource.adaptTo(Page.class);
            jsonResponse = new JSONObject();
            if (page != null) {
                try {
                    if ("getDPSProjects".equals(action)) {
                        Configuration dpsConfiguration = DPSUtil.getDPSConfiguration(configurationManager, page.getPath());
                        DPSConnection dpsConnection = this.dpsConnectionAdapterFactory.getAdapter((Object)dpsConfiguration, DPSConnection.class);
                        jsonResponse = this.getProjects(dpsConnection, page, configurationManager);
                    } else if ("validateConnect".equals(action)) {
                        long start = System.currentTimeMillis();
                        String deviceId = request.getParameter("deviceId");
                        if (StringUtils.isBlank((CharSequence)deviceId)) {
                            throw new ServletException(this.i18n.get("Missing mandatory request parameter {0}", "parameter name", new Object[]{"deviceId"}));
                        }
                        String deviceToken = request.getParameter("deviceToken");
                        if (StringUtils.isBlank((CharSequence)deviceToken)) {
                            throw new ServletException(this.i18n.get("Missing mandatory request parameter {0}", "parameter name", new Object[]{"deviceToken"}));
                        }
                        AdobeDPSClient.clearCache(deviceId + deviceToken);
                        Config config = this.adobePublishClient.getConfig(deviceId, deviceToken);
                        AccessToken accessToken = this.adobePublishClient.getAccessToken(config);
                        AuthorizationDao dao = new AuthorizationDao(config);
                        List projects = dao.getAuthorizedProjects(accessToken);
                        jsonResponse.put("status", (Object)(projects == null ? "invalid" : "valid"));
                        jsonResponse.put("duration", System.currentTimeMillis() - start);
                    }
                }
                catch (Exception ex) {
                    LOGGER.error("Experience Manager Mobile Action failed: " + action, (Throwable)ex);
                    jsonResponse.put("error", (Object)this.i18n.get(ex.getMessage()));
                }
            } else {
                jsonResponse.put("error", (Object)this.i18n.get("Not a valid resource {0}", "path to resource", new Object[]{resource.getPath()}));
            }
            response.getWriter().print(jsonResponse.toString());
        }
        catch (Exception ex) {
            LOGGER.error("Experience Manager Mobile Action failed: " + action, (Throwable)ex);
            throw new ServletException((Throwable)ex);
        }
    }

    private JSONObject getProjects(DPSConnection dpsConnection, Page page, ConfigurationManager configurationManager) throws Exception {
        Configuration dpsConfiguration = DPSUtil.getDPSConfiguration(configurationManager, page.getPath());
        if (dpsConfiguration == null) {
            throw new DPSException(this.i18n.get("DPS CloudService configuration not found on: {0}", "path to page", new Object[]{page.getPath()}));
        }
        JSONObject projectsJSON = new JSONObject();
        try {
            JSONArray dpsProjects = new JSONArray();
            AccessToken accessToken = dpsConnection.getAccessToken();
            AuthorizationDao authorizationDao = dpsConnection.getAuthorizationAccess();
            for (Project project : authorizationDao.getAuthorizedProjects(accessToken)) {
                JSONObject projectJSON = new JSONObject();
                projectJSON.put("projectId", (Object)project.projectGuid);
                projectJSON.put("name", (Object)project.projectName);
                projectJSON.put("title", (Object)project.projectTitle);
                dpsProjects.put((Object)projectJSON);
            }
            projectsJSON.put("projects", (Object)dpsProjects);
        }
        catch (Exception ex) {
            LOGGER.error("Failed to find projects", (Throwable)ex);
            throw ex;
        }
        return projectsJSON;
    }

    protected void bindAdapterManager(AdapterManager adapterManager) {
        this.adapterManager = adapterManager;
    }

    protected void unbindAdapterManager(AdapterManager adapterManager) {
        if (this.adapterManager == adapterManager) {
            this.adapterManager = null;
        }
    }

    protected void bindDpsConnectionAdapterFactory(DPSConnectionAdapterFactory dPSConnectionAdapterFactory) {
        this.dpsConnectionAdapterFactory = dPSConnectionAdapterFactory;
    }

    protected void unbindDpsConnectionAdapterFactory(DPSConnectionAdapterFactory dPSConnectionAdapterFactory) {
        if (this.dpsConnectionAdapterFactory == dPSConnectionAdapterFactory) {
            this.dpsConnectionAdapterFactory = null;
        }
    }

    protected void bindAdobePublishClient(AdobeDPSClient adobeDPSClient) {
        this.adobePublishClient = adobeDPSClient;
    }

    protected void unbindAdobePublishClient(AdobeDPSClient adobeDPSClient) {
        if (this.adobePublishClient == adobeDPSClient) {
            this.adobePublishClient = null;
        }
    }
}