Add deployment for ADML agent with http interface
[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   // Operation:
37   std::string response_content;
38
39   bool opOk{};
40
41   try {
42     Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
43     opOk = my_app.eventOperation(body_content, response_content);
44   } catch(RuntimeException &ex) {
45     ex.trace();
46     opOk = false;
47   }
48
49   anna::http::Response* response = allocateResponse();
50   response->setStatusCode(200);  // http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
51
52 // EXAMPLES TO SET HEADERS:
53 //   response->find(anna::http::Header::Type::Date)->setValue("Mon, 30 Jan 2006 14:36:18 GMT");
54 //   anna::http::Header* keepAlive = response->find("Keep-Alive");
55 //
56 //   if (keepAlive == NULL)
57 //      keepAlive = response->createHeader("Keep-Alive");
58 //
59 //   keepAlive->setValue("Verificacion del cambio 1.0.7");
60
61   // Content-Type header:
62   anna::http::Header* contentType = response->find("Content-Type");
63   if (contentType == NULL)
64     contentType = response->createHeader("Content-Type");
65   contentType->setValue("application/json");
66
67   // Json Body for response:
68   std::stringstream ss;
69
70   ss << R"({ "result":")" << (opOk ? "true":"false") << R"(", "data": )" << std::quoted(response_content) << R"( })";
71   anna::DataBlock db_content(true);
72   db_content = ss.str();
73   response->setBody(db_content);
74
75   try {
76     clientSocket.send(*response);
77   } catch(Exception& ex) {
78     ex.trace();
79   }
80 }