-
Notifications
You must be signed in to change notification settings - Fork 146
Controllers
Controllers provide custom execution logic for configured HTTP requests either directly modifying the response or in some way responsible for the eventual HTTP response outcome. A controller has complete access to the HTTP request and the response entities. It can either change the response and return immediately or can allow the request to continue execution even after the controller completes its logic.
-
Full Controller
A full controller can be implemented with the following method signature,
bool service(HttpRequest* req, HttpResponse* res)
Either the Controller class can be extended or simply the method signature can be implemented, the boolean return value decides whether the request processing stops or continues after this controller is executed.
Every controller needs to be mapped to requests by providing wildcard based url patterns, this configuration can be either done by markers or xml configuration.
Marker Based
(Header)
#ifndef DEFAULTCONTROLLER_H_ #define DEFAULTCONTROLLER_H_ #include <iostream> #include "Controller.h" #pragma @Controller path="*.action" #pragma @Controller path="*.do" class DefaultController { public: bool service(HttpRequest* req, HttpResponse* res); }; #endif
(Source)
#include "DefaultController.h" bool DefaultController::service(HttpRequest* req, HttpResponse* res) { res->setHTTPResponseStatus(HTTPResponseStatus:ok); res->addHeaderValue(HttpResponse::ContentType, "text/plain"); res->setContent("Hello World"); return true; }
XML Based
(Header)
#ifndef DEFAULTCONTROLLER_H_ #define DEFAULTCONTROLLER_H_ #include <iostream> #include "Controller.h" class DefaultController : public Controller { public: bool service(HttpRequest* req, HttpResponse* res); }; #endif
(Source)
#include "DefaultController.h" bool DefaultController::service(HttpRequest* req, HttpResponse* res) { res->setHTTPResponseStatus(HTTPResponseStatus:ok); res->addHeaderValue(HttpResponse::ContentType, "text/plain"); res->setContent("Hello World"); return true; }
(Configuration)
<app> <controllers> <!-- All requests with the extension .action will be processed by 'DefaultController' controller --> <controller class="DefaultController" path="*.action" /> <!-- All requests with the extension .do will be processed by 'DefaultController' controller --> <controller class="DefaultController" path="*.do" /> </controllers> </app>
-
URL Mapping Controller
Here a request URL is simply mapped to another URL so that the HTTP request target URL will change to the newly mapped URL and the request processing will continue thereafter as usual.
(Configuration)
<app> <controllers> <controller from="/some/path" to="/somenew/path" /> </controllers> </app>
-
Extension Mapping Controller
This is similar to the URL mapping controller except that in place of the entire URL just the extension is converted or mapped.
(Configuration)
<app> <controllers> <controller fromext="yourext" toext="html" /> </controllers> </app>