InitiatePhoneGapBuildProcess.java 10.3 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.workflow.WorkflowException
 *  com.adobe.granite.workflow.WorkflowSession
 *  com.adobe.granite.workflow.exec.WorkItem
 *  com.adobe.granite.workflow.exec.Workflow
 *  com.adobe.granite.workflow.exec.WorkflowData
 *  com.adobe.granite.workflow.exec.WorkflowProcess
 *  com.adobe.granite.workflow.metadata.MetaDataMap
 *  com.day.cq.wcm.api.Page
 *  javax.jcr.Session
 *  org.apache.felix.scr.annotations.Component
 *  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.adapter.AdapterManager
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceResolverFactory
 *  org.apache.sling.commons.json.JSONObject
 *  org.ccil.cowan.tagsoup.jaxp.SAXParserImpl
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.mobile.phonegap.impl.build.workflow;

import com.adobe.cq.mobile.phonegap.impl.PGException;
import com.adobe.cq.mobile.phonegap.impl.build.PGBuildManager;
import com.adobe.cq.mobile.phonegap.impl.build.metadata.AppInfo;
import com.adobe.cq.mobile.phonegap.impl.build.service.PGBuildManagerAdapterFactory;
import com.adobe.granite.workflow.WorkflowException;
import com.adobe.granite.workflow.WorkflowSession;
import com.adobe.granite.workflow.exec.WorkItem;
import com.adobe.granite.workflow.exec.Workflow;
import com.adobe.granite.workflow.exec.WorkflowData;
import com.adobe.granite.workflow.exec.WorkflowProcess;
import com.adobe.granite.workflow.metadata.MetaDataMap;
import com.day.cq.wcm.api.Page;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Map;
import javax.jcr.Session;
import org.apache.felix.scr.annotations.Component;
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.adapter.AdapterManager;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.commons.json.JSONObject;
import org.ccil.cowan.tagsoup.jaxp.SAXParserImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

@Component
@Service
@Properties(value={@Property(name="service.description", value={"PhoneGap Workflow process that creates and updates app builds on PhoneGap build and tracks its build status."}), @Property(name="service.vendor", value={"Adobe"}), @Property(name="process.label", value={"Initiate PhoneGap Build"})})
public class InitiatePhoneGapBuildProcess
implements WorkflowProcess {
    private static final String WORKFLOW_PAYLOAD_TYPE_JCR_PATH = "JCR_PATH";
    private static final Logger logger = LoggerFactory.getLogger(InitiatePhoneGapBuildProcess.class);
    @Reference
    private AdapterManager adapterManager;
    @Reference
    PGBuildManagerAdapterFactory phonegapBuildManagerAdapterFactory;
    @Reference
    private ResourceResolverFactory resourceResolverFactory;

    public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap) throws WorkflowException {
        WorkflowData workflowData;
        block14 : {
            workflowData = workItem.getWorkflowData();
            try {
                logger.info("Initiating build of {} with PhoneGap Build", workflowData.getPayload());
                if (workflowData.getPayloadType().equals("JCR_PATH")) {
                    String payloadPath = String.valueOf(workflowData.getPayload());
                    ResourceResolver resourceResolver = (ResourceResolver)workflowSession.adaptTo(ResourceResolver.class);
                    Resource resource = resourceResolver.getResource(payloadPath);
                    Page page = (Page)this.adapterManager.getAdapter((Object)resource, Page.class);
                    if (page == null) {
                        throw new PGException("Not a valid page resource for PhoneGap Build workflow: " + (Object)resource);
                    }
                    PGBuildManager pgBuildManager = this.phonegapBuildManagerAdapterFactory.getAdapter((Object)resourceResolver, PGBuildManager.class);
                    AppInfo appinfo = pgBuildManager.getApp(page);
                    if (appinfo == null) {
                        pgBuildManager.createApp(page);
                    } else {
                        pgBuildManager.updateApp(page);
                    }
                    logger.info("Initiating build of {} with PhoneGap Build completed", workflowData.getPayload());
                    break block14;
                }
                throw new PGException("Not a valid payload type for PhoneGap Build workflow: " + workflowData.getPayloadType());
            }
            catch (Exception ex) {
                try {
                    String cause = ex.getCause().getMessage();
                    if (cause.contains("ErrorDetails: ")) {
                        if ((cause = cause.substring(cause.indexOf("ErrorDetails: ") + 14).replaceAll("\\s$", "")).startsWith("<!DOCTYPE")) {
                            PhoneGapBuildHtmlErrorHandler handler = new PhoneGapBuildHtmlErrorHandler();
                            SAXParserImpl.newInstance((Map)null).parse((InputStream)new ByteArrayInputStream(cause.getBytes("UTF-8")), (DefaultHandler)handler);
                            cause = handler.getErrorMessage();
                        } else {
                            JSONObject causeJson = new JSONObject(cause);
                            cause = causeJson.getString("error");
                        }
                    }
                    this.updateWorkflowData(workItem, workflowSession, "PGBuildError", cause, true);
                    WorkflowException wfEx = new WorkflowException(cause, (Throwable)ex);
                    logger.error(wfEx.getMessage(), (Throwable)wfEx);
                    throw wfEx;
                }
                catch (Exception ex2) {
                    WorkflowException wfEx = new WorkflowException("Attempt to save PGBuild failure message to workflow data failed", (Throwable)ex2);
                    logger.error(wfEx.getMessage(), (Throwable)wfEx);
                    throw wfEx;
                }
            }
        }
        try {
            if (workflowData.getMetaDataMap().get("PGBuildError", String.class) != null) {
                this.updateWorkflowData(workItem, workflowSession, "PGBuildError", "Resolved", false);
            }
        }
        catch (Exception ex3) {
            WorkflowException wfEx = new WorkflowException("Attempt to clear PGBuild failure message failed", (Throwable)ex3);
            logger.error(wfEx.getMessage(), (Throwable)wfEx);
            throw wfEx;
        }
    }

    protected void updateWorkflowData(WorkItem item, WorkflowSession wfSession, String key, String value, boolean saveSession) throws Exception {
        WorkflowData data = item.getWorkflowData();
        String currentValue = (String)data.getMetaDataMap().get(key, String.class);
        if (currentValue != null && value.isEmpty() || currentValue == null || !currentValue.equals(value)) {
            MetaDataMap metaData = data.getMetaDataMap();
            metaData.put((Object)key, (Object)value);
            wfSession.updateWorkflowData(item.getWorkflow(), data);
            if (saveSession) {
                ((Session)wfSession.adaptTo(Session.class)).save();
            }
        }
    }

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

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

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

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

    protected void bindResourceResolverFactory(ResourceResolverFactory resourceResolverFactory) {
        this.resourceResolverFactory = resourceResolverFactory;
    }

    protected void unbindResourceResolverFactory(ResourceResolverFactory resourceResolverFactory) {
        if (this.resourceResolverFactory == resourceResolverFactory) {
            this.resourceResolverFactory = null;
        }
    }

    private class PhoneGapBuildHtmlErrorHandler
    extends DefaultHandler {
        private StringBuilder errorMessage;
        private boolean inBody;

        private PhoneGapBuildHtmlErrorHandler() {
            this.errorMessage = new StringBuilder();
            this.inBody = false;
        }

        @Override
        public void startElement(String uri, String localName, String qName, Attributes attributes) {
            if (this.inBody) {
                if (qName.equals("h1")) {
                    qName = "h3";
                }
                this.errorMessage.append("<" + qName + ">");
            }
            if (qName.equalsIgnoreCase("body")) {
                this.inBody = true;
            }
        }

        @Override
        public void characters(char[] ch, int start, int length) throws SAXException {
            if (this.inBody) {
                this.errorMessage.append(Arrays.copyOfRange(ch, start, start + length));
            }
        }

        @Override
        public void endElement(String uri, String localName, String qName) throws SAXException {
            if (this.inBody) {
                this.errorMessage.append("</" + qName + ">");
            }
            if (qName.equals("body")) {
                this.inBody = false;
            }
        }

        public String getErrorMessage() {
            return this.errorMessage.toString();
        }
    }

}