DeviceService.java
16.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.api.PageManager
* com.day.cq.wcm.api.WCMException
* javax.annotation.CheckForNull
* javax.annotation.Nonnull
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.Value
* javax.jcr.ValueFactory
* javax.jcr.util.TraversingItemVisitor
* javax.jcr.util.TraversingItemVisitor$Default
* org.apache.commons.collections.Predicate
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Service
* org.apache.jackrabbit.api.security.user.Authorizable
* org.apache.jackrabbit.api.security.user.Group
* org.apache.jackrabbit.api.security.user.User
* org.apache.jackrabbit.api.security.user.UserManager
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.JSONObject
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.screens.device.impl;
import com.adobe.cq.screens.device.Device;
import com.adobe.cq.screens.device.DeviceConfig;
import com.adobe.cq.screens.device.DeviceStatus;
import com.adobe.cq.screens.device.impl.DeviceConfigImpl;
import com.adobe.cq.screens.device.impl.DeviceImpl;
import com.adobe.cq.screens.device.impl.DeviceStatusImpl;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.WCMException;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import javax.jcr.ValueFactory;
import javax.jcr.util.TraversingItemVisitor;
import org.apache.commons.collections.Predicate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.jackrabbit.api.security.user.Group;
import org.apache.jackrabbit.api.security.user.User;
import org.apache.jackrabbit.api.security.user.UserManager;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Service(value={DeviceService.class})
@Component
public class DeviceService {
private static final Logger log = LoggerFactory.getLogger(DeviceService.class);
public static final String DEVICE_ID_CHARACTERS = "abcdefghijklmnopqrstuvwxyz0123456789";
private ConcurrentHashMap<String, DeviceStatusImpl> devices = new ConcurrentHashMap();
public DeviceStatus getDeviceStatus(ResourceResolver resolver, String deviceId) {
Device device = this.getDevice(resolver, deviceId);
if (device == null) {
return null;
}
deviceId = device.getId();
DeviceStatusImpl s = this.devices.get(deviceId);
if (s == null) {
s = new DeviceStatusImpl();
this.devices.put(deviceId, s);
}
log.debug("retrieving status of {}", (Object)deviceId);
return s;
}
public DeviceStatus.Ping updatePing(ResourceResolver resolver, String devicePath) {
DeviceStatusImpl s = (DeviceStatusImpl)this.getDeviceStatus(resolver, devicePath);
if (s == null) {
return null;
}
return s.updatePing(System.currentTimeMillis()).getLastPing();
}
public DeviceStatus.Firmware updateFirmwareVersion(ResourceResolver resolver, String devicePath, String version, long timestamp) {
DeviceStatusImpl s = (DeviceStatusImpl)this.getDeviceStatus(resolver, devicePath);
if (s == null) {
return null;
}
return s.updateFirmwareVersion(version, timestamp).getFirmware();
}
public Iterator<Device> getDevices(final ResourceResolver resolver, final Predicate predicate) {
final ArrayList list = new ArrayList();
TraversingItemVisitor.Default v = new TraversingItemVisitor.Default(){
protected void entering(Node node, int level) throws RepositoryException {
if (node.isNodeType("rep:User")) {
Resource userResource = resolver.getResource(node.getPath());
Device d = DeviceService.this.getDevice(userResource);
if (predicate == null || predicate.evaluate((Object)d)) {
list.add(d);
}
}
}
};
String path = "/home/users/screens";
Resource root = resolver.getResource(path);
if (root != null) {
try {
v.visit((Node)root.adaptTo(Node.class));
}
catch (RepositoryException e) {
log.error("Error while retrieving devices", (Throwable)e);
}
}
return list.iterator();
}
protected Device getDevice(Resource resource) {
User user = (User)resource.adaptTo(User.class);
if (user == null) {
if (resource.getResourceResolver().isResourceType(resource, "screens/core/components/device")) {
resource = resource.getParent();
user = (User)resource.adaptTo(User.class);
}
if (user == null) {
log.debug("Unable to get device. resource is not adaptable to a user: {}", (Object)resource);
return null;
}
} else {
Resource profile = resource.getChild("profile_screens");
if (profile == null) {
log.debug("Unable to get device. user has no screens profile {}", (Object)resource);
return null;
}
if (!profile.getResourceResolver().isResourceType(profile, "screens/core/components/device")) {
log.debug("Unable to get device. profile has wrong resource type {}", (Object)profile);
return null;
}
}
try {
return new DeviceImpl(resource, user.getID());
}
catch (RepositoryException e) {
log.error("Error while trying to get device from resource {}", (Object)resource);
return null;
}
}
protected DeviceConfig getDeviceConfig(Resource resource) {
ResourceResolver resolver = resource.getResourceResolver();
Page cfgPage = (Page)resource.adaptTo(Page.class);
if (cfgPage != null) {
if (resolver.isResourceType(cfgPage.getContentResource(), "screens/core/components/deviceconfig")) {
return new DeviceConfigImpl(resource);
}
log.debug("Unable to get device config. resource does have correct resource type: {}", (Object)resource);
return null;
}
if (resolver.isResourceType(resource, "screens/core/components/deviceconfig")) {
return new DeviceConfigImpl(resource);
}
log.debug("Unable to get device config. resource does have correct resource type: {}", (Object)resource);
return null;
}
public Device getDevice(ResourceResolver resolver, String id) {
try {
Resource resource;
if (id == null || id.length() == 0) {
log.debug("Unable to get device. id is null or empty: {}", (Object)id);
return null;
}
UserManager userManager = (UserManager)resolver.adaptTo(UserManager.class);
if (userManager == null) {
log.debug("Unable to get device. resolve is not adaptable to user manager: {}", (Object)id);
return null;
}
String path = id;
if (id.charAt(0) != '/') {
Authorizable auth = userManager.getAuthorizable(id);
if (auth == null) {
log.debug("Unable to get device. user {} does not exist", (Object)id);
return null;
}
path = auth.getPath();
}
if ((resource = resolver.getResource(path)) == null) {
log.debug("Unable to get device with id {}. does not exist.", (Object)id);
return null;
}
return this.getDevice(resource);
}
catch (RepositoryException e) {
log.error("Error while retrieving device", (Throwable)e);
return null;
}
}
private User getUser(ResourceResolver resolver, Device device) throws RepositoryException {
UserManager userManager = (UserManager)resolver.adaptTo(UserManager.class);
if (userManager == null) {
return null;
}
Authorizable auth = userManager.getAuthorizable(device.getId());
if (!(auth instanceof User)) {
return null;
}
return (User)auth;
}
public Device createDevice(ResourceResolver resolver, String tenant) throws RepositoryException {
UserManager userManager = (UserManager)resolver.adaptTo(UserManager.class);
String devicesPath = "screens/" + tenant + "/devices";
String deviceName = this.generateDeviceName("screens-" + tenant + "-", 36);
User user = userManager.createUser(deviceName, null, this.getPrincipal(deviceName), devicesPath);
String groupName = "screens-" + tenant + "-devices";
String defaultGroupPath = "screens/" + tenant;
Group group = (Group)userManager.getAuthorizable(groupName);
if (group == null) {
group = userManager.createGroup(groupName, this.getPrincipal(groupName), defaultGroupPath);
}
group.addMember((Authorizable)user);
Session s = (Session)resolver.adaptTo(Session.class);
ValueFactory fac = s.getValueFactory();
user.setProperty("profile_screens/sling:resourceType", fac.createValue("screens/core/components/device"));
user.setProperty("profile_screens/id", fac.createValue(deviceName));
s.save();
return this.getDevice(resolver.getResource(user.getPath()));
}
public void deleteDevice(ResourceResolver resolver, String id) throws NullPointerException, RepositoryException {
if (id == null || id.length() == 0) {
throw new NullPointerException("Unable to get device. id is null or empty: " + id);
}
Device device = this.getDevice(resolver, id);
if (device == null) {
throw new RepositoryException("Unable to get device. device " + id + " does not exist");
}
User user = this.getUser(resolver, device);
if (user == null) {
throw new RepositoryException("Unable to get user. user " + id + " does not exist");
}
this.removeDisplay(resolver, device, false);
user.remove();
Session s = (Session)resolver.adaptTo(Session.class);
s.save();
}
private Principal getPrincipal(final String name) {
return new Principal(){
@Override
public String getName() {
return name;
}
};
}
private String generateDeviceName(String prefix, int length) {
StringBuilder name = new StringBuilder(prefix);
Random random = new Random();
while (length-- > 0) {
name.append("abcdefghijklmnopqrstuvwxyz0123456789".charAt(random.nextInt("abcdefghijklmnopqrstuvwxyz0123456789".length())));
}
return name.toString();
}
@Nonnull
public String setPassword(@Nonnull ResourceResolver resolver, @Nonnull Device device) throws RepositoryException {
User user = this.getUser(resolver, device);
if (user == null) {
throw new RepositoryException("Device is not associated with a user. unable to set password.");
}
String password = this.generateDevicePassword(36);
user.changePassword(password);
((Session)resolver.adaptTo(Session.class)).save();
return password;
}
private String generateDevicePassword(int length) {
Random random = new Random();
StringBuilder pwd = new StringBuilder(length);
while (length-- > 0) {
int code = random.nextInt(89) + 32;
pwd.append((char)code);
}
return pwd.toString();
}
@CheckForNull
public Device setMetadata(ResourceResolver resolver, Device device, JSONObject metadata) throws JSONException, RepositoryException {
User user = this.getUser(resolver, device);
if (user == null) {
throw new RepositoryException("Device is not associated with a user. unable to update profile.");
}
Session s = (Session)resolver.adaptTo(Session.class);
ValueFactory fac = s.getValueFactory();
Iterator iter = metadata.keys();
while (iter.hasNext()) {
String key = (String)iter.next();
user.setProperty("profile_screens/" + key, fac.createValue(metadata.getString(key)));
}
s.save();
return device;
}
public Device assignDisplay(ResourceResolver resolver, Device device, String displayPath) throws RepositoryException {
Page deviceConfigPage;
Resource displayResource = resolver.getResource(displayPath);
if (displayResource == null || device == null) {
return device;
}
Page displayPage = (Page)displayResource.adaptTo(Page.class);
if (displayPage == null) {
return device;
}
Resource displayContent = displayPage.getContentResource();
if (displayContent == null) {
return device;
}
User user = this.getUser(resolver, device);
if (user == null) {
return device;
}
PageManager pMgr = (PageManager)resolver.adaptTo(PageManager.class);
try {
deviceConfigPage = pMgr.create(displayPath, null, "screens/core/templates/deviceconfig", "Device Config", true);
}
catch (WCMException e) {
throw new RepositoryException((Throwable)e);
}
DeviceConfig cfg = (DeviceConfig)((Resource)deviceConfigPage.adaptTo(Resource.class)).adaptTo(DeviceConfig.class);
return this.assignDeviceConfig(resolver, device, cfg);
}
public Device assignDeviceConfig(ResourceResolver resolver, Device device, DeviceConfig config) throws RepositoryException {
Page configPage = (Page)config.adaptTo(Page.class);
if (configPage == null) {
return device;
}
Resource configContent = configPage.getContentResource();
if (configContent == null) {
return device;
}
User user = this.getUser(resolver, device);
if (user == null) {
return device;
}
DeviceConfig oldConfig = device.getDeviceConfig();
if (oldConfig != null) {
device = this.removeDisplay(resolver, device, false);
}
Session s = (Session)resolver.adaptTo(Session.class);
ValueFactory fac = s.getValueFactory();
user.setProperty("profile_screens/configPath", fac.createValue(config.getPath()));
Node node = (Node)configContent.adaptTo(Node.class);
node.setProperty("deviceId", device.getId());
s.save();
return this.getDevice(resolver.getResource(device.getPath()));
}
public Device removeDisplay(@Nonnull ResourceResolver resolver, @Nonnull Device device, boolean removeConfig) throws RepositoryException {
DeviceConfig config = device.getDeviceConfig();
if (config == null) {
return device;
}
User user = this.getUser(resolver, device);
if (user == null) {
return device;
}
Session s = (Session)resolver.adaptTo(Session.class);
user.removeProperty("profile_screens/configPath");
if (removeConfig) {
((Node)config.adaptTo(Node.class)).remove();
s.save();
} else {
Page configPage = (Page)config.adaptTo(Page.class);
if (configPage == null) {
return device;
}
Resource configContent = configPage.getContentResource();
if (configContent == null) {
return device;
}
Node node = (Node)configContent.adaptTo(Node.class);
if (node.hasProperty("deviceId")) {
node.getProperty("deviceId").remove();
s.save();
}
}
return this.getDevice(resolver.getResource(device.getPath()));
}
}