CommentingOperationHandler.java
5.37 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.RepositoryException
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.ReferenceCardinality
* org.apache.felix.scr.annotations.ReferencePolicy
* org.apache.felix.scr.annotations.Service
* org.apache.jackrabbit.api.security.user.Authorizable
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.servlets.post.PostOperation
* org.apache.sling.servlets.post.PostResponse
* org.apache.sling.servlets.post.SlingPostProcessor
* org.osgi.service.event.Event
* org.osgi.service.event.EventAdmin
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.granite.comments;
import com.adobe.granite.comments.Comment;
import com.adobe.granite.comments.CommentCollection;
import com.adobe.granite.comments.CommentManager;
import com.adobe.granite.comments.CommentingEvent;
import javax.jcr.RepositoryException;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.Service;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.servlets.post.PostOperation;
import org.apache.sling.servlets.post.PostResponse;
import org.apache.sling.servlets.post.SlingPostProcessor;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component
@Service
@Properties(value={@Property(name="sling.post.operation", value={"granite:comment"}), @Property(name="sling.servlet.methods", value={"POST"}), @Property(name="sling.servlet.extensions", value={"html", "json"})})
public class CommentingOperationHandler
implements PostOperation {
private static final long serialVersionUID = -5076236106916461224L;
private static final Logger log = LoggerFactory.getLogger(CommentingOperationHandler.class);
@Reference
private CommentManager cm = null;
@Reference(cardinality=ReferenceCardinality.OPTIONAL_UNARY, policy=ReferencePolicy.DYNAMIC)
private volatile EventAdmin eventAdmin = null;
private final String DEFAULT_CREATOR = "anonymous";
private final String MESSAGE = "message";
private final String ANNOTATION_DATA = "annotationData";
public void run(SlingHttpServletRequest request, PostResponse response, SlingPostProcessor[] processors) {
String createdBy;
Resource resource = request.getResource();
try {
createdBy = this.getAuthor(request);
}
catch (RepositoryException re) {
log.error("Unable to find ID of author", (Throwable)re);
response.setError((Throwable)re);
return;
}
String path = resource.getPath();
String message = request.getParameter("message");
String annotationData = request.getParameter("annotationData");
try {
Comment comment = this.createComment(resource, path, message, annotationData, createdBy);
EventAdmin ea = this.eventAdmin;
if (ea != null && comment != null) {
ea.postEvent(CommentingEvent.commented(comment.getPath()).toEvent());
} else if (ea == null) {
log.debug("event admin is null");
} else {
log.debug("comment could not be created");
}
}
catch (Exception e) {
log.error("Unable to create comment.", (Throwable)e);
response.setError((Throwable)e);
}
}
private String getAuthor(SlingHttpServletRequest request) throws RepositoryException {
Authorizable auth = (Authorizable)request.getResourceResolver().adaptTo(Authorizable.class);
String createdBy = "anonymous";
if (auth != null) {
createdBy = auth.getID();
}
return createdBy;
}
private Comment createComment(Resource resource, String path, String message, String annotationData, String createdBy) throws Exception {
if (path == null || message == null) {
throw new IllegalArgumentException("Path and message may not be null");
}
return this.cm.getOrCreateCollection(resource, CommentCollection.class).addComment(message, createdBy, annotationData);
}
void bindEventAdmin(EventAdmin ea) {
this.eventAdmin = ea;
log.debug("binding Event Admin");
}
void unbindEventAdmin(EventAdmin ea) {
if (this.eventAdmin == ea) {
this.eventAdmin = null;
log.debug("un-binding Event Admin");
}
}
protected void bindCm(CommentManager commentManager) {
this.cm = commentManager;
}
protected void unbindCm(CommentManager commentManager) {
if (this.cm == commentManager) {
this.cm = null;
}
}
}