comparison OrthancStone/Sources/Oracle/HttpCommand.h @ 1512:244ad1e4e76a

reorganization of folders
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 07 Jul 2020 16:21:02 +0200
parents Framework/Oracle/HttpCommand.h@30deba7bc8e2
children 85e117739eca
comparison
equal deleted inserted replaced
1511:9dfeee74c1e6 1512:244ad1e4e76a
1 /**
2 * Stone of Orthanc
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU Affero General Public License
9 * as published by the Free Software Foundation, either version 3 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/
20
21
22 #pragma once
23
24 #include "../Messages/IMessage.h"
25 #include "OracleCommandBase.h"
26
27 #include <Enumerations.h>
28
29 #include <map>
30 #include <json/value.h>
31
32 namespace OrthancStone
33 {
34 class HttpCommand : public OracleCommandBase
35 {
36 public:
37 typedef std::map<std::string, std::string> HttpHeaders;
38
39 class SuccessMessage : public OriginMessage<HttpCommand>
40 {
41 ORTHANC_STONE_MESSAGE(__FILE__, __LINE__);
42
43 private:
44 const HttpHeaders& headers_;
45 const std::string& answer_;
46
47 public:
48 SuccessMessage(const HttpCommand& command,
49 const HttpHeaders& answerHeaders,
50 const std::string& answer) :
51 OriginMessage(command),
52 headers_(answerHeaders),
53 answer_(answer)
54 {
55 }
56
57 const std::string& GetAnswer() const
58 {
59 return answer_;
60 }
61
62 void ParseJsonBody(Json::Value& target) const;
63
64 const HttpHeaders& GetAnswerHeaders() const
65 {
66 return headers_;
67 }
68 };
69
70
71 private:
72 Orthanc::HttpMethod method_;
73 std::string url_;
74 std::string body_;
75 HttpHeaders headers_;
76 unsigned int timeout_;
77 std::string username_;
78 std::string password_;
79
80 HttpCommand(const HttpCommand& other) :
81 method_(other.method_),
82 url_(other.url_),
83 body_(other.body_),
84 headers_(other.headers_),
85 timeout_(other.timeout_),
86 username_(other.username_),
87 password_(other.password_)
88 {
89 }
90
91 public:
92 HttpCommand();
93
94 virtual Type GetType() const
95 {
96 return Type_Http;
97 }
98
99 virtual IOracleCommand* Clone() const
100 {
101 return new HttpCommand(*this);
102 }
103
104 void SetMethod(Orthanc::HttpMethod method)
105 {
106 method_ = method;
107 }
108
109 void SetUrl(const std::string& url)
110 {
111 url_ = url;
112 }
113
114 void SetBody(const std::string& body)
115 {
116 body_ = body;
117 }
118
119 void SetBody(const Json::Value& json);
120
121 void SwapBody(std::string& body)
122 {
123 body_.swap(body);
124 }
125
126 void SetHttpHeaders(const HttpHeaders& headers)
127 {
128 headers_ = headers;
129 }
130
131 void SetHttpHeader(const std::string& key,
132 const std::string& value)
133 {
134 headers_[key] = value;
135 }
136
137 Orthanc::HttpMethod GetMethod() const
138 {
139 return method_;
140 }
141
142 const std::string& GetUrl() const
143 {
144 return url_;
145 }
146
147 const std::string& GetBody() const;
148
149 const HttpHeaders& GetHttpHeaders() const
150 {
151 return headers_;
152 }
153
154 void SetTimeout(unsigned int seconds)
155 {
156 timeout_ = seconds;
157 }
158
159 unsigned int GetTimeout() const
160 {
161 return timeout_;
162 }
163
164 void SetCredentials(const std::string& username,
165 const std::string& password)
166 {
167 username_ = username;
168 password_ = password;
169 }
170
171 void ClearCredentials()
172 {
173 username_.clear();
174 password_.clear();
175 }
176
177 bool HasCredentials() const
178 {
179 return !username_.empty();
180 }
181
182 const std::string& GetUsername() const;
183
184 const std::string& GetPassword() const;
185 };
186 }