mod_servlet
C++Servlets
 All Classes Files Functions Variables Typedefs Macros Pages
context.h
1 /*
2 Copyright (c) 2016 Alexei Novakov
3 https://github.com/novalexei
4 
5 Distributed under the Boost Software License, Version 1.0.
6 http://boost.org/LICENSE_1_0.txt
7 */
8 #ifndef MOD_SERVLET_CONTEXT_H
9 #define MOD_SERVLET_CONTEXT_H
10 
11 #include <experimental/any>
12 #include <experimental/string_view>
13 
14 #include <servlet/lib/any_map.h>
15 #include <servlet/lib/optional.h>
16 
17 namespace servlet
18 {
19 
20 using std::experimental::string_view;
21 
45 {
46 public:
50  typedef std::map<std::string, std::string, std::less<>> init_params_map;
51 
55  virtual ~servlet_context() noexcept = default;
56 
62  const std::string& get_context_path() const { return _ctx_path; }
63 
68  const std::string& get_webapp_path() const { return _webapp_path; }
69 
88  template <typename T, typename KeyType>
89  optional_ref<const T> get_attribute(const KeyType& key) const { return _attr_map.get<T>(key); }
90 
99  template <typename KeyType, typename ValueType>
101  {
103  if (!found.has_value()) return optional_ref<const ValueType>{};
104  return optional_ref<const ValueType>{any_cast<const ValueType>(found.value())};
105  }
106 
119  template <typename ValueType>
120  bool set_attribute(const std::string &key, ValueType &&value) { return _attr_map.put(key, std::forward(value)); }
121 
131  template <typename ValueType>
132  bool set_attribute(std::string &&key, ValueType &&value)
133  { return _attr_map.put(std::move(key), std::forward(value)); }
134 
144  template <typename KeyType>
145  bool remove_attribute(const KeyType& key) { return _attr_map.erase(key) > 0; }
146 
155 
161  const tree_any_map& get_attributes() const { return _attr_map; }
162 
179  template <typename KeyType>
181  {
182  auto it = _init_params_map.find(key);
183  return it == _init_params_map.end() ? optional_ref<const std::string>{} :
185  }
186 
197 
208  virtual optional_ref<const std::string> get_mime_type(string_view file_name) const = 0;
209 
210 protected:
217  servlet_context(const std::string& ctx_path, const std::string& webapp_path, init_params_map &&init_params) :
218  _ctx_path{ctx_path}, _webapp_path{webapp_path}, _init_params_map{std::move(init_params)} {}
219 
223  const std::string& _ctx_path;
232 
236  const std::string& _webapp_path;
237 };
238 
244 {
245 public:
250  servlet_config(const std::string &servlet_name) : _servlet_name{servlet_name} {}
251 
257  servlet_config(std::string &&servlet_name) : _servlet_name{std::move(servlet_name)} {}
258 
262  virtual ~servlet_config() noexcept = default;
263 
272  const std::string& get_servlet_name() const { return _servlet_name; }
273 
282  virtual const servlet_context& get_servlet_context() const = 0;
283 
294  template <typename KeyType>
296  { return get_servlet_context().get_init_parameter(key); }
297 
304  const std::map<std::string, std::string, std::less<>>& get_init_parameters() const
305  { return get_servlet_context().get_init_parameters(); }
306 
307 protected:
311  const std::string _servlet_name;
312 };
313 
321 {
322 public:
327  filter_config(const std::string &filter_name) : _filter_name{filter_name} {}
328 
334  filter_config(std::string &&filter_name) : _filter_name{std::move(filter_name)} {}
335 
339  virtual ~filter_config() noexcept = default;
340 
347  const std::string& get_filter_name() const { return _filter_name; }
348 
358  virtual servlet_context& get_servlet_context() = 0;
359 
370  template <typename KeyType>
372  { return get_servlet_context().get_init_parameter(key); }
373 
380  const std::map<std::string, std::string, std::less<>>& get_init_parameters()
381  { return get_servlet_context().get_init_parameters(); }
382 
383 protected:
387  const std::string _filter_name;
388 };
389 
390 } // end of servlet namespace
391 
392 #endif // MOD_SERVLET_CONTEXT_H
servlet_context(const std::string &ctx_path, const std::string &webapp_path, init_params_map &&init_params)
Protected constructor to be used from derrived classes.
Definition: context.h:217
virtual ~servlet_context() noexcept=default
virtual destructor
init_params_map _init_params_map
Initial parameters map.
Definition: context.h:231
bool put(key_type &&key, Args &&...args)
Associates a value of specified type created with a given arguments with the specified key in this ma...
Definition: any_map.h:283
Defines optional container objects and related methods.
bool set_attribute(const std::string &key, ValueType &&value)
Binds an object to a given attribute name in this servlet context.
Definition: context.h:120
Defines a set of methods that a servlet uses to communicate with its servlet container.
Definition: context.h:44
const std::string _servlet_name
Servlet name.
Definition: context.h:311
const tree_any_map & get_attributes() const
Const version of get_attributes method.
Definition: context.h:161
filter_config(std::string &&filter_name)
Move version of the object constructor.
Definition: context.h:334
bool set_attribute(std::string &&key, ValueType &&value)
Move version of set_attribute(const std::string&, ValueType&&)
Definition: context.h:132
std::map< std::string, std::string, std::less<> > init_params_map
Type definition for initial parameters map.
Definition: context.h:50
Optional reference implementation.
Definition: optional.h:221
const std::string & _ctx_path
Context path.
Definition: context.h:223
const std::string & get_webapp_path() const
Returns filesystem path of current web application.
Definition: context.h:68
Covenience class on top of associative container to facilitate work with value type std::any...
Definition: any_map.h:84
const std::map< std::string, std::string, std::less<> > & get_init_parameters()
Returns all the filter's initialization parameters as a tree_map.
Definition: context.h:380
servlet_config(std::string &&servlet_name)
Move version of the object constructor.
Definition: context.h:257
const std::string & get_context_path() const
Return the main path associated with this context.
Definition: context.h:62
constexpr T & value()&
Accesses the contained value as a reference.
Definition: optional.h:332
const init_params_map & get_init_parameters() const
Returns the context's initialization parameters map of std::string objects.
Definition: context.h:196
const std::string & _webapp_path
Filesystem path of current webapp.
Definition: context.h:236
filter_config(const std::string &filter_name)
Creates new filter_config object with a given filter name.
Definition: context.h:327
optional_ref< const ValueType > get_attribute(const KeyType &key) const
Finds attribute by key and casts it to specified ValueType.
Definition: context.h:100
A servlet configuration object used by a servlet container to pass information to a servlet during in...
Definition: context.h:243
tree_any_map & get_attributes()
Returns an tree_map containing all the attributes available within this servlet context.
Definition: context.h:154
const std::string _filter_name
Filter name.
Definition: context.h:387
A filter configuration object used by a servlet container to pass information to a filter during init...
Definition: context.h:320
virtual optional_ref< const std::string > get_mime_type(string_view file_name) const =0
Returns the MIME type of the specified file, or null if the MIME type is not known.
servlet_config(const std::string &servlet_name)
Creates new servlet_config object with a given servlet name.
Definition: context.h:250
bool remove_attribute(const KeyType &key)
Removes the attribute with the given name from the servlet context.
Definition: context.h:145
optional_ref< const std::string > get_init_parameter(const KeyType &key)
Returns a std::string containing the value of the named initialization parameter, or empty_reference ...
Definition: context.h:371
const std::map< std::string, std::string, std::less<> > & get_init_parameters() const
Returns all the servlet's initialization parameters as a tree_map.
Definition: context.h:304
optional_ref< const T > get_attribute(const KeyType &key) const
Returns the servlet container attribute with the given name, or empty reference if there is no attrib...
Definition: context.h:89
optional_ref< const std::string > get_init_parameter(const KeyType &key) const
Returns a reference to a std::string containing the value of the named context-wide initialization pa...
Definition: context.h:180
Containes the implementation of any_map class and related type definitions.
optional_ref< const std::string > get_init_parameter(const KeyType &key) const
Returns a std::string containing the value of the named initialization parameter, or empty_reference ...
Definition: context.h:295
constexpr bool has_value() const noexcept
Checks whether this object contains valid pointer.
Definition: optional.h:325
optional_ref< const T > get(const KeyType &key) const
Returns optional_ref object to a value with a specified type, if that value exists and can be casted ...
Definition: any_map.h:203
tree_any_map _attr_map
Attributes map (to be filled by inheriting class).
Definition: context.h:227