ScreensResourceProvider.java
6.75 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.rest.ApiResourceProvider
* com.day.text.Text
* javax.jcr.RepositoryException
* org.apache.commons.lang.StringUtils
* org.apache.sling.api.resource.PersistenceException
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.screens.impl.rest;
import com.adobe.cq.screens.device.Device;
import com.adobe.cq.screens.device.DeviceManager;
import com.adobe.cq.screens.device.impl.DeviceService;
import com.adobe.cq.screens.device.registration.PendingDevice;
import com.adobe.cq.screens.device.registration.RegistrationService;
import com.adobe.cq.screens.impl.rest.DeviceResource;
import com.adobe.cq.screens.impl.rest.DevicesResource;
import com.adobe.cq.screens.impl.rest.RegistrationResource;
import com.adobe.cq.screens.impl.rest.RegistrationsResource;
import com.adobe.cq.screens.impl.rest.ScreensResource;
import com.adobe.cq.screens.impl.rest.ScreensResourceFactory;
import com.adobe.granite.rest.ApiResourceProvider;
import com.day.text.Text;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import javax.jcr.RepositoryException;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ScreensResourceProvider
implements ApiResourceProvider {
private static final Logger log = LoggerFactory.getLogger(ScreensResourceProvider.class);
public static final String TYPE = "screens";
public static final String SCREENS_ROOT = "/content/screens";
private ScreensResourceFactory resourceFactory = new ScreensResourceFactory();
private String apiContextPath;
private final DeviceService deviceService;
private final RegistrationService registrationService;
public ScreensResourceProvider(String contextPath, DeviceService deviceService, RegistrationService registrationService) {
this.apiContextPath = contextPath + "/" + "screens";
this.deviceService = deviceService;
this.registrationService = registrationService;
}
public Resource getResource(ResourceResolver resolver, String path) {
Object device;
String id;
String token;
log.debug("screens.getResource({})", (Object)path);
Resource collectionRoot = resolver.getResource("/content/screens");
if (collectionRoot == null) {
log.error("Collection root {} not found.", (Object)"/content/screens");
return null;
}
if (StringUtils.isEmpty((String)path)) {
return new ScreensResource(resolver, this.apiContextPath);
}
if ("/devices".equals(path)) {
return new DevicesResource(resolver, this.apiContextPath + path, this.deviceService);
}
if ("/devices".equals(Text.getRelativeParent((String)path, (int)1)) && (device = this.deviceService.getDevice(resolver, id = Text.getName((String)path))) != null) {
return new DeviceResource(resolver, this.apiContextPath + path, (Device)device);
}
if ("/registration".equals(path)) {
return new RegistrationsResource(resolver, this.apiContextPath + path, this.registrationService);
}
if ("/registration".equals(Text.getRelativeParent((String)path, (int)1)) && (device = this.registrationService.getDevice(token = Text.getName((String)path))) != null) {
return new RegistrationResource(resolver, this.apiContextPath + path, (PendingDevice)device);
}
return null;
}
public Iterator<Resource> listChildren(Resource parent) {
log.debug("screens.listChildren({})", (Object)parent.getPath());
String parentPath = parent.getPath();
if (!StringUtils.isEmpty((String)parentPath)) {
ResourceResolver resolver = parent.getResourceResolver();
Resource collectionRoot = resolver.getResource("/content/screens");
if (collectionRoot == null) {
log.error("Collection root {} not found.", (Object)"/content/screens");
return null;
}
Resource parentResource = resolver.getResource(this.unmap(parentPath));
if (parentResource == null) {
return null;
}
LinkedList<Resource> children = new LinkedList<Resource>();
Iterator it = parentResource.listChildren();
while (it.hasNext()) {
Resource child = (Resource)it.next();
Resource childRes = this.resourceFactory.createResource(child, this.map(child.getPath()));
if (childRes == null) continue;
children.add(childRes);
}
return children.iterator();
}
return null;
}
public Resource create(ResourceResolver resolver, String path, Map<String, Object> properties) throws PersistenceException {
log.debug("screens.create({})", (Object)path);
throw new PersistenceException("Unable to create resource at " + path);
}
public void delete(ResourceResolver resolver, String path) throws PersistenceException {
log.debug("screens.delete({})", (Object)path);
Resource resource = this.getResource(resolver, path);
if (resource == null) {
throw new PersistenceException("Unable to delete resource at " + path);
}
Device device = (Device)resource.adaptTo(Device.class);
if (device != null) {
DeviceManager deviceManager = (DeviceManager)resolver.adaptTo(DeviceManager.class);
try {
deviceManager.deleteDevice(device.getId());
}
catch (RepositoryException e) {
throw new PersistenceException("Unable to delete device " + device.getId());
}
return;
}
throw new PersistenceException("Unable to delete resource at " + path);
}
String map(String resourcePath) {
String path = resourcePath.replace("/content/screens", this.apiContextPath);
if (path != null && path.matches(".*?/jcr:content/(renditions|comments|folderThumbnail).*")) {
path = path.replaceFirst("jcr:content/(renditions|comments|folderThumbnail)", "$1");
}
return path;
}
String unmap(String apiPath) {
String path = apiPath.replace(this.apiContextPath, "/content/screens");
if (path != null && path.matches(".*?/(renditions|comments|folderThumbnail).*")) {
return path.replaceFirst("(renditions|comments|folderThumbnail)", "jcr:content/$1");
}
return path;
}
}