mod_servlet
C++Servlets
 All Classes Files Functions Variables Typedefs Macros Pages
filter.h
Go to the documentation of this file.
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_FILTER_H
9 #define MOD_SERVLET_FILTER_H
10 
16 #include <servlet/request.h>
17 #include <servlet/response.h>
18 #include <servlet/context.h>
19 
25 #define FILTER_EXPORT(factoryName, className) extern "C" servlet::http_filter* factoryName() { return new className{}; }
26 
27 namespace servlet
28 {
29 
30 class filter_chain;
31 
87 {
88 public:
89  http_filter() = default;
90  virtual ~http_filter() noexcept = default;
91 
101  virtual void init() {}
102 
121  virtual void init(filter_config& cfg);
122 
150  virtual void do_filter(http_request& request, http_response& response, filter_chain& chain) = 0;
151 
164  const filter_config &get_filter_config() const { return *_cfg; }
165 
174  const std::string &get_filter_name() const { return _cfg->get_filter_name(); }
175 
188  const std::map<std::string, std::string, std::less<>>& get_init_parameters() const
189  { return _cfg->get_init_parameters(); };
190 
203  template <typename KeyType>
205  { return _cfg->get_init_parameter(name); }
206 
207 private:
208  filter_config* _cfg;
209 };
210 
221 {
222 public:
223  virtual ~filter_chain() noexcept {};
224 
233  virtual void do_filter(http_request& request, http_response& response) = 0;
234 };
235 
236 } // end of servlet namespace
237 
238 #endif // MOD_SERVLET_FILTER_H
virtual void do_filter(http_request &request, http_response &response)=0
Causes the next filter in the chain to be invoked, or if the calling filter is the last filter in the...
A filter is an object that performs filtering tasks on either the request to a resource (a servlet or...
Definition: filter.h:86
const std::map< std::string, std::string, std::less<> > & get_init_parameters() const
Returns the all of the filter's initialization parameters as an tree_map of std::string objects...
Definition: filter.h:188
const std::string & get_filter_name() const
Get the name of the filter.
Definition: context.h:347
Defines an object to provide client request information to a servlet.
Definition: request.h:35
const filter_config & get_filter_config() const
Returns a filter_config object, which contains initialization and startup parameters for this filter...
Definition: filter.h:164
Optional reference implementation.
Definition: optional.h:221
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
virtual void init()
A convenience method which can be overridden so that there's no need to call init(filter_config).
Definition: filter.h:101
A filter_chain is an object provided by the servlet container to the developer giving a view into the...
Definition: filter.h:220
optional_ref< const std::string > get_init_parameter(const KeyType &name) const
Returns an optional std::string containing the value of the named initialization parameter, or null if the parameter does not exist.
Definition: filter.h:204
const std::string & get_filter_name() const
Returns the name of this filter instance.
Definition: filter.h:174
A filter configuration object used by a servlet container to pass information to a filter during init...
Definition: context.h:320
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
Defines an object to assist a servlet in sending a response to the client.
Definition: response.h:33
virtual void do_filter(http_request &request, http_response &response, filter_chain &chain)=0
The do_filter method of the http_filter is called by the container each time a request/response pair ...