Exemple #1
0
void Controller::handle(const std::vector<std::string>& parameters, Poco::Net::HTTPServerRequest& request, Poco::Net::HTTPServerResponse& response)
{
	_parameters = parameters;
	_request = &request;
	_response = &response;

	if ( _parameters.size() > 0 )
	{
		_action = _parameters.front();
		_parameters.erase(_parameters.begin());
	}
	else
	{
		setResponseStatus(Poco::Net::HTTPResponse::HTTP_BAD_REQUEST, "Invalid URI parameters");
		return;
	}

	for(std::vector<std::string>::iterator it = _parameters.begin(); it != _parameters.end(); ++it)
	{
		int pos = it->find_first_of(':');
		if ( pos != std::string::npos )
		{
			std::string name = it->substr(0, pos);
			std::string value = it->substr(pos+1);
			_namedParameters[name] = value;
		}
	}

	std::string contentType = request.getContentType();
	if ( contentType == "application/json" )
	{
		Poco::JSON::Parser parser;
		try
		{
			Poco::Dynamic::Var json = parser.parse(request.stream());
			if ( ! json.isEmpty() && json.type() == typeid(Poco::JSON::Object::Ptr) )
			{
				_data->set("filter", json.extract<Poco::JSON::Object::Ptr>());
			}
		}
		catch(Poco::JSON::JSONException& jsone)
		{
			// Make sure everything is read, otherwise this can result
			// in Bad Request error in the next call.
			Poco::NullOutputStream nos;
			Poco::StreamCopier::copyStream(request.stream(), nos);

			setResponseStatus(Poco::Net::HTTPResponse::HTTP_BAD_REQUEST, "JSON error occurred: " + jsone.displayText());
			return;
		}
	}
	else
	{
		_form.load(request, request.stream(), *this);
	}

	// Make sure everything is read, otherwise this can result
	// in Bad Request error in the next call.
	Poco::NullOutputStream nos;
	Poco::StreamCopier::copyStream(request.stream(), nos);

	beforeAction();

	if ( response.getStatus() != Poco::Net::HTTPResponse::HTTP_OK
		|| _data->has("error") )
	{
		//TODO: return error template file or json error
	}

	const ActionMap& actions = getActions();
	ActionMap::const_iterator it = actions.find(_action);
	if ( it == actions.end() )
	{
		setResponseStatus(Poco::Net::HTTPResponse::HTTP_NOT_FOUND, "Invalid action '" + _action + "' specified.");
		return;
	}

	ActionFn action = it->second;
	(this->*action)();

	afterAction();
}
void Controller::ingestRequest(const Poco::Dynamic::Var& request) {
    if (request.type() != typeid(Poco::JSON::Array::Ptr)) {
        throw RequestException("Invalid JSON format, root must be array.");
    }
    
    try {
        Poco::JSON::Array::Ptr jsonArray = request.extract<Poco::JSON::Array::Ptr>();
        
        for (Poco::JSON::Array::ConstIterator it = jsonArray->begin(); it != jsonArray->end(); it++) {
            Poco::JSON::Object::Ptr json = it->extract<Poco::JSON::Object::Ptr>();
            
            Record record;
            vector<uint8_t>* image_data = NULL;
            
            if (json->has("id")) {
                record.setId(json->getValue<string>("id"));
            }
            if (json->has("thumbnail")) {
                record.setThumbnail(json->getValue<string>("thumbnail"));
            }
            if (json->has("metadata")) {
                record.setMetadata(json->getValue<string>("metadata"));
            }
            if (json->has("blockHash")) {
                record.getHashes()[BlockHash] = str2bin(json->getValue<string>("blockHash"));
            }
            if (json->has("dHash")) {
                record.getHashes()[DHash] = str2bin(json->getValue<string>("dHash"));
            }
            if (json->has("gaussDHash")) {
                record.getHashes()[GaussDHash] = str2bin(json->getValue<string>("gaussDHash"));
            }
            if (json->has("gauss2DHash")) {
                record.getHashes()[Gauss2DHash] = str2bin(json->getValue<string>("gauss2DHash"));
            }
            if (json->has("gaussBlockHash")) {
                record.getHashes()[GaussBlockHash] = str2bin(json->getValue<string>("gaussBlockHash"));
            }
            if (json->has("image_base64")) {
                image_data = Image::fromBase64(json->getValue<string>("image_base64"));
            }
            if (!image_data && json->has("image_url")) {
                image_data = Image::fromUrl(json->getValue<string>("image_url"));
            }
            
            if (json->has("status")) {
                string status = json->getValue<string>("status");
                if (status == "deleted") {
                    deleteRequest(record);
                    continue;
                } else {
                    throw RequestException("Invalid status value.");
                }
            }

            if (image_data) {
                HashAlgorithmManager::getInstance().attachHashes(record, image_data);
                delete image_data;
            }
            putRequest(record);
        }
    } catch (Poco::JSON::JSONException& e) {
        throw RequestException("Invalid JSON format.");
    }
    appio::print_ok("Data was successfully changed.");
}