String lDataType = aToken.getString("datatype");
String lValue = aToken.getString("value");
if (lType != null && (lNS == null || lNS.equals(NS_SHARED_OBJECTS))) {
Token lResponse = getServer().createResponse(aToken);
// create
if (lType.equals("create")) {
if (log.isDebugEnabled()) {
log.debug("Processing 'create'...");
}
if (!isDataTypeValid(lDataType, aConnector, lResponse)) {
return;
}
if (alreadyExists(lID, aConnector, lResponse)) {
return;
}
sharedObjects.put(lID, string2Object(lDataType, lValue));
Token lBCT = new Token(lNS, "event");
lBCT.put("name", "created");
lBCT.put("id", lID);
lBCT.put("datatype", lDataType);
lBCT.put("value", lValue);
getServer().broadcastToken(aConnector, lBCT);
// destroy
} else if (lType.equals("destroy")) {
if (log.isDebugEnabled()) {
log.debug("Processing 'destroy'...");
}
if (!doesContain(lID, aConnector, lResponse)) {
return;
}
sharedObjects.remove(lID);
Token lBCT = new Token(lNS, "event");
lBCT.put("name", "destroyed");
lBCT.put("id", lID);
getServer().broadcastToken(aConnector, lBCT);
// get
} else if (lType.equals("get")) {
if (log.isDebugEnabled()) {
log.debug("Processing 'get'...");
}
if (!doesContain(lID, aConnector, lResponse)) {
return;
}
Object lObj = sharedObjects.get(lID);
lResponse.put("id", lID);
lResponse.put("result", lObj.toString());
// put
} else if (lType.equals("update")) {
if (log.isDebugEnabled()) {
log.debug("Processing 'update'...");
}
if (!isDataTypeValid(lDataType, aConnector, lResponse)) {
return;
}
sharedObjects.put(lID, string2Object(lDataType, lValue));
Token lBCT = new Token(lNS, "event");
lBCT.put("name", "updated");
lBCT.put("id", lID);
lBCT.put("datatype", lDataType);
lBCT.put("value", lValue);
getServer().broadcastToken(aConnector, lBCT);
// init
} else if (lType.equals("init")) {
if (log.isDebugEnabled()) {
log.debug("Processing 'init'...");
}
Token lBCT = new Token(lNS, "event");
lBCT.put("name", "init");
String lData = null;
try {
JSONStringer jsonStringer = new JSONStringer();
// start main object
jsonStringer.object();
// iterate through all items (fields) of the token
Iterator<String> lIterator = sharedObjects.getKeys().iterator();
while (lIterator.hasNext()) {
String lKey = lIterator.next();
Object lVal = sharedObjects.get(lKey);
if (lVal instanceof Collection) {
jsonStringer.key(lKey).array();
for (Object item : (Collection) lVal) {
jsonStringer.value(item);
}
jsonStringer.endArray();
} else {
jsonStringer.key(lKey).value(lVal);
}
}
// end main object
jsonStringer.endObject();
lData = jsonStringer.toString();
} catch (JSONException ex) {
log.error(ex.getClass().getSimpleName() + ": " + ex.getMessage());
}
lBCT.put("value", lData);
getServer().sendToken(aConnector, lBCT);
} else {
log.warn("Invalid command " + lType + " received...");
lResponse.put("code", -1);