Back to index

nordugrid-arc-nox  1.1.0~rc6
test_clientinterface.cpp
Go to the documentation of this file.
00001 #ifdef HAVE_CONFIG_H
00002 #include <config.h>
00003 #endif
00004 
00005 #include <iostream>
00006 #include <signal.h>
00007 #include <stdexcept>
00008 
00009 #include <arc/GUID.h>
00010 #include <arc/ArcConfig.h>
00011 #include <arc/Logger.h>
00012 #include <arc/URL.h>
00013 #include <arc/message/PayloadSOAP.h>
00014 #include <arc/message/MCC.h>
00015 #include <arc/client/ClientInterface.h>
00016 #include <arc/client/ClientSAML2SSO.h>
00017 #include <arc/client/ClientX509Delegation.h>
00018 #ifdef WIN32
00019 #include <arc/win32.h>
00020 #endif
00021 
00022 int main(void) {
00023   signal(SIGTTOU,SIG_IGN);
00024   signal(SIGTTIN,SIG_IGN);
00025   Arc::Logger logger(Arc::Logger::rootLogger, "Test");
00026   Arc::LogStream logcerr(std::cerr);
00027   Arc::Logger::rootLogger.addDestination(logcerr);
00028 
00029   /************************************************/
00030 
00031   std::string url_str("https://127.0.0.1:60000/echo");
00032   Arc::URL url(url_str);
00033 
00034   Arc::MCCConfig mcc_cfg;
00035   mcc_cfg.AddPrivateKey("testkey-nopass.pem");
00036   mcc_cfg.AddCertificate("testcert.pem");
00037   mcc_cfg.AddCAFile("testcacert.pem");
00038   mcc_cfg.AddCADir("certificates");
00039 
00040   Arc::NS echo_ns; echo_ns["echo"]="http://www.nordugrid.org/schemas/echo";
00041 
00042   /******** Test to service **********/
00043   //Create a SOAP client
00044   logger.msg(Arc::INFO, "Creating a soap client");
00045 
00046 #if 0
00047   Arc::XMLNode sechanlder_nd("\
00048         <SecHandler name='usernametoken.handler' id='usernametoken' event='outgoing'>\
00049             <Process>generate</Process>\
00050             <PasswordEncoding>digest</PasswordEncoding>\
00051             <Username>user</Username>\
00052             <Password>passwd</Password>\
00053         </SecHandler>");
00054 #endif
00055 
00056   Arc::XMLNode sechanlder_nd("\
00057         <SecHandler name='x509token.handler' id='x509token' event='outgoing'>\
00058             <Process>generate</Process>\
00059             <CertificatePath>./testcert.pem</CertificatePath>\
00060             <KeyPath>./testkey-nopass.pem</KeyPath>\
00061         </SecHandler>");
00062 
00063 
00064   Arc::ClientSOAP *client;
00065   client = new Arc::ClientSOAP(mcc_cfg,url,60);
00066 
00067   client->AddSecHandler(sechanlder_nd, "arcshc");
00068 
00069   // Create and send echo request
00070   logger.msg(Arc::INFO, "Creating and sending request");
00071   Arc::PayloadSOAP req(echo_ns);
00072   req.NewChild("echo").NewChild("say")="HELLO";
00073 
00074   Arc::PayloadSOAP* resp = NULL;
00075 
00076   if(client) {
00077     std::string str;
00078     req.GetXML(str);
00079     std::cout<<"request: "<<str<<std::endl;
00080     Arc::MCC_Status status = client->process(&req,&resp);
00081     if(!status) {
00082       logger.msg(Arc::ERROR, "SOAP invokation failed");
00083       throw std::runtime_error("SOAP invokation failed");
00084     }
00085     if(resp == NULL) {
00086       logger.msg(Arc::ERROR,"There was no SOAP response");
00087       throw std::runtime_error("There was no SOAP response");
00088     }
00089   }
00090 
00091   std::string xml;
00092   resp->GetXML(xml);
00093   std::cout << "XML: "<< xml << std::endl;
00094   std::cout << "Response: " << (std::string)((*resp)["echoResponse"]["hear"]) << std::endl;
00095 
00096   if(resp) delete resp;
00097   if(client) delete client;
00098 
00099 
00100 #if 0
00101   std::string idp_name = "https://idp.testshib.org/idp/shibboleth";
00102 
00103   /******** Test to service with SAML2SSO **********/
00104   //Create a HTTP client
00105   logger.msg(Arc::INFO, "Creating a http client");
00106   
00107   std::string username = "myself";
00108   std::string password = "myself";
00109 
00110   Arc::ClientHTTPwithSAML2SSO *client_http;
00111   client_http = new Arc::ClientHTTPwithSAML2SSO(mcc_cfg,url);
00112   logger.msg(Arc::INFO, "Creating and sending request");
00113   Arc::PayloadRaw req_http;
00114   //req_http.Insert();
00115 
00116   Arc::PayloadRawInterface* resp_http = NULL;
00117   Arc::HTTPClientInfo info;
00118 
00119   if(client_http) {
00120     Arc::MCC_Status status = client_http->process("GET", "echo", &req_http,&info,&resp_http, idp_name, username, password);
00121     if(!status) {
00122       logger.msg(Arc::ERROR, "HTTP with SAML2SSO invokation failed");
00123       throw std::runtime_error("HTTP with SAML2SSO invokation failed");
00124     }
00125     if(resp_http == NULL) {
00126       logger.msg(Arc::ERROR,"There was no HTTP response");
00127       throw std::runtime_error("There was no HTTP response");
00128     }
00129   }
00130 
00131   if(resp_http) delete resp_http;
00132   if(client_http) delete client_http;
00133 
00134 
00135 
00136   //Create a SOAP client
00137   logger.msg(Arc::INFO, "Creating a soap client");
00138  
00139   Arc::ClientSOAPwithSAML2SSO *client_soap;
00140   client_soap = new Arc::ClientSOAPwithSAML2SSO(mcc_cfg,url);
00141   logger.msg(Arc::INFO, "Creating and sending request");
00142   Arc::PayloadSOAP req_soap(echo_ns);
00143   req_soap.NewChild("echo").NewChild("say")="HELLO";
00144 
00145   Arc::PayloadSOAP* resp_soap = NULL;
00146 
00147   if(client_soap) {
00148     Arc::MCC_Status status = client_soap->process(&req_soap,&resp_soap, idp_name, username, password);
00149     if(!status) {
00150       logger.msg(Arc::ERROR, "SOAP with SAML2SSO invokation failed");
00151       throw std::runtime_error("SOAP with SAML2SSO invokation failed");
00152     }
00153     if(resp_soap == NULL) {
00154       logger.msg(Arc::ERROR,"There was no SOAP response");
00155       throw std::runtime_error("There was no SOAP response");
00156     }
00157   }
00158   std::string xml_soap;
00159   resp_soap->GetXML(xml_soap);
00160   std::cout << "XML: "<< xml_soap << std::endl;
00161   std::cout << "Response: " << (std::string)((*resp_soap)["echoResponse"]["hear"]) << std::endl;
00162 
00163   if(resp_soap) delete resp_soap;
00164   if(client_soap) delete client_soap;
00165 #endif
00166 
00167 #if 0
00168   /******** Test to ARC delegation service **********/
00169   std::string arc_deleg_url_str("https://127.0.0.1:60000/delegation");
00170   Arc::URL arc_deleg_url(arc_deleg_url_str);
00171   Arc::MCCConfig arc_deleg_mcc_cfg;
00172   arc_deleg_mcc_cfg.AddPrivateKey("testuserkey-nopass.pem");
00173   arc_deleg_mcc_cfg.AddCertificate("testusercert.pem");
00174   //arc_deleg_mcc_cfg.AddCAFile("testcacert.pem");
00175   arc_deleg_mcc_cfg.AddCADir("certificates");
00176   //Create a delegation SOAP client 
00177   logger.msg(Arc::INFO, "Creating a delegation soap client");
00178   Arc::ClientX509Delegation *arc_deleg_client = NULL;
00179   arc_deleg_client = new Arc::ClientX509Delegation(arc_deleg_mcc_cfg, arc_deleg_url);
00180   std::string arc_delegation_id;
00181   if(arc_deleg_client) {
00182     if(!(arc_deleg_client->createDelegation(Arc::DELEG_ARC, arc_delegation_id))) {
00183       logger.msg(Arc::ERROR, "Delegation to ARC delegation service failed");
00184       throw std::runtime_error("Delegation to ARC delegation service failed");
00185     }
00186   }
00187   logger.msg(Arc::INFO, "Delegation ID: %s", arc_delegation_id.c_str());
00188   if(arc_deleg_client) delete arc_deleg_client;  
00189 
00190   /******** Test to gridsite delegation service **********/
00191   std::string gs_deleg_url_str("https://cream.grid.upjs.sk:8443/ce-cream/services/gridsite-delegation");
00192   Arc::URL gs_deleg_url(gs_deleg_url_str);
00193   Arc::MCCConfig gs_deleg_mcc_cfg;
00194   gs_deleg_mcc_cfg.AddProxy("x509up_u126587");
00195   //gs_deleg_mcc_cfg.AddPrivateKey("userkey-nopass.pem");
00196   //gs_deleg_mcc_cfg.AddCertificate("usercert.pem");
00197   gs_deleg_mcc_cfg.AddCADir("certificates");
00198   //Create a delegation SOAP client
00199   logger.msg(Arc::INFO, "Creating a delegation soap client");
00200   Arc::ClientX509Delegation *gs_deleg_client = NULL;
00201   gs_deleg_client = new Arc::ClientX509Delegation(gs_deleg_mcc_cfg, gs_deleg_url);
00202   std::string gs_delegation_id;
00203   gs_delegation_id = Arc::UUID();
00204   if(gs_deleg_client) {
00205     if(!(gs_deleg_client->createDelegation(Arc::DELEG_GRIDSITE, gs_delegation_id))) {
00206       logger.msg(Arc::ERROR, "Delegation to gridsite delegation service failed");
00207       throw std::runtime_error("Delegation to gridsite delegation service failed");
00208     }
00209   }
00210   logger.msg(Arc::INFO, "Delegation ID: %s", gs_delegation_id.c_str());
00211   if(gs_deleg_client) delete gs_deleg_client;
00212 #endif
00213 
00214   return 0;
00215 }