AssetManagerImpl.java
4.3 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.asset.api.Asset
* com.adobe.granite.asset.api.AssetException
* com.adobe.granite.asset.api.AssetManager
* javax.jcr.Node
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.commons.lang.StringUtils
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.jcr.resource.JcrResourceUtil
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.granite.asset.core.impl;
import com.adobe.granite.asset.api.Asset;
import com.adobe.granite.asset.api.AssetException;
import com.adobe.granite.asset.api.AssetManager;
import com.adobe.granite.asset.core.impl.AssetUtils;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.jcr.resource.JcrResourceUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class AssetManagerImpl
implements AssetManager {
private static final Logger log = LoggerFactory.getLogger(AssetManager.class);
private final ResourceResolver resolver;
private final Session session;
public AssetManagerImpl(ResourceResolver resolver) {
this.resolver = resolver;
this.session = (Session)resolver.adaptTo(Session.class);
}
public Asset createAsset(String path) {
if (this.assetExists(path)) {
log.warn("Asset already existing at the given path {}. Aborting", (Object)path);
throw new AssetException("Asset at path " + path + " exists.");
}
try {
Node assetNode = JcrResourceUtil.createPath((String)path, (String)"sling:OrderedFolder", (String)"dam:Asset", (Session)this.session, (boolean)false);
Node content = assetNode.addNode("jcr:content", "dam:AssetContent");
content.addNode("metadata", "nt:unstructured");
content.addNode("renditions", "nt:folder");
content.addNode("related", "nt:unstructured");
}
catch (RepositoryException e) {
throw new AssetException("Failed to create Asset at path [ " + path + " ]", (Throwable)e);
}
return (Asset)this.resolver.getResource(path).adaptTo(Asset.class);
}
public Asset getAsset(String assetPath) {
Resource resource = this.resolver.getResource(assetPath);
if (resource != null) {
return (Asset)resource.adaptTo(Asset.class);
}
return null;
}
public Asset getAssetByIdentifier(String id) {
try {
Node node = this.session.getNodeByIdentifier(id);
Resource resource = this.resolver.getResource(node.getPath());
if (resource != null) {
return (Asset)resource.adaptTo(Asset.class);
}
}
catch (RepositoryException e) {
throw new AssetException("Failed to get Asset with ID [ " + id + " ]", (Throwable)e);
}
return null;
}
public boolean assetExists(String assetPath) {
return this.getAsset(assetPath) != null;
}
public void removeAsset(String assetPath) {
try {
this.session.removeItem(assetPath);
}
catch (RepositoryException e) {
throw new AssetException("Failed to remove Asset [ " + assetPath + " ]", (Throwable)e);
}
}
public void copyAsset(String assetPath, String destAbsPath) {
try {
String name = StringUtils.substringAfterLast((String)destAbsPath, (String)"/");
String destAbsParent = StringUtils.substringBeforeLast((String)destAbsPath, (String)"/");
Node nodeToCopy = this.session.getNode(assetPath);
Node destinationParent = this.session.getNode(destAbsParent);
AssetUtils.copy(nodeToCopy, destinationParent, name);
}
catch (RepositoryException e) {
throw new AssetException((Throwable)e);
}
}
public void moveAsset(String assetPath, String destAbsPath) {
try {
this.session.move(assetPath, destAbsPath);
}
catch (RepositoryException e) {
throw new AssetException((Throwable)e);
}
}
}