DeviceRegistrationServlet.java
5.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
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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.annotation.Nonnull
* javax.servlet.ServletException
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.sling.SlingServlet
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.SlingHttpServletResponse
* org.apache.sling.api.servlets.SlingAllMethodsServlet
* org.apache.sling.commons.json.JSONObject
* org.apache.sling.commons.json.io.JSONWriter
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.screens.device.registration.impl;
import com.adobe.cq.screens.device.registration.PendingDevice;
import com.adobe.cq.screens.device.registration.RegistrationResult;
import com.adobe.cq.screens.device.registration.impl.PendingDeviceImpl;
import com.adobe.cq.screens.device.registration.impl.RegistrationManager;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import javax.annotation.Nonnull;
import javax.servlet.ServletException;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.commons.json.JSONObject;
import org.apache.sling.commons.json.io.JSONWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@SlingServlet(paths={"/bin/screens/registration"}, methods={"GET", "POST"})
public class DeviceRegistrationServlet
extends SlingAllMethodsServlet {
private final Logger log = LoggerFactory.getLogger(DeviceRegistrationServlet.class);
@Reference
RegistrationManager service;
protected void doPost(@Nonnull SlingHttpServletRequest request, @Nonnull SlingHttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/json");
try {
JSONWriter out = new JSONWriter((Writer)response.getWriter());
out.object();
String token = request.getParameter("id");
if (token != null && token.length() > 0) {
PendingDeviceImpl device = (PendingDeviceImpl)this.service.getDevice(token);
if (device != null) {
switch (device.getState()) {
case UNREGISTERED: {
out.key("message").value((Object)"wait for pre-registration phase...");
break;
}
case PREREGISTRATION: {
String deviceCode = request.getParameter("code");
if (deviceCode != null) {
device.setRegistrationCode(deviceCode).setState(PendingDevice.State.REGISTRATION);
out.key("message").value((Object)"device code received. wait for registration phase...");
break;
}
out.key("message").value((Object)"provide your device code...");
break;
}
case REGISTRATION: {
out.key("message").value((Object)"registration in progress...");
break;
}
case REGISTERED: {
this.service.removeDevice(token);
RegistrationResult result = device.getRegistrationResult();
if (result != null) {
out.key("message").value((Object)"you have been registered");
out.key("info").value((Object)result);
break;
}
out.key("message").value((Object)"you have been registered but no data has been provided");
break;
}
}
} else {
device = new PendingDeviceImpl(token);
String metadata = request.getParameter("metadata");
if (metadata != null) {
device.setMetadata(new JSONObject(metadata));
}
this.service.addDevice(device);
out.key("message").value((Object)"you entered in registration process. wait for pre-registration phase...");
}
out.key("status").value((Object)device.getState());
}
out.endObject();
}
catch (Exception jex) {
this.log.error("Error during device registration", (Throwable)jex);
}
}
protected void bindService(RegistrationManager registrationManager) {
this.service = registrationManager;
}
protected void unbindService(RegistrationManager registrationManager) {
if (this.service == registrationManager) {
this.service = null;
}
}
}