Back to index

nordugrid-arc-nox  1.1.0~rc6
test.cpp
Go to the documentation of this file.
00001 #ifdef HAVE_CONFIG_H
00002 #include <config.h>
00003 #endif
00004 
00005 #include <string>
00006 #include <iostream>
00007 #include <fstream>
00008 #include <signal.h>
00009 
00010 #include <arc/ArcConfig.h>
00011 #include <arc/Logger.h>
00012 #include <arc/XMLNode.h>
00013 #include <arc/message/MCCLoader.h>
00014 #include <arc/message/SOAPEnvelope.h>
00015 #include <arc/message/PayloadSOAP.h>
00016 #include <arc/StringConv.h>
00017 #include <arc/XMLNode.h>
00018 
00020 int main(void) {
00021 
00022 #ifndef WIN32
00023   signal(SIGTTOU,SIG_IGN);
00024   signal(SIGTTIN,SIG_IGN);
00025   signal(SIGPIPE,SIG_IGN);
00026 #endif
00027 
00028   Arc::Logger logger(Arc::Logger::rootLogger, "Test");
00029   Arc::LogStream logcerr(std::cerr);
00030   Arc::Logger::rootLogger.addDestination(logcerr);
00031 
00032   // Load service chain
00033   logger.msg(Arc::INFO, "Creating service side chain");
00034   Arc::Config service_config("service.xml");
00035   if(!service_config) {
00036     logger.msg(Arc::ERROR, "Failed to load service configuration");
00037     return -1;
00038   };
00039   Arc::MCCLoader service_loader(service_config);
00040   logger.msg(Arc::INFO, "Service side MCCs are loaded");
00041   logger.msg(Arc::INFO, "Creating client side chain");
00042 
00043   // Create client chain
00044   Arc::Config client_config("client.xml");
00045   if(!client_config) {
00046     logger.msg(Arc::ERROR, "Failed to load client configuration");
00047     return -1;
00048   };
00049   Arc::MCCLoader client_loader(client_config);
00050   logger.msg(Arc::INFO, "Client side MCCs are loaded");
00051   Arc::MCC* client_entry = client_loader["soap"];
00052   if(!client_entry) {
00053     logger.msg(Arc::ERROR, "Client chain does not have entry point");
00054     return -1;
00055   };
00056   
00057   // -------------------------------------------------------
00058   //    Compose request and send to pdp service
00059   // -------------------------------------------------------
00060   //Compose request
00061   Arc::NS ns;
00062   ns["ra"] = "http://www.nordugrid.org/schemas/request-arc";
00063   ns["pdp"] = "http://www.nordugrid.org/schemas/pdp";
00064   Arc::PayloadSOAP reqdoc(ns);
00065 
00066   Arc::XMLNode reqbody = reqdoc.NewChild("pdp:GetPolicyDecisionRequest"); 
00067  
00068   Arc::XMLNode request = reqbody.NewChild("ra:Request");
00069   Arc::XMLNode requestitem = request.NewChild("ra:RequestItem");
00070 
00071   Arc::XMLNode sub = requestitem.NewChild("ra:Subject");
00072   Arc::XMLNode subattr1 = sub.NewChild("ra:Attribute");
00073   //Fill in a fake value
00074   std::string remotehost = "127.0.0.1";
00075   subattr1 = remotehost;
00076   Arc::XMLNode subattr1Id = subattr1.NewAttribute("ra:AttributeId");
00077   subattr1Id = "http://www.nordugrid.org/schemas/policy-arc/types/tcp/ipaddress";
00078   Arc::XMLNode subattr1Type = subattr1.NewAttribute("ra:Type");
00079   subattr1Type = "string";
00080 
00081   Arc::XMLNode subattr2 = sub.NewChild("ra:Attribute");
00082   //Fill in a fake value
00083   std::string subject = "/O=Grid/O=Test/CN=test";
00084   subattr2 = subject;
00085   Arc::XMLNode subattr2Id = subattr2.NewAttribute("ra:AttributeId");
00086   subattr2Id = "http://www.nordugrid.org/schemas/policy-arc/types/tls/identity";
00087   Arc::XMLNode subattr2Type = subattr2.NewAttribute("ra:Type");
00088   subattr2Type = "string";
00089 
00090   Arc::XMLNode act = requestitem.NewChild("ra:Action");
00091   //Fill in a fake value
00092   std::string action = "POST";
00093   act=action;
00094   Arc::XMLNode actionId = act.NewAttribute("ra:AttributeId");
00095   actionId = "http://www.nordugrid.org/schemas/policy-arc/types/http/method";
00096   Arc::XMLNode actionType = act.NewAttribute("ra:Type");
00097   actionType = "string";
00098 
00099   std::string req_str;
00100   reqdoc.GetXML(req_str);
00101   logger.msg(Arc::INFO, "Request: %s", req_str); 
00102 
00103   // Send request
00104   Arc::MessageContext context;
00105   Arc::Message reqmsg;
00106   Arc::Message repmsg;
00107   Arc::MessageAttributes attributes_in;
00108   Arc::MessageAttributes attributes_out;
00109   reqmsg.Payload(&reqdoc);
00110   reqmsg.Attributes(&attributes_in);
00111   reqmsg.Context(&context);
00112   repmsg.Attributes(&attributes_out);
00113   repmsg.Context(&context);
00114 
00115   Arc::MCC_Status status = client_entry->process(reqmsg,repmsg);
00116   if(!status) {
00117     logger.msg(Arc::ERROR, "Policy Decision Request failed");
00118     return -1;
00119   };
00120   logger.msg(Arc::INFO, "Policy Decision Request succeed!!!");
00121   if(repmsg.Payload() == NULL) {
00122     logger.msg(Arc::ERROR, "There is no response");
00123     return -1;
00124   };
00125   Arc::PayloadSOAP* resp = NULL;
00126   try {
00127    resp = dynamic_cast<Arc::PayloadSOAP*>(repmsg.Payload());
00128   } catch(std::exception&) { };
00129   if(resp == NULL) {
00130     logger.msg(Arc::ERROR, "Response is not SOAP");
00131     delete repmsg.Payload();
00132     return -1;
00133   };
00134   
00135   std::string str;
00136   resp->GetXML(str);
00137   logger.msg(Arc::INFO, "Response: %s", str);
00138   
00139   delete repmsg.Payload();
00140 
00141   return 0;
00142 }