RegistrationInfoHelper.java
3.19 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
/*************************************************************************
*
* ADOBE CONFIDENTIAL
* __________________
*
* Copyright 2015 Adobe Systems Incorporated
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe Systems Incorporated and its suppliers,
* if any. The intellectual and technical concepts contained
* herein are proprietary to Adobe Systems Incorporated and its
* suppliers and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe Systems Incorporated.
**************************************************************************/
package libs.screens.dcc.components.registrationcheck;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.jackrabbit.util.Text;
import org.apache.sling.api.request.RequestParameter;
import org.apache.sling.commons.json.JSONObject;
import org.apache.sling.tenant.Tenant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.adobe.cq.screens.device.registration.PendingDevice;
import com.adobe.cq.screens.device.registration.RegistrationService;
import com.adobe.cq.sightly.WCMUsePojo;
public class RegistrationInfoHelper extends WCMUsePojo {
/**
* default logger
*/
private static final Logger log = LoggerFactory.getLogger(RegistrationInfoHelper.class);
private String registrationCode;
private Map<String, String> attributes;
@Override
public void activate() throws Exception {
final RegistrationService registrationService = getSlingScriptHelper().getService(RegistrationService.class);
final RequestParameter itemParameter = getRequest().getRequestParameter("item");
final String deviceToken = itemParameter != null ? itemParameter.getString() : null;
if (deviceToken == null) {
log.warn("Unable to get registered device. deviceToken is missing as request parameter.");
return;
}
PendingDevice pendingDevice = registrationService.getDevice(deviceToken);
if (pendingDevice == null) {
log.warn("Unable to get registered device for {}. no such device.", deviceToken);
return;
}
registrationCode = pendingDevice.getRegistrationCode();
attributes = new HashMap<String, String>();
JSONObject metadata = pendingDevice.getMetadata();
Iterator<String> iter = metadata.keys();
while(iter.hasNext()) {
String key = iter.next();
attributes.put("data-" + key, metadata.getString(key));
}
}
public String getTenant() {
Tenant tenant = getResourceResolver().adaptTo(Tenant.class);
if (tenant != null) {
return tenant.getName();
}
String tenantRoot = Text.getAbsoluteParent(getRequest().getRequestPathInfo().getSuffix(), 2);
return tenantRoot.length() > 0 ? Text.getName(tenantRoot) : null;
}
public String getRegistrationCode() {
return registrationCode;
}
public Map<String, String> getAttributes() {
return attributes;
}
}