@Context final HttpServletRequest request) {
String response = new ResponseTemplate().execute(logger, connectionId, request, new ResponseCallback() {
public Object execute() throws Exception {
DocumentService documentService = new DocumentServiceImpl(connectionId);
JSONObject resultJSON = new JSONObject();
String result = null;
RequestMethod method = null;
for (RequestMethod m : RequestMethod.values()) {
if ((m.toString()).equals(action)) {
method = m;
break;
}
}
switch (method) {
case PUT: {
if ("".equals(documentData)) {
ApplicationException e = new DocumentException(ErrorCodes.DOCUMENT_DOES_NOT_EXIST, "Document Data Missing in Request Body");
result = formErrorResponse(logger, e);
} else {
DBObject document = (DBObject) JSON.parse(documentData);
result = documentService.insertDocument(dbName, collectionName, document);
}
break;
}
case DELETE: {
if ("".equals(_id)) {
ApplicationException e = new DocumentException(ErrorCodes.DOCUMENT_DOES_NOT_EXIST, "Document Data Missing in Request Body");
result = formErrorResponse(logger, e);
} else {
result = documentService.deleteDocument(dbName, collectionName, _id);
}
break;
}
case POST: {
if ("".equals(_id) || "".equals(keys)) {
ApplicationException e = new DocumentException(ErrorCodes.DOCUMENT_DOES_NOT_EXIST, "Document Data Missing in Request Body");
formErrorResponse(logger, e);
} else {
// New Document Keys
DBObject newDoc = (DBObject) JSON.parse(keys);
result = documentService.updateDocument(dbName, collectionName, _id, newDoc);
Set<String> completeSet = new HashSet<String>();
getNestedKeys(newDoc, completeSet, "");
completeSet.remove("_id");
resultJSON.put("keys", completeSet);
}