DeviceStatusServlet.java
6.5 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:
* javax.servlet.Servlet
* javax.servlet.ServletException
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.SlingHttpServletResponse
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.servlets.SlingAllMethodsServlet
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.io.JSONWriter
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.screens.impl;
import com.adobe.cq.screens.DeviceMessageQueue;
import com.adobe.cq.screens.device.Device;
import com.adobe.cq.screens.device.DeviceConfig;
import com.adobe.cq.screens.device.DeviceManager;
import com.adobe.cq.screens.device.DeviceStatus;
import com.adobe.cq.screens.device.impl.DeviceService;
import com.adobe.cq.screens.impl.DeviceStatusWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.io.JSONWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=1)
@Service(value={Servlet.class})
@Properties(value={@Property(name="sling.servlet.resourceTypes", value={"screens/core/components/device", "screens/core/components/deviceconfig", "screens/core/components/display"}), @Property(name="sling.servlet.selectors", value={"ping"}), @Property(name="sling.servlet.extensions", value={"json"}), @Property(name="sling.servlet.methods", value={"GET", "POST"})})
public class DeviceStatusServlet
extends SlingAllMethodsServlet {
private final Logger log = LoggerFactory.getLogger(DeviceStatusServlet.class);
private static final String HEADER_SET_HEARTBEAT = "X-SET-HEARTBEAT";
private static final String HEADER_REQUEST_COMMAND = "X-REQUEST-COMMAND";
@Reference
private DeviceMessageQueue queue = null;
@Reference
private DeviceService deviceSvc = null;
private void handleRequest(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
Device device;
Resource resource = request.getResource();
ResourceResolver resolver = request.getResourceResolver();
DeviceManager dm = (DeviceManager)resolver.adaptTo(DeviceManager.class);
DeviceConfig config = (DeviceConfig)resource.adaptTo(DeviceConfig.class);
if (config != null) {
String deviceId = config.getAssignedDeviceId();
device = dm.getDevice(deviceId);
} else {
device = (Device)resource.adaptTo(Device.class);
DeviceConfig deviceConfig = config = device == null ? null : device.getDeviceConfig();
}
if (device != null || config != null) {
DeviceStatus status = null;
if (device != null) {
status = dm.getDeviceStatus(device.getId());
if (status == null) {
response.setStatus(404);
return;
}
String version = request.getParameter("v");
if (version == null || version.length() == 0) {
version = "n/a";
}
long timestamp = -1;
try {
timestamp = Long.parseLong(request.getParameter("t"));
}
catch (NumberFormatException e) {
// empty catch block
}
this.deviceSvc.updateFirmwareVersion(resolver, device.getId(), version, timestamp);
String heartbeat = request.getHeader("X-SET-HEARTBEAT");
if (heartbeat != null) {
this.deviceSvc.updatePing(resolver, device.getId());
}
}
try {
response.setContentType("application/json");
DeviceStatusWriter w = new DeviceStatusWriter(resolver, new JSONWriter((Writer)response.getWriter()));
if (request.getHeader("X-REQUEST-COMMAND") != null) {
w.setMessageQueue(this.queue);
}
w.writeDevice(device, status, config);
}
catch (JSONException jex) {
this.log.error("Error creating JSON output for device", (Throwable)jex);
}
} else if (resolver.isResourceType(resource, "screens/core/components/display")) {
response.setContentType("application/json");
try {
DeviceStatusWriter w = new DeviceStatusWriter(resolver, new JSONWriter((Writer)response.getWriter()));
w.writeDisplay(resource);
}
catch (JSONException jex) {
this.log.error("Error creating JSON output for device", (Throwable)jex);
}
}
}
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
this.handleRequest(request, response);
}
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
this.handleRequest(request, response);
}
protected void bindQueue(DeviceMessageQueue deviceMessageQueue) {
this.queue = deviceMessageQueue;
}
protected void unbindQueue(DeviceMessageQueue deviceMessageQueue) {
if (this.queue == deviceMessageQueue) {
this.queue = null;
}
}
protected void bindDeviceSvc(DeviceService deviceService) {
this.deviceSvc = deviceService;
}
protected void unbindDeviceSvc(DeviceService deviceService) {
if (this.deviceSvc == deviceService) {
this.deviceSvc = null;
}
}
}