5 #include "RelationalAccess/AuthenticationCredentials.h"
6 #include "CoralCommon/Cipher.h"
7 #include "RelationalAccess/AuthenticationServiceException.h"
8 #include "CoralKernel/IPropertyManager.h"
9 #include "CoralKernel/Property.h"
10 #include "CoralKernel/Context.h"
13 #include "xercesc/parsers/XercesDOMParser.hpp"
14 #include "xercesc/framework/MemBufInputSource.hpp"
15 #include "xercesc/dom/DOM.hpp"
16 #include "xercesc/sax/HandlerBase.hpp"
17 #include "xercesc/util/XMLString.hpp"
18 #include "xercesc/util/PlatformUtils.hpp"
26 #include <boost/filesystem.hpp>
27 #include <boost/version.hpp>
28 #include <boost/bind.hpp>
30 #include "CoralBase/MessageStream.h"
36 m_serviceName( serviceName ),
37 m_connectionName( connectionName ),
38 m_default( new coral::AuthenticationCredentials( serviceName ) ),
45 for ( std::map< std::string, coral::AuthenticationCredentials* >::iterator iData = m_data.begin();
46 iData != m_data.end(); ++iData )
54 m_default->registerItem( item, value );
62 std::map< std::string, coral::AuthenticationCredentials* >::iterator iRole = m_data.find( role );
63 if ( iRole == m_data.end() ) {
64 iRole = m_data.insert( std::make_pair( role,
new coral::AuthenticationCredentials( m_serviceName ) ) ).first;
66 iRole->second->registerItem( item, value );
69 const coral::IAuthenticationCredentials&
75 const coral::IAuthenticationCredentials&
91 m_isInitialized(
false ),
92 m_inputFileName(
"" ),
108 for ( std::map< std::string, cond::XMLAuthenticationService::DataSourceEntry* >::iterator iConnection = m_data.begin();
109 iConnection != m_data.end(); ++iConnection )
delete iConnection->second;
116 if(boost::filesystem::is_directory(boostAuthPath)){
120 m_inputFileName = boostAuthPath.string();
127 coral::MessageStream
log(
"cond::XMLAuthenticationService::processFile");
134 inputFile.
read(inputFileName);
137 log << coral::Error <<
"File \"" << inputFileName <<
"\" not found."<<
std::string(exc.
what())<<coral::MessageStream::endmsg;
143 #if (BOOST_VERSION / 100000) >= 1 && ((BOOST_VERSION / 100) % 1000) >= 47
169 xercesc::MemBufInputSource* memBufInputSource = 0;
173 xercesc::XercesDOMParser
parser;
174 parser.setValidationScheme( xercesc::XercesDOMParser::Val_Always );
175 parser.setDoNamespaces(
true );
177 xercesc::HandlerBase errorHandler;
178 parser.setErrorHandler( &errorHandler );
181 const char* bufferId=
"InMemoryDocument";
182 const char* buffer = cont.c_str();
184 memBufInputSource =
new xercesc::MemBufInputSource( (
const XMLByte*)buffer,strlen(buffer),bufferId,
false );
186 parser.parse(*memBufInputSource);
188 xercesc::DOMDocument* document = parser.getDocument();
191 xercesc::XMLString::transcode(
"connection", tempStr, 19);
193 xercesc::DOMNodeList* connectionList = document->getElementsByTagName( tempStr );
195 if ( connectionList )
197 XMLSize_t numberOfConnections = connectionList->getLength();
199 for ( XMLSize_t iConnection = 0; iConnection < numberOfConnections; ++iConnection )
201 xercesc::DOMNode* connectionNode = connectionList->item( iConnection );
203 if ( connectionNode )
205 char* connectionName = xercesc::XMLString::transcode( connectionNode->getAttributes()->item( 0 )->getNodeValue() );
211 std::map< std::string, cond::XMLAuthenticationService::DataSourceEntry* >::iterator iConnection = m_data.find( sConnectionName );
212 if ( iConnection != m_data.end() ) {
213 credential = iConnection->second;
216 log<<
coral::Debug<<
"Credential parameters for connection string \""
218 <<
"\" have already been defined. Only new elements are appended, while existing will be ignored."
219 << coral::MessageStream::endmsg;
222 m_data.insert( std::make_pair( sConnectionName, credential ) );
225 xercesc::DOMNodeList* parameterList = connectionNode->getChildNodes();
229 XMLSize_t numberOfParameters = parameterList->getLength();
231 for ( XMLSize_t iParameter = 0; iParameter < numberOfParameters; ++iParameter )
233 xercesc::DOMNode* parameterNode = parameterList->item( iParameter );
235 if ( parameterNode && parameterNode->getNodeType() == xercesc::DOMNode::ELEMENT_NODE )
237 char* nodeName = xercesc::XMLString::transcode( parameterNode->getNodeName() );
241 if ( sNodeName ==
"parameter" ) {
242 char* parameterName = xercesc::XMLString::transcode( parameterNode->getAttributes()->item( 0 )->getNodeValue() );
245 char* parameterValue = xercesc::XMLString::transcode( parameterNode->getAttributes()->item( 1 )->getNodeValue() );
251 else if ( sNodeName ==
"role" ) {
252 char* roleName = xercesc::XMLString::transcode( parameterNode->getAttributes()->item( 0 )->getNodeValue() );
257 xercesc::DOMNodeList* roleParameterList = parameterNode->getChildNodes();
260 if ( roleParameterList )
262 XMLSize_t numberOfRoleParameters = roleParameterList->getLength();
264 for ( XMLSize_t iRoleParameter = 0; iRoleParameter < numberOfRoleParameters; ++iRoleParameter )
266 xercesc::DOMNode* roleParameterNode = roleParameterList->item( iRoleParameter );
267 if ( roleParameterNode && roleParameterNode->getNodeType() == xercesc::DOMNode::ELEMENT_NODE )
269 char* roleNodeName = xercesc::XMLString::transcode( roleParameterNode->getNodeName() );
273 if ( sRoleNodeName ==
"parameter" ) {
274 char* roleParameterName = xercesc::XMLString::transcode( roleParameterNode->getAttributes()->item( 0 )->getNodeValue() );
275 std::string sRoleParameterName = roleParameterName;
277 char* roleParameterValue = xercesc::XMLString::transcode( roleParameterNode->getAttributes()->item( 1 )->getNodeValue() );
278 std::string sRoleParameterValue = roleParameterValue;
296 catch (
const xercesc::XMLException& toCatch )
298 char*
message = xercesc::XMLString::transcode( toCatch.getMessage() );
301 log<<coral::Error<<
std::string(message)<<coral::MessageStream::endmsg;
305 catch (
const xercesc::DOMException& toCatch )
307 char*
message = xercesc::XMLString::transcode( toCatch.msg );
310 log<<coral::Error<<
std::string(message)<<coral::MessageStream::endmsg;
314 catch (
const xercesc::SAXException& toCatch )
316 char*
message = xercesc::XMLString::transcode( toCatch.getMessage() );
319 log<<coral::Error<<
std::string(message)<<coral::MessageStream::endmsg;
326 log<<coral::Error<<
"Unexpected Exception parsing file \"" << inputFileName <<
"\"" <<coral::MessageStream::endmsg;
329 if(memBufInputSource)
delete memBufInputSource;
337 coral::MessageStream
log(
"cond::XMLAuthenticationService::initialize");
339 if ( inputFileNames.empty() )
343 log<<
coral::Debug<<
"Could not open \"" << m_inputFileName <<
"\" for reading" <<coral::MessageStream::endmsg;
351 catch (
const xercesc::XMLException& toCatch )
353 char*
message = xercesc::XMLString::transcode( toCatch.getMessage() );
356 log<<coral::Error<<
std::string(message)<<coral::MessageStream::endmsg;
362 for ( std::set< std::string >::const_reverse_iterator iFileName = inputFileNames.rbegin();
363 iFileName != inputFileNames.rend(); ++iFileName ) {
372 if(!m_isInitialized)
reset();
377 for ( std::map< std::string, cond::XMLAuthenticationService::DataSourceEntry* >::iterator iConnection = m_data.begin();
378 iConnection != m_data.end(); ++iConnection )
delete iConnection->second;
380 m_isInitialized =
false;
385 const coral::IAuthenticationCredentials&
388 boost::mutex::scoped_lock
lock(m_mutexLock);
389 if ( ! m_isInitialized ) {
392 std::map< std::string, cond::XMLAuthenticationService::DataSourceEntry* >::const_iterator iConnection = m_data.find( connectionString );
393 if ( iConnection == m_data.end() )
394 throw coral::UnknownConnectionException( this->
name(), connectionString );
395 return iConnection->second->credentials();
399 const coral::IAuthenticationCredentials&
403 boost::mutex::scoped_lock
lock(m_mutexLock);
404 if ( ! m_isInitialized ) {
407 std::map< std::string, cond::XMLAuthenticationService::DataSourceEntry* >::const_iterator iConnection = m_data.find( connectionString );
408 if ( iConnection == m_data.end() )
409 throw coral::UnknownConnectionException( this->
name(), connectionString );
410 return iConnection->second->credentials( role );
414 std::set< std::string >
417 coral::MessageStream
log(
"cond::XMLAuthenticationService::verifyFileName");
422 if ( boost::filesystem::exists( m_inputFileName ) ) {
423 if(boost::filesystem::is_directory( m_inputFileName )){
425 log <<coral::Error <<
"Provided path \"" << m_inputFileName <<
"\" is a directory." <<coral::MessageStream::endmsg;
429 fileNames.insert( fullPath.string() );
430 if(filePath.is_complete())
return fileNames;
434 const char* thePathVariable = ::getenv(
"CORAL_AUTH_PATH" );
435 if ( ! thePathVariable )
return fileNames;
436 log<<
coral::Debug<<
"File \"" << m_inputFileName <<
"\" not found in the current directory. Trying in the search path." <<coral::MessageStream::endmsg;
440 if(boost::filesystem::exists(searchPath)){
441 if(!boost::filesystem::is_directory( searchPath )){
442 log<<
coral::Debug<<
"Search path \"" << searchPath <<
"\" is not a directory."<<coral::MessageStream::endmsg;
446 fullPath /= filePath;
447 fileNames.insert( fullPath.string() );
449 log<<
coral::Debug<<
"Search path \"" << searchPath <<
"\" does not exist."<<coral::MessageStream::endmsg;
virtual char const * what() const
static AlgebraicMatrix initialize()
void appendCredentialItemForRole(const std::string &item, const std::string &value, const std::string &role)
bool initialize()
Service framework related initialization.
static PFTauRenderPlugin instance
static const std::string COND_AUTH_PATH_PROPERTY
static const std::string serviceName
void reset()
Reset parsed data.
#define DEFINE_CORALSERVICE(type, name)
std::set< std::string > verifyFileName()
Verifies the existence of the authentication files.
tuple path
else: Piece not in the list, fine.
char XML_AUTHENTICATION_FILE[]
bool read(const std::string &fileName)
XMLAuthenticationService(const std::string &name)
Standard Constructor.
void setAuthenticationPath(const std::string &inputPath)
Sets the input file name.
virtual ~XMLAuthenticationService()
Standard Destructor.
const std::string & content() const
bool processFile(const std::string &inputFileName)
Parses an xml file.
void appendCredentialItem(const std::string &item, const std::string &value)
coral::Property::CallbackID m_callbackID
const coral::IAuthenticationCredentials & credentials() const
volatile std::atomic< bool > shutdown_flag false
~DataSourceEntry()
Destructor.
void reset(double vett[256])
const coral::IAuthenticationCredentials & credentials(const std::string &connectionString) const