NotificationImpl.java
12.6 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
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.security.user.UserProperties
* com.adobe.granite.security.user.UserPropertiesManager
* com.adobe.granite.workflow.exec.InboxItem
* com.adobe.granite.workflow.exec.WorkItem
* com.adobe.granite.workflow.payload.PayloadInfo
* com.adobe.granite.workflow.payload.PayloadInfoBuilderContext
* com.adobe.granite.workflow.payload.PayloadInfoBuilderContext$INITIATOR_HINT
* com.adobe.granite.workflow.payload.PayloadInfoBuilderManager
* com.day.cq.workflow.event.WorkflowEvent
* com.day.cq.workflow.exec.WorkItem
* com.day.cq.workflow.exec.Workflow
* com.day.cq.workflow.exec.WorkflowData
* com.day.cq.workflow.metadata.MetaDataMap
* com.day.cq.workflow.model.WorkflowModel
* com.day.cq.workflow.model.WorkflowNode
* javax.jcr.RepositoryException
* javax.jcr.Value
* org.apache.jackrabbit.api.security.user.Authorizable
* org.apache.jackrabbit.api.security.user.UserManager
* org.apache.jackrabbit.util.Text
* org.apache.sling.api.adapter.Adaptable
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceResolverFactory
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.api.wrappers.ValueMapDecorator
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.workflow.impl.email;
import com.adobe.granite.security.user.UserProperties;
import com.adobe.granite.security.user.UserPropertiesManager;
import com.adobe.granite.workflow.exec.InboxItem;
import com.adobe.granite.workflow.exec.WorkItem;
import com.adobe.granite.workflow.payload.PayloadInfo;
import com.adobe.granite.workflow.payload.PayloadInfoBuilderContext;
import com.adobe.granite.workflow.payload.PayloadInfoBuilderManager;
import com.day.cq.workflow.event.WorkflowEvent;
import com.day.cq.workflow.exec.Workflow;
import com.day.cq.workflow.exec.WorkflowData;
import com.day.cq.workflow.impl.ServiceLoginUtil;
import com.day.cq.workflow.impl.email.EMailNotificationService;
import com.day.cq.workflow.impl.email.Notification;
import com.day.cq.workflow.impl.email.NotificationHelper;
import com.day.cq.workflow.metadata.MetaDataMap;
import com.day.cq.workflow.model.WorkflowModel;
import com.day.cq.workflow.model.WorkflowNode;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.TreeSet;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.jackrabbit.api.security.user.UserManager;
import org.apache.jackrabbit.util.Text;
import org.apache.sling.api.adapter.Adaptable;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.wrappers.ValueMapDecorator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class NotificationImpl
implements Notification {
private static final Logger log = LoggerFactory.getLogger(NotificationImpl.class);
private final Workflow workflow;
private final com.day.cq.workflow.exec.WorkItem workItem;
private final WorkflowEvent event;
private final ValueMap properties = new ValueMapDecorator(new HashMap());
private String emailFromAddress;
private String hostPrefix;
private Authorizable initiator;
private Authorizable participant;
private ResourceResolverFactory resolverFactory;
public NotificationImpl(Workflow wf, com.day.cq.workflow.exec.WorkItem wi, WorkflowEvent event, ResourceResolverFactory resolverFactory) {
this.workflow = wf;
this.workItem = wi;
this.event = event;
this.resolverFactory = resolverFactory;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public void load(EMailNotificationService svc, Authorizable participant) {
this.putAll(this.event, "event.");
this.participant = participant;
ResourceBundle bundle = null;
if (participant != null) {
try {
this.putAll(participant, "participant.");
}
catch (RepositoryException e) {
log.error("Unable to add properties for the participant.", (Throwable)e);
}
String language = null;
try {
language = svc.getUserLanguage(participant.getID());
}
catch (RepositoryException e) {
log.error("Unable to lookup language for participant.", (Throwable)e);
}
if (language != null) {
bundle = svc.getUserResourceBundle(language);
}
if (bundle != null) {
this.put("participant.language", bundle.getLocale().getLanguage());
} else if (language != null) {
this.put("participant.language", language);
}
}
if (this.workItem != null) {
this.put("item.id", this.workItem.getId());
this.put("item.node.id", this.workItem.getNode().getId());
this.put("item.node.title", this.workItem.getNode().getTitle());
this.put("item.node.type", this.workItem.getNode().getType());
this.putAll(this.workItem.getNode().getMetaDataMap(), "item.node.data.");
this.putAll(this.workItem.getWorkflowData().getMetaDataMap(), "item.workflow.data.");
this.putAll(this.workItem.getMetaDataMap(), "item.data.");
} else {
this.put("item.id", bundle != null ? bundle.getString("[Information not available]") : "[Information not available]");
this.put("item.node.id", bundle != null ? bundle.getString("[Information not available]") : "[Information not available]");
this.put("item.node.title", bundle != null ? bundle.getString("[Information not available]") : "[Information not available]");
this.put("item.node.type", bundle != null ? bundle.getString("[Information not available]") : "[Information not available]");
}
this.put("instance.id", this.workflow.getId());
this.put("instance.state", this.workflow.getState());
this.putAll(this.workflow.getMetaDataMap(), "instance.data.");
this.put("model.title", this.workflow.getWorkflowModel().getTitle());
this.put("model.id", this.workflow.getWorkflowModel().getId());
this.put("model.version", this.workflow.getWorkflowModel().getVersion());
this.putAll(this.workflow.getWorkflowModel().getMetaDataMap(), "model.data.");
String initiatorId = this.workflow.getInitiator();
ResourceResolver userResolver = null;
try {
userResolver = ServiceLoginUtil.createWorkflowUserReaderResolver(this.resolverFactory);
this.initiator = NotificationHelper.getAuthorizable(this.getUserManager(userResolver), initiatorId);
this.putAll(this.initiator, "initiator.");
}
catch (RepositoryException e) {
log.error("Unable to query the properties for the initiator with ID [{}].", (Object)initiatorId, (Object)e);
}
finally {
if (userResolver != null && userResolver.isLive()) {
userResolver.close();
}
}
WorkflowData workflowData = this.workflow.getWorkflowData();
this.putAll(workflowData.getMetaDataMap(), "data");
this.put("payload.data", workflowData.getPayload());
this.put("payload.type", workflowData.getPayloadType());
if ("JCR_PATH".equals(workflowData.getPayloadType())) {
String payloadOpenPath;
ResourceResolver repoReaderResolver = null;
payloadOpenPath = null;
this.put("payload.path", workflowData.getPayload());
try {
repoReaderResolver = ServiceLoginUtil.createRepositoryReaderResolver(this.resolverFactory);
PayloadInfoBuilderManager payloadBuilder = (PayloadInfoBuilderManager)repoReaderResolver.adaptTo(PayloadInfoBuilderManager.class);
WorkItem wi = null;
if (this.workItem instanceof Adaptable) {
wi = (WorkItem)((Adaptable)this.workItem).adaptTo(WorkItem.class);
}
if (wi != null) {
payloadOpenPath = payloadBuilder.getPayloadInfo((InboxItem)wi, PayloadInfoBuilderContext.INITIATOR_HINT.NOTIFICATION.name()).getBrowserPath();
}
}
finally {
if (repoReaderResolver != null && repoReaderResolver.isLive()) {
repoReaderResolver.close();
}
}
if (payloadOpenPath == null && workflowData.getPayload() instanceof String) {
payloadOpenPath = (String)workflowData.getPayload();
}
this.put("payload.path.open", Text.escapePath((String)payloadOpenPath));
}
this.hostPrefix = svc.getHostPrefix();
this.put("host.prefix", this.hostPrefix);
if (log.isDebugEnabled()) {
StringBuilder props = new StringBuilder();
TreeSet names = new TreeSet(this.properties.keySet());
for (String name : names) {
Object value = this.properties.get((Object)name);
log.debug("- {}:{}", (Object)name, value);
props.append(name).append(":").append(value).append("\n");
}
this.put("properties", props.toString());
}
}
public void put(String name, Object value) {
if (value != null) {
this.properties.put((Object)name, value);
}
}
public void putAll(MetaDataMap metaData, String prefix) {
for (String name : metaData.keySet()) {
this.properties.put((Object)(prefix + name), metaData.get(name, String.class));
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public void putAll(Authorizable auth, String prefix) throws RepositoryException {
Iterator iter = auth.getPropertyNames();
while (iter.hasNext()) {
String name = (String)iter.next();
this.properties.put((Object)(prefix + name), (Object)auth.getProperty(name));
}
ResourceResolver userResolver = null;
try {
userResolver = ServiceLoginUtil.createWorkflowUserReaderResolver(this.resolverFactory);
UserPropertiesManager upManager = (UserPropertiesManager)userResolver.adaptTo(UserPropertiesManager.class);
if (upManager != null) {
UserProperties up = upManager.getUserProperties(auth, "profile");
String[] propNames = up.getPropertyNames();
for (int i = 0; i < propNames.length; ++i) {
this.properties.put((Object)(prefix + propNames[i]), (Object)up.getProperty(propNames[i]));
}
this.properties.put((Object)(prefix + "name"), (Object)up.getDisplayName());
}
}
finally {
if (userResolver != null && userResolver.isLive()) {
userResolver.close();
}
}
this.properties.put((Object)(prefix + "id"), (Object)auth.getID());
this.properties.put((Object)(prefix + "home"), (Object)auth.getPath());
}
public void putAll(WorkflowEvent evt, String prefix) {
for (String name : evt.getPropertyNames()) {
this.properties.put((Object)(prefix + name), evt.getProperty(name));
}
}
@Override
public ValueMap getProperties() {
return this.properties;
}
@Override
public com.day.cq.workflow.exec.WorkItem getWorkItem() {
return this.workItem;
}
@Override
public WorkflowEvent getEvent() {
return this.event;
}
@Override
public Workflow getWorkflow() {
return this.workflow;
}
@Override
public String getEmailFromAddress() {
return this.emailFromAddress;
}
public void setEmailFromAddress(String emailFromAddress) {
this.emailFromAddress = emailFromAddress;
}
@Override
public String getHostPrefix() {
return this.hostPrefix;
}
@Override
public Authorizable getInitiator() {
return this.initiator;
}
@Override
public Authorizable getParticipant() {
return this.participant;
}
private UserManager getUserManager(ResourceResolver resolver) {
UserManager um = null;
if (resolver != null) {
um = (UserManager)resolver.adaptTo(UserManager.class);
}
return um;
}
}