Fawkes API
Fawkes Development Version
rest_api_manager.cpp
1
2
/***************************************************************************
3
* rest_api_manager.cpp - Web REST API manager
4
*
5
* Created: Fri Mar 16 16:49:02 2018
6
* Copyright 2006-2018 Tim Niemueller [www.niemueller.de]
7
*
8
****************************************************************************/
9
10
/* This program is free software; you can redistribute it and/or modify
11
* it under the terms of the GNU General Public License as published by
12
* the Free Software Foundation; either version 2 of the License, or
13
* (at your option) any later version.
14
*
15
* This program is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
* GNU Library General Public License for more details.
19
*
20
* Read the full text in the LICENSE.GPL file in the doc directory.
21
*/
22
23
#include <core/exception.h>
24
#include <core/threading/mutex.h>
25
#include <core/threading/mutex_locker.h>
26
#include <webview/rest_api.h>
27
#include <webview/rest_api_manager.h>
28
29
namespace
fawkes
{
30
31
/** @class WebviewRestApiManager <webview/url_manager.h>
32
* Manage URL mappings.
33
* This class maps (base) URLs to web request processors which handle all
34
* requests for the given URL.
35
* @author Tim Niemueller
36
*/
37
38
/** Constructor. */
39
WebviewRestApiManager::WebviewRestApiManager
()
40
{
41
}
42
43
/** Destructor. */
44
WebviewRestApiManager::~WebviewRestApiManager
()
45
{
46
}
47
48
/** Add a REST API.
49
* @param api REST api handler
50
* @exception Exception thrown if an API of that name has already been
51
* registered
52
*/
53
void
54
WebviewRestApiManager::register_api
(
WebviewRestApi
*api)
55
{
56
MutexLocker
lock(&mutex_);
57
if
(apis_.find(api->
name
()) != apis_.end()) {
58
throw
Exception
(
"A REST API for %s has already been registered"
, api->
name
().c_str());
59
}
60
apis_[api->
name
()] = api;
61
}
62
63
/** Remove a request processor.
64
* @param api REST api handler
65
*/
66
void
67
WebviewRestApiManager::unregister_api
(WebviewRestApi *api)
68
{
69
MutexLocker lock(&mutex_);
70
apis_.erase(api->name());
71
}
72
73
/** Find API by name.
74
* This method determines if a processor has been registered for the URL.
75
* It is the callers duty to ensure that the mutex has been locked while
76
* searching and while using the found processor.
77
* @param name name of REST API to retrieve
78
* @return request processor if found, NULL otherwise
79
*/
80
WebviewRestApi
*
81
WebviewRestApiManager::get_api
(std::string &name)
82
{
83
if
(apis_.find(name) == apis_.end()) {
84
return
NULL;
85
}
86
return
apis_[name];
87
}
88
89
/** Get internal mutex.
90
* Use this mutex to guard find_processor() and a following invocation of
91
* a found processor against changes due to registering/unregistering of
92
* processors.
93
* @return internal mutex
94
*/
95
Mutex &
96
WebviewRestApiManager::mutex
()
97
{
98
return
mutex_;
99
}
100
101
}
// end namespace fawkes
fawkes::WebviewRestApi::name
const std::string & name() const
Get name of component.
Definition:
rest_api.cpp:56
fawkes::WebviewRestApiManager::get_api
WebviewRestApi * get_api(std::string &name)
Find API by name.
Definition:
rest_api_manager.cpp:85
fawkes::WebviewRestApiManager::mutex
Mutex & mutex()
Get internal mutex.
Definition:
rest_api_manager.cpp:100
fawkes::WebviewRestApiManager::~WebviewRestApiManager
~WebviewRestApiManager()
Destructor.
Definition:
rest_api_manager.cpp:48
fawkes::WebviewRestApiManager::unregister_api
void unregister_api(WebviewRestApi *api)
Remove a request processor.
Definition:
rest_api_manager.cpp:71
fawkes::MutexLocker
Definition:
mutex_locker.h:37
fawkes::WebviewRestApiManager::register_api
void register_api(WebviewRestApi *api)
Add a REST API.
Definition:
rest_api_manager.cpp:58
fawkes
fawkes::WebviewRestApi
Definition:
rest_api.h:223
fawkes::WebviewRestApiManager::WebviewRestApiManager
WebviewRestApiManager()
Constructor.
Definition:
rest_api_manager.cpp:43
fawkes::Exception
Definition:
exception.h:39
src
libs
webview
rest_api_manager.cpp
Generated by
1.8.16