Back to index

nordugrid-arc-nox  1.1.0~rc6
arcwsrf.cpp
Go to the documentation of this file.
00001 // -*- indent-tabs-mode: nil -*-
00002 
00003 #ifdef HAVE_CONFIG_H
00004 #include <config.h>
00005 #endif
00006 
00007 #include <iostream>
00008 #include <list>
00009 #include <string>
00010 
00011 #include <arc/ArcLocation.h>
00012 #include <arc/Utils.h>
00013 #include <arc/IString.h>
00014 #include <arc/Logger.h>
00015 #include <arc/OptionParser.h>
00016 #include <arc/StringConv.h>
00017 #include <arc/UserConfig.h>
00018 #include <arc/infosys/InformationInterface.h>
00019 #include <arc/message/MCC.h>
00020 #include <arc/client/ClientInterface.h>
00021 
00022 using namespace Arc;
00023 
00024 int main(int argc, char **argv) {
00025 
00026   setlocale(LC_ALL, "");
00027 
00028   Arc::Logger logger(Arc::Logger::getRootLogger(), "arcwsrf");
00029   Arc::LogStream logcerr(std::cerr);
00030   logcerr.setFormat(Arc::ShortFormat);
00031   Arc::Logger::getRootLogger().addDestination(logcerr);
00032   Arc::Logger::getRootLogger().setThreshold(Arc::WARNING);
00033 
00034   //Arc::ArcLocation::Init(argv[0]);
00035   Arc::OptionParser options(istring("url [query]"),
00036                             istring("The arcwsrf command is used for "
00037                                     "obtaining the WS-ResourceProperties of\n"
00038                                     "services."));
00039 
00040   std::list<std::string> paths;
00041   options.AddOption('p', "property",
00042                     istring("Request for specific Resource Property"),
00043                     istring("[-]name"),
00044                     paths);
00045 
00046   int timeout = -1;
00047   options.AddOption('t', "timeout", istring("timeout in seconds (default 20)"),
00048                     istring("seconds"), timeout);
00049                     
00050   std::string conffile;
00051   options.AddOption('z', "conffile",
00052                     istring("configuration file (default ~/.arc/client.conf)"),
00053                     istring("filename"), conffile);
00054 
00055   std::string debug;
00056   options.AddOption('d', "debug",
00057                     istring("FATAL, ERROR, WARNING, INFO, VERBOSE or DEBUG"),
00058                     istring("debuglevel"), debug);
00059 
00060   bool version = false;
00061   options.AddOption('v', "version", istring("print version information"),
00062                     version);
00063 
00064   std::string url;
00065   std::string query;
00066   {
00067     std::list<std::string> args = options.Parse(argc, argv);
00068 
00069     if (version) {
00070       std::cout << Arc::IString("%s version %s", "arcinfo", VERSION)
00071                 << std::endl;
00072       return 0;
00073     }
00074 
00075     if(args.size() < 1) {
00076       logger.msg(Arc::ERROR, "Missing URL");
00077       return 1;
00078     }
00079 
00080     if(url.size() > 2) {
00081       logger.msg(Arc::ERROR, "Too many parameters");
00082       return 1;
00083     }
00084 
00085     std::list<std::string>::iterator arg = args.begin();
00086     url = *arg;
00087     ++arg;
00088     if(arg != args.end()) query = *arg;
00089   }
00090 
00091   // If debug is specified as argument, it should be set before loading the configuration.
00092   if (!debug.empty())
00093     Arc::Logger::getRootLogger().setThreshold(Arc::string_to_level(debug));
00094 
00095   Arc::UserConfig usercfg(conffile);
00096   if (!usercfg) {
00097     logger.msg(Arc::ERROR, "Failed configuration initialization");
00098     return 1;
00099   }
00100   
00101   if (timeout > 0)
00102     usercfg.Timeout(timeout);
00103 
00104   if (debug.empty() && !usercfg.Verbosity().empty())
00105     Arc::Logger::getRootLogger().setThreshold(Arc::string_to_level(usercfg.Verbosity()));
00106 
00107   //if (timeout > 0) {
00108   //  usercfg.SetTimeout(timeout);
00109   //}
00110 
00111   // Proxy check
00112   //if (!usercfg.CheckProxy())
00113   //  return 1;
00114 
00115   CountedPointer<InformationRequest> request(NULL);
00116   if(!query.empty()) {
00117     XMLNode q(query);
00118     if(!q) {
00119       logger.msg(Arc::ERROR, "Query is not valid XML");
00120       return 1;
00121     }
00122     request = new InformationRequest(q);
00123   } else {
00124     std::list<std::list<std::string> > qpaths;
00125     for(std::list<std::string>::iterator path = paths.begin();
00126                                          path != paths.end();++path) {
00127       std::list<std::string> qpath;
00128       qpath.push_back(*path);
00129       qpaths.push_back(qpath);
00130     }
00131     request = new InformationRequest(qpaths);
00132   }
00133   if(!(*request)) {
00134     logger.msg(Arc::ERROR, "Failed to create WSRP request");
00135     return 1;
00136   }
00137   //SOAPEnvelope* SOAP(void);
00138  
00139   URL u(url);
00140   if(!u) {
00141     logger.msg(Arc::ERROR, "Specified URL is not valid");
00142     return 1;
00143   }
00144 
00145   MCCConfig cfg;
00146   usercfg.ApplyToConfig(cfg);
00147   ClientSOAP client(cfg, u, usercfg.Timeout());
00148   PayloadSOAP req(*(request->SOAP()));
00149   PayloadSOAP* resp_ = NULL;
00150   MCC_Status r = client.process(&req,&resp_);
00151   CountedPointer<PayloadSOAP> resp(resp_);
00152   if(!r) {
00153     logger.msg(Arc::ERROR, "Failed to send request");
00154     return 1;
00155   }
00156   if(!resp) {
00157     logger.msg(Arc::ERROR, "Failed to obtain SOAP response");
00158     return 1;
00159   }
00160   std::string o;
00161   resp->Body().Child().GetXML(o);
00162   if(resp->IsFault()) {
00163     logger.msg(Arc::ERROR, "SOAP Fault received");
00164     std::cerr<<o<<std::endl;
00165     return 1;
00166   }
00167   std::cout<<o<<std::endl;
00168   return 0;
00169 }