C++ Web Framework  3.0
Public Member Functions | List of all members
Filter Class Reference

The Filter class works like a filter. You can use this class to validate sessions or measuring runtime of a specific method, for example. To use this class, you will need to create a derived class and reconstruct the doFilter method, after this, you will need to configure it into the ConfigureCppWebServer, using the setFilter method. More...

#include <filter.h>

Public Member Functions

virtual ~Filter ()
 Virtual destructor.
 
virtual void doFilter (CWF::Request &request, CWF::Response &response, FilterChain &chain)
 This method will be called always that the CppWebServer receives a requisition. More...
 

Detailed Description

The Filter class works like a filter. You can use this class to validate sessions or measuring runtime of a specific method, for example. To use this class, you will need to create a derived class and reconstruct the doFilter method, after this, you will need to configure it into the ConfigureCppWebServer, using the setFilter method.

Member Function Documentation

◆ doFilter()

virtual void Filter::doFilter ( CWF::Request &  request,
CWF::Response &  response,
FilterChain chain 
)
inlinevirtual

This method will be called always that the CppWebServer receives a requisition.

Parameters
request: This is a reference to the Request.
response: This is a reference to the Response.
chain: This is a reference to the FilterChain.
Example
//----loginfilter.h-----
#ifndef LOGINFILTER_H
#define LOGINFILTER_H
#include "cwf/filter.h"
class LoginFilter : public CWF::Filter
{
public:
void doFilter(CWF::Request &request, CWF::Response &response, CWF::FilterChain &chain)
{
QString url = request.getRequestURL();
if(url.endsWith(".css") || url.endsWith(".png") || url.endsWith(".jpg"))
{
chain.doFilter(request, response);
}
else if(url != "/login")
{
if(request.getSession().getAttribute("user") == nullptr || request.getSession().isExpired())
{
request.getRequestDispatcher("/pages/login").forward(request, response);
}
else
{
chain.doFilter(request, response);
}
}
else
{
chain.doFilter(request, response);
}
}
};
#endif // LOGINFILTER_H
//----main.h-----
#include <filter/loginfilter.h>
#include "cwf/cppwebapplication.h"
int main(int argc, char *argv[])
{
CWF::CppWebApplication server(argc,
argv,
"PATH_TO_SERVER_FOLDER",
new LoginFilter);
return server.start();
}

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