TemplatedContainerAdapterFactory.java 3.28 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.wcm.api.TemplateManager
 *  com.day.cq.wcm.api.components.ComponentContext
 *  com.day.cq.wcm.commons.WCMUtils
 *  javax.servlet.ServletRequest
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Properties
 *  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.ResourceResolver
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.wcm.foundation.impl.templated;

import com.day.cq.wcm.api.TemplateManager;
import com.day.cq.wcm.api.components.ComponentContext;
import com.day.cq.wcm.commons.WCMUtils;
import com.day.cq.wcm.foundation.TemplatedContainer;
import javax.servlet.ServletRequest;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
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.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
@Service(value={AdapterFactory.class})
@Properties(value={@Property(name="service.description", value={"WCM Templated Container Adapter Factory"}), @Property(name="adapter.condition", value={"Adapts Request to TemplatedContainer"})})
public class TemplatedContainerAdapterFactory
implements AdapterFactory {
    private static final Logger log = LoggerFactory.getLogger(TemplatedContainerAdapterFactory.class);
    private static final Class<SlingHttpServletRequest> REQUEST_CLASS = SlingHttpServletRequest.class;
    private static final Class<TemplatedContainer> TEMPLATED_CONTAINER_CLASS = TemplatedContainer.class;
    @Property(name="adapters")
    protected static final String[] ADAPTER_CLASSES = new String[]{TEMPLATED_CONTAINER_CLASS.getName()};
    @Property(name="adaptables")
    protected static final String[] ADAPTABLE_CLASSES = new String[]{REQUEST_CLASS.getName()};

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

    private <AdapterType> AdapterType getAdapter(SlingHttpServletRequest request, Class<AdapterType> type) {
        if (request != null) {
            ResourceResolver resourceResolver = request.getResourceResolver();
            TemplateManager templateManager = (TemplateManager)resourceResolver.adaptTo(TemplateManager.class);
            ComponentContext componentContext = WCMUtils.getComponentContext((ServletRequest)request);
            if (type == TEMPLATED_CONTAINER_CLASS && componentContext != null && templateManager != null) {
                return (AdapterType)((Object)new TemplatedContainer(templateManager, componentContext));
            }
        }
        log.warn("Unable to adapt resource to type {}", (Object)type.getName());
        return null;
    }
}