CRXUrlConnection.java 3.15 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.resourceresolverhelper.ResourceResolverHelper
 *  javax.jcr.Node
 *  javax.jcr.Session
 *  javax.jcr.nodetype.NodeType
 *  org.apache.sling.api.resource.ResourceResolver
 */
package com.adobe.aemds.datamanager.impl;

import com.adobe.aemds.datamanager.impl.AbstractUrlConnection;
import com.adobe.aemds.datamanager.impl.CRXUrlStreamHandler;
import com.adobe.aemds.datamanager.impl.DamAssetUrlConnection;
import com.adobe.aemds.datamanager.impl.NodeBasedUrlConnection;
import com.adobe.aemds.datamanager.impl.NodeUrlConnection;
import com.adobe.aemds.datamanager.impl.ServiceContainer;
import com.adobe.granite.resourceresolverhelper.ResourceResolverHelper;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import javax.jcr.Node;
import javax.jcr.Session;
import javax.jcr.nodetype.NodeType;
import org.apache.sling.api.resource.ResourceResolver;

class CRXUrlConnection
extends AbstractUrlConnection {
    private static final Map<String, Class<? extends NodeBasedUrlConnection>> typeBasedConnections = new HashMap<String, Class<? extends NodeBasedUrlConnection>>();
    private NodeBasedUrlConnection delegate;

    CRXUrlConnection(URL url) {
        super(url);
    }

    protected Node getNode() throws Exception {
        String nodePath = this.getResourcePath(CRXUrlStreamHandler.CRX_PROTOCOL);
        ResourceResolver resolver = ServiceContainer.resourceResolverHelper.getResourceResolver();
        if (resolver == null) {
            throw new IllegalStateException("No JCR resource resolver bound to the current thread context!");
        }
        Session session = (Session)resolver.adaptTo(Session.class);
        return session.getNode(nodePath);
    }

    protected NodeBasedUrlConnection getDelegate(Node n) throws Exception {
        String nodeType = n.getPrimaryNodeType().getName();
        Class<? extends NodeBasedUrlConnection> clazz = typeBasedConnections.get(nodeType);
        if (clazz == null) {
            clazz = typeBasedConnections.get(null);
        }
        if (clazz == null) {
            throw new IllegalArgumentException("No handler registered for node of type " + nodeType + " referenced by URL " + this.url);
        }
        Constructor<? extends NodeBasedUrlConnection> constr = clazz.getDeclaredConstructor(URL.class, Node.class);
        return constr.newInstance(new Object[]{this.url, n});
    }

    protected void doConnect() throws Exception {
        Node n = this.getNode();
        this.delegate = this.getDelegate(n);
        this.delegate.doConnect();
    }

    protected String doGetContentType() throws Exception {
        return this.delegate.doGetContentType();
    }

    protected InputStream doGetInputStream() throws Exception {
        return this.delegate.doGetInputStream();
    }

    protected long doGetContentLength() throws Exception {
        return this.delegate.doGetContentLength();
    }

    static {
        typeBasedConnections.put("dam:Asset", DamAssetUrlConnection.class);
        typeBasedConnections.put(null, NodeUrlConnection.class);
    }
}