protected void doService(ToolPageContext page) throws IOException, ServletException {
UUID id = page.param(UUID.class, "id");
if (id == null) {
ToolUser user = page.getUser();
ToolUserDevice device = user.findRecentDevice();
if (device == null) {
device = user.findOrCreateCurrentDevice(page.getRequest());
}
id = device.getOrCreateLookingGlassId();
page.redirect("", "id", id);
return;
}
ToolUserDevice device = Query.
from(ToolUserDevice.class).
where("lookingGlassId = ?", id).
first();
if (device == null) {
throw new IllegalArgumentException(String.format(
"No looking glass at [%s]!", id));
}
ToolUserAction lastAction = Query.
from(ToolUserAction.class).
where("device = ?", device).
sortDescending("time").
noCache().
first();
if (lastAction != null &&
"ping".equals(page.param(String.class, "action"))) {
long end = System.currentTimeMillis() + 30000;
while (System.currentTimeMillis() < end &&
lastAction.getTime() == page.param(long.class, "time")) {
try {
Thread.sleep(500);
} catch (InterruptedException error) {
break;
}
lastAction = Query.
from(ToolUserAction.class).
where("device = ?", device).
sortDescending("time").
noCache().
first();
}
Map<String, Object> response = new HashMap<String, Object>();
response.put("changed", lastAction == null || lastAction.getTime() != page.param(long.class, "time"));
page.getResponse().setContentType("application/json");
page.writeRaw(ObjectUtils.toJson(response));
return;
}
ToolUser user = device.getUser();
page.writeHeader();
page.writeStart("div", "class", "message message-info");
page.writeHtml("Mirroring ");
page.writeObjectLabel(user);
page.writeHtml(" in ");
page.writeHtml(device.getUserAgentDisplay());
if (lastAction != null) {
Object lastActionContent = lastAction.getContent();
if (lastActionContent != null) {