ScreensResourceProvider.java 6.75 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.rest.ApiResourceProvider
 *  com.day.text.Text
 *  javax.jcr.RepositoryException
 *  org.apache.commons.lang.StringUtils
 *  org.apache.sling.api.resource.PersistenceException
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.screens.impl.rest;

import com.adobe.cq.screens.device.Device;
import com.adobe.cq.screens.device.DeviceManager;
import com.adobe.cq.screens.device.impl.DeviceService;
import com.adobe.cq.screens.device.registration.PendingDevice;
import com.adobe.cq.screens.device.registration.RegistrationService;
import com.adobe.cq.screens.impl.rest.DeviceResource;
import com.adobe.cq.screens.impl.rest.DevicesResource;
import com.adobe.cq.screens.impl.rest.RegistrationResource;
import com.adobe.cq.screens.impl.rest.RegistrationsResource;
import com.adobe.cq.screens.impl.rest.ScreensResource;
import com.adobe.cq.screens.impl.rest.ScreensResourceFactory;
import com.adobe.granite.rest.ApiResourceProvider;
import com.day.text.Text;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import javax.jcr.RepositoryException;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ScreensResourceProvider
implements ApiResourceProvider {
    private static final Logger log = LoggerFactory.getLogger(ScreensResourceProvider.class);
    public static final String TYPE = "screens";
    public static final String SCREENS_ROOT = "/content/screens";
    private ScreensResourceFactory resourceFactory = new ScreensResourceFactory();
    private String apiContextPath;
    private final DeviceService deviceService;
    private final RegistrationService registrationService;

    public ScreensResourceProvider(String contextPath, DeviceService deviceService, RegistrationService registrationService) {
        this.apiContextPath = contextPath + "/" + "screens";
        this.deviceService = deviceService;
        this.registrationService = registrationService;
    }

    public Resource getResource(ResourceResolver resolver, String path) {
        Object device;
        String id;
        String token;
        log.debug("screens.getResource({})", (Object)path);
        Resource collectionRoot = resolver.getResource("/content/screens");
        if (collectionRoot == null) {
            log.error("Collection root {} not found.", (Object)"/content/screens");
            return null;
        }
        if (StringUtils.isEmpty((String)path)) {
            return new ScreensResource(resolver, this.apiContextPath);
        }
        if ("/devices".equals(path)) {
            return new DevicesResource(resolver, this.apiContextPath + path, this.deviceService);
        }
        if ("/devices".equals(Text.getRelativeParent((String)path, (int)1)) && (device = this.deviceService.getDevice(resolver, id = Text.getName((String)path))) != null) {
            return new DeviceResource(resolver, this.apiContextPath + path, (Device)device);
        }
        if ("/registration".equals(path)) {
            return new RegistrationsResource(resolver, this.apiContextPath + path, this.registrationService);
        }
        if ("/registration".equals(Text.getRelativeParent((String)path, (int)1)) && (device = this.registrationService.getDevice(token = Text.getName((String)path))) != null) {
            return new RegistrationResource(resolver, this.apiContextPath + path, (PendingDevice)device);
        }
        return null;
    }

    public Iterator<Resource> listChildren(Resource parent) {
        log.debug("screens.listChildren({})", (Object)parent.getPath());
        String parentPath = parent.getPath();
        if (!StringUtils.isEmpty((String)parentPath)) {
            ResourceResolver resolver = parent.getResourceResolver();
            Resource collectionRoot = resolver.getResource("/content/screens");
            if (collectionRoot == null) {
                log.error("Collection root {} not found.", (Object)"/content/screens");
                return null;
            }
            Resource parentResource = resolver.getResource(this.unmap(parentPath));
            if (parentResource == null) {
                return null;
            }
            LinkedList<Resource> children = new LinkedList<Resource>();
            Iterator it = parentResource.listChildren();
            while (it.hasNext()) {
                Resource child = (Resource)it.next();
                Resource childRes = this.resourceFactory.createResource(child, this.map(child.getPath()));
                if (childRes == null) continue;
                children.add(childRes);
            }
            return children.iterator();
        }
        return null;
    }

    public Resource create(ResourceResolver resolver, String path, Map<String, Object> properties) throws PersistenceException {
        log.debug("screens.create({})", (Object)path);
        throw new PersistenceException("Unable to create resource at " + path);
    }

    public void delete(ResourceResolver resolver, String path) throws PersistenceException {
        log.debug("screens.delete({})", (Object)path);
        Resource resource = this.getResource(resolver, path);
        if (resource == null) {
            throw new PersistenceException("Unable to delete resource at " + path);
        }
        Device device = (Device)resource.adaptTo(Device.class);
        if (device != null) {
            DeviceManager deviceManager = (DeviceManager)resolver.adaptTo(DeviceManager.class);
            try {
                deviceManager.deleteDevice(device.getId());
            }
            catch (RepositoryException e) {
                throw new PersistenceException("Unable to delete device " + device.getId());
            }
            return;
        }
        throw new PersistenceException("Unable to delete resource at " + path);
    }

    String map(String resourcePath) {
        String path = resourcePath.replace("/content/screens", this.apiContextPath);
        if (path != null && path.matches(".*?/jcr:content/(renditions|comments|folderThumbnail).*")) {
            path = path.replaceFirst("jcr:content/(renditions|comments|folderThumbnail)", "$1");
        }
        return path;
    }

    String unmap(String apiPath) {
        String path = apiPath.replace(this.apiContextPath, "/content/screens");
        if (path != null && path.matches(".*?/(renditions|comments|folderThumbnail).*")) {
            return path.replaceFirst("(renditions|comments|folderThumbnail)", "jcr:content/$1");
        }
        return path;
    }
}