AbstractCommentingProvider.java
13.2 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* aQute.bnd.annotation.ConsumerType
* javax.jcr.Binary
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.ValueFactory
* org.apache.commons.collections.Predicate
* org.apache.commons.collections.iterators.FilterIterator
* org.apache.commons.lang.StringUtils
* org.apache.jackrabbit.commons.JcrUtils
* org.apache.jackrabbit.util.Text
* 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.comments;
import aQute.bnd.annotation.ConsumerType;
import com.adobe.granite.comments.CommentException;
import com.adobe.granite.comments.CommentingProvider;
import com.adobe.granite.comments.internal.Util;
import java.io.InputStream;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.ValueFactory;
import org.apache.commons.collections.Predicate;
import org.apache.commons.collections.iterators.FilterIterator;
import org.apache.commons.lang.StringUtils;
import org.apache.jackrabbit.commons.JcrUtils;
import org.apache.jackrabbit.util.Text;
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;
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
@ConsumerType
public abstract class AbstractCommentingProvider
implements CommentingProvider {
private final Logger log = LoggerFactory.getLogger(AbstractCommentingProvider.class);
private final Predicate COMMENT_RESOURCE_TYPE_PREDICATE;
public static final String RELATIVE_TARGET_ROOT = "comments";
public static final String PN_ANNOTATIONDATA = "annotationData";
public static final String PN_AUTHOR = "author";
public static final String PN_MESSAGE = "jcr:description";
public static final String JCR_CREATED_BY = "jcr:createdBy";
public static final String SLING_RESOURCE_TYPE = "sling:resourceType";
public AbstractCommentingProvider() {
this.COMMENT_RESOURCE_TYPE_PREDICATE = new Predicate(){
public boolean evaluate(Object o) {
Resource resource = (Resource)o;
return ResourceUtil.isA((Resource)resource, (String)AbstractCommentingProvider.this.getCommentResourceType());
}
};
}
public final Resource createCollectionResource(Resource target) {
String rootPath = this.getCollectionResourcePath(target);
Session session = (Session)target.getResourceResolver().adaptTo(Session.class);
try {
if (session.itemExists(rootPath)) {
throw new CommentException("Collection already exists: " + rootPath);
}
Node collectionRoot = this.createCollectionNode(rootPath, session);
if (StringUtils.isNotBlank((String)this.getCollectionResourceType())) {
collectionRoot.setProperty("sling:resourceType", this.getCollectionResourceType());
}
collectionRoot.addMixin("{http://www.jcp.org/jcr/mix/1.0}lastModified");
if (!collectionRoot.hasProperty("jcr:created")) {
collectionRoot.setProperty("jcr:created", Calendar.getInstance());
}
if (!collectionRoot.hasProperty("jcr:createdBy")) {
collectionRoot.setProperty("jcr:createdBy", session.getUserID());
}
this.customizeCollectionNode(target, collectionRoot);
session.save();
return target.getResourceResolver().getResource(collectionRoot.getPath());
}
catch (RepositoryException e) {
this.log.error("error while creating collection root for target [{}]: ", (Object)target.getPath(), (Object)e);
throw new CommentException("Could not create collection root for target: " + target.getPath(), (Throwable)e);
}
}
public final Resource createCommentResource(Resource collectionResource, String message, String author, String annotationData) {
try {
ResourceResolver resolver = collectionResource.getResourceResolver();
Session session = (Session)resolver.adaptTo(Session.class);
Node collectionNode = session.getNode(collectionResource.getPath());
String commentPath = this.getCommentResourcePath(collectionResource, message);
Node commentNode = this.createCommentNode(commentPath, collectionNode, session);
if (StringUtils.isNotBlank((String)this.getCommentResourceType())) {
commentNode.setProperty("sling:resourceType", this.getCommentResourceType());
}
commentNode.setProperty("jcr:description", message);
this.customizeCommentNode(collectionResource, commentNode);
Calendar now = Calendar.getInstance();
commentNode.addMixin("{http://www.jcp.org/jcr/mix/1.0}lastModified");
if (!commentNode.hasProperty("jcr:created")) {
commentNode.setProperty("jcr:created", now);
}
if (!commentNode.hasProperty("jcr:createdBy")) {
commentNode.setProperty("jcr:createdBy", session.getUserID());
}
if (StringUtils.isNotBlank((String)author)) {
commentNode.setProperty("author", author);
}
if (StringUtils.isNotBlank((String)annotationData)) {
commentNode.setProperty("annotationData", annotationData);
}
collectionNode.setProperty("jcr:lastModified", now);
session.save();
return resolver.getResource(commentNode.getPath());
}
catch (RepositoryException e) {
this.log.error("error while creating comment for collection [{}]: ", (Object)collectionResource.getPath(), (Object)e);
throw new CommentException("Could not x for collection: " + collectionResource.getPath(), (Throwable)e);
}
}
public Iterator<Resource> getCommentResources(Resource collectionResource) {
Iterator children = collectionResource.listChildren();
return new FilterIterator(children, this.COMMENT_RESOURCE_TYPE_PREDICATE);
}
public void removeCommentResource(Resource resource) {
try {
Session session = (Session)resource.getResourceResolver().adaptTo(Session.class);
Node node = session.getNode(resource.getPath());
node.remove();
session.save();
}
catch (RepositoryException e) {
this.log.error("error while removing comment [{}]: ", (Object)resource.getPath(), (Object)e);
throw new CommentException("Could not remove comment: " + resource.getPath(), (Throwable)e);
}
}
public void removeCollectionResource(Resource resource) {
try {
ResourceResolver resolver = resource.getResourceResolver();
if (null != resolver.getResource(resource.getPath())) {
Session session = (Session)resolver.adaptTo(Session.class);
session.removeItem(resource.getPath());
session.save();
}
}
catch (RepositoryException e) {
this.log.error("error while removing collection [{}]: ", (Object)resource.getPath(), (Object)e);
throw new CommentException("Could not remove collection: " + resource.getPath(), (Throwable)e);
}
}
public final Resource createAttachmentResource(Resource commentResource, String name, InputStream inputStream, String mimeType) {
try {
ResourceResolver resolver = commentResource.getResourceResolver();
Session session = (Session)resolver.adaptTo(Session.class);
Calendar time = Calendar.getInstance();
Node node = session.getNode(commentResource.getPath());
Node attachment = JcrUtils.getOrCreateUniqueByPath((Node)node, (String)this.getAttachmentResourcePath(name), (String)"nt:file");
Node content = attachment.addNode("jcr:content", "nt:unstructured");
content.setProperty("jcr:created", time);
content.setProperty("jcr:createdBy", node.getSession().getUserID());
content.setProperty("jcr:lastModified", time);
content.setProperty("jcr:mimeType", mimeType);
content.setProperty("jcr:data", node.getSession().getValueFactory().createBinary(inputStream));
this.customizeAttachmentNode(commentResource, content);
session.save();
return resolver.getResource(attachment.getPath());
}
catch (RepositoryException e) {
this.log.error("error while creating attachment for comment [{}]: ", (Object)commentResource.getPath(), (Object)e);
throw new CommentException("Could not create attachment for comment: " + commentResource.getPath(), (Throwable)e);
}
}
public Resource getAttachmentResource(Resource commentResource, String name) {
Resource child = commentResource.getChild(this.getAttachmentResourcePath(name));
return ResourceUtil.isA((Resource)child, (String)"nt:file") ? child : null;
}
public void removeAttachmentResource(Resource commentResource, String name) {
try {
Resource attachment = commentResource.getChild(this.getAttachmentResourcePath(name));
if (null != attachment) {
Session session = (Session)attachment.getResourceResolver().adaptTo(Session.class);
session.removeItem(attachment.getPath());
session.save();
}
}
catch (RepositoryException e) {
this.log.error("error while removing attachment from comment [{}]: ", (Object)commentResource.getPath(), (Object)e);
throw new CommentException("Could not remove attachment from comment: " + commentResource.getPath(), (Throwable)e);
}
}
public final Map<String, Resource> getAttachmentMap(Resource commentResource) throws CommentException {
HashMap<String, Resource> attachments = new HashMap<String, Resource>();
Iterator<Resource> iterator = this.getAttachments(commentResource);
while (iterator.hasNext()) {
Resource child = iterator.next();
if (!ResourceUtil.isA((Resource)child, (String)"nt:file")) continue;
attachments.put(child.getName(), child);
}
return attachments;
}
protected final Resource getCollectionResource(Resource target) throws CommentException {
return target.getResourceResolver().getResource(this.getCollectionResourcePath(target));
}
protected Node createCollectionNode(String rootPath, Session session) {
try {
Node root = null;
String path = rootPath;
while (root == null && StringUtils.isNotBlank((String)path)) {
if (!session.nodeExists(path = Text.getRelativeParent((String)path, (int)1))) continue;
root = session.getNode(path);
}
if (root != null) {
String relPath = StringUtils.removeStart((String)rootPath, (String)(path + '/'));
return JcrUtils.getOrCreateByPath((Node)root, (String)relPath, (boolean)false, (String)"nt:unstructured", (String)"nt:unstructured", (boolean)false);
}
throw new CommentException("Unable to access parent nodes of new collection");
}
catch (RepositoryException e) {
throw new CommentException("Error creating collection root at: " + rootPath, (Throwable)e);
}
}
protected Node createCommentNode(String commentPath, Node collectionNode, Session session) {
try {
return JcrUtils.getOrCreateUniqueByPath((Node)collectionNode, (String)commentPath, (String)"nt:unstructured");
}
catch (RepositoryException e) {
throw new CommentException("Error creating comment node at: " + commentPath, (Throwable)e);
}
}
protected void customizeCollectionNode(Resource target, Node collectionNode) {
}
protected void customizeCommentNode(Resource collectionResource, Node commentNode) {
}
protected void customizeAttachmentNode(Resource commentResource, Node attachmentContentNode) {
}
protected String getCollectionResourcePath(Resource target) {
Resource contentResource = target.getChild("jcr:content");
String path = null != contentResource ? contentResource.getPath() : target.getPath();
return path + "/" + "comments";
}
protected String getCommentResourcePath(Resource collectionResource, String message) {
return Util.createValidName(message);
}
protected String getAttachmentResourcePath(String name) {
return name;
}
protected Iterator<Resource> getAttachments(Resource commentResource) {
return commentResource.listChildren();
}
public abstract String getCollectionResourceType();
public abstract String getCommentResourceType();
}