Remove operation help.
[anna.git] / example / diameter / launcher / MyHandler.cpp
index f35c89f..6638fc3 100644 (file)
@@ -8,13 +8,16 @@
 
 // Standard
 #include <string>
+#include <sstream>
+#include <iomanip>
 
 // Project
 #include <anna/http/Request.hpp>
 
 // Process
-#include "MyHandler.hpp"
-#include "Launcher.hpp"
+#include <MyHandler.hpp>
+#include <Launcher.hpp>
+#include <EventOperation.hpp>
 
 
 void MyHandler::evRequest(anna::comm::ClientSocket& clientSocket, const anna::http::Request& request)
@@ -31,21 +34,25 @@ throw(anna::RuntimeException) {
   );
   std::string body_content;
   body_content.assign(body.getData(), body.getSize());
+  auto json_body = nlohmann::json::parse(body_content);
+
   // Operation:
   std::string response_content;
 
+  bool opOk{};
+
   try {
     Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
-    my_app.eventOperation(body_content, response_content);
+    opOk = my_app.eventOperation(EventOperation::json2piped(json_body), response_content);
   } catch(RuntimeException &ex) {
     ex.trace();
+    opOk = false;
   }
 
   anna::http::Response* response = allocateResponse();
   response->setStatusCode(200);  // http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
-  anna::DataBlock db_content(true);
-  db_content = response_content;
-  response->setBody(db_content);
+
+// EXAMPLES TO SET HEADERS:
 //   response->find(anna::http::Header::Type::Date)->setValue("Mon, 30 Jan 2006 14:36:18 GMT");
 //   anna::http::Header* keepAlive = response->find("Keep-Alive");
 //
@@ -54,6 +61,20 @@ throw(anna::RuntimeException) {
 //
 //   keepAlive->setValue("Verificacion del cambio 1.0.7");
 
+  // Content-Type header:
+  anna::http::Header* contentType = response->find("Content-Type");
+  if (contentType == NULL)
+    contentType = response->createHeader("Content-Type");
+  contentType->setValue("application/json");
+
+  // Json Body for response:
+  std::stringstream ss;
+
+  ss << R"({ "result":")" << (opOk ? "true":"false") << R"(", "data": )" << std::quoted(response_content) << R"( })";
+  anna::DataBlock db_content(true);
+  db_content = ss.str();
+  response->setBody(db_content);
+
   try {
     clientSocket.send(*response);
   } catch(Exception& ex) {