MobileAppScreenshotServlet.java 9.54 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.xss.JSONUtil
 *  com.adobe.granite.xss.XSSFilter
 *  javax.jcr.RepositoryException
 *  javax.servlet.Servlet
 *  javax.servlet.ServletException
 *  org.apache.commons.collections.CollectionUtils
 *  org.apache.commons.lang3.StringUtils
 *  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.ReferencePolicy
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.SlingHttpServletResponse
 *  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.io.JSONWriter
 *  org.apache.sling.resource.collection.ResourceCollection
 *  org.apache.sling.resource.collection.ResourceCollectionManager
 *  org.osgi.framework.BundleContext
 *  org.osgi.framework.InvalidSyntaxException
 *  org.osgi.framework.ServiceReference
 *  org.osgi.service.component.ComponentContext
 */
package com.adobe.cq.mobile.platform.impl.servlets;

import com.adobe.cq.mobile.platform.MobileResource;
import com.adobe.cq.mobile.platform.MobileResourceType;
import com.adobe.cq.mobile.platform.impl.store.MobileDevice;
import com.adobe.cq.mobile.platform.impl.store.MobilePlatformProvider;
import com.adobe.granite.xss.JSONUtil;
import com.adobe.granite.xss.XSSFilter;
import java.awt.Dimension;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import javax.jcr.RepositoryException;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.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.Reference;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
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.io.JSONWriter;
import org.apache.sling.resource.collection.ResourceCollection;
import org.apache.sling.resource.collection.ResourceCollectionManager;
import org.osgi.framework.BundleContext;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.osgi.service.component.ComponentContext;

@Component(immediate=1, metatype=1, label="Lists the associated screenshots for the mobile application.")
@Service(value={Servlet.class})
@Properties(value={@Property(name="sling.servlet.resourceTypes", value={"sling/servlet/default"}), @Property(name="sling.servlet.methods", value={"GET"}), @Property(name="sling.servlet.selectors", value={"mobileapps.screenshots"}), @Property(name="sling.servlet.extensions", value={"json"})})
public class MobileAppScreenshotServlet
extends SlingAllMethodsServlet {
    @Reference(policy=ReferencePolicy.STATIC)
    private XSSFilter xss;
    public static final String PARAM_DEVICE_TARGET = "deviceTarget";
    public static final String PARAM_STORE = "store";
    public static final String PARAM_WIDTH = "width";
    public static final String PARAM_HEIGHT = "height";
    private ComponentContext context;

    protected void activate(ComponentContext context) throws RepositoryException {
        this.context = context;
    }

    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");
        Resource resource = request.getResource();
        MobileResource mobileResource = (MobileResource)resource.adaptTo(MobileResource.class);
        if (mobileResource != null && !mobileResource.isA(MobileResourceType.INSTANCE.getType())) {
            throw new IllegalArgumentException("Resource must be a Mobile Resource of type " + MobileResourceType.INSTANCE.getType());
        }
        String store = request.getParameter("store");
        String deviceTarget = request.getParameter("deviceTarget");
        String deviceWidth = request.getParameter("width");
        String deviceHeight = request.getParameter("height");
        Dimension dimension = null;
        if (StringUtils.isNotBlank((CharSequence)deviceWidth) && StringUtils.isNotBlank((CharSequence)deviceHeight)) {
            dimension = new Dimension(Integer.parseInt(deviceWidth), Integer.parseInt(deviceHeight));
        }
        List<Resource> screenCollection = this.getScreenshotCollections(resource, store, deviceTarget, dimension);
        PrintWriter out = response.getWriter();
        JSONWriter writer = new JSONWriter((Writer)out);
        try {
            writer.object();
            writer.key("screenshots").array();
            for (Resource screenEntry : screenCollection) {
                if (screenEntry == null) continue;
                MobileAppScreenshotServlet.writeScreenshotCollection(screenEntry, this.xss, writer);
            }
            writer.endArray();
            writer.endObject();
        }
        catch (JSONException e) {
            throw new ServletException((Throwable)e);
        }
    }

    private List<Resource> getScreenshotCollections(Resource resource, String store, String target, Dimension dimension) {
        boolean onlyOne;
        ArrayList<Resource> storeCollection = new ArrayList<Resource>();
        boolean bl = onlyOne = StringUtils.isNotBlank((CharSequence)store) && StringUtils.isNotBlank((CharSequence)target);
        if (onlyOne) {
            String screenshotsPath = "jcr:content/screenshots/" + store + "/" + target;
            storeCollection.add(resource.getChild(screenshotsPath));
        } else {
            Resource screenshots = resource.getChild("jcr:content/screenshots");
            if (screenshots != null) {
                for (Resource storeRes : screenshots.getChildren()) {
                    if (StringUtils.isBlank((CharSequence)store)) {
                        CollectionUtils.addAll(storeCollection, (Iterator)storeRes.listChildren());
                        continue;
                    }
                    if (!store.equals(storeRes.getName())) continue;
                    CollectionUtils.addAll(storeCollection, this.filterDevicesByDimension(storeRes, dimension));
                }
            }
        }
        return storeCollection;
    }

    private Iterator<Resource> filterDevicesByDimension(Resource storeRes, Dimension dimension) {
        ArrayList<Resource> deviceList = new ArrayList<Resource>();
        String filter = "(cq.mobile.apps.platformprovider=" + storeRes.getName() + ")";
        try {
            ServiceReference[] refs = this.context.getBundleContext().getServiceReferences(MobilePlatformProvider.class.getName(), filter);
            MobilePlatformProvider provider = (MobilePlatformProvider)this.context.getBundleContext().getService(refs[0]);
            Collection<MobileDevice> supportedDevices = provider.getSupportedDevices();
            for (MobileDevice device : supportedDevices) {
                if (dimension != null && !device.isDimensionSupported(dimension)) continue;
                deviceList.add(storeRes.getChild(device.getId()));
            }
        }
        catch (InvalidSyntaxException e) {
            e.printStackTrace();
        }
        return deviceList.iterator();
    }

    private static void writeScreenshotCollection(Resource resource, XSSFilter xss, JSONWriter writer) throws JSONException {
        ResourceResolver resolver = resource.getResourceResolver();
        ResourceCollectionManager manager = (ResourceCollectionManager)resolver.adaptTo(ResourceCollectionManager.class);
        ResourceCollection collection = manager.getCollection(resource);
        if (collection == null) {
            return;
        }
        Resource slingMembers = resource.getChild("sling:members");
        Iterable children = slingMembers.getChildren();
        for (Resource screenshot : children) {
            ValueMap map = (ValueMap)screenshot.adaptTo(ValueMap.class);
            writer.object();
            JSONUtil.writeProtected((JSONWriter)writer, (String)"path", (String)((String)map.get("sling:resource", String.class)), (XSSFilter)xss);
            JSONUtil.writeProtected((JSONWriter)writer, (String)"orientation", (String)((String)map.get("orientation", String.class)), (XSSFilter)xss);
            JSONUtil.writeProtected((JSONWriter)writer, (String)"deviceTarget", (String)((String)map.get("deviceTarget", String.class)), (XSSFilter)xss);
            JSONUtil.writeProtected((JSONWriter)writer, (String)"store", (String)resource.getParent().getName(), (XSSFilter)xss);
            writer.endObject();
        }
    }

    protected void bindXss(XSSFilter xSSFilter) {
        this.xss = xSSFilter;
    }

    protected void unbindXss(XSSFilter xSSFilter) {
        if (this.xss == xSSFilter) {
            this.xss = null;
        }
    }
}