MobileAppScreenshotDeleteOperation.java 6.39 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.jackrabbit.util.Text
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.request.RequestParameter
 *  org.apache.sling.api.resource.ModifiableValueMap
 *  org.apache.sling.api.resource.PersistenceException
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.resource.collection.ResourceCollection
 *  org.apache.sling.resource.collection.ResourceCollectionManager
 *  org.apache.sling.servlets.post.Modification
 */
package com.adobe.cq.mobile.platform.impl.operations;

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.jackrabbit.util.Text;
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.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.resource.collection.ResourceCollection;
import org.apache.sling.resource.collection.ResourceCollectionManager;
import org.apache.sling.servlets.post.Modification;

@Component(metatype=0, label="AEM Mobile Screenshot Delete Handler")
@Service
@Properties(value={@Property(name="sling.post.operation", value={"mobileapps:screenshotDelete"})})
public class MobileAppScreenshotDeleteOperation
extends MobileAbstractOperation {
    public static final String OPERATION_NAME = "screenshotDelete";
    public static final String PARAM_PATH = "path";
    public static final String PARAM_STORE = "store";
    private static final String PARAM_DEVICE_TARGET = "deviceTarget";

    @Override
    protected void perform(SlingHttpServletRequest request, HtmlResponse response, List<Modification> changes) {
        I18n i18n = new I18n((HttpServletRequest)request);
        Resource appInstance = null;
        RequestParameter appInstanceParam = request.getRequestParameter("appInstance");
        if (appInstanceParam != null) {
            appInstance = request.getResourceResolver().getResource(appInstanceParam.toString());
        }
        if (appInstance == null) {
            String message = i18n.get("App instance not found");
            String title = i18n.get("Error");
            this.generateError(response, message, title);
            return;
        }
        String store = request.getParameter("store");
        if (StringUtils.isBlank((String)store)) {
            String message = i18n.get("Missing mandatory parameter {0}", null, new Object[]{"store"});
            String title = i18n.get("Error");
            this.generateError(response, message, title);
            return;
        }
        String deviceTarget = request.getParameter("deviceTarget");
        if (StringUtils.isBlank((String)deviceTarget)) {
            String message = i18n.get("Missing mandatory parameter {0}", null, new Object[]{"deviceTarget"});
            String title = i18n.get("Error");
            this.generateError(response, message, title);
            return;
        }
        String path = request.getParameter("path");
        if (StringUtils.isBlank((String)path)) {
            String message = i18n.get("Missing mandatory parameter {0}", null, new Object[]{"path"});
            String title = i18n.get("Error");
            this.generateError(response, message, title);
            return;
        }
        Resource assetResource = request.getResourceResolver().getResource(path);
        Resource content = appInstance.getChild("jcr:content");
        ResourceCollectionManager manager = (ResourceCollectionManager)request.getResourceResolver().adaptTo(ResourceCollectionManager.class);
        if (content != null) {
            String storePath = content.getPath() + "/screenshots/" + store + "/" + deviceTarget;
            Resource storeResource = request.getResourceResolver().getResource(storePath);
            ResourceCollection collection = manager.getCollection(storeResource);
            try {
                if (assetResource == null) {
                    String filename = Text.getName((String)path);
                    Resource members = storeResource.getChild("sling:members");
                    ModifiableValueMap map = (ModifiableValueMap)members.adaptTo(ModifiableValueMap.class);
                    String[] resources = (String[])map.get("sling:resources", String[].class);
                    ArrayList<String> values = new ArrayList<String>();
                    if (resources != null) {
                        for (int i = 0; i < resources.length; ++i) {
                            String resource = resources[i];
                            if (resource.equals(path)) continue;
                            values.add(resource);
                        }
                        String[] newProps = new String[values.size()];
                        newProps = values.toArray(newProps);
                        map.put((Object)"sling:resources", (Object)newProps);
                        Resource imageResource = members.getChild(filename);
                        if (imageResource != null) {
                            request.getResourceResolver().delete(imageResource);
                        }
                    }
                } else {
                    collection.remove(assetResource);
                }
            }
            catch (PersistenceException e) {
                String message = i18n.get("Unable to remove asset from the collection.");
                String title = i18n.get("Error");
                this.generateError(response, message, title);
            }
        }
    }
}