예제 #1
0
파일: RowFilter.cpp 프로젝트: 12307/poco
void RowFilter::doCompare(Poco::Dynamic::Var& ret,
	Poco::Dynamic::Var& val,
	CompT comp,
	const ComparisonEntry& ce)
{
	if (ret.isEmpty()) ret = comp(val, ce.get<0>());
	else
	{
		if (ce.get<2>() == OP_OR)
			ret = ret || comp(val, ce.get<0>());
		else if (ce.get<2>() == OP_AND)
			ret = ret && comp(val, ce.get<0>());
		else
			throw IllegalStateException("Unknown logical operation.");
	}
}
void Controller::searchSimilarRequest(std::map<std::string, std::string>& params, const Poco::Dynamic::Var& request) {
    Record record;
    vector<uint8_t>* image_data = NULL;
    int count = 10;
    
    if (params.count("blockHash")) {
        record.getHashes()[BlockHash] = str2bin(params["blockHash"]);
    }
    if (params.count("dHash")) {
        record.getHashes()[DHash] = str2bin(params["dHash"]);
    }
    if (params.count("gaussDHash")) {
        record.getHashes()[GaussDHash] = str2bin(params["gaussDHash"]);
    }
    if (params.count("gauss2DHash")) {
        record.getHashes()[Gauss2DHash] = str2bin(params["gauss2DHash"]);
    }
    if (params.count("gaussBlockHash")) {
        record.getHashes()[GaussBlockHash] = str2bin(params["gaussBlockHash"]);
    }
    if (params.count("url")) {
        image_data = Image::fromUrl(params["url"]);
    }
    if (params.count("count")) {
        count = atoi(params["count"].c_str());
    }
    
    if (!request.isEmpty()) {
        Poco::JSON::Object::Ptr json = request.extract<Poco::JSON::Object::Ptr>();
        if (!image_data && 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 (image_data) {
        HashAlgorithmManager::getInstance().attachSimilarHashes(record, image_data);
        delete image_data;
    }
    
    appio::print_ok(HashManager::getInstance().searchSimilar(record, count));
}
예제 #3
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();
}
예제 #4
0
파일: RowFilter.cpp 프로젝트: 12307/poco
bool RowFilter::isAllowed(std::size_t row) const
{
	Poco::Dynamic::Var retVal;
	const RecordSet& rs = recordSet();
	
	std::size_t columns = rs.columnCount();
	ComparisonMap::const_iterator it = _comparisonMap.begin();
	ComparisonMap::const_iterator end = _comparisonMap.end();
	for (; it != end; ++it)
	{
		for (std::size_t col = 0; col < columns; ++col)
		{
			const std::string name = toUpper(rs.metaColumn(static_cast<UInt32>(col)).name());
			if (_comparisonMap.find(name) == _comparisonMap.end()) continue;

			Poco::Dynamic::Var ret;
			CompT compOp = 0;
			Poco::Dynamic::Var val = rs.value(col, row, false);

			switch (it->second.get<1>())
			{
			case VALUE_LESS_THAN:
				compOp = less; break;
			case VALUE_LESS_THAN_OR_EQUAL:
				compOp = lessOrEqual; break;
			case VALUE_EQUAL:
				compOp = equal; break;
			case VALUE_GREATER_THAN:
				compOp = greater; break;
			case VALUE_GREATER_THAN_OR_EQUAL:
				compOp = greaterOrEqual; break;
			case VALUE_NOT_EQUAL:
				compOp = notEqual; break;
			case VALUE_IS_NULL:
				compOp = isNull; break;
			default:
				throw IllegalStateException("Unsupported comparison criteria.");
			}
			
			doCompare(ret, val, compOp, it->second);
			if (retVal.isEmpty()) retVal = ret;
			else retVal = retVal || ret;
		}
	}

	// iterate through children
	FilterMap::const_iterator fIt = _filterMap.begin();
	FilterMap::const_iterator fEnd = _filterMap.end();
	for (; fIt != fEnd; ++fIt)
	{
		if (OP_OR == fIt->second)
		{
			if (retVal.isEmpty())
				retVal = fIt->first->isAllowed(row);
			else
				retVal = retVal || fIt->first->isAllowed(row);
		}
		else if (OP_AND == fIt->second)
		{
			if (retVal.isEmpty())
				retVal = fIt->first->isAllowed(row);
			else
				retVal = retVal && fIt->first->isAllowed(row);
		}
		else
			throw IllegalStateException("Unknown logical operation.");
	}

	if (retVal.isEmpty()) retVal = true; // no filtering found
	return (!_not) && retVal.extract<bool>();
}