Mercurial > hg > orthanc-stone
annotate Framework/Oracle/HttpCommand.h @ 1205:6009c59d8676 broker
fix to sdl
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 02 Dec 2019 14:32:05 +0100 |
parents | a0a33e5ea5bb |
children | 0ca50d275b9a |
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 | |
5 * Copyright (C) 2017-2019 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" | |
1128
8e3763d1736a
removing CustomOracleCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1079
diff
changeset
|
25 #include "OracleCommandBase.h" |
979 | 26 |
27 #include <Core/Enumerations.h> | |
28 | |
29 #include <map> | |
30 #include <json/value.h> | |
31 | |
32 namespace OrthancStone | |
33 { | |
1128
8e3763d1736a
removing CustomOracleCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1079
diff
changeset
|
34 class HttpCommand : public OracleCommandBase |
979 | 35 { |
36 public: | |
37 typedef std::map<std::string, std::string> HttpHeaders; | |
38 | |
1135
a0a33e5ea5bb
IOracleCommand::Clone()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1134
diff
changeset
|
39 class SuccessMessage : public OriginMessage<HttpCommand> |
979 | 40 { |
41 ORTHANC_STONE_MESSAGE(__FILE__, __LINE__); | |
42 | |
43 private: | |
1134
87fbeb823375
allocating messages from oracle commands on the stack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1128
diff
changeset
|
44 const HttpHeaders& headers_; |
87fbeb823375
allocating messages from oracle commands on the stack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1128
diff
changeset
|
45 const std::string& answer_; |
979 | 46 |
47 public: | |
1135
a0a33e5ea5bb
IOracleCommand::Clone()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1134
diff
changeset
|
48 SuccessMessage(const HttpCommand& command, |
979 | 49 const HttpHeaders& answerHeaders, |
1135
a0a33e5ea5bb
IOracleCommand::Clone()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1134
diff
changeset
|
50 const std::string& answer) : |
a0a33e5ea5bb
IOracleCommand::Clone()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1134
diff
changeset
|
51 OriginMessage(command), |
a0a33e5ea5bb
IOracleCommand::Clone()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1134
diff
changeset
|
52 headers_(answerHeaders), |
a0a33e5ea5bb
IOracleCommand::Clone()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1134
diff
changeset
|
53 answer_(answer) |
a0a33e5ea5bb
IOracleCommand::Clone()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1134
diff
changeset
|
54 { |
a0a33e5ea5bb
IOracleCommand::Clone()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1134
diff
changeset
|
55 } |
979 | 56 |
57 const std::string& GetAnswer() const | |
58 { | |
59 return answer_; | |
60 } | |
61 | |
62 void ParseJsonBody(Json::Value& target) const; | |
63 | |
1134
87fbeb823375
allocating messages from oracle commands on the stack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1128
diff
changeset
|
64 const HttpHeaders& GetAnswerHeaders() const |
979 | 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_; | |
1079
e6d2ff8f1ab4
credentials in HttpCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
979
diff
changeset
|
77 std::string username_; |
e6d2ff8f1ab4
credentials in HttpCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
979
diff
changeset
|
78 std::string password_; |
979 | 79 |
1135
a0a33e5ea5bb
IOracleCommand::Clone()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1134
diff
changeset
|
80 HttpCommand(const HttpCommand& other) : |
a0a33e5ea5bb
IOracleCommand::Clone()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1134
diff
changeset
|
81 method_(other.method_), |
a0a33e5ea5bb
IOracleCommand::Clone()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1134
diff
changeset
|
82 url_(other.url_), |
a0a33e5ea5bb
IOracleCommand::Clone()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1134
diff
changeset
|
83 body_(other.body_), |
a0a33e5ea5bb
IOracleCommand::Clone()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1134
diff
changeset
|
84 headers_(other.headers_), |
a0a33e5ea5bb
IOracleCommand::Clone()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1134
diff
changeset
|
85 timeout_(other.timeout_), |
a0a33e5ea5bb
IOracleCommand::Clone()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1134
diff
changeset
|
86 username_(other.username_), |
a0a33e5ea5bb
IOracleCommand::Clone()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1134
diff
changeset
|
87 password_(other.password_) |
a0a33e5ea5bb
IOracleCommand::Clone()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1134
diff
changeset
|
88 { |
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 public: |
92 HttpCommand(); | |
93 | |
94 virtual Type GetType() const | |
95 { | |
96 return Type_Http; | |
97 } | |
98 | |
1135
a0a33e5ea5bb
IOracleCommand::Clone()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1134
diff
changeset
|
99 virtual IOracleCommand* Clone() const |
a0a33e5ea5bb
IOracleCommand::Clone()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1134
diff
changeset
|
100 { |
a0a33e5ea5bb
IOracleCommand::Clone()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1134
diff
changeset
|
101 return new HttpCommand(*this); |
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 |
979 | 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 } | |
1079
e6d2ff8f1ab4
credentials in HttpCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
979
diff
changeset
|
163 |
e6d2ff8f1ab4
credentials in HttpCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
979
diff
changeset
|
164 void SetCredentials(const std::string& username, |
e6d2ff8f1ab4
credentials in HttpCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
979
diff
changeset
|
165 const std::string& password) |
e6d2ff8f1ab4
credentials in HttpCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
979
diff
changeset
|
166 { |
e6d2ff8f1ab4
credentials in HttpCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
979
diff
changeset
|
167 username_ = username; |
e6d2ff8f1ab4
credentials in HttpCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
979
diff
changeset
|
168 password_ = password; |
e6d2ff8f1ab4
credentials in HttpCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
979
diff
changeset
|
169 } |
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 void ClearCredentials() |
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 username_.clear(); |
e6d2ff8f1ab4
credentials in HttpCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
979
diff
changeset
|
174 password_.clear(); |
e6d2ff8f1ab4
credentials in HttpCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
979
diff
changeset
|
175 } |
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 bool HasCredentials() const |
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 return !username_.empty(); |
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 |
e6d2ff8f1ab4
credentials in HttpCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
979
diff
changeset
|
182 const std::string& GetUsername() const; |
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& GetPassword() const; |
979 | 185 }; |
186 } |