ResourceAdapterFactory.java 2.96 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  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.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.granite.rest.impl;

import com.adobe.granite.rest.converter.ResourceConverter;
import com.adobe.granite.rest.impl.ApiEndpointResource;
import com.adobe.granite.rest.impl.ApiEndpointResourceConverter;
import com.adobe.granite.rest.impl.ApiResourceWrapper;
import com.adobe.granite.rest.impl.SearchResultsResource;
import com.adobe.granite.rest.impl.SearchResultsResourceConverter;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
@Service
public class ResourceAdapterFactory
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[]{SlingHttpServletRequest.class.getName(), Resource.class.getName()};

    public ResourceAdapterFactory() {
        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 SlingHttpServletRequest) {
            resource = ((SlingHttpServletRequest)adaptable).getResource();
            if (resource instanceof ApiResourceWrapper) {
                resource = ((ApiResourceWrapper)resource).getResource();
            }
        } else if (adaptable instanceof Resource) {
            resource = (Resource)adaptable;
        }
        if (resource != null) {
            if (resource instanceof ApiEndpointResource) {
                return (AdapterType)new ApiEndpointResourceConverter(resource);
            }
            if (resource instanceof SearchResultsResource) {
                return (AdapterType)new SearchResultsResourceConverter(resource);
            }
        }
        this.log.warn("Unable to handle adaptable {}", (Object)adaptable.getClass().getName());
        return null;
    }
}