ResourceManagerAdapterFactory.java 2.81 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.rest.ResourceManager
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.adapter.AdapterFactory
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceWrapper
 *  org.apache.sling.api.resource.SyntheticResource
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.granite.rest.assets.impl;

import com.adobe.granite.rest.ResourceManager;
import com.adobe.granite.rest.assets.impl.AbstractAssetResource;
import com.adobe.granite.rest.assets.impl.ResourceManagerImpl;
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.SlingHttpServletRequest;
import org.apache.sling.api.adapter.AdapterFactory;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceWrapper;
import org.apache.sling.api.resource.SyntheticResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

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

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

    private <AdapterType> AdapterType getResourceManagerAdapter(Object adaptable, Class<AdapterType> type) {
        Resource resource = null;
        if (adaptable instanceof SlingHttpServletRequest) {
            resource = ((SlingHttpServletRequest)adaptable).getResource();
            resource = this.unwrap(resource);
        }
        if (resource != null && (resource instanceof SyntheticResource || resource instanceof AbstractAssetResource)) {
            return (AdapterType)new ResourceManagerImpl();
        }
        return null;
    }

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