Mercurial > hg > orthanc-stone
annotate OrthancStone/Sources/Oracle/HttpCommand.h @ 1750:5f74ebe2516b
fixed includes
author | Benjamin Golinvaux <bgo@osimis.io> |
---|---|
date | Mon, 22 Feb 2021 14:56:06 +0100 |
parents | 9ac2a65d4172 |
children | 3889ae96d2e9 |
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 | |
1739
9ac2a65d4172
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1598
diff
changeset
|
5 * Copyright (C) 2017-2021 Osimis S.A., Belgium |
979 | 6 * |
7 * 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
|
8 * modify it under the terms of the GNU Lesser General Public License |
979 | 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 | |
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
|
14 * 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
|
15 * Lesser General Public License for more details. |
1596
4fb8fdf03314
removed annoying whitespace
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1571
diff
changeset
|
16 * |
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
|
17 * 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
|
18 * 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
|
19 * <http://www.gnu.org/licenses/>. |
979 | 20 **/ |
21 | |
22 | |
23 #pragma once | |
24 | |
25 #include "../Messages/IMessage.h" | |
1128
8e3763d1736a
removing CustomOracleCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1079
diff
changeset
|
26 #include "OracleCommandBase.h" |
979 | 27 |
1455
30deba7bc8e2
simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1279
diff
changeset
|
28 #include <Enumerations.h> |
979 | 29 |
30 #include <map> | |
31 #include <json/value.h> | |
32 | |
33 namespace OrthancStone | |
34 { | |
1128
8e3763d1736a
removing CustomOracleCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1079
diff
changeset
|
35 class HttpCommand : public OracleCommandBase |
979 | 36 { |
37 public: | |
38 typedef std::map<std::string, std::string> HttpHeaders; | |
39 | |
40 class SuccessMessage : public OriginMessage<HttpCommand> | |
41 { | |
42 ORTHANC_STONE_MESSAGE(__FILE__, __LINE__); | |
43 | |
44 private: | |
1134
87fbeb823375
allocating messages from oracle commands on the stack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1128
diff
changeset
|
45 const HttpHeaders& headers_; |
87fbeb823375
allocating messages from oracle commands on the stack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1128
diff
changeset
|
46 const std::string& answer_; |
979 | 47 |
48 public: | |
49 SuccessMessage(const HttpCommand& command, | |
50 const HttpHeaders& answerHeaders, | |
1135
a0a33e5ea5bb
IOracleCommand::Clone()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1134
diff
changeset
|
51 const std::string& answer) : |
a0a33e5ea5bb
IOracleCommand::Clone()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1134
diff
changeset
|
52 OriginMessage(command), |
a0a33e5ea5bb
IOracleCommand::Clone()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1134
diff
changeset
|
53 headers_(answerHeaders), |
a0a33e5ea5bb
IOracleCommand::Clone()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1134
diff
changeset
|
54 answer_(answer) |
a0a33e5ea5bb
IOracleCommand::Clone()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1134
diff
changeset
|
55 { |
a0a33e5ea5bb
IOracleCommand::Clone()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1134
diff
changeset
|
56 } |
979 | 57 |
58 const std::string& GetAnswer() const | |
59 { | |
60 return answer_; | |
61 } | |
62 | |
63 void ParseJsonBody(Json::Value& target) const; | |
64 | |
1134
87fbeb823375
allocating messages from oracle commands on the stack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1128
diff
changeset
|
65 const HttpHeaders& GetAnswerHeaders() const |
979 | 66 { |
67 return headers_; | |
68 } | |
69 }; | |
70 | |
71 | |
72 private: | |
73 Orthanc::HttpMethod method_; | |
74 std::string url_; | |
75 std::string body_; | |
76 HttpHeaders headers_; | |
77 unsigned int timeout_; | |
1079
e6d2ff8f1ab4
credentials in HttpCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
979
diff
changeset
|
78 std::string username_; |
e6d2ff8f1ab4
credentials in HttpCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
979
diff
changeset
|
79 std::string password_; |
979 | 80 |
1135
a0a33e5ea5bb
IOracleCommand::Clone()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1134
diff
changeset
|
81 HttpCommand(const HttpCommand& other) : |
a0a33e5ea5bb
IOracleCommand::Clone()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1134
diff
changeset
|
82 method_(other.method_), |
a0a33e5ea5bb
IOracleCommand::Clone()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1134
diff
changeset
|
83 url_(other.url_), |
a0a33e5ea5bb
IOracleCommand::Clone()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1134
diff
changeset
|
84 body_(other.body_), |
a0a33e5ea5bb
IOracleCommand::Clone()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1134
diff
changeset
|
85 headers_(other.headers_), |
a0a33e5ea5bb
IOracleCommand::Clone()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1134
diff
changeset
|
86 timeout_(other.timeout_), |
a0a33e5ea5bb
IOracleCommand::Clone()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1134
diff
changeset
|
87 username_(other.username_), |
a0a33e5ea5bb
IOracleCommand::Clone()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1134
diff
changeset
|
88 password_(other.password_) |
a0a33e5ea5bb
IOracleCommand::Clone()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1134
diff
changeset
|
89 { |
a0a33e5ea5bb
IOracleCommand::Clone()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1134
diff
changeset
|
90 } |
979 | 91 |
92 public: | |
93 HttpCommand(); | |
94 | |
1571 | 95 virtual Type GetType() const ORTHANC_OVERRIDE |
979 | 96 { |
97 return Type_Http; | |
98 } | |
99 | |
1571 | 100 virtual IOracleCommand* Clone() const ORTHANC_OVERRIDE |
1135
a0a33e5ea5bb
IOracleCommand::Clone()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1134
diff
changeset
|
101 { |
a0a33e5ea5bb
IOracleCommand::Clone()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1134
diff
changeset
|
102 return new HttpCommand(*this); |
a0a33e5ea5bb
IOracleCommand::Clone()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1134
diff
changeset
|
103 } |
a0a33e5ea5bb
IOracleCommand::Clone()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1134
diff
changeset
|
104 |
979 | 105 void SetMethod(Orthanc::HttpMethod method) |
106 { | |
107 method_ = method; | |
108 } | |
109 | |
110 void SetUrl(const std::string& url) | |
111 { | |
112 url_ = url; | |
113 } | |
114 | |
115 void SetBody(const std::string& body) | |
116 { | |
117 body_ = body; | |
118 } | |
119 | |
120 void SetBody(const Json::Value& json); | |
121 | |
122 void SwapBody(std::string& body) | |
123 { | |
124 body_.swap(body); | |
125 } | |
126 | |
127 void SetHttpHeaders(const HttpHeaders& headers) | |
128 { | |
129 headers_ = headers; | |
130 } | |
131 | |
132 void SetHttpHeader(const std::string& key, | |
133 const std::string& value) | |
134 { | |
135 headers_[key] = value; | |
136 } | |
137 | |
138 Orthanc::HttpMethod GetMethod() const | |
139 { | |
140 return method_; | |
141 } | |
142 | |
143 const std::string& GetUrl() const | |
144 { | |
145 return url_; | |
146 } | |
147 | |
148 const std::string& GetBody() const; | |
149 | |
150 const HttpHeaders& GetHttpHeaders() const | |
151 { | |
152 return headers_; | |
153 } | |
154 | |
155 void SetTimeout(unsigned int seconds) | |
156 { | |
157 timeout_ = seconds; | |
158 } | |
159 | |
160 unsigned int GetTimeout() const | |
161 { | |
162 return timeout_; | |
163 } | |
1079
e6d2ff8f1ab4
credentials in HttpCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
979
diff
changeset
|
164 |
e6d2ff8f1ab4
credentials in HttpCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
979
diff
changeset
|
165 void SetCredentials(const std::string& username, |
e6d2ff8f1ab4
credentials in HttpCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
979
diff
changeset
|
166 const std::string& password) |
e6d2ff8f1ab4
credentials in HttpCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
979
diff
changeset
|
167 { |
e6d2ff8f1ab4
credentials in HttpCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
979
diff
changeset
|
168 username_ = username; |
e6d2ff8f1ab4
credentials in HttpCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
979
diff
changeset
|
169 password_ = password; |
e6d2ff8f1ab4
credentials in HttpCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
979
diff
changeset
|
170 } |
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 void ClearCredentials() |
e6d2ff8f1ab4
credentials in HttpCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
979
diff
changeset
|
173 { |
e6d2ff8f1ab4
credentials in HttpCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
979
diff
changeset
|
174 username_.clear(); |
e6d2ff8f1ab4
credentials in HttpCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
979
diff
changeset
|
175 password_.clear(); |
e6d2ff8f1ab4
credentials in HttpCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
979
diff
changeset
|
176 } |
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 bool HasCredentials() const |
e6d2ff8f1ab4
credentials in HttpCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
979
diff
changeset
|
179 { |
e6d2ff8f1ab4
credentials in HttpCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
979
diff
changeset
|
180 return !username_.empty(); |
e6d2ff8f1ab4
credentials in HttpCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
979
diff
changeset
|
181 } |
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 const std::string& GetUsername() const; |
e6d2ff8f1ab4
credentials in HttpCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
979
diff
changeset
|
184 |
e6d2ff8f1ab4
credentials in HttpCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
979
diff
changeset
|
185 const std::string& GetPassword() const; |
979 | 186 }; |
187 } |