ResourceManagerImpl.java
8.55 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.rest.RequestException
* com.adobe.granite.rest.ResourceManager
* com.adobe.granite.rest.RestException
* com.adobe.granite.rest.utils.Resources
* javax.jcr.ItemExistsException
* javax.jcr.Node
* javax.jcr.PathNotFoundException
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.Workspace
* javax.jcr.lock.LockException
* javax.jcr.nodetype.ConstraintViolationException
* javax.jcr.query.Query
* javax.jcr.query.QueryManager
* javax.jcr.query.QueryResult
* javax.jcr.query.Row
* javax.jcr.query.RowIterator
* javax.jcr.version.VersionException
* org.apache.sling.api.resource.PersistenceException
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceUtil
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.granite.rest.assets.impl;
import com.adobe.granite.rest.RequestException;
import com.adobe.granite.rest.ResourceManager;
import com.adobe.granite.rest.RestException;
import com.adobe.granite.rest.assets.impl.AbstractAssetResource;
import com.adobe.granite.rest.assets.impl.AssetQueryTransformer;
import com.adobe.granite.rest.assets.impl.AssetResource;
import com.adobe.granite.rest.utils.Resources;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import javax.jcr.ItemExistsException;
import javax.jcr.Node;
import javax.jcr.PathNotFoundException;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Workspace;
import javax.jcr.lock.LockException;
import javax.jcr.nodetype.ConstraintViolationException;
import javax.jcr.query.Query;
import javax.jcr.query.QueryManager;
import javax.jcr.query.QueryResult;
import javax.jcr.query.Row;
import javax.jcr.query.RowIterator;
import javax.jcr.version.VersionException;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ResourceManagerImpl
implements ResourceManager {
private Logger logger;
private AssetQueryTransformer queryTransformer;
private static final String CONTENT_DAM = "/content/dam/";
private static final String API_PREFIX = "/api/assets/";
public ResourceManagerImpl() {
this.logger = LoggerFactory.getLogger(this.getClass());
this.queryTransformer = new AssetQueryTransformer();
}
public boolean copy(Resource resource, String destUri, int depth) throws UnsupportedOperationException, RequestException, RestException {
try {
String name;
this.logger.debug("copying resource {} to {}", (Object)resource.getPath(), (Object)destUri);
ResourceResolver resolver = resource.getResourceResolver();
boolean created = true;
boolean trailingSlash = destUri.endsWith("/");
String dstParentPath = trailingSlash ? destUri : ResourceUtil.getParent((String)destUri);
String string = name = trailingSlash ? null : ResourceUtil.getName((String)destUri);
if (!destUri.contains("assets")) {
throw new RequestException(403, "Destination not allowed");
}
Resource src = resolver.getResource(((AbstractAssetResource)resource).getWrappedResourcePath());
if (src == null) {
throw new RequestException(412, "Source is not accessible or does not exist.");
}
Resource dstWrappedParent = resolver.getResource(dstParentPath);
if (dstWrappedParent == null) {
throw new RequestException(412, "Parent of destination does not exist.");
}
Resource dstParentRes = resolver.getResource(((AbstractAssetResource)dstWrappedParent).getWrappedResourcePath());
if (dstParentRes == null) {
throw new RequestException(412, "Parent of destination does not exist.");
}
Resource dst = resolver.getResource(dstParentRes.getPath() + "/" + name);
if (dst != null) {
created = false;
}
HashSet<String> ignoreProperties = new HashSet<String>();
ignoreProperties.add("jcr:uuid");
ignoreProperties.add("jcr:baseVersion");
ignoreProperties.add("jcr:isCheckedOut");
ignoreProperties.add("jcr:predecessors");
ignoreProperties.add("jcr:versionHistory");
Resources.copy((Resource)src, (Resource)dstParentRes, (String)name, (int)depth, ignoreProperties);
return created;
}
catch (PersistenceException e) {
throw new RestException("Unable to copy resource " + resource.getPath() + ": " + e.getMessage(), (Throwable)e);
}
}
public boolean move(Resource resource, String destUri) throws UnsupportedOperationException, RequestException, RestException {
try {
ResourceResolver resolver = resource.getResourceResolver();
Session session = (Session)resolver.adaptTo(Session.class);
String srcPath = this.getNodePath(resource.getPath());
String destPath = this.getNodePath(destUri);
session.move(srcPath, destPath);
return true;
}
catch (ItemExistsException e) {
throw new RestException("Unable to move resource " + resource.getPath() + ": " + e.getMessage(), (Throwable)e);
}
catch (PathNotFoundException e) {
throw new RestException("Unable to move resource " + resource.getPath() + ": " + e.getMessage(), (Throwable)e);
}
catch (VersionException e) {
throw new RestException("Unable to move resource " + resource.getPath() + ": " + e.getMessage(), (Throwable)e);
}
catch (ConstraintViolationException e) {
throw new RestException("Unable to move resource " + resource.getPath() + ": " + e.getMessage(), (Throwable)e);
}
catch (LockException e) {
throw new RestException("Unable to move resource " + resource.getPath() + ": " + e.getMessage(), (Throwable)e);
}
catch (RepositoryException e) {
throw new RestException("Unable to move resource " + resource.getPath() + ": " + e.getMessage(), (Throwable)e);
}
}
public List<Resource> find(Resource resource, String query) throws UnsupportedOperationException, RequestException, RestException {
this.logger.debug("finding resources for query={}", (Object)query);
if (!(resource instanceof AbstractAssetResource)) {
throw new IllegalArgumentException("Resource is not an AssetResource.");
}
ResourceResolver resolver = resource.getResourceResolver();
LinkedList<Resource> results = new LinkedList<Resource>();
String transformedQuery = this.queryTransformer.transform(resource, query);
this.logger.debug("Transformed query {} to {}", (Object)query, (Object)transformedQuery);
if (transformedQuery != null) {
String apiContextPath = resource.getPath().replaceFirst("^(/.*?/.*?)(/.*)", "$1");
try {
Session session = (Session)resolver.adaptTo(Session.class);
QueryManager queryMgr = session.getWorkspace().getQueryManager();
Query jcrQuery = queryMgr.createQuery(transformedQuery, "JCR-SQL2");
QueryResult queryResults = jcrQuery.execute();
String selector = this.queryTransformer.getSelectorName(resource, query);
RowIterator it = queryResults.getRows();
while (it.hasNext()) {
Row row = it.nextRow();
Node node = row.getNode(selector);
String path = node.getPath().replaceFirst("/content/dam", apiContextPath);
Resource result = resolver.getResource(path);
if (result == null || !(result instanceof AssetResource)) continue;
results.add(result);
}
}
catch (RepositoryException e) {
throw new RestException("Unable to find resources: " + e.getMessage(), (Throwable)e);
}
}
return results;
}
protected String getNodePath(String resourcePath) {
if (resourcePath.startsWith("/api/assets/")) {
return String.format("%s%s", "/content/dam/", resourcePath.substring("/api/assets/".length()));
}
return resourcePath;
}
}