ResourceConverterAdapterFactory.java 3.97 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.granite.rest.assets.impl;

import com.adobe.granite.rest.assets.impl.AssetResource;
import com.adobe.granite.rest.assets.impl.AssetResourceConverter;
import com.adobe.granite.rest.assets.impl.CommentResource;
import com.adobe.granite.rest.assets.impl.CommentResourceConverter;
import com.adobe.granite.rest.assets.impl.CommentsResource;
import com.adobe.granite.rest.assets.impl.CommentsResourceConverter;
import com.adobe.granite.rest.assets.impl.FolderResource;
import com.adobe.granite.rest.assets.impl.FolderResourceConverter;
import com.adobe.granite.rest.assets.impl.RenditionResource;
import com.adobe.granite.rest.assets.impl.RenditionResourceConverter;
import com.adobe.granite.rest.assets.impl.RenditionsResource;
import com.adobe.granite.rest.assets.impl.RenditionsResourceConverter;
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 AssetResource) {
                return (AdapterType)((Object)new AssetResourceConverter(resource));
            }
            if (resource instanceof FolderResource) {
                return (AdapterType)((Object)new FolderResourceConverter(resource));
            }
            if (resource instanceof RenditionsResource) {
                return (AdapterType)((Object)new RenditionsResourceConverter(resource));
            }
            if (resource instanceof RenditionResource) {
                return (AdapterType)((Object)new RenditionResourceConverter(resource));
            }
            if (resource instanceof CommentsResource) {
                return (AdapterType)((Object)new CommentsResourceConverter(resource));
            }
            if (resource instanceof CommentResource) {
                return (AdapterType)((Object)new CommentResourceConverter(resource));
            }
        }
        return null;
    }

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