// dial_ws_repository_imp.cxx // Define functions for the DIAL repository service. #include #include #include "../gsoap/dial_ws_repositoryH.h" #include "dataset_util/XmlElement.h" #include "dataset_util/Text.h" #include "dataset_util/ThreadCredential.h" #include "dataset_catalog/GenericRepository.h" #include "dial_ws/GsoapRegistry.h" #include "dial_ws/DialWs.h" using std::string; using std::endl; using std::map; using dset::GenericRepository; using dial::GsoapRegistry; using dial::DialWs; using namespace dial_ws_repository; extern SOAP_NMAC struct Namespace dial_ws_repository_namespaces[]; typedef GenericRepository::IdList IdList; typedef map RepMap; //********************************************************************** // Helper functions. //********************************************************************** namespace { //********************************************************************** // Service initialization. // This function is called when the service is started. int init_service() { return 0; } //********************************************************************** // HTTP initialization. // This function is called when the http service is started. int init_http() { return 0; } //********************************************************************** // Registration. // The "repository" service is associated with the listed // initialization function, gsoap server function and gsoap namespace. int dial_ws_file_reg = dial::GsoapRegistry::insert( "repository", init_service, dial_ws_repository_serve, dial_ws_repository_namespaces ); int dial_ws_file_reg_http = dial::GsoapRegistry::insert( "repository_http", init_http, dial_ws_repository_serve, dial_ws_repository_namespaces ); //********************************************************************** // Function to create and cache repositories. GenericRepository* findrep(string type, string conn) { static RepMap reps; string repname = type + " " + conn; RepMap::const_iterator irep = reps.find(repname); if ( irep != reps.end() ) { return irep->second; } DialWs::lout() << "Establishing new connection" << endl; DialWs::lout() << " Type: " << type << endl; DialWs::lout() << " Conn: " << conn << endl; GenericRepository* pcat = GenericRepository::create(type, conn); if ( pcat == 0 ) { DialWs::lout() << "Unable to find repository" << endl; } else { reps[repname] = pcat; } return pcat; } } // end unnamed namespace //********************************************************************** // Common server functions. //********************************************************************** // Validity. int dial_ws_repository::dial__is_valid(soap*, bool& stat) { DialWs::lout() << ">>>Received request for validity " << endl; DialWs::lout() << "Return true" << endl; stat = true; return SOAP_OK; } //********************************************************************** // Termination. int dial_ws_repository::dial__terminate(soap*, int& stat) { DialWs::lout() << ">>>Received request for termination " << endl; DialWs::terminate() = true; stat = 0; return SOAP_OK; } //********************************************************************** // Local server functions. //********************************************************************** // Validity. int dial_ws_repository:: dial__repository_is_valid(soap*, string type, string conn, bool& val) { DialWs::lout() << ">>>Received validity request" << endl; DialWs::lout() << "Type: " << type << endl; DialWs::lout() << "Connection: " << conn << endl; GenericRepository* pgr = findrep(type, conn); if ( pgr == 0 ) { val = false; } else { val = pgr->is_valid(); } DialWs::lout() << "Validity: " << val << endl; return SOAP_OK; } //********************************************************************** // Error code. int dial_ws_repository:: dial__repository_error(soap*, string type, string conn, int& err) { DialWs::lout() << ">>>Received error status request" << endl; DialWs::lout() << "Type: " << type << endl; DialWs::lout() << "Connection: " << conn << endl; GenericRepository* pgr = findrep(type, conn); if ( pgr == 0 ) { err = 101; } else { err = pgr->error(); } DialWs::lout() << "Return error code: " << err << endl; return SOAP_OK; } //********************************************************************** // Error string. int dial_ws_repository::dial__repository_error_string( soap* psoap, string type, string conn, int err, string& smsg) { DialWs::lout() << ">>>Received error string request" << endl; DialWs::lout() << "Type: " << type << endl; DialWs::lout() << "Connection: " << conn << endl; const char* emsg1 = "Error in DIAL error string request"; GenericRepository* pgr = findrep(type, conn); if ( pgr == 0 ) { return soap_receiver_fault(psoap, emsg1, "Unknown repository"); } smsg = pgr->error_string(err); DialWs::lout() << "Return error message: \n " << smsg << endl; return SOAP_OK; } //********************************************************************** // Insertion. int dial_ws_repository::dial__repository_insert( soap* psoap, string type, string conn, string sid, string sdesc, int& stat) { DialWs::lout() << ">>>Received insert request" << endl; DialWs::lout() << "Type: " << type << endl; DialWs::lout() << "Connection: " << conn << endl; DialWs::lout() << "Input ID is " << sid << endl; DialWs::lout() << sdesc << endl; const char* emsg1 = "Error in insert request"; GenericRepository* pgr = findrep(type, conn); if ( pgr == 0 ) { return soap_receiver_fault(psoap, emsg1, "Unknown repository"); } // Check authorization. if ( ThreadCredential().owner() == "" ) { return soap_sender_fault(psoap, emsg1, "Credential owner not defined."); } stat = pgr->insert(sid, sdesc); DialWs::lout() << "Return status: " << stat << endl; return SOAP_OK; } //********************************************************************** // Removal. int dial_ws_repository::dial__repository_remove( soap* psoap, string type, string conn, string sid, int& stat) { DialWs::lout() << ">>>Received remove request" << endl; DialWs::lout() << "Type: " << type << endl; DialWs::lout() << "Connection: " << conn << endl; DialWs::lout() << "Input ID is " << sid << endl; const char* emsg1 = "Error in remove request"; GenericRepository* pgr = findrep(type, conn); if ( pgr == 0 ) { return soap_receiver_fault(psoap, emsg1, "Unknown repository"); } // Check authorization. if ( ThreadCredential().owner() == "" ) { return soap_sender_fault(psoap, emsg1, "Credential owner not defined."); } stat = pgr->remove(sid); DialWs::lout() << "Return status: " << stat << endl; return SOAP_OK; } //********************************************************************** // Has. int dial_ws_repository::dial__repository_has( soap* psoap, string type, string conn, string sid, bool& stat) { DialWs::lout() << ">>>Received has request" << endl; DialWs::lout() << "Type: " << type << endl; DialWs::lout() << "Connection: " << conn << endl; DialWs::lout() << "Input ID is " << sid << endl; const char* emsg1 = "Error in has request"; GenericRepository* pgr = findrep(type, conn); if ( pgr == 0 ) { return soap_receiver_fault(psoap, emsg1, "Unknown repository"); } stat = pgr->has(sid); DialWs::lout() << "Return status: " << stat << endl; return SOAP_OK; } //********************************************************************** // Retrieval. int dial_ws_repository::dial__repository_get( soap* psoap, string type, string conn, string sid, dial__StringResponse& res) { res.Response = ""; DialWs::lout() << ">>>Received get request" << endl; DialWs::lout() << "Type: " << type << endl; DialWs::lout() << "Connection: " << conn << endl; DialWs::lout() << "Input ID: " << sid << endl; const char* emsg1 = "Error in get request"; GenericRepository* pgr = findrep(type, conn); if ( pgr == 0 ) { return soap_receiver_fault(psoap, emsg1, "Unknown repository"); } // Check authorization. if ( ThreadCredential().owner() == "" ) { return soap_sender_fault(psoap, emsg1, "Credential owner not defined."); } string sdesc = pgr->get(sid); if ( sdesc.size() ) { DialWs::lout() << "Returned object with length " << sdesc.size() << ":" << endl; if ( sdesc.size() <= 200 ) { DialWs::lout() << sdesc << endl; } else { DialWs::lout() << sdesc.substr(0, 197); DialWs::lout() << "..."; } DialWs::lout() << endl; } else { DialWs::lout() << "Repository returned blank" << endl; return soap_receiver_fault(psoap, emsg1, "Local repository retrieval failed"); } res.Response = sdesc; return SOAP_OK; } //********************************************************************** // Multiple retrieval. int dial_ws_repository::dial__repository_mget( soap* psoap, string type, string conn, dial__StringVector vids, dial__StringVector& res) { const GenericRepository::IdList& ids = *vids.pvec; static GenericRepository::ElementList eles; eles.erase(eles.begin(), eles.end()); DialWs::lout() << ">>>Received mget request" << endl; DialWs::lout() << "Type: " << type << endl; DialWs::lout() << "Connection: " << conn << endl; DialWs::lout() << "Input ID count: " << ids.size() << endl; const char* emsg1 = "Error in mget request"; GenericRepository* pgr = findrep(type, conn); if ( pgr == 0 ) { DialWs::lout() << "Unable to access repository" << endl; return soap_receiver_fault(psoap, emsg1, "Unknown repository"); } // Check authorization. if ( ThreadCredential().owner() == "" ) { return soap_sender_fault(psoap, emsg1, "Credential owner not defined."); } int mstat = pgr->mget(ids, eles); size_t sz = 0; for ( GenericRepository::ElementList::const_iterator iele=eles.begin(); iele!=eles.end(); ++iele ) { sz += iele->size(); } DialWs::lout() << "Returned object description count: " << eles.size() << endl; DialWs::lout() << "Total size: " << sz << endl; if ( mstat != 0 ) { DialWs::lout() << "Failed with error " << mstat << endl; DialWs::lout() << pgr->error_string(mstat) << endl; } res.pvec= &eles; return SOAP_OK; } //********************************************************************** // Time. int dial_ws_repository::dial__repository_time( soap* psoap, string type, string conn, string sid, Time& time) { DialWs::lout() << ">>>Received time request" << endl; DialWs::lout() << "Type: " << type << endl; DialWs::lout() << "Connection: " << conn << endl; DialWs::lout() << "Input ID: " << sid << endl; const char* emsg1 = "Error in get request"; GenericRepository* pgr = findrep(type, conn); if ( pgr == 0 ) { return soap_receiver_fault(psoap, emsg1, "Unknown repository"); } time = pgr->time(sid); DialWs::lout() << "Returned time: " << time << endl; return SOAP_OK; } //********************************************************************** // Size. int dial_ws_repository::dial__repository_size( soap* psoap, string type, string conn, unsigned int& count) { DialWs::lout() << ">>>Received size request" << endl; DialWs::lout() << "Type: " << type << endl; DialWs::lout() << "Connection: " << conn << endl; const char* emsg1 = "Error in size request"; GenericRepository* pgr = findrep(type, conn); if ( pgr == 0 ) { return soap_receiver_fault(psoap, emsg1, "Unknown repository"); } count = pgr->size(); DialWs::lout() << "Returned count: " << count << endl; return SOAP_OK; } //********************************************************************** // Size since. int dial_ws_repository::dial__repository_size_since( soap* psoap, string type, string conn, Time time, unsigned int& count) { DialWs::lout() << ">>>Received size since request" << endl; DialWs::lout() << "Type: " << type << endl; DialWs::lout() << "Connection: " << conn << endl; DialWs::lout() << "Time: " << time << endl; const char* emsg1 = "Error in size since request"; GenericRepository* pgr = findrep(type, conn); if ( pgr == 0 ) { return soap_receiver_fault(psoap, emsg1, "Unknown repository"); } count = pgr->size_since(time); DialWs::lout() << "Returned count: " << count << endl; return SOAP_OK; } //********************************************************************** // List of ID's. int dial_ws_repository::dial__repository_get_ids( soap* psoap, string type, string conn, int maxids, char*& cids) { DialWs::lout() << ">>>Received get ID's request" << endl; DialWs::lout() << "Type: " << type << endl; DialWs::lout() << "Connection: " << conn << endl; const char* emsg1 = "Error in get ID's request"; GenericRepository* pgr = findrep(type, conn); if ( pgr == 0 ) { return soap_receiver_fault(psoap, emsg1, "Unknown repository"); } IdList ids = pgr->get_ids(); static string sids; sids = ""; int count = 0; DialWs::lout() << "ID list:"; for ( IdList::const_iterator iid=ids.begin(); iid!=ids.end(); ++iid ) { if ( ++count > maxids ) break; string sid = *iid; if ( iid != ids.begin() ) { sids += " "; } DialWs::lout() << "\n " << sid; sids += sid; } DialWs::lout() << endl; cids = const_cast(sids.c_str()); DialWs::lout() << "Returned count: " << sids.size() << endl; return SOAP_OK; } //********************************************************************** // List of ID's since. int dial_ws_repository::dial__repository_get_ids_since( soap* psoap, string type, string conn, Time time, int maxids, char*& cids) { DialWs::lout() << ">>>Received get ID's since request" << endl; DialWs::lout() << "Type: " << type << endl; DialWs::lout() << "Connection: " << conn << endl; DialWs::lout() << "Timm: " << time << endl; const char* emsg1 = "Error in get ID's since request"; GenericRepository* pgr = findrep(type, conn); if ( pgr == 0 ) { return soap_receiver_fault(psoap, emsg1, "Unknown repository"); } IdList ids = pgr->get_ids_since(time); static string sids; sids = ""; int count = 0; DialWs::lout() << "ID list:"; for ( IdList::const_iterator iid=ids.begin(); iid!=ids.end(); ++iid ) { if ( ++count > maxids ) break; string sid = *iid; if ( iid != ids.begin() ) { sids += " "; } DialWs::lout() << "\n " << sid; sids += sid; } DialWs::lout() << endl; cids = const_cast(sids.c_str()); DialWs::lout() << "Returned count: " << sids.size() << endl; return SOAP_OK; } //**********************************************************************