Skip to content

Filters

Sumeet Chhetri edited this page Sep 13, 2018 · 2 revisions

Filter

Filters provide filtering tasks either on the request or the response or both. Filters can be chained, multiple Input/Output filters can be applied to an incoming request or be used to transform the output response. The Handle Filter is simply a full blown Controller and can take complete control of the request/response exactly like a Controller.

  1. In Filter

    Responsible for filtering the incoming request entity. It can be implemented with the following method signature,

    void doInputFilter(HttpRequest *req)

    Marker Based

    (Header)

    #ifndef DEFAULTIOFILTER_H_
    #define DEFAULTIOFILTER_H_
    
    #include "Filter.h"
    #include <iostream>
    
    #pragma @Filter type="in" path="*"
    class DefaultIOFilter {
    public:
    	void doInputFilter(HttpRequest *req);
    };
    
    #endif

    (Source)

    #include "DefaultIOFilter.h"
    
    void DefaultIOFilter::doInputFilter(HttpRequest *req)
    {
    	//Process the input request here...
    	cout << "Processed input request" << endl;
    }

    XML Based

    (Header)

    #ifndef DEFAULTIOFILTER_H_
    #define DEFAULTIOFILTER_H_
    
    #include "Filter.h"
    #include <iostream>
    
    class DefaultIOFilter: public Filter {
    public:
    	void doInputFilter(HttpRequest *req);
    };
    
    #endif

    (Source)

    #include "DefaultIOFilter.h"
    
    void DefaultIOFilter::doInputFilter(HttpRequest *req)
    {
    	//Process the input request here...
    	cout << "Processed input request" << endl;
    }

    (Configuration)

    <app>
    	<filters>
    		<!-- All requests will be processed by 'DefaultIOFilter' filter -->
    		<filter class="DefaultIOFilter" type="in" />
    	</filters>
    </app>
  2. Out Filter

    Responsible for filtering the outgoing response entity. It can be implemented with the following method signature,

    void doOutputFilter(HttpResponse *req)

    Marker Based

    (Header)

    #ifndef DEFAULTIOFILTER_H_
    #define DEFAULTIOFILTER_H_
    
    #include "Filter.h"
    #include <iostream>
    
    #pragma @Filter type="out" path="*"
    class DefaultIOFilter {
    public:
    	void doOutputFilter(HttpResponse *req);
    };
    
    #endif

    (Source)

    #include "DefaultIOFilter.h"
    
    void DefaultIOFilter::doOutputFilter(HttpResponse *req)
    {
    	//Process the output response here...
    	cout << "Processed output response" << endl;
    }

    XML Based

    (Header)

    #ifndef DEFAULTIOFILTER_H_
    #define DEFAULTIOFILTER_H_
    
    #include "Filter.h"
    #include <iostream>
    
    class DefaultIOFilter: public Filter {
    public:
    	void doOutputFilter(HttpResponse *req);
    };
    
    #endif

    (Source)

    #include "DefaultIOFilter.h"
    
    void DefaultIOFilter::doOutputFilter(HttpResponse *req)
    {
    	//Process the output response here...
    	cout << "Processed output response" << endl;
    }

    (Configuration)

    <app>
    	<filters>
    		<!-- All responses will be processed by 'DefaultIOFilter' filter -->
    		<filter class="DefaultIOFilter" type="out" />
    	</filters>
    </app>
  3. Handle Filter

    Responsible for filtering both the incoming request as well as the response entity. It can be implemented with the following method signature,

    bool doHandle(HttpRequest *req, HttpResponse* res)

    The following examples combine all the filter type examples below.

    Marker Based

    (Header)

    #ifndef DEFAULTIOFILTER_H_
    #define DEFAULTIOFILTER_H_
    
    #include "Filter.h"
    #include <iostream>
    
    #pragma @Filter type="in" path="*"
    #pragma @Filter type="out" path="*"
    #pragma @Filter type="handle" path="*.filter"
    class DefaultIOFilter {
    public:
    	void doInputFilter(HttpRequest *req);
    	void doOutputFilter(HttpResponse *res);
    	bool doHandle(HttpRequest *req, HttpResponse* res);
    };
    
    #endif

    (Source)

    #include "DefaultIOFilter.h"
    
    void DefaultIOFilter::doInputFilter(HttpRequest *req)
    {
    	//Process the input request here...
    	cout << "Processed input request" << endl;
    }
    
    void DefaultIOFilter::doOutputFilter(HttpResponse *res)
    {
    	//Process the output response here...
    	cout << "Processed output response" << endl;
    }
    
    bool DefaultIOFilter::doHandle(HttpRequest *req, HttpResponse* res)
    {
    	bool continue_proc_request = false;
    	res->setContent("Filter handled your request as you visited a *.filter location");
    	res->setHTTPResponseStatus(HTTPResponseStatus::Ok);
    	return continue_proc_request;
    }

    XML Based

    (Header)

    #ifndef DEFAULTIOFILTER_H_
    #define DEFAULTIOFILTER_H_
    
    #include "Filter.h"
    #include <iostream>
    
    class DefaultIOFilter: public Filter {
    public:
    	void doInputFilter(HttpRequest *req);
    	void doOutputFilter(HttpResponse *res);
    	bool doHandle(HttpRequest *req, HttpResponse* res);
    };
    
    #endif

    (Source)

    #include "DefaultIOFilter.h"
    
    void DefaultIOFilter::doInputFilter(HttpRequest *req)
    {
    	//Process the input request here...
    	cout << "Processed input request" << endl;
    }
    
    void DefaultIOFilter::doOutputFilter(HttpResponse *res)
    {
    	//Process the output response here...
    	cout << "Processed output response" << endl;
    }
    
    bool DefaultIOFilter::doHandle(HttpRequest *req, HttpResponse* res)
    {
    	bool continue_proc_request = false;
    	res->setContent("Filter handled your request as you visited a *.filter location");
    	res->setHTTPResponseStatus(HTTPResponseStatus::Ok);
    	return continue_proc_request;
    }

    (Configuration)

    <app>
    	<filters>
    		<!-- All requests with the URL pattern matching *.filter will be processed by 'DefaultIOFilter' filter -->
    		<filter class="DefaultIOFilter" type="handle" path="*.filter" />
    		<!-- All requests will be processed by 'DefaultIOFilter' filter -->
    		<filter class="DefaultIOFilter" type="in" />
    		<!-- All responses will be processed by 'DefaultIOFilter' filter -->					
    		<filter class="DefaultIOFilter" type="out" />
    	</filters>
    </app>
Clone this wiki locally