comparison Framework/Messaging/PluginOrthancConnection.cpp @ 0:4a7a53257c7d

initial commit
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sat, 22 Oct 2016 21:48:33 +0200
parents
children 7a88c614be04
comparison
equal deleted inserted replaced
-1:000000000000 0:4a7a53257c7d
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 *
6 * This program is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU Affero General Public License
8 * as published by the Free Software Foundation, either version 3 of
9 * the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 **/
19
20
21 #include "PluginOrthancConnection.h"
22
23 #include "../Orthanc/Core/OrthancException.h"
24
25 namespace OrthancWSI
26 {
27 class PluginOrthancConnection::MemoryBuffer : public boost::noncopyable
28 {
29 private:
30 OrthancPluginContext* context_;
31 OrthancPluginMemoryBuffer buffer_;
32
33 void Clear()
34 {
35 if (buffer_.data != NULL)
36 {
37 OrthancPluginFreeMemoryBuffer(context_, &buffer_);
38 buffer_.data = NULL;
39 buffer_.size = 0;
40 }
41 }
42
43
44 public:
45 MemoryBuffer(OrthancPluginContext* context) :
46 context_(context)
47 {
48 buffer_.data = NULL;
49 buffer_.size = 0;
50 }
51
52 ~MemoryBuffer()
53 {
54 Clear();
55 }
56
57 void RestApiGet(const std::string& uri)
58 {
59 Clear();
60
61 OrthancPluginErrorCode error = OrthancPluginRestApiGet(context_, &buffer_, uri.c_str());
62
63 if (error == OrthancPluginErrorCode_Success)
64 {
65 // OK, success
66 }
67 else if (error == OrthancPluginErrorCode_UnknownResource ||
68 error == OrthancPluginErrorCode_InexistentItem)
69 {
70 throw Orthanc::OrthancException(Orthanc::ErrorCode_UnknownResource);
71 }
72 else
73 {
74 throw Orthanc::OrthancException(Orthanc::ErrorCode_Plugin);
75 }
76 }
77
78 void RestApiPost(const std::string& uri,
79 const std::string& body)
80 {
81 Clear();
82
83 OrthancPluginErrorCode error = OrthancPluginRestApiPost(context_, &buffer_, uri.c_str(), body.c_str(), body.size());
84
85 if (error == OrthancPluginErrorCode_Success)
86 {
87 // OK, success
88 }
89 else if (error == OrthancPluginErrorCode_UnknownResource ||
90 error == OrthancPluginErrorCode_InexistentItem)
91 {
92 throw Orthanc::OrthancException(Orthanc::ErrorCode_UnknownResource);
93 }
94 else
95 {
96 throw Orthanc::OrthancException(Orthanc::ErrorCode_Plugin);
97 }
98 }
99
100 void ToString(std::string& target) const
101 {
102 if (buffer_.size == 0)
103 {
104 target.clear();
105 }
106 else
107 {
108 target.assign(reinterpret_cast<const char*>(buffer_.data), buffer_.size);
109 }
110 }
111 };
112
113
114
115 void PluginOrthancConnection::ApplyGet(std::string& result,
116 const std::string& uri)
117 {
118 MemoryBuffer buffer(context_);
119 buffer.RestApiGet(uri);
120 buffer.ToString(result);
121 }
122
123
124 void PluginOrthancConnection::ApplyPost(std::string& result,
125 const std::string& uri,
126 const std::string& body)
127 {
128 MemoryBuffer buffer(context_);
129 buffer.RestApiPost(uri, body);
130 buffer.ToString(result);
131 }
132 }