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. */
40 {
41 }
42 
43 /** Destructor. */
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
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
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  */
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 &
97 {
98  return mutex_;
99 }
100 
101 } // end namespace fawkes
Base class for exceptions in Fawkes.
Definition: exception.h:36
Mutex locking helper.
Definition: mutex_locker.h:34
Mutex mutual exclusion lock.
Definition: mutex.h:33
void unregister_api(WebviewRestApi *api)
Remove a request processor.
WebviewRestApi * get_api(std::string &name)
Find API by name.
void register_api(WebviewRestApi *api)
Add a REST API.
Mutex & mutex()
Get internal mutex.
Webview REST API component.
Definition: rest_api.h:221
const std::string & name() const
Get name of component.
Definition: rest_api.cpp:53
Fawkes library namespace.