Add nlohmann/json parser
[anna.git] / example / diameter / launcher / MyHandler.cpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite                           //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7
8
9 // Standard
10 #include <string>
11 #include <sstream>
12 #include <iomanip>
13
14 // Project
15 #include <anna/http/Request.hpp>
16
17 // Process
18 #include <MyHandler.hpp>
19 #include <Launcher.hpp>
20
21
22 void MyHandler::evRequest(anna::comm::ClientSocket& clientSocket, const anna::http::Request& request)
23 throw(anna::RuntimeException) {
24   const anna::DataBlock& body = request.getBody();
25
26   if(body.getSize() == 0)
27     throw anna::RuntimeException("Missing operation parameters on HTTP request", ANNA_FILE_LOCATION);
28
29   LOGINFORMATION(
30     std::string msg("Received body: ");
31     msg += anna::functions::asString(body);
32     anna::Logger::information(msg, ANNA_FILE_LOCATION);
33   );
34   std::string body_content;
35   body_content.assign(body.getData(), body.getSize());
36
37   // Operation:
38   std::string response_content;
39
40   bool opOk{};
41
42   try {
43     Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
44     opOk = my_app.eventOperation(body_content, response_content);
45   } catch(RuntimeException &ex) {
46     ex.trace();
47     opOk = false;
48   }
49
50   anna::http::Response* response = allocateResponse();
51   response->setStatusCode(200);  // http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
52
53 // EXAMPLES TO SET HEADERS:
54 //   response->find(anna::http::Header::Type::Date)->setValue("Mon, 30 Jan 2006 14:36:18 GMT");
55 //   anna::http::Header* keepAlive = response->find("Keep-Alive");
56 //
57 //   if (keepAlive == NULL)
58 //      keepAlive = response->createHeader("Keep-Alive");
59 //
60 //   keepAlive->setValue("Verificacion del cambio 1.0.7");
61
62   // Content-Type header:
63   anna::http::Header* contentType = response->find("Content-Type");
64   if (contentType == NULL)
65     contentType = response->createHeader("Content-Type");
66   contentType->setValue("application/json");
67
68   // Json Body for response:
69   std::stringstream ss;
70
71   ss << R"({ "result":")" << (opOk ? "true":"false") << R"(", "data": )" << std::quoted(response_content) << R"( })";
72   anna::DataBlock db_content(true);
73   db_content = ss.str();
74   response->setBody(db_content);
75
76   try {
77     clientSocket.send(*response);
78   } catch(Exception& ex) {
79     ex.trace();
80   }
81 }