ResourceConverterAdapterFactory.java 3.95 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.rest.converter.ResourceConverter
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.adapter.AdapterFactory
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceWrapper
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.screens.impl.rest;

import com.adobe.cq.screens.impl.rest.DeviceResource;
import com.adobe.cq.screens.impl.rest.DeviceResourceConverter;
import com.adobe.cq.screens.impl.rest.DevicesResource;
import com.adobe.cq.screens.impl.rest.DevicesResourceConverter;
import com.adobe.cq.screens.impl.rest.FolderResource;
import com.adobe.cq.screens.impl.rest.FolderResourceConverter;
import com.adobe.cq.screens.impl.rest.RegistrationResource;
import com.adobe.cq.screens.impl.rest.RegistrationResourceConverter;
import com.adobe.cq.screens.impl.rest.RegistrationsResource;
import com.adobe.cq.screens.impl.rest.RegistrationsResourceConverter;
import com.adobe.cq.screens.impl.rest.ScreensResource;
import com.adobe.cq.screens.impl.rest.ScreensResourceConverter;
import com.adobe.granite.rest.converter.ResourceConverter;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.adapter.AdapterFactory;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
@Service
public class ResourceConverterAdapterFactory
implements AdapterFactory {
    private Logger log;
    @Property(name="adapters")
    public static final String[] ADAPTER_CLASSES = new String[]{ResourceConverter.class.getName()};
    @Property(name="adaptables")
    public static final String[] ADAPTABLE_CLASSES = new String[]{Resource.class.getName()};

    public ResourceConverterAdapterFactory() {
        this.log = LoggerFactory.getLogger(this.getClass());
    }

    public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType> type) {
        if (type == ResourceConverter.class) {
            return this.getResourceConverterAdapter(adaptable, type);
        }
        this.log.warn("Unable to handle adaptable {}", (Object)adaptable.getClass().getName());
        return null;
    }

    private <AdapterType> AdapterType getResourceConverterAdapter(Object adaptable, Class<AdapterType> type) {
        Resource resource = null;
        if (adaptable instanceof Resource) {
            resource = (Resource)adaptable;
            resource = this.unwrap(resource);
        }
        if (resource != null) {
            if (resource instanceof FolderResource) {
                return (AdapterType)((Object)new FolderResourceConverter(resource));
            }
            if (resource instanceof ScreensResource) {
                return (AdapterType)((Object)new ScreensResourceConverter(resource));
            }
            if (resource instanceof DevicesResource) {
                return (AdapterType)((Object)new DevicesResourceConverter(resource));
            }
            if (resource instanceof DeviceResource) {
                return (AdapterType)((Object)new DeviceResourceConverter(resource));
            }
            if (resource instanceof RegistrationResource) {
                return (AdapterType)((Object)new RegistrationResourceConverter(resource));
            }
            if (resource instanceof RegistrationsResource) {
                return (AdapterType)((Object)new RegistrationsResourceConverter(resource));
            }
        }
        return null;
    }

    private Resource unwrap(Resource resource) {
        Resource result = resource;
        while (result instanceof ResourceWrapper) {
            result = ((ResourceWrapper)result).getResource();
        }
        return result;
    }
}