mod_servlet
C++Servlets
 All Classes Files Functions Variables Typedefs Macros Pages
servlet::servlet_context Class Referenceabstract

Defines a set of methods that a servlet uses to communicate with its servlet container. More...

#include <context.h>

Public Types

typedef std::map< std::string,
std::string, std::less<> > 
init_params_map
 Type definition for initial parameters map.
 

Public Member Functions

virtual ~servlet_context () noexcept=default
 virtual destructor
 
const std::string & get_context_path () const
 Return the main path associated with this context. More...
 
const std::string & get_webapp_path () const
 Returns filesystem path of current web application. More...
 
template<typename T , typename KeyType >
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 attribute by that name. More...
 
template<typename KeyType , typename ValueType >
optional_ref< const ValueType > get_attribute (const KeyType &key) const
 Finds attribute by key and casts it to specified ValueType. More...
 
template<typename ValueType >
bool set_attribute (const std::string &key, ValueType &&value)
 Binds an object to a given attribute name in this servlet context. More...
 
template<typename ValueType >
bool set_attribute (std::string &&key, ValueType &&value)
 Move version of set_attribute(const std::string&, ValueType&&) More...
 
template<typename KeyType >
bool remove_attribute (const KeyType &key)
 Removes the attribute with the given name from the servlet context. More...
 
tree_any_mapget_attributes ()
 Returns an tree_map containing all the attributes available within this servlet context. More...
 
const tree_any_mapget_attributes () const
 Const version of get_attributes method. More...
 
template<typename KeyType >
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 parameter, or an empty reference if the parameter does not exist. More...
 
const init_params_mapget_init_parameters () const
 Returns the context's initialization parameters map of std::string objects. More...
 
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. More...
 

Protected Member Functions

 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. More...
 

Protected Attributes

const std::string & _ctx_path
 Context path.
 
tree_any_map _attr_map
 Attributes map (to be filled by inheriting class).
 
init_params_map _init_params_map
 Initial parameters map.
 
const std::string & _webapp_path
 Filesystem path of current webapp.
 

Detailed Description

Defines a set of methods that a servlet uses to communicate with its servlet container.

There is one context per "web application". (A "web application" is a collection of servlets and content installed under a specific subset of the server's URL namespace such as /catalog and possibly installed via a .war file.)

In the case of a web application marked "distributed" in its deployment descriptor. In this situation, the context cannot be used as a location to share global information (because the information won't be truly global). Use an external resource like a database instead.

The servlet_context object is contained within the servlet_config object, which the Web server provides the servlet when the servlet is initialized.

See Also
http_servlet::get_servlet_config
http_filter::get_filter_config
servlet_config::get_servlet_context

Constructor & Destructor Documentation

servlet::servlet_context::servlet_context ( const std::string &  ctx_path,
const std::string &  webapp_path,
init_params_map &&  init_params 
)
inlineprotected

Protected constructor to be used from derrived classes.

Parameters
ctx_pathContext path
webapp_pathFilesystem path to the web application
init_paramsInitial parameters map

Member Function Documentation

template<typename T , typename KeyType >
optional_ref<const T> servlet::servlet_context::get_attribute ( const KeyType &  key) const
inline

Returns the servlet container attribute with the given name, or empty reference if there is no attribute by that name.

An attribute allows a servlet container to give the servlet additional information not already provided by this interface. See your server documentation for information about its attributes. Full attributes map can be retrieved using get_attributes.

The attribute is returned as a reference to any.

Template Parameters
Ttype of the value to return
KeyTypea type comparable to std::string
Parameters
keya std::string specifying the name of the attribute
Returns
a reference to any containing the value of the attribute, or empty reference if no attribute exists matching the given name
Exceptions
any_bad_castif the stored type cannot be casted to the requested type.
See Also
get_attributes
template<typename KeyType , typename ValueType >
optional_ref<const ValueType> servlet::servlet_context::get_attribute ( const KeyType &  key) const
inline

Finds attribute by key and casts it to specified ValueType.

Template Parameters
Ttype of the value to return
KeyTypea type comparable to std::string
Parameters
keyKey to find attribute value by.
Returns
optional reference to attribute corresponding to key
Exceptions
any_bad_castif the stored type cannot be casted to the requested type.
tree_any_map& servlet::servlet_context::get_attributes ( )
inline

Returns an tree_map containing all the attributes available within this servlet context.

Returns
a tree_map of attributes
See Also
get_attribute
const tree_any_map& servlet::servlet_context::get_attributes ( ) const
inline

Const version of get_attributes method.

Returns
a const tree_map of attributes
const std::string& servlet::servlet_context::get_context_path ( ) const
inline

Return the main path associated with this context.

Returns
The main context path
template<typename KeyType >
optional_ref<const std::string> servlet::servlet_context::get_init_parameter ( const KeyType &  key) const
inline

Returns a reference to a std::string containing the value of the named context-wide initialization parameter, or an empty reference if the parameter does not exist.

This method can make available configuration information useful to an entire "web application". For example, it can provide a webmaster's email address or the name of a system that holds critical data.

Template Parameters
KeyTypea type comparable to std::string
Parameters
keya std::string containing the name of the parameter whose value is requested
Returns
a reference to a std::string containing the value of the initialization parameter
See Also
servlet_config::get_init_parameter
const init_params_map& servlet::servlet_context::get_init_parameters ( ) const
inline

Returns the context's initialization parameters map of std::string objects.

The returned map can be empty if the context has no initialization parameters.

Returns
an tree_map of the context's initialization parameters
See Also
servlet_config::get_init_parameters
servlet_config::get_init_parameter
virtual optional_ref<const std::string> servlet::servlet_context::get_mime_type ( string_view  file_name) const
pure virtual

Returns the MIME type of the specified file, or null if the MIME type is not known.

The MIME type is determined by the configuration of the servlet container, and may be specified in a web application deployment descriptor. Common MIME types are "text/html" and "image/gif".

Parameters
file_namea std::string specifying the name of a file
Returns
a std::string specifying the file's MIME type
const std::string& servlet::servlet_context::get_webapp_path ( ) const
inline

Returns filesystem path of current web application.

Returns
The webapp filesystem path.
template<typename KeyType >
bool servlet::servlet_context::remove_attribute ( const KeyType &  key)
inline

Removes the attribute with the given name from the servlet context.

After removal, subsequent calls to get_attribute to retrieve the attribute's value will return an empty reference.

Template Parameters
KeyTypea type comparable to std::string
Parameters
keya string specifying the name of the attribute to be removed
template<typename ValueType >
bool servlet::servlet_context::set_attribute ( const std::string &  key,
ValueType &&  value 
)
inline

Binds an object to a given attribute name in this servlet context.

If the name specified is already used for an attribute, this method will replace the attribute with the new to the new attribute.

Template Parameters
ValueTypetype of the value to set.
Parameters
keya std::string specifying the name of the attribute
valuea value of the arbitrary type which will be stored in any object representing the attribute
Returns
true if value in the map was replaced.
template<typename ValueType >
bool servlet::servlet_context::set_attribute ( std::string &&  key,
ValueType &&  value 
)
inline

Move version of set_attribute(const std::string&, ValueType&&)

Template Parameters
ValueTypetype of the value to set.
Parameters
keya map key by which to store element
valuevalue of the element to store
Returns
true if value in the map was replaced.
See Also
set_attribute(const std::string&, ValueType&&)

The documentation for this class was generated from the following file: