|
#include <Wt/WApplication>
|
|
#include <Wt/WBreak>
|
|
#include <Wt/WContainerWidget>
|
|
#include <Wt/WLineEdit>
|
|
#include <Wt/WPushButton>
|
|
#include <Wt/WText>
|
|
#include <Wt/WResource>
|
|
#include <Wt/Http/Client>
|
|
#include <Wt/Http/Message>
|
|
#include <Wt/Json/Parser>
|
|
#include <Wt/Json/Object>
|
|
#include <Wt/Json/Array>
|
|
#include <Wt/WText>
|
|
#include <Wt/WTextArea>
|
|
#include <iostream>
|
|
using namespace std;
|
|
|
|
class HelloApplication : public Wt::WApplication
|
|
{
|
|
public:
|
|
HelloApplication(const Wt::WEnvironment& env);
|
|
|
|
private:
|
|
Wt::WLineEdit *vehicleModelEdit_;
|
|
Wt::WLineEdit *vehicleRegEdit_;
|
|
Wt::WLineEdit *vehicleMakeEdit_;
|
|
Wt::WLineEdit *vehicleOwnerEdit_;
|
|
Wt::WText *response_;
|
|
Wt::WTextArea *ta;
|
|
void Submit();
|
|
void handleHttpResponse(boost::system::error_code err, const Wt::Http::Message& response);
|
|
};
|
|
|
|
HelloApplication::HelloApplication(const Wt::WEnvironment& env)
|
|
: Wt::WApplication(env)
|
|
{
|
|
|
|
root()->addWidget(new Wt::WBreak());
|
|
Wt::WPushButton *button = new Wt::WPushButton("Submit.", root());
|
|
root()->addWidget(new Wt::WBreak());
|
|
response_ = new Wt::WText(root());
|
|
button->clicked().connect(this, &HelloApplication::Submit);
|
|
|
|
Wt::WContainerWidget *container = new Wt::WContainerWidget();
|
|
ta = new Wt::WTextArea(container);
|
|
ta->setColumns(80);
|
|
ta->setRows(5);
|
|
ta->setText("hi");
|
|
root()->addWidget(new Wt::WBreak());
|
|
root()->addWidget(container);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void HelloApplication::handleHttpResponse(boost::system::error_code err, const Wt::Http::Message& response) {
|
|
|
|
cout << "Respose Handle called" << endl;
|
|
if (!err && response.status() == 200) {
|
|
|
|
cout << response.body() << endl;
|
|
Wt::Json::Value initial;
|
|
Wt::Json::parse(response.body(),initial);
|
|
Wt::Json::Array arr = initial;
|
|
cout << "Size of array is this" << arr.size() << endl;
|
|
Wt::Json::Object obj;
|
|
string queryResult = "";
|
|
for(int i = 0 ; i < arr.size() ; i++) {
|
|
|
|
obj = arr[i];
|
|
queryResult += "Make ";
|
|
string str = obj.get("Make").toString();
|
|
queryResult += str;
|
|
queryResult += "\n";
|
|
queryResult += "Model ";
|
|
string str1 = obj.get("Model").toString();
|
|
queryResult += str1;
|
|
queryResult += "\n";
|
|
queryResult += "Registration ";
|
|
string str2 = obj.get("Reg").toString();
|
|
queryResult += str2;
|
|
queryResult += "\n";
|
|
queryResult += "Owner ";
|
|
string str3 = obj.get("Owner").toString();
|
|
queryResult += str3;
|
|
queryResult += "\n";
|
|
queryResult += "\n";
|
|
}
|
|
|
|
|
|
ta->setText(queryResult);
|
|
|
|
cout << queryResult << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void HelloApplication::Submit()
|
|
{
|
|
//response_->setText("Hello there, " + vehicleModelEdit_->text());
|
|
string model = vehicleModelEdit_->text().toUTF8();
|
|
string make = vehicleMakeEdit_->text().toUTF8();
|
|
string reg = vehicleRegEdit_->text().toUTF8();
|
|
string owner = vehicleOwnerEdit_->text().toUTF8();
|
|
Wt::Http::Client *client = new Wt::Http::Client(this);
|
|
client->setTimeout(15);
|
|
client->setMaximumResponseSize(10 * 1024);
|
|
client->done().connect(boost::bind(&HelloApplication::handleHttpResponse, this, _1, _2));
|
|
Wt::Http::Message::Header head = Wt::Http::Message::Header("Reg", "LED2510");
|
|
std::vector< Wt::Http::Message::Header> headers;
|
|
headers.push_back(head);
|
|
|
|
cout << "Head name is " << head.name();
|
|
cout << "Head value is " << head.value();
|
|
|
|
string URL = "http://localhost:8001/search";
|
|
|
|
if(!model.empty() || !make.empty() || !reg.empty() || !owner.empty()) {
|
|
//Append ?
|
|
URL.append("?");
|
|
}
|
|
|
|
|
|
if(!model.empty()) {
|
|
|
|
string str = "Model=" + model + "&";
|
|
URL+= str;
|
|
}
|
|
|
|
if(!make.empty()) {
|
|
|
|
string str = "Make=" + make + "&";
|
|
URL+= str;
|
|
}
|
|
|
|
if(!reg.empty()) {
|
|
string str = "Reg=" + reg + "&";
|
|
URL+= str;
|
|
|
|
}
|
|
|
|
if(!owner.empty()) {
|
|
|
|
string str = "Owner=" + owner + "&";
|
|
URL+= str;
|
|
}
|
|
|
|
cout << "Hitting this URL" << URL << endl;
|
|
|
|
if (client->get(URL)) {
|
|
cout << "Hit the URL success";
|
|
}
|
|
else {
|
|
cout << "Hit the URL fail";
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Wt::WApplication *createApplication(const Wt::WEnvironment& env)
|
|
{
|
|
return new HelloApplication(env);
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
return Wt::WRun(argc, argv, &createApplication);
|
|
}
|