Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkRESTServer.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #include <mitkIRESTManager.h>
14 #include <mitkRESTServer.h>
15 
16 #include <usGetModuleContext.h>
17 #include <usModuleContext.h>
18 
19 #include <cpprest/http_listener.h>
20 
21 using namespace std::placeholders;
22 
28 
29 namespace mitk
30 {
31  class RESTServer::Impl
32  {
33  public:
34  Impl(const web::uri &uri);
35  ~Impl();
36 
37  void HandleRequest(const http_request &request);
38 
40  web::uri uri;
41  };
42 
43  RESTServer::Impl::Impl(const web::uri &uri) : uri{uri} {}
44 
45  RESTServer::Impl::~Impl() {}
46 
47  void RESTServer::Impl::HandleRequest(const http_request &request)
48  {
49  web::uri_builder builder(this->listener.uri());
50  builder.append(request.absolute_uri());
51 
52  auto uriString = builder.to_uri().to_string();
53 
54  http_response response(status_codes::InternalError);
55  response.set_body(U("There went something wrong after receiving the request."));
56 
57  auto context = us::GetModuleContext();
58  auto managerRef = context->GetServiceReference<IRESTManager>();
59 
60  if (managerRef)
61  {
62  auto manager = context->GetService(managerRef);
63  if (manager)
64  {
65  // not every request contains JSON data
66  web::json::value data = {};
67  if (request.headers().content_type() == U("application/json"))
68  {
69  data = request.extract_json().get();
70  }
71 
73  auto begin = request.headers().begin();
74  auto end = request.headers().end();
75 
76  for (; begin != end; ++begin)
77  {
78  headers.insert(mitk::RESTUtil::ParamMap::value_type(begin->first, begin->second));
79  }
80 
81  response = manager->Handle(builder.to_uri(), data, request.method(), headers);
82  }
83  }
84 
85  request.reply(response);
86  }
87 } // namespace mitk
88 
89 mitk::RESTServer::RESTServer(const web::uri &uri) : m_Impl{std::make_unique<Impl>(uri)} {}
90 
92 
94 {
95  m_Impl->listener = http_listener(m_Impl->uri);
96  m_Impl->listener.support(std::bind(&Impl::HandleRequest, m_Impl.get(), _1));
97  m_Impl->listener.support(methods::OPTIONS, std::bind(&Impl::HandleRequest, m_Impl.get(), _1));
98  m_Impl->listener.open().wait();
99 }
100 
102 {
103  m_Impl->listener.close().wait();
104 }
105 
107 {
108  return m_Impl->uri;
109 }
web::http::http_response http_response
DataCollection - Class to facilitate loading/accessing structured data.
web::http::experimental::listener::http_listener http_listener
web::http::methods methods
web::http::status_codes status_codes
void OpenListener()
Opens the listener and starts the listening process.
RESTServer(const web::uri &uri)
Creates an server listening to the given URI.
void CloseListener()
Closes the listener and stops the listening process.
web::http::http_response http_response
web::http::http_request http_request
web::http::http_request http_request
std::map< utility::string_t, utility::string_t > ParamMap
Definition: mitkRESTUtil.h:27
web::http::status_codes status_codes
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.
web::http::methods methods