Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 10:45:52

0001 #include "CondCore/CondDB/interface/Exception.h"
0002 #include "CondCore/CondDB/interface/Utils.h"
0003 #include "DbConnectionString.h"
0004 //
0005 #include "FWCore/ParameterSet/interface/FileInPath.h"
0006 #include "FWCore/Catalog/interface/SiteLocalConfig.h"
0007 #include "FWCore/ServiceRegistry/interface/Service.h"
0008 
0009 namespace cond {
0010 
0011   namespace persistency {
0012 
0013     unsigned int countslash(const std::string& input) {
0014       unsigned int count = 0;
0015       std::string::size_type slashpos(0);
0016       while (slashpos != std::string::npos) {
0017         slashpos = input.find('/', slashpos);
0018         if (slashpos != std::string::npos) {
0019           ++count;
0020           // start next search after this word
0021           slashpos += 1;
0022         }
0023       }
0024       return count;
0025     }
0026 
0027     std::string parseFipConnectionString(const std::string& fipConnect) {
0028       std::string connect("sqlite_file:");
0029       std::string::size_type pos = fipConnect.find(':');
0030       std::string fipLocation = fipConnect.substr(pos + 1);
0031       edm::FileInPath fip(fipLocation);
0032       connect.append(fip.fullPath());
0033       return connect;
0034     }
0035 
0036     //FIXME: sdg this function does not support frontier connections strings like
0037     //frontier://cmsfrontier.cern.ch:8000/FrontierPrep/CMS_CONDITIONS
0038     //as http://cmsfrontier.cern.ch:8000/FrontierPrep(freshkey=foo) is an invalid URI.
0039     std::pair<std::string, std::string> getConnectionParams(const std::string& connectionString,
0040                                                             const std::string& transactionId,
0041                                                             const std::string& signature) {
0042       if (connectionString.empty())
0043         cond::throwException("The connection string is empty.", "getConnectionParams");
0044       std::string protocol = getConnectionProtocol(connectionString);
0045       std::string finalConn = connectionString;
0046       std::string refreshConn("");
0047       if (protocol == "frontier") {
0048         std::string protocol("frontier://");
0049         std::string::size_type fpos = connectionString.find(protocol);
0050         unsigned int nslash = countslash(connectionString.substr(protocol.size(), connectionString.size() - fpos));
0051         if (nslash == 1) {
0052           edm::Service<edm::SiteLocalConfig> localconfservice;
0053           if (!localconfservice.isAvailable()) {
0054             cond::throwException("edm::SiteLocalConfigService is not available", "getConnectionParams");
0055           }
0056           finalConn = localconfservice->lookupCalibConnect(connectionString);
0057         }
0058         if (!transactionId.empty()) {
0059           size_t l = finalConn.rfind('/');
0060           finalConn.insert(l, "(freshkey=" + transactionId + ')');
0061         }
0062 
0063         //When the signature parameter is set to sig, FroNTier requests that the server sends digital signatures on every response.
0064         //We test here that the signature string, if defined, is actually set to sig, otherwise we throw an exception
0065         std::string signatureParameter("sig");
0066         if (!signature.empty()) {
0067           if (signature == signatureParameter) {
0068             std::string::size_type s = finalConn.rfind('/');
0069             finalConn.insert(s, "(security=" + signature + ')');
0070           } else {
0071             cond::throwException("The FroNTier security option is invalid.", "getConnectionParams");
0072           }
0073         }
0074 
0075         std::string::size_type startRefresh = finalConn.find("://");
0076         if (startRefresh != std::string::npos) {
0077           startRefresh += 3;
0078         }
0079         std::string::size_type endRefresh = finalConn.rfind('/', std::string::npos);
0080         if (endRefresh == std::string::npos) {
0081           refreshConn = finalConn;
0082         } else {
0083           refreshConn = finalConn.substr(startRefresh, endRefresh - startRefresh);
0084           if (refreshConn.substr(0, 1) != "(") {
0085             //if the connect string is not a complicated parenthesized string,
0086             // an http:// needs to be at the beginning of it
0087             refreshConn.insert(0, "http://");
0088           }
0089         }
0090       } else if (protocol == "sqlite_fip") {
0091         finalConn = parseFipConnectionString(connectionString);
0092       }
0093       return std::make_pair(finalConn, refreshConn);
0094     }
0095 
0096   }  // namespace persistency
0097 }  // namespace cond