PhoneGapBuildSettingsOperation.java 5.7 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.ui.components.HtmlResponse
 *  com.day.cq.i18n.I18n
 *  javax.servlet.http.HttpServletRequest
 *  org.apache.commons.lang.StringUtils
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Properties
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.request.RequestParameter
 *  org.apache.sling.api.resource.ModifiableValueMap
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.servlets.post.Modification
 */
package com.adobe.cq.mobile.platform.impl.operations;

import com.adobe.cq.mobile.platform.MobileResource;
import com.adobe.cq.mobile.platform.MobileResourceType;
import com.adobe.cq.mobile.platform.impl.operations.MobileAbstractOperation;
import com.adobe.granite.ui.components.HtmlResponse;
import com.day.cq.i18n.I18n;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
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.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.request.RequestParameter;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.servlets.post.Modification;

@Component(metatype=0, label="Associate a PhoneGap Build Cloud Service with the Mobile Application.")
@Service
@Properties(value={@Property(name="sling.post.operation", value={"mobileapps:phoneGapBuildSettings"})})
public class PhoneGapBuildSettingsOperation
extends MobileAbstractOperation {
    public static final String OPERATION_NAME = "phoneGapBuildSettings";
    public static final String PARAM_APP_INSTANCE = "appInstance";
    public static final String PARAM_PHONEGAP_CONFIG = "phonegapConfig";

    @Override
    protected void perform(SlingHttpServletRequest request, HtmlResponse response, List<Modification> changes) {
        I18n i18n = new I18n((HttpServletRequest)request);
        RequestParameter appInstanceParam = request.getRequestParameter("appInstance");
        if (appInstanceParam == null) {
            String message = i18n.get("Missing mandatory parameter {0}", null, new Object[]{"appInstance"});
            String title = i18n.get("Error");
            this.generateError(response, message, title);
            return;
        }
        Resource appInstance = request.getResourceResolver().getResource(appInstanceParam.toString());
        if (appInstance == null) {
            String message = i18n.get("Unable to resolve mobile application {0}", null, new Object[]{appInstanceParam.toString()});
            String title = i18n.get("Error");
            this.generateError(response, message, title);
            return;
        }
        MobileResource mobileResource = (MobileResource)appInstance.adaptTo(MobileResource.class);
        if (!mobileResource.isA(MobileResourceType.INSTANCE.getType())) {
            String message = i18n.get("The resource {0} must be a mobile application", null, new Object[]{mobileResource.getPath()});
            String title = i18n.get("Error");
            this.generateError(response, message, title);
            return;
        }
        String configPath = request.getParameter("phonegapConfig");
        if (StringUtils.isEmpty((String)configPath)) {
            String message = i18n.get("Missing mandatory parameter {0}", null, new Object[]{"phonegapConfig"});
            String title = i18n.get("Error");
            this.generateError(response, message, title);
            return;
        }
        this.updateMobileServicesConfig(appInstance, configPath, response);
        response.setPath(appInstance.getPath());
        response.setLocation(appInstance.getPath());
    }

    private void updateMobileServicesConfig(Resource appInstance, String configPath, HtmlResponse response) {
        Resource appContent = appInstance.getChild("jcr:content");
        String appContentPath = appContent.getPath();
        ModifiableValueMap properties = (ModifiableValueMap)appContent.adaptTo(ModifiableValueMap.class);
        boolean modified = properties.containsKey((Object)"phonegapConfig");
        properties.put((Object)"phonegapConfig", (Object)configPath);
        boolean hasCloudConfig = properties.containsKey((Object)"mobileServiceConfig") || properties.containsKey((Object)"notificationConfig");
        String[] configs = (String[])properties.get("cq:cloudserviceconfigs", String[].class);
        ArrayList<String> newConfigs = new ArrayList<String>();
        if (configs != null) {
            for (String config : configs) {
                if (StringUtils.startsWith((String)config, (String)"/etc/cloudservices/phonegap-build")) continue;
                newConfigs.add(config);
            }
        }
        newConfigs.add(configPath);
        String[] props = new String[newConfigs.size()];
        props = newConfigs.toArray(props);
        properties.put((Object)"cq:cloudserviceconfigs", (Object)props);
        String property = appContentPath + "/" + "phonegapConfig";
        String cloud = appContentPath + "/" + "cq:cloudserviceconfigs";
        if (modified) {
            response.onModified(property);
        } else {
            response.onCreated(property);
        }
        if (hasCloudConfig) {
            response.onModified(cloud);
        } else {
            response.onCreated(cloud);
        }
    }
}