Remove operation help.
[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 #include <EventOperation.hpp>
21
22
23 void MyHandler::evRequest(anna::comm::ClientSocket& clientSocket, const anna::http::Request& request)
24 throw(anna::RuntimeException) {
25   const anna::DataBlock& body = request.getBody();
26
27   if(body.getSize() == 0)
28     throw anna::RuntimeException("Missing operation parameters on HTTP request", ANNA_FILE_LOCATION);
29
30   LOGINFORMATION(
31     std::string msg("Received body: ");
32     msg += anna::functions::asString(body);
33     anna::Logger::information(msg, ANNA_FILE_LOCATION);
34   );
35   std::string body_content;
36   body_content.assign(body.getData(), body.getSize());
37   auto json_body = nlohmann::json::parse(body_content);
38
39   // Operation:
40   std::string response_content;
41
42   bool opOk{};
43
44   try {
45     Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
46     opOk = my_app.eventOperation(EventOperation::json2piped(json_body), response_content);
47   } catch(RuntimeException &ex) {
48     ex.trace();
49     opOk = false;
50   }
51
52   anna::http::Response* response = allocateResponse();
53   response->setStatusCode(200);  // http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
54
55 // EXAMPLES TO SET HEADERS:
56 //   response->find(anna::http::Header::Type::Date)->setValue("Mon, 30 Jan 2006 14:36:18 GMT");
57 //   anna::http::Header* keepAlive = response->find("Keep-Alive");
58 //
59 //   if (keepAlive == NULL)
60 //      keepAlive = response->createHeader("Keep-Alive");
61 //
62 //   keepAlive->setValue("Verificacion del cambio 1.0.7");
63
64   // Content-Type header:
65   anna::http::Header* contentType = response->find("Content-Type");
66   if (contentType == NULL)
67     contentType = response->createHeader("Content-Type");
68   contentType->setValue("application/json");
69
70   // Json Body for response:
71   std::stringstream ss;
72
73   ss << R"({ "result":")" << (opOk ? "true":"false") << R"(", "data": )" << std::quoted(response_content) << R"( })";
74   anna::DataBlock db_content(true);
75   db_content = ss.str();
76   response->setBody(db_content);
77
78   try {
79     clientSocket.send(*response);
80   } catch(Exception& ex) {
81     ex.trace();
82   }
83 }