Mercurial > hg > orthanc
comparison Plugins/Engine/PluginsHttpHandler.cpp @ 897:bafc9d592632 plugins
REST callbacks are working
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 17 Jun 2014 17:43:39 +0200 |
parents | |
children | bb0a51561016 |
comparison
equal
deleted
inserted
replaced
896:c4053ac5db04 | 897:bafc9d592632 |
---|---|
1 /** | |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege, | |
4 * Belgium | |
5 * | |
6 * This program is free software: you can redistribute it and/or | |
7 * modify it under the terms of the GNU General Public License as | |
8 * published by the Free Software Foundation, either version 3 of the | |
9 * License, or (at your option) any later version. | |
10 * | |
11 * In addition, as a special exception, the copyright holders of this | |
12 * program give permission to link the code of its release with the | |
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
14 * that use the same license as the "OpenSSL" library), and distribute | |
15 * the linked executables. You must obey the GNU General Public License | |
16 * in all respects for all of the code used other than "OpenSSL". If you | |
17 * modify file(s) with this exception, you may extend this exception to | |
18 * your version of the file(s), but you are not obligated to do so. If | |
19 * you do not wish to do so, delete this exception statement from your | |
20 * version. If you delete this exception statement from all source files | |
21 * in the program, then also delete it here. | |
22 * | |
23 * This program is distributed in the hope that it will be useful, but | |
24 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
26 * General Public License for more details. | |
27 * | |
28 * You should have received a copy of the GNU General Public License | |
29 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
30 **/ | |
31 | |
32 | |
33 #include "PluginsHttpHandler.h" | |
34 | |
35 #include "../../Core/OrthancException.h" | |
36 #include "../../Core/Toolbox.h" | |
37 | |
38 #include <boost/regex.hpp> | |
39 #include <glog/logging.h> | |
40 | |
41 namespace Orthanc | |
42 { | |
43 struct PluginsHttpHandler::PImpl | |
44 { | |
45 typedef std::pair<boost::regex*, OrthancPluginRestCallback> Callback; | |
46 typedef std::list<Callback> Callbacks; | |
47 | |
48 Callbacks callbacks_; | |
49 OrthancPluginRestCallback currentCallback_; | |
50 | |
51 PImpl() : currentCallback_(NULL) | |
52 { | |
53 } | |
54 }; | |
55 | |
56 | |
57 PluginsHttpHandler::PluginsHttpHandler(const PluginsManager& manager) | |
58 { | |
59 pimpl_.reset(new PImpl); | |
60 | |
61 for (PluginsManager::RestCallbacks::const_iterator | |
62 it = manager.GetRestCallbacks().begin(); it != manager.GetRestCallbacks().end(); ++it) | |
63 { | |
64 pimpl_->callbacks_.push_back(std::make_pair(new boost::regex(it->first), it->second)); | |
65 } | |
66 } | |
67 | |
68 | |
69 PluginsHttpHandler::~PluginsHttpHandler() | |
70 { | |
71 for (PImpl::Callbacks::iterator it = pimpl_->callbacks_.begin(); | |
72 it != pimpl_->callbacks_.end(); it++) | |
73 { | |
74 delete it->first; | |
75 } | |
76 } | |
77 | |
78 | |
79 bool PluginsHttpHandler::IsServedUri(const UriComponents& uri) | |
80 { | |
81 pimpl_->currentCallback_ = NULL; | |
82 std::string tmp = Toolbox::FlattenUri(uri); | |
83 | |
84 for (PImpl::Callbacks::const_iterator it = pimpl_->callbacks_.begin(); | |
85 it != pimpl_->callbacks_.end(); it++) | |
86 { | |
87 if (boost::regex_match(tmp, *(it->first))) | |
88 { | |
89 pimpl_->currentCallback_ = it->second; | |
90 return true; | |
91 } | |
92 } | |
93 | |
94 return false; | |
95 } | |
96 | |
97 bool PluginsHttpHandler::Handle(HttpOutput& output, | |
98 HttpMethod method, | |
99 const UriComponents& uri, | |
100 const Arguments& headers, | |
101 const Arguments& getArguments, | |
102 const std::string& postData) | |
103 { | |
104 std::string flatUri = Toolbox::FlattenUri(uri); | |
105 | |
106 LOG(INFO) << "Delegating HTTP request to plugin for URI: " << flatUri; | |
107 | |
108 std::vector<const char*> getKeys(getArguments.size()); | |
109 std::vector<const char*> getValues(getArguments.size()); | |
110 | |
111 OrthancPluginHttpMethod methodPlugin; | |
112 switch (method) | |
113 { | |
114 case HttpMethod_Get: | |
115 { | |
116 methodPlugin = OrthancPluginHttpMethod_Get; | |
117 | |
118 size_t i = 0; | |
119 for (Arguments::const_iterator it = getArguments.begin(); | |
120 it != getArguments.end(); it++, i++) | |
121 { | |
122 getKeys[i] = it->first.c_str(); | |
123 getValues[i] = it->second.c_str(); | |
124 } | |
125 | |
126 break; | |
127 } | |
128 | |
129 case HttpMethod_Post: | |
130 methodPlugin = OrthancPluginHttpMethod_Post; | |
131 break; | |
132 | |
133 case HttpMethod_Delete: | |
134 methodPlugin = OrthancPluginHttpMethod_Delete; | |
135 break; | |
136 | |
137 case HttpMethod_Put: | |
138 methodPlugin = OrthancPluginHttpMethod_Put; | |
139 break; | |
140 | |
141 default: | |
142 throw OrthancException(ErrorCode_InternalError); | |
143 } | |
144 | |
145 assert(pimpl_->currentCallback_ != NULL); | |
146 assert(getKeys.size() == getValues.size()); | |
147 int32_t error = (*pimpl_->currentCallback_) (reinterpret_cast<OrthancPluginRestOutput*>(&output), | |
148 methodPlugin, flatUri.c_str(), | |
149 getKeys.size() ? &getKeys[0] : NULL, | |
150 getKeys.size() ? &getValues[0] : NULL, | |
151 getKeys.size(), | |
152 postData.size() ? &postData[0] : NULL, postData.size()); | |
153 | |
154 if (error < 0) | |
155 { | |
156 LOG(ERROR) << "Plugin failed with error code " << error; | |
157 return false; | |
158 } | |
159 else | |
160 { | |
161 if (error > 0) | |
162 { | |
163 LOG(WARNING) << "Plugin finished with warning code " << error; | |
164 } | |
165 | |
166 return true; | |
167 } | |
168 } | |
169 } |