9f893a75d50e608f5cad1d7d68620e1d94f4ba1a
[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
12 // Project
13 #include <anna/http/Request.hpp>
14
15 // Process
16 #include <MyHandler.hpp>
17 #include <Launcher.hpp>
18
19
20 void MyHandler::evRequest(anna::comm::ClientSocket& clientSocket, const anna::http::Request& request)
21 throw(anna::RuntimeException) {
22   const anna::DataBlock& body = request.getBody();
23
24   if(body.getSize() == 0)
25     throw anna::RuntimeException("Missing operation parameters on HTTP request", ANNA_FILE_LOCATION);
26
27   LOGINFORMATION(
28     std::string msg("Received body: ");
29     msg += anna::functions::asString(body);
30     anna::Logger::information(msg, ANNA_FILE_LOCATION);
31   );
32   std::string body_content;
33   body_content.assign(body.getData(), body.getSize());
34   // Operation:
35   std::string response_content;
36
37   try {
38     Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
39     my_app.eventOperation(body_content, response_content);
40   } catch(RuntimeException &ex) {
41     ex.trace();
42   }
43
44   anna::http::Response* response = allocateResponse();
45   response->setStatusCode(200);  // http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
46   anna::DataBlock db_content(true);
47   db_content = response_content;
48   response->setBody(db_content);
49 //   response->find(anna::http::Header::Type::Date)->setValue("Mon, 30 Jan 2006 14:36:18 GMT");
50 //   anna::http::Header* keepAlive = response->find("Keep-Alive");
51 //
52 //   if (keepAlive == NULL)
53 //      keepAlive = response->createHeader("Keep-Alive");
54 //
55 //   keepAlive->setValue("Verificacion del cambio 1.0.7");
56
57   try {
58     clientSocket.send(*response);
59   } catch(Exception& ex) {
60     ex.trace();
61   }
62 }