this.session = null;
}
- private void reply(Object json) {
- reply(json, true);
+ private void reply(Object json, String msgid) {
+ reply(json, msgid, true);
}
- private void reply(Object json, boolean success) {
+ private void reply(Object json, String msgid, boolean success) {
HashMap<String, Object> response = new HashMap<String, Object>();
+ response.put("msgid", msgid);
if (success) {
response.put("success", json);
} else {
try {
params = (HashMap<String,String>) JSON.parse(message);
} catch (ClassCastException e) {
- reply("Invalid WebSockets JSON message " + message, false);
+ reply("Invalid WebSockets JSON message " + message, "", false);
}
FileIO io;
+ String msgid = params.get("msgid");
String action = params.get("action");
String key = params.get("key");
String value = params.get("value");
String mime = params.get("mime");
+ // all requets require a message ID
+ if (msgid == null || msgid.equals("")) {
+ reply("No msgid specified in request", msgid, false);
+ return;
+ }
+
// all requests require an action
if (action == null || action.equals("")) {
- reply("No action specified in request", false);
+ reply("No action specified in request", msgid, false);
return;
}
io = new FileIO(profileDirectory);
String[] keys = io.keys(key); // OK for key to be null
if (keys != null) {
- reply(keys);
+ reply(keys, msgid);
} else {
- reply("key lookup error", false);
+ reply("key lookup error", msgid, false);
}
return;
}
if (action.equals("printers")) {
List printers = new PrintDriver().getPrinters();
- reply(printers);
+ reply(printers, msgid);
return;
}
// all remaining requests require a key
if (key == null || key.equals("")) {
- reply("No key specified in request", false);
+ reply("No key specified in request", msgid, false);
return;
}
// relay lines of text to the caller as we read them
// assume the text content is JSON and return it
// un-JSON-ified.
- reply(line);
+ reply(line, msgid);
}
} catch (IOException e) {
logger.warn(e);
}
} else {
- reply("Error accessing property " + key, false);
+ reply("Error accessing property " + key, msgid, false);
}
return;
}
if (action.equals("delete")) {
io = new FileIO(profileDirectory);
if (io.delete(key)) {
- reply("Delete of " + key + " successful");
+ reply("Delete of " + key + " successful", msgid);
} else {
- reply("Delete of " + key + " failed", false);
+ reply("Delete of " + key + " failed", msgid, false);
}
return;
}
// all remaining actions require value
if (value == null) {
- reply("No value specified in request", false);
+ reply("No value specified in request", msgid, false);
return;
}
case "print" :
boolean ok = new PrintDriver().printWithDialog(mime, value);
if (ok) {
- reply("print succeeded");
+ reply("print succeeded", msgid);
} else {
- reply("print failed", false);
+ reply("print failed", msgid, false);
}
break;
case "set" :
io = new FileIO(profileDirectory);
if (io.set(key, value)) {
- reply("setting value for " + key + " succeeded");
+ reply("setting value for " + key + " succeeded", msgid);
} else {
- reply("setting value for " + key + " succeeded", false);
+ reply("setting value for " + key + " succeeded", msgid, false);
}
break;
case "append" :
io = new FileIO(profileDirectory);
if (io.append(key, value)) {
- reply("appending value for " + key + " succeeded");
+ reply("appending value for " + key + " succeeded", msgid);
} else {
- reply("appending value for " + key + " succeeded", false);
+ reply("appending value for " + key + " succeeded", msgid, false);
}
break;
default:
- reply("No such action: " + action, false);
+ reply("No such action: " + action, msgid, false);
}
}
}