From 8565fcc8631ccfa36836fcde4e1aebac476022a8 Mon Sep 17 00:00:00 2001 From: Rajith Muditha Attapattu Date: Thu, 8 Apr 2010 22:12:03 +0000 Subject: Committing the patch attached to QPID-2488 git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk/qpid@932148 13f79535-47bb-0310-9956-ffa450edef68 --- cpp/src/qpid/acl/AclValidator.cpp | 141 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 cpp/src/qpid/acl/AclValidator.cpp (limited to 'cpp/src/qpid/acl/AclValidator.cpp') diff --git a/cpp/src/qpid/acl/AclValidator.cpp b/cpp/src/qpid/acl/AclValidator.cpp new file mode 100644 index 0000000000..7c7fb231b5 --- /dev/null +++ b/cpp/src/qpid/acl/AclValidator.cpp @@ -0,0 +1,141 @@ +/* + * + * Copyright (c) 2006 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include "qpid/acl/AclValidator.h" +#include "qpid/acl/AclData.h" +#include "qpid/Exception.h" +#include "qpid/log/Statement.h" +#include "qpid/sys/IntegerTypes.h" +#include +#include +#include + +namespace qpid { +namespace acl { + +AclValidator::AclIntProperty::AclIntProperty(int64_t i,int64_t j) : min(i), max(j){ +} + +bool AclValidator::AclIntProperty::validate(const std::string& val) { + int64_t v; + try + { + v = boost::lexical_cast(val); + }catch(const boost::bad_lexical_cast& e){ + return 0; + } + + if (v < min || v >= max){ + return 0; + }else{ + return 1; + } +} + +std::string AclValidator::AclIntProperty::allowedValues() { + return "values should be between " + + boost::lexical_cast(min) + " and " + + boost::lexical_cast(max); +} + +AclValidator::AclEnumProperty::AclEnumProperty(std::vector& allowed): values(allowed){ +} + +bool AclValidator::AclEnumProperty::validate(const std::string& val) { + for (std::vector::iterator itr = values.begin(); itr != values.end(); ++itr ){ + if (val.compare(*itr) == 0){ + return 1; + } + } + + return 0; +} + +std::string AclValidator::AclEnumProperty::allowedValues() { + std::ostringstream oss; + oss << "possible values are one of { "; + for (std::vector::iterator itr = values.begin(); itr != values.end(); itr++ ){ + oss << "'" << *itr << "' "; + } + oss << "}"; + return oss.str(); +} + +AclValidator::AclValidator(){ + validators.insert(Validator(acl::PROP_MAXQUEUESIZE, + boost::shared_ptr( + new AclIntProperty(0,std::numeric_limits::max())) + ) + ); + + validators.insert(Validator(acl::PROP_MAXQUEUECOUNT, + boost::shared_ptr( + new AclIntProperty(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)) + ) + ); + +} + +AclValidator::~AclValidator(){ +} + +/* Iterate through the data model and validate the parameters. */ +void AclValidator::validate(boost::shared_ptr d) { + + for (unsigned int cnt=0; cnt< qpid::acl::ACTIONSIZE; cnt++){ + + if (d->actionList[cnt]){ + + for (unsigned int cnt1=0; cnt1< qpid::acl::OBJECTSIZE; cnt1++){ + + 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 + }//if + }//for + }//if + }//for +} + +}} -- cgit v1.2.1