CRXUrlConnection.java
3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
* 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);
}
}