From 8e870ed84cb86894f6051db2fdede88990b3683b Mon Sep 17 00:00:00 2001 From: Rajith Muditha Attapattu Date: Mon, 12 Apr 2010 21:10:29 +0000 Subject: Simplified the vaidate method using for_each to make it more readable. Also renamed AclProperty to PropertyType to avoid any confusion with acl::Property. git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk/qpid@933417 13f79535-47bb-0310-9956-ffa450edef68 --- cpp/src/qpid/acl/AclValidator.cpp | 71 ++++++++++++++++++++++----------------- cpp/src/qpid/acl/AclValidator.h | 34 +++++++++---------- 2 files changed, 56 insertions(+), 49 deletions(-) (limited to 'cpp/src') diff --git a/cpp/src/qpid/acl/AclValidator.cpp b/cpp/src/qpid/acl/AclValidator.cpp index 7c7fb231b5..aeaf638f54 100644 --- a/cpp/src/qpid/acl/AclValidator.cpp +++ b/cpp/src/qpid/acl/AclValidator.cpp @@ -22,16 +22,17 @@ #include "qpid/log/Statement.h" #include "qpid/sys/IntegerTypes.h" #include +#include #include #include namespace qpid { namespace acl { -AclValidator::AclIntProperty::AclIntProperty(int64_t i,int64_t j) : min(i), max(j){ +AclValidator::IntPropertyType::IntPropertyType(int64_t i,int64_t j) : min(i), max(j){ } -bool AclValidator::AclIntProperty::validate(const std::string& val) { +bool AclValidator::IntPropertyType::validate(const std::string& val) { int64_t v; try { @@ -47,16 +48,16 @@ bool AclValidator::AclIntProperty::validate(const std::string& val) { } } -std::string AclValidator::AclIntProperty::allowedValues() { +std::string AclValidator::IntPropertyType::allowedValues() { return "values should be between " + boost::lexical_cast(min) + " and " + boost::lexical_cast(max); } -AclValidator::AclEnumProperty::AclEnumProperty(std::vector& allowed): values(allowed){ +AclValidator::EnumPropertyType::EnumPropertyType(std::vector& allowed): values(allowed){ } -bool AclValidator::AclEnumProperty::validate(const std::string& val) { +bool AclValidator::EnumPropertyType::validate(const std::string& val) { for (std::vector::iterator itr = values.begin(); itr != values.end(); ++itr ){ if (val.compare(*itr) == 0){ return 1; @@ -66,7 +67,7 @@ bool AclValidator::AclEnumProperty::validate(const std::string& val) { return 0; } -std::string AclValidator::AclEnumProperty::allowedValues() { +std::string AclValidator::EnumPropertyType::allowedValues() { std::ostringstream oss; oss << "possible values are one of { "; for (std::vector::iterator itr = values.begin(); itr != values.end(); itr++ ){ @@ -78,21 +79,21 @@ std::string AclValidator::AclEnumProperty::allowedValues() { AclValidator::AclValidator(){ validators.insert(Validator(acl::PROP_MAXQUEUESIZE, - boost::shared_ptr( - new AclIntProperty(0,std::numeric_limits::max())) + boost::shared_ptr( + new IntPropertyType(0,std::numeric_limits::max())) ) ); validators.insert(Validator(acl::PROP_MAXQUEUECOUNT, - boost::shared_ptr( - new AclIntProperty(0,std::numeric_limits::max())) + boost::shared_ptr( + new IntPropertyType(0,std::numeric_limits::max())) ) ); std::string policyTypes[] = {"ring", "ring_strict", "flow_to_disk", "reject"}; std::vector v(policyTypes, policyTypes + sizeof(policyTypes) / sizeof(std::string)); validators.insert(Validator(acl::PROP_POLICYTYPE, - boost::shared_ptr(new AclEnumProperty(v)) + boost::shared_ptr(new EnumPropertyType(v)) ) ); @@ -112,30 +113,38 @@ void AclValidator::validate(boost::shared_ptr d) { if (d->actionList[cnt][cnt1]){ - for (AclData::actObjItr actionMapItr = d->actionList[cnt][cnt1]->begin(); - actionMapItr != d->actionList[cnt][cnt1]->end(); actionMapItr++) { - - for (AclData::ruleSetItr i = actionMapItr->second.begin(); i < actionMapItr->second.end(); i++) { - - for (AclData::propertyMapItr pMItr = i->props.begin(); pMItr != i->props.end(); pMItr++) { - - ValidatorItr itr = validators.find(pMItr->first); - if (itr != validators.end()){ - QPID_LOG(debug,"Found validator for property " << itr->second->allowedValues()); - - if (!itr->second->validate(pMItr->second)){ - throw Exception( pMItr->second + " is not a valid value for '" + - AclHelper::getPropertyStr(pMItr->first) + "', " + - itr->second->allowedValues()); - }//if - }//if - }//for - }//for - }//for + std::for_each(d->actionList[cnt][cnt1]->begin(), + d->actionList[cnt][cnt1]->end(), + boost::bind(&AclValidator::validateRuleSet, this, _1)); }//if }//for }//if }//for } +void AclValidator::validateRuleSet(std::pair& rules){ + std::for_each(rules.second.begin(), + rules.second.end(), + boost::bind(&AclValidator::validateRule, this, _1)); +} + +void AclValidator::validateRule(qpid::acl::AclData::rule& rule){ + std::for_each(rule.props.begin(), + rule.props.end(), + boost::bind(&AclValidator::validateProperty, this, _1)); +} + +void AclValidator::validateProperty(std::pair& prop){ + ValidatorItr itr = validators.find(prop.first); + if (itr != validators.end()){ + QPID_LOG(debug,"Found validator for property " << itr->second->allowedValues()); + + if (!itr->second->validate(prop.second)){ + throw Exception( prop.second + " is not a valid value for '" + + AclHelper::getPropertyStr(prop.first) + "', " + + itr->second->allowedValues()); + } + } +} + }} diff --git a/cpp/src/qpid/acl/AclValidator.h b/cpp/src/qpid/acl/AclValidator.h index 333547649d..966e5d326b 100644 --- a/cpp/src/qpid/acl/AclValidator.h +++ b/cpp/src/qpid/acl/AclValidator.h @@ -33,49 +33,47 @@ namespace acl { class AclValidator { /* Base Property */ - class AclProperty{ - public: - enum PropertyType { INT, STRING, ENUM }; - + class PropertyType{ + public: - virtual ~AclProperty(){}; - virtual int getType()=0; + virtual ~PropertyType(){}; virtual bool validate(const std::string& val)=0; virtual std::string allowedValues()=0; }; - class AclIntProperty : public AclProperty{ + class IntPropertyType : public PropertyType{ int64_t min; int64_t max; public: - AclIntProperty(int64_t min,int64_t max); - virtual ~AclIntProperty (){}; - int getType(){ return AclProperty::INT; } + IntPropertyType(int64_t min,int64_t max); + virtual ~IntPropertyType (){}; virtual bool validate(const std::string& val); virtual std::string allowedValues(); }; - class AclEnumProperty : public AclProperty{ + class EnumPropertyType : public PropertyType{ std::vector values; public: - AclEnumProperty(std::vector& allowed); - virtual ~AclEnumProperty (){}; - int getType(){ return AclProperty::ENUM; } + EnumPropertyType(std::vector& allowed); + virtual ~EnumPropertyType (){}; virtual bool validate(const std::string& val); virtual std::string allowedValues(); }; - typedef std::pair > Validator; - typedef std::map > ValidatorMap; + typedef std::pair > Validator; + typedef std::map > ValidatorMap; typedef ValidatorMap::iterator ValidatorItr; ValidatorMap validators; public: - - void validate(boost::shared_ptr d); + + void validateRuleSet(std::pair& rules); + void validateRule(qpid::acl::AclData::rule& rule); + void validateProperty(std::pair& prop); + void validate(boost::shared_ptr d); AclValidator(); ~AclValidator(); }; -- cgit v1.2.1