Esempio n. 1
0
void handle_list(web::http::http_request& request) {
	std::map<jsonextend, std::string> list;
	json::value result;
	try {

		list = g_mainDB->getAllKV();
	} catch (std::exception const & ex) {
		LOG(error)<<"DB Error "<<ex.what();
		result["status"] = json::value::string("ERROR");
		result["error_msg"] = json::value::string(ex.what());
		request.reply(status_codes::InternalError, result).wait();
		return;
	}
	uint32_t counter = 0;
	json::value tmp;
	result[0] = tmp;
	for (auto iterator = list.begin(); iterator != list.end(); iterator++) {
		tmp["waveProperties"] = iterator->first;
		tmp["command"] = json::value::string(iterator->second);
		result[counter++] = tmp;
	}

	request.reply(status_codes::OK, result).wait();

}
Esempio n. 2
0
      void CartController::handle_get(web::http::http_request request)
      {

        web::http::http_response response;

        granada::http::session::MapSession simple_session(request,response);
        business::Cart cart(&simple_session);

        auto paths = uri::split_path(uri::decode(request.relative_uri().path()));
        if (!paths.empty()){
          std::string name = utility::conversions::to_utf8string(paths[0]);

          if(name == "count"){

            // returns the number of products added to the cart.
            int count = cart.Count();
            response.set_body("{\"count\":\"" + std::to_string(count) + "\"}");

          }else if (name == "list"){

            // returns a list with the products added to the cart but only
            // if the user is logged in.
            if (simple_session.roles()->Is("USER")){
              response.set_body("{\"data\":" + cart.List() + "}");
            }else{
              response.set_body("{\"data\":[]}");
            }
          }
        }

        response.set_status_code(status_codes::OK);
        response.headers().add(U("Content-Type"), U("text/json; charset=utf-8"));

        request.reply(response);
      }
Esempio n. 3
0
void handle_execute(web::http::http_request& request) {

	json::value response;
	concurrency::streams::istream body = request.body();
	uint64_t content_lenght = request.headers().content_length();
	LOG(debug)<<"Content lenght of request "<<content_lenght;
	if (content_lenght == 0) {
		LOG(error)<<"Bad request! Empty body";
		response["error_msg"] = json::value::string("Bad request.Empty body!");
		response["status"] = json::value::string("ERROR");
		request.reply(status_codes::BadRequest, response).wait();
		return;

	}
	uint8_t * waveData = new uint8_t[content_lenght];
	Concurrency::streams::rawptr_buffer<uint8_t> buffer(waveData,
			content_lenght);
	body.read(buffer, content_lenght).get();
	WaveFile wave(waveData, content_lenght);
	delete[] waveData;
	Samples waveSamples(wave);
	jsonextend wavePropertiesJSON = g_processAndAnlyze.getSummary(waveSamples);

	std::map<jsonextend, std::string> list;
	list = g_mainDB->getAllKV();
	auto tmpFun = g_policies[DEFAULT_POLICY];

	std::string command = tmpFun(list, wavePropertiesJSON);

	if (!command.empty()) {

		web::json::value cmdResult = Runner::run(command, " ");
		response["status"] = json::value::string("OK");
		response["command"] = json::value::string(command);
		response["command_ret"] = cmdResult;
		request.reply(status_codes::OK, response).wait();
		return;
	}

	response["status"] = json::value::string("NOT_FOUND");

	request.reply(status_codes::OK, response).wait();

}
Esempio n. 4
0
      void CartController::handle_put(web::http::http_request request)
      {
        web::http::http_response response;
        granada::http::session::MapSession simple_session(request,response);
        business::Cart cart(&simple_session);

        std::string product_id = "";
        int quantity = 0;
        std::string body = request.extract_utf8string(true).get();
        std::unordered_map<std::string,std::string> parsed_query = granada::http::parser::ParseQueryString(body);

        auto it = parsed_query.find("id");
        if (it != parsed_query.end()){
          product_id.assign(it->second);
        }

        auto it2 = parsed_query.find("quantity");
        if (it2 != parsed_query.end()){
          quantity = std::atoi(it2->second.c_str());
        }

        int count = 0;
        if (product_id.empty()){

          // invalid product id, do not add the product,
          // just return the number of products already added.
          count = cart.Count();

        }else{

          // adds a product to the cart.
          count = cart.Add(product_id,quantity);

        }

        response.set_status_code(status_codes::OK);
        response.headers().add(U("Content-Type"), U("text/json; charset=utf-8"));
        response.set_body("{\"count\":\"" + std::to_string(count) + "\"}");
        request.reply(response);
      }
Esempio n. 5
0
void handle_add(web::http::http_request& request) {

	json::value response;
	std::map<std::string, std::string> querymap = uri::split_query(
			request.relative_uri().query());
	if (querymap.find("name") == querymap.end()
			|| querymap.find("command") == querymap.end()) {
		LOG(error)<<"Bad request";
		response["error_msg"] = json::value::string("Bad request. Read doc for info. Missing name or command field in request.");
		response["status"] = json::value::string("ERROR");
		request.reply(status_codes::BadRequest, response).wait();
		return;

	}
	std::string commandName = uri::decode(querymap["name"]);
	std::string commandString = uri::decode(querymap["command"]);
	LOG(debug)<<"Add command name "<<commandName<<" command string: "<<commandString;
	concurrency::streams::istream body = request.body();
	uint64_t content_lenght = request.headers().content_length();
	LOG(debug)<<"Content lenght of request "<<content_lenght;
	if (content_lenght == 0) {
		LOG(error)<<"Bad request! Empty body";
		response["error_msg"] = json::value::string("Bad request.Empty body!");
		response["status"] = json::value::string("ERROR");
		request.reply(status_codes::BadRequest, response).wait();
		return;

	}
	try {
		uint8_t * waveData = new uint8_t[content_lenght];
		Concurrency::streams::rawptr_buffer<uint8_t> buffer(waveData,
				content_lenght);
		//body.read_to_end(buffer).get();
		body.read(buffer, content_lenght).get();
		WaveFile wave(waveData, content_lenght);
		delete[] waveData;
		Samples waveSamples(wave);
		jsonextend wavepropertiesJSON = g_processAndAnlyze.getSummary(waveSamples);
		wavepropertiesJSON["name"] =  web::json::value::string(commandName);

		try {
			LOG(debug)<<"Saving "<<wavepropertiesJSON.to_string();
			g_mainDB->put(wavepropertiesJSON, commandString);
		} catch (std::exception const & ex) {
			LOG(error)<<"Error "<<ex.what();
			response["status"] = json::value::string("ERROR");
			response["error_msg"] = json::value::string(ex.what());
			request.reply(status_codes::InternalError, response).wait();
			return;
		}
		response["status"] = json::value::string("OK");
		response["command"] = json::value::string(commandString);
		request.reply(status_codes::OK, response).wait();
	} catch (std::exception &e) {

		LOG(error)<<"Error "<<e.what();
		response["status"] = json::value::string("ERROR");
		response["command"] = json::value::string(e.what());
		request.reply(status_codes::BadRequest, response).wait();
	}

}
Esempio n. 6
0
void handle_test(web::http::http_request& request) {
	json::value result;
	result["status"] = json::value::string("OK");
	request.reply(status_codes::OK, result).wait();
}