comparison Plugins/Engine/PluginsHttpHandler.cpp @ 899:bb0a51561016 plugins

refactoring
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 18 Jun 2014 13:29:09 +0200
parents bafc9d592632
children 1b92ce45cc8d
comparison
equal deleted inserted replaced
898:7000fc86fe62 899:bb0a51561016
32 32
33 #include "PluginsHttpHandler.h" 33 #include "PluginsHttpHandler.h"
34 34
35 #include "../../Core/OrthancException.h" 35 #include "../../Core/OrthancException.h"
36 #include "../../Core/Toolbox.h" 36 #include "../../Core/Toolbox.h"
37 #include "../../Core/HttpServer/HttpOutput.h"
37 38
38 #include <boost/regex.hpp> 39 #include <boost/regex.hpp>
39 #include <glog/logging.h> 40 #include <glog/logging.h>
40 41
41 namespace Orthanc 42 namespace Orthanc
52 { 53 {
53 } 54 }
54 }; 55 };
55 56
56 57
57 PluginsHttpHandler::PluginsHttpHandler(const PluginsManager& manager) 58 PluginsHttpHandler::PluginsHttpHandler()
58 { 59 {
59 pimpl_.reset(new PImpl); 60 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 } 61 }
67 62
68 63
69 PluginsHttpHandler::~PluginsHttpHandler() 64 PluginsHttpHandler::~PluginsHttpHandler()
70 { 65 {
106 LOG(INFO) << "Delegating HTTP request to plugin for URI: " << flatUri; 101 LOG(INFO) << "Delegating HTTP request to plugin for URI: " << flatUri;
107 102
108 std::vector<const char*> getKeys(getArguments.size()); 103 std::vector<const char*> getKeys(getArguments.size());
109 std::vector<const char*> getValues(getArguments.size()); 104 std::vector<const char*> getValues(getArguments.size());
110 105
111 OrthancPluginHttpMethod methodPlugin; 106 OrthancPluginHttpRequest request;
107 memset(&request, 0, sizeof(OrthancPluginHttpRequest));
108
112 switch (method) 109 switch (method)
113 { 110 {
114 case HttpMethod_Get: 111 case HttpMethod_Get:
115 { 112 {
116 methodPlugin = OrthancPluginHttpMethod_Get; 113 request.method = OrthancPluginHttpMethod_Get;
117 114
118 size_t i = 0; 115 size_t i = 0;
119 for (Arguments::const_iterator it = getArguments.begin(); 116 for (Arguments::const_iterator it = getArguments.begin();
120 it != getArguments.end(); it++, i++) 117 it != getArguments.end(); it++, i++)
121 { 118 {
125 122
126 break; 123 break;
127 } 124 }
128 125
129 case HttpMethod_Post: 126 case HttpMethod_Post:
130 methodPlugin = OrthancPluginHttpMethod_Post; 127 request.method = OrthancPluginHttpMethod_Post;
131 break; 128 break;
132 129
133 case HttpMethod_Delete: 130 case HttpMethod_Delete:
134 methodPlugin = OrthancPluginHttpMethod_Delete; 131 request.method = OrthancPluginHttpMethod_Delete;
135 break; 132 break;
136 133
137 case HttpMethod_Put: 134 case HttpMethod_Put:
138 methodPlugin = OrthancPluginHttpMethod_Put; 135 request.method = OrthancPluginHttpMethod_Put;
139 break; 136 break;
140 137
141 default: 138 default:
142 throw OrthancException(ErrorCode_InternalError); 139 throw OrthancException(ErrorCode_InternalError);
143 } 140 }
144 141
142
143 request.getCount = getArguments.size();
144 request.body = (postData.size() ? &postData[0] : NULL);
145 request.bodySize = postData.size();
146
147 if (getArguments.size() > 0)
148 {
149 request.getKeys = &getKeys[0];
150 request.getValues = &getValues[0];
151 }
152
145 assert(pimpl_->currentCallback_ != NULL); 153 assert(pimpl_->currentCallback_ != NULL);
146 assert(getKeys.size() == getValues.size());
147 int32_t error = (*pimpl_->currentCallback_) (reinterpret_cast<OrthancPluginRestOutput*>(&output), 154 int32_t error = (*pimpl_->currentCallback_) (reinterpret_cast<OrthancPluginRestOutput*>(&output),
148 methodPlugin, flatUri.c_str(), 155 flatUri.c_str(),
149 getKeys.size() ? &getKeys[0] : NULL, 156 &request);
150 getKeys.size() ? &getValues[0] : NULL,
151 getKeys.size(),
152 postData.size() ? &postData[0] : NULL, postData.size());
153 157
154 if (error < 0) 158 if (error < 0)
155 { 159 {
156 LOG(ERROR) << "Plugin failed with error code " << error; 160 LOG(ERROR) << "Plugin failed with error code " << error;
157 return false; 161 return false;
164 } 168 }
165 169
166 return true; 170 return true;
167 } 171 }
168 } 172 }
173
174
175 bool PluginsHttpHandler::InvokeService(OrthancPluginService service,
176 const void* parameters)
177 {
178
179
180 /*void PluginsManager::RegisterRestCallback(const OrthancPluginContext* context,
181 const char* pathRegularExpression,
182 OrthancPluginRestCallback callback)
183 {
184 LOG(INFO) << "Plugin has registered a REST callback on: " << pathRegularExpression;
185 PluginsManager* manager = reinterpret_cast<PluginsManager*>(context->pluginsManager);
186 manager->restCallbacks_.push_back(std::make_pair(pathRegularExpression, callback));
187 }*/
188
189
190 /*static void AnswerBuffer(OrthancPluginRestOutput* output,
191 const char* answer,
192 uint32_t answerSize,
193 const char* mimeType)
194 {
195 HttpOutput* translatedOutput = reinterpret_cast<HttpOutput*>(output);
196 translatedOutput->AnswerBufferWithContentType(answer, answerSize, mimeType);
197 }*/
198
199
200
201 /*for (PluginsManager::RestCallbacks::const_iterator
202 it = manager.GetRestCallbacks().begin(); it != manager.GetRestCallbacks().end(); ++it)
203 {
204 pimpl_->callbacks_.push_back(std::make_pair(new boost::regex(it->first), it->second));
205 }*/
206
207
208 switch (service)
209 {
210 case OrthancPluginService_RegisterRestCallback:
211 {
212 const OrthancPluginRestCallbackParams& p =
213 *reinterpret_cast<const OrthancPluginRestCallbackParams*>(parameters);
214
215 LOG(INFO) << "Plugin has registered a REST callback on: " << p.pathRegularExpression;
216 pimpl_->callbacks_.push_back(std::make_pair(new boost::regex(p.pathRegularExpression), p.callback));
217
218 return true;
219 }
220
221 case OrthancPluginService_AnswerBuffer:
222 {
223 const OrthancPluginAnswerBufferParams& p =
224 *reinterpret_cast<const OrthancPluginAnswerBufferParams*>(parameters);
225
226 HttpOutput* translatedOutput = reinterpret_cast<HttpOutput*>(p.output);
227 translatedOutput->AnswerBufferWithContentType(p.answer, p.answerSize, p.mimeType);
228
229 return true;
230 }
231
232 default:
233 return false;
234 }
235 }
236
169 } 237 }