PGBuildActionsServlet.java 10.6 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.wcm.api.Page
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.servlet.ServletException
 *  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.request.RequestPathInfo
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.api.servlets.SlingAllMethodsServlet
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.JSONObject
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.mobile.phonegap.impl.build.servlets;

import com.adobe.cq.mobile.phonegap.impl.PGBuildException;
import com.adobe.cq.mobile.phonegap.impl.PGException;
import com.adobe.cq.mobile.phonegap.impl.build.PGBErrorCodes;
import com.adobe.cq.mobile.phonegap.impl.build.PGBuildManager;
import com.adobe.cq.mobile.phonegap.impl.build.metadata.AccountInfo;
import com.adobe.cq.mobile.phonegap.impl.build.metadata.AppInfo;
import com.adobe.cq.mobile.phonegap.impl.build.service.PGBuildManagerAdapterFactory;
import com.day.cq.wcm.api.Page;
import java.io.IOException;
import java.io.PrintWriter;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.servlet.ServletException;
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.request.RequestPathInfo;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Property(name="service.description", value={"PGBuild Actions Servlet"})
@Component(metatype=0)
@SlingServlet(methods={"GET"}, resourceTypes={"cq:Page"}, selectors={"pgbuild.getApp", "pgbuild.getAccountProfile"}, extensions={"json"}, generateComponent=0)
public class PGBuildActionsServlet
extends SlingAllMethodsServlet {
    public static final String ACTION_PREFIX = "pgbuild.";
    public static final String ACTION_GET_APP = "pgbuild.getApp";
    public static final String ACTION_GET_ACCOUNT_PROFILE = "pgbuild.getAccountProfile";
    private static final Logger LOGGER = LoggerFactory.getLogger(PGBuildActionsServlet.class);
    private static final String PARAM_USER = "user";
    private static final String PARAM_PASSWORD = "password";
    private static final String KEY_ERROR = "error";
    private static final String KEY_ERROR_CODE = "errorCode";
    private static final String KEY_ERROR_DETAILS = "errorDetails";
    private static final String KEY_RESPONSE = "response";
    private static final String KEY_STATUS = "status";
    private static final String STATUS_OK = "ok";
    private static final String PN_PHONEGAP_BUILD_CONFIG = "phonegapConfig";
    private static final String PN_PHONEGAP_BUILD_ID = "phonegap-buildId";
    @Reference
    PGBuildManagerAdapterFactory phonegapBuildManagerAdapterFactory;

    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        String[] selectors = request.getRequestPathInfo().getSelectors();
        String action = selectors[0] + "." + selectors[1];
        try {
            JSONObject jsonResponse;
            ResourceResolver resolver = request.getResourceResolver();
            Session session = (Session)resolver.adaptTo(Session.class);
            Resource resource = request.getResource();
            Page page = (Page)resource.adaptTo(Page.class);
            PGBuildManager pgBuildManager = this.phonegapBuildManagerAdapterFactory.getAdapter((Object)session, PGBuildManager.class);
            jsonResponse = new JSONObject();
            if (page != null) {
                try {
                    if (action.equals("pgbuild.getApp")) {
                        jsonResponse.put("response", (Object)PGBuildActionsServlet.getApp(pgBuildManager, page));
                    } else if (action.equals("pgbuild.getAccountProfile")) {
                        String user = request.getParameter("user");
                        String password = request.getParameter("password");
                        if (user == null || password == null || user.isEmpty() || password.isEmpty()) {
                            jsonResponse.put("response", (Object)PGBuildActionsServlet.getAccountProfile(pgBuildManager, page, session));
                        } else {
                            jsonResponse.put("response", (Object)PGBuildActionsServlet.getAccountProfile(pgBuildManager, user, password));
                        }
                    } else {
                        throw new Exception("Unknown request");
                    }
                    jsonResponse.put("status", (Object)"ok");
                }
                catch (PGException ex) {
                    LOGGER.debug("Failed request", (Throwable)ex);
                    jsonResponse.put("error", (Object)ex.getMessage());
                    PGBuildException pgBuildException = this.getPGBuildException(ex);
                    if (pgBuildException != null) {
                        jsonResponse.put("errorCode", (Object)pgBuildException.getErrorCode());
                        jsonResponse.put("errorDetails", (Object)pgBuildException.getErrorDetails());
                    }
                }
                catch (Exception ex) {
                    LOGGER.error("Failed request", (Throwable)ex);
                    jsonResponse.put("error", (Object)ex.getMessage());
                }
            } else {
                jsonResponse.put("error", (Object)("Not a valid PGBuild enabled page" + resource.getPath()));
            }
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write(jsonResponse.toString());
        }
        catch (Exception ex) {
            LOGGER.error("PBBuild Action failed: " + action, (Throwable)ex);
            throw new ServletException((Throwable)ex);
        }
    }

    private PGBuildException getPGBuildException(PGException pgException) {
        if (pgException instanceof PGBuildException) {
            return (PGBuildException)pgException;
        }
        Throwable causeException = pgException.getCause();
        if (causeException != null && causeException instanceof PGException) {
            return this.getPGBuildException((PGException)causeException);
        }
        return null;
    }

    private static JSONObject getAccountProfile(PGBuildManager pgBuildManager, Page page, Session session) throws PGException, JSONException {
        PGBuildActionsServlet.validatePageProperty(page, "phonegapConfig", PGBErrorCodes.PGBERROR_CODE_PGBUILD_CONFIG_MISSING, PGBErrorCodes.PGBERROR_CODE_PGBUILD_CONFIG_UNLINKED, true, session);
        AccountInfo accinfo = pgBuildManager.getAccountProfile(page);
        if (accinfo != null) {
            return accinfo.getJSON();
        }
        throw new PGException("Failed to get account profile");
    }

    private static JSONObject getAccountProfile(PGBuildManager pgBuildManager, String userId, String password) throws PGException, JSONException {
        AccountInfo accInfo = pgBuildManager.getAccountProfile(userId, password);
        if (accInfo != null) {
            return accInfo.getJSON();
        }
        throw new PGException("Failed to get account profile");
    }

    private static JSONObject getApp(PGBuildManager pgBuildManager, Page page) throws PGException, JSONException {
        PGBuildActionsServlet.validatePageProperty(page, "phonegap-buildId", PGBErrorCodes.PGBERROR_CODE_BUILD_ID_MISSING);
        AppInfo appinfo = pgBuildManager.getApp(page);
        if (appinfo != null) {
            return appinfo.getJSON();
        }
        throw new PGException("Failed to get app for " + page.getPath());
    }

    private static void validatePageProperty(Page page, String propertyName, Integer missingErrorCode) throws PGBuildException {
        PGBuildActionsServlet.validatePageProperty(page, propertyName, missingErrorCode, -1, false, null);
    }

    private static void validatePageProperty(Page page, String propertyName, Integer missingErrorCode, Integer unlinkedErrorCode, boolean isJcrPath, Session session) throws PGBuildException {
        Resource contentRes = page.getContentResource();
        if (contentRes == null) {
            throw new PGBuildException("Property " + propertyName + " not found on page " + page.getPath(), missingErrorCode, "Property " + propertyName + " not found on page " + page.getPath());
        }
        ValueMap valueMap = (ValueMap)contentRes.adaptTo(ValueMap.class);
        if (!valueMap.containsKey((Object)propertyName)) {
            throw new PGBuildException("Property " + propertyName + " not found on page " + page.getPath(), missingErrorCode, "Property " + propertyName + " not found on page " + page.getPath());
        }
        if (isJcrPath) {
            String cloudCfg = (String)valueMap.get(propertyName, String.class);
            try {
                if (!session.nodeExists(cloudCfg)) {
                    throw new PGBuildException("Property " + propertyName + " targets non-existent entity on " + page.getPath(), unlinkedErrorCode, "Property " + propertyName + " targets non-existent entity on " + page.getPath());
                }
            }
            catch (RepositoryException repEx) {
                throw new PGBuildException("Property " + propertyName + " targets non-existent entity on " + page.getPath(), unlinkedErrorCode, "Property " + propertyName + " targets non-existent entity on " + page.getPath());
            }
        }
    }

    protected void bindPhonegapBuildManagerAdapterFactory(PGBuildManagerAdapterFactory pGBuildManagerAdapterFactory) {
        this.phonegapBuildManagerAdapterFactory = pGBuildManagerAdapterFactory;
    }

    protected void unbindPhonegapBuildManagerAdapterFactory(PGBuildManagerAdapterFactory pGBuildManagerAdapterFactory) {
        if (this.phonegapBuildManagerAdapterFactory == pGBuildManagerAdapterFactory) {
            this.phonegapBuildManagerAdapterFactory = null;
        }
    }
}