ReplicationEventEmailBuilder.java
8.27 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
/*
* 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.security.user.UserPropertiesService
* com.day.cq.replication.ReplicationAction
* com.day.cq.replication.ReplicationActionType
* javax.jcr.Credentials
* javax.jcr.GuestCredentials
* javax.jcr.Repository
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.commons.lang.StringUtils
* org.apache.commons.lang.text.StrSubstitutor
* org.apache.felix.scr.annotations.Activate
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.ReferencePolicy
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.resource.LoginException
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceResolverFactory
* org.apache.sling.commons.osgi.OsgiUtil
* org.apache.sling.jcr.api.SlingRepository
* org.osgi.service.component.ComponentContext
* org.osgi.service.event.Event
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.wcm.notification.email.impl;
import com.adobe.granite.security.user.UserProperties;
import com.adobe.granite.security.user.UserPropertiesManager;
import com.adobe.granite.security.user.UserPropertiesService;
import com.day.cq.replication.ReplicationAction;
import com.day.cq.replication.ReplicationActionType;
import com.day.cq.wcm.notification.NotificationContext;
import com.day.cq.wcm.notification.email.AbstractEmailBuilder;
import java.util.Collections;
import java.util.Date;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.jcr.Credentials;
import javax.jcr.GuestCredentials;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.text.StrSubstitutor;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.commons.osgi.OsgiUtil;
import org.apache.sling.jcr.api.SlingRepository;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.event.Event;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=1)
@Service
@Property(name="topic", value={"com/day/cq/replication"})
public class ReplicationEventEmailBuilder
extends AbstractEmailBuilder {
private static final Logger log = LoggerFactory.getLogger(ReplicationEventEmailBuilder.class);
private static final String DEFAULT_PROFILE_PATH = "profile";
@Reference
private ResourceResolverFactory resourceResolverFactory;
@Reference
private SlingRepository repository;
@Reference(policy=ReferencePolicy.STATIC)
private UserPropertiesService userPropertiesManagerFactory = null;
@Property(label="%public.profile.path.name", description="%public.profile.path.description")
private static final String PROPERTY_PUBLIC_PROFILE_RELATIVE_PATH = "public.profile.path";
private String publicProfileRelativePath;
@Activate
protected void activate(ComponentContext context) {
Dictionary props = context.getProperties();
this.publicProfileRelativePath = StringUtils.defaultIfEmpty((String)OsgiUtil.toString(props.get("public.profile.path"), (String)"profile"), (String)"profile");
}
@Override
protected ResourceResolverFactory getResolverFactory() {
return this.resourceResolverFactory;
}
@Override
protected Map<String, String> getHeaders(NotificationContext context, Event event) {
return Collections.emptyMap();
}
@Override
protected String getSubject(NotificationContext context, Event event, Properties properties) {
Properties values = this.getValues(context, event);
return StrSubstitutor.replace(properties.get("subject"), (Map)values);
}
@Override
protected String getMessage(NotificationContext context, Event event, Properties properties) {
Properties values = this.getValues(context, event);
StringBuilder msg = new StringBuilder();
msg.append(StrSubstitutor.replace(properties.get("header"), (Map)values));
msg.append(StrSubstitutor.replace(properties.get("message"), (Map)values));
msg.append(StrSubstitutor.replace(properties.get("footer"), (Map)values));
return msg.toString();
}
@Override
public boolean accepts(NotificationContext context, Event event) {
return "com/day/cq/replication".equalsIgnoreCase(event.getTopic());
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private Properties getValues(NotificationContext context, Event event) {
ReplicationAction action = ReplicationAction.fromEvent((Event)event);
String name = null;
UserProperties userProperties = null;
Session guestSession = null;
ResourceResolver guestResolver = null;
try {
guestSession = this.repository.login((Credentials)new GuestCredentials());
HashMap<String, Session> authMap = new HashMap<String, Session>();
authMap.put("user.jcr.session", guestSession);
guestResolver = this.getResolverFactory().getResourceResolver(authMap);
UserPropertiesManager upm = this.userPropertiesManagerFactory.createUserPropertiesManager(guestResolver);
userProperties = null != upm ? upm.getUserProperties(action.getUserId(), this.publicProfileRelativePath) : null;
name = null != userProperties ? userProperties.getDisplayName() : null;
}
catch (LoginException e) {
log.error("failure to obtain resolver: ", (Throwable)e);
}
catch (RepositoryException e) {
log.error("failure to obtain user properties manager: ", (Throwable)e);
}
finally {
if (guestSession != null && guestSession.isLive()) {
guestSession.logout();
}
}
String userId = null != userProperties ? userProperties.getAuthorizableID() : "";
String fullName = StringUtils.defaultIfEmpty((String)name, (String)userId);
Properties values = new Properties();
values.setProperty("time", new Date(action.getTime()).toString());
values.setProperty("userFullName", fullName);
values.setProperty("userId", action.getUserId());
values.setProperty("type", action.getType().getName());
values.setProperty("path", action.getPath());
values.setProperty("revision", StringUtils.defaultIfEmpty((String)action.getRevision(), (String)""));
return values;
}
@Override
public Repository getRepository() {
return this.repository;
}
protected void bindResourceResolverFactory(ResourceResolverFactory resourceResolverFactory) {
this.resourceResolverFactory = resourceResolverFactory;
}
protected void unbindResourceResolverFactory(ResourceResolverFactory resourceResolverFactory) {
if (this.resourceResolverFactory == resourceResolverFactory) {
this.resourceResolverFactory = null;
}
}
protected void bindRepository(SlingRepository slingRepository) {
this.repository = slingRepository;
}
protected void unbindRepository(SlingRepository slingRepository) {
if (this.repository == slingRepository) {
this.repository = null;
}
}
protected void bindUserPropertiesManagerFactory(UserPropertiesService userPropertiesService) {
this.userPropertiesManagerFactory = userPropertiesService;
}
protected void unbindUserPropertiesManagerFactory(UserPropertiesService userPropertiesService) {
if (this.userPropertiesManagerFactory == userPropertiesService) {
this.userPropertiesManagerFactory = null;
}
}
}