annotate Plugins/Engine/PluginsHttpHandler.cpp @ 901:7d88f3f4a3b3 plugins

refactoring IsServedUri, answer PNG images, regular expression groups
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 18 Jun 2014 15:22:13 +0200
parents 1b92ce45cc8d
children 2732b5f57d9c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
897
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege,
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Belgium
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 *
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 * This program is free software: you can redistribute it and/or
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * modify it under the terms of the GNU General Public License as
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * published by the Free Software Foundation, either version 3 of the
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * License, or (at your option) any later version.
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 *
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 * In addition, as a special exception, the copyright holders of this
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * program give permission to link the code of its release with the
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * that use the same license as the "OpenSSL" library), and distribute
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * the linked executables. You must obey the GNU General Public License
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 * in all respects for all of the code used other than "OpenSSL". If you
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * modify file(s) with this exception, you may extend this exception to
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * your version of the file(s), but you are not obligated to do so. If
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 * you do not wish to do so, delete this exception statement from your
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 * version. If you delete this exception statement from all source files
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 * in the program, then also delete it here.
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 *
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 * This program is distributed in the hope that it will be useful, but
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 * WITHOUT ANY WARRANTY; without even the implied warranty of
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 * General Public License for more details.
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 *
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 * You should have received a copy of the GNU General Public License
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 **/
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 #include "PluginsHttpHandler.h"
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 #include "../../Core/OrthancException.h"
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 #include "../../Core/Toolbox.h"
899
bb0a51561016 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 897
diff changeset
37 #include "../../Core/HttpServer/HttpOutput.h"
901
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
38 #include "../../Core/ImageFormats/PngWriter.h"
897
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 #include <boost/regex.hpp>
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 #include <glog/logging.h>
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 namespace Orthanc
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 {
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 struct PluginsHttpHandler::PImpl
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 {
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 typedef std::pair<boost::regex*, OrthancPluginRestCallback> Callback;
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 typedef std::list<Callback> Callbacks;
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 Callbacks callbacks_;
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 };
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53
899
bb0a51561016 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 897
diff changeset
54 PluginsHttpHandler::PluginsHttpHandler()
897
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 {
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 pimpl_.reset(new PImpl);
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 }
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 PluginsHttpHandler::~PluginsHttpHandler()
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 {
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 for (PImpl::Callbacks::iterator it = pimpl_->callbacks_.begin();
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 it != pimpl_->callbacks_.end(); it++)
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 {
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 delete it->first;
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 }
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 }
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 bool PluginsHttpHandler::Handle(HttpOutput& output,
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 HttpMethod method,
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 const UriComponents& uri,
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 const Arguments& headers,
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 const Arguments& getArguments,
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 const std::string& postData)
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 {
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 std::string flatUri = Toolbox::FlattenUri(uri);
901
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
78 OrthancPluginRestCallback callback = NULL;
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
79
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
80 std::vector<std::string> groups;
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
81 std::vector<const char*> cgroups;
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
82
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
83 bool found = false;
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
84 for (PImpl::Callbacks::const_iterator it = pimpl_->callbacks_.begin();
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
85 it != pimpl_->callbacks_.end() && !found; it++)
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
86 {
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
87 boost::cmatch what;
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
88 if (boost::regex_match(flatUri.c_str(), what, *(it->first)))
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
89 {
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
90 callback = it->second;
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
91
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
92 if (what.size() > 1)
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
93 {
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
94 groups.resize(what.size() - 1);
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
95 cgroups.resize(what.size() - 1);
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
96 for (size_t i = 1; i < what.size(); i++)
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
97 {
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
98 groups[i - 1] = what[i];
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
99 cgroups[i - 1] = groups[i - 1].c_str();
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
100 }
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
101 }
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
102
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
103 found = true;
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
104 }
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
105 }
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
106
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
107 if (!found)
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
108 {
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
109 return false;
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
110 }
897
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 LOG(INFO) << "Delegating HTTP request to plugin for URI: " << flatUri;
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 std::vector<const char*> getKeys(getArguments.size());
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 std::vector<const char*> getValues(getArguments.size());
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116
899
bb0a51561016 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 897
diff changeset
117 OrthancPluginHttpRequest request;
bb0a51561016 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 897
diff changeset
118 memset(&request, 0, sizeof(OrthancPluginHttpRequest));
bb0a51561016 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 897
diff changeset
119
897
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 switch (method)
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 {
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 case HttpMethod_Get:
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 {
899
bb0a51561016 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 897
diff changeset
124 request.method = OrthancPluginHttpMethod_Get;
897
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 size_t i = 0;
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 for (Arguments::const_iterator it = getArguments.begin();
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 it != getArguments.end(); it++, i++)
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 {
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130 getKeys[i] = it->first.c_str();
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 getValues[i] = it->second.c_str();
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 }
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 break;
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 }
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 case HttpMethod_Post:
899
bb0a51561016 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 897
diff changeset
138 request.method = OrthancPluginHttpMethod_Post;
897
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 break;
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141 case HttpMethod_Delete:
899
bb0a51561016 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 897
diff changeset
142 request.method = OrthancPluginHttpMethod_Delete;
897
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143 break;
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 case HttpMethod_Put:
899
bb0a51561016 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 897
diff changeset
146 request.method = OrthancPluginHttpMethod_Put;
897
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147 break;
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149 default:
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150 throw OrthancException(ErrorCode_InternalError);
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151 }
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152
899
bb0a51561016 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 897
diff changeset
153
901
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
154 request.groupValues = (cgroups.size() ? &cgroups[0] : NULL);
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
155 request.groupCount = cgroups.size();
899
bb0a51561016 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 897
diff changeset
156 request.getCount = getArguments.size();
bb0a51561016 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 897
diff changeset
157 request.body = (postData.size() ? &postData[0] : NULL);
bb0a51561016 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 897
diff changeset
158 request.bodySize = postData.size();
bb0a51561016 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 897
diff changeset
159
bb0a51561016 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 897
diff changeset
160 if (getArguments.size() > 0)
bb0a51561016 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 897
diff changeset
161 {
bb0a51561016 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 897
diff changeset
162 request.getKeys = &getKeys[0];
bb0a51561016 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 897
diff changeset
163 request.getValues = &getValues[0];
bb0a51561016 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 897
diff changeset
164 }
bb0a51561016 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 897
diff changeset
165
901
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
166 assert(callback != NULL);
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
167 int32_t error = callback(reinterpret_cast<OrthancPluginRestOutput*>(&output),
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
168 flatUri.c_str(),
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
169 &request);
897
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
170
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
171 if (error < 0)
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
172 {
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
173 LOG(ERROR) << "Plugin failed with error code " << error;
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
174 return false;
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
175 }
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
176 else
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
177 {
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
178 if (error > 0)
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
179 {
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
180 LOG(WARNING) << "Plugin finished with warning code " << error;
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
181 }
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
182
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
183 return true;
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
184 }
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
185 }
899
bb0a51561016 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 897
diff changeset
186
bb0a51561016 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 897
diff changeset
187
bb0a51561016 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 897
diff changeset
188 bool PluginsHttpHandler::InvokeService(OrthancPluginService service,
bb0a51561016 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 897
diff changeset
189 const void* parameters)
bb0a51561016 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 897
diff changeset
190 {
bb0a51561016 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 897
diff changeset
191 switch (service)
bb0a51561016 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 897
diff changeset
192 {
bb0a51561016 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 897
diff changeset
193 case OrthancPluginService_RegisterRestCallback:
bb0a51561016 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 897
diff changeset
194 {
900
1b92ce45cc8d plugin doc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 899
diff changeset
195 const _OrthancPluginRestCallbackParams& p =
1b92ce45cc8d plugin doc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 899
diff changeset
196 *reinterpret_cast<const _OrthancPluginRestCallbackParams*>(parameters);
899
bb0a51561016 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 897
diff changeset
197
bb0a51561016 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 897
diff changeset
198 LOG(INFO) << "Plugin has registered a REST callback on: " << p.pathRegularExpression;
bb0a51561016 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 897
diff changeset
199 pimpl_->callbacks_.push_back(std::make_pair(new boost::regex(p.pathRegularExpression), p.callback));
bb0a51561016 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 897
diff changeset
200
bb0a51561016 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 897
diff changeset
201 return true;
bb0a51561016 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 897
diff changeset
202 }
bb0a51561016 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 897
diff changeset
203
bb0a51561016 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 897
diff changeset
204 case OrthancPluginService_AnswerBuffer:
bb0a51561016 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 897
diff changeset
205 {
900
1b92ce45cc8d plugin doc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 899
diff changeset
206 const _OrthancPluginAnswerBufferParams& p =
1b92ce45cc8d plugin doc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 899
diff changeset
207 *reinterpret_cast<const _OrthancPluginAnswerBufferParams*>(parameters);
899
bb0a51561016 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 897
diff changeset
208
bb0a51561016 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 897
diff changeset
209 HttpOutput* translatedOutput = reinterpret_cast<HttpOutput*>(p.output);
bb0a51561016 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 897
diff changeset
210 translatedOutput->AnswerBufferWithContentType(p.answer, p.answerSize, p.mimeType);
bb0a51561016 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 897
diff changeset
211
bb0a51561016 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 897
diff changeset
212 return true;
bb0a51561016 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 897
diff changeset
213 }
bb0a51561016 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 897
diff changeset
214
901
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
215 case OrthancPluginService_CompressAndAnswerPngImage:
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
216 {
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
217 const _OrthancPluginCompressAndAnswerPngImageParams& p =
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
218 *reinterpret_cast<const _OrthancPluginCompressAndAnswerPngImageParams*>(parameters);
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
219
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
220 HttpOutput* translatedOutput = reinterpret_cast<HttpOutput*>(p.output);
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
221
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
222 PixelFormat format;
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
223 switch (p.format)
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
224 {
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
225 case OrthancPluginPixelFormat_Grayscale8:
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
226 format = PixelFormat_Grayscale8;
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
227 break;
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
228
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
229 case OrthancPluginPixelFormat_Grayscale16:
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
230 format = PixelFormat_Grayscale16;
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
231 break;
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
232
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
233 case OrthancPluginPixelFormat_SignedGrayscale16:
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
234 format = PixelFormat_SignedGrayscale16;
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
235 break;
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
236
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
237 case OrthancPluginPixelFormat_RGB24:
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
238 format = PixelFormat_RGB24;
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
239 break;
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
240
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
241 case OrthancPluginPixelFormat_RGBA32:
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
242 format = PixelFormat_RGBA32;
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
243 break;
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
244
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
245 default:
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
246 throw OrthancException(ErrorCode_ParameterOutOfRange);
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
247 }
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
248
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
249 ImageAccessor accessor;
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
250 accessor.AssignReadOnly(format, p.width, p.height, p.pitch, p.buffer);
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
251
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
252 PngWriter writer;
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
253 std::string png;
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
254 writer.WriteToMemory(png, accessor);
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
255
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
256 translatedOutput->AnswerBufferWithContentType(png, "image/png");
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
257
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
258 return true;
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
259 }
7d88f3f4a3b3 refactoring IsServedUri, answer PNG images, regular expression groups
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 900
diff changeset
260
899
bb0a51561016 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 897
diff changeset
261 default:
bb0a51561016 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 897
diff changeset
262 return false;
bb0a51561016 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 897
diff changeset
263 }
bb0a51561016 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 897
diff changeset
264 }
bb0a51561016 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 897
diff changeset
265
897
bafc9d592632 REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
266 }