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