Salome HOME
Fix renaming of Group as UserGroup
[modules/gde.git] / projects / GDE_API_CPP / api / src / HttpConnection.cpp
1 #include "HttpConnection.hpp"
2 #include "JsonFormatter.hpp"
3
4 #include <iostream>
5 #include <fstream>
6 #include <sstream>
7
8 #include <Poco/StreamCopier.h>
9 #include <Poco/Net/HTTPResponse.h>
10 #include <Poco/Base64Encoder.h>
11
12 using namespace Poco::Net;
13
14 gde::HttpConnection::HttpConnection(Poco::URI uri, const Credentials& cred)
15   : _path("/"), _credentials(cred)
16 {
17   try {
18     _session.setHost(uri.getHost());
19     _session.setPort(uri.getPort());
20     //_session.setProxy(machine, port);
21   } catch (Poco::Exception& e) {
22     std::cerr << e.displayText() << std::endl;
23     return;
24   }
25
26   _path = uri.getPathAndQuery();
27   if (_path.empty())
28     _path = "/";
29 }
30
31 gde::HttpConnection::~HttpConnection()
32 {
33 }
34
35 // Workflow:
36 // 1. request (json) --> CommandTO object
37 // 2. command data (json) from CommandTO --> <something>TO
38 // 3. call DAO with <something>TO and obtain another <something>TO
39 // 4. latter <something>TO --> (json) CommandResultTO data
40 // 5. set CommandResultTO code
41 // 6. CommandResultTO --> (json) response
42
43 gde::CommandResultTO
44 gde::HttpConnection::_processRequest(const std::string& method,
45                                      const CommandTO& cto)
46 {
47   int code = CommandResultTO::OK;
48   std::string message = "";
49   std::string data = "";
50
51   std::string requestBody = cto.toJson();
52
53   try {
54     HTTPRequest request(method, _path, HTTPMessage::HTTP_1_1);
55
56     // Authenticating
57     std::stringstream ss;
58     Poco::Base64Encoder b64enc(ss);
59     b64enc << _credentials.getLogin() << ":" << _credentials.getPassword(); // user::password
60     b64enc.close();
61     request.set("Authorization" , "Basic "+ss.str());
62
63     // Sending request
64     request.setContentLength(requestBody.length());
65     std::ostream& os = _session.sendRequest((HTTPRequest&)request);
66     // Send request body, if any
67     if (!requestBody.empty()) {
68       //std::istringstream iss(requestBody);
69       //Poco::StreamCopier::copyStream((std::istream&)iss, os);
70       os << requestBody;
71     }
72
73     // Get response
74     HTTPResponse response;
75     std::istream& rs = _session.receiveResponse(response);
76     //std::cout << response.getStatus() << " " << response.getReason() << std::endl;
77     std::ostringstream responseBody;
78     Poco::StreamCopier::copyStream(rs, responseBody);
79     // responseBody contains a CommandResultTO in json format
80     return CommandResultTO::fromJson(responseBody.str());
81   } catch (Poco::Exception& e) {
82     std::cerr << e.displayText() << std::endl;
83     return CommandResultTO(CommandResultTO::ERROR);
84   }
85 }
86
87 gde::CommandResultTO
88 gde::HttpConnection::doGet(const CommandTO& cto)
89 {
90   return _processRequest(HTTPRequest::HTTP_GET, cto);
91 }
92
93 gde::CommandResultTO
94 gde::HttpConnection::doPost(const CommandTO& cto)
95 {
96   return _processRequest(HTTPRequest::HTTP_POST, cto);
97 }