Back to index

nordugrid-arc-nox  1.1.0~rc6
echo.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 <sstream>
00007 
00008 #include <arc/message/MCCLoader.h>
00009 #include <arc/infosys/RegisteredService.h>
00010 #include <arc/message/PayloadSOAP.h>
00011 
00012 #include "echo.h"
00013 
00014 static Arc::Plugin* get_service(Arc::PluginArgument* arg) {
00015     Arc::ServicePluginArgument* mccarg =
00016             arg?dynamic_cast<Arc::ServicePluginArgument*>(arg):NULL;
00017     if(!mccarg) return NULL;
00018     return new Echo::Service_Echo((Arc::Config*)(*mccarg));
00019 }
00020 
00021 Arc::PluginDescriptor PLUGINS_TABLE_NAME[] = {
00022     { "echo", "HED:SERVICE", 0, &get_service },
00023     { NULL, NULL, 0, NULL }
00024 };
00025 
00026 using namespace Arc;
00027 
00028 namespace Echo {
00029 
00030 Service_Echo::Service_Echo(Arc::Config *cfg):RegisteredService(cfg),logger(Arc::Logger::rootLogger, "Echo") {
00031   ns_["echo"]="http://www.nordugrid.org/schemas/echo";
00032   prefix_=(std::string)((*cfg)["prefix"]);
00033   suffix_=(std::string)((*cfg)["suffix"]);
00034 
00035 #if 0
00036   // Parse the policy location information, and put them into a map container for later using
00037   for(int i=0;; i++) {
00038     Arc::XMLNode cn = (*cfg).Child(i);
00039     if(!cn) break;
00040     if(MatchXMLName(cn, "SecHandler")) {
00041       for(int j=0;; j++) {
00042         Arc::XMLNode gn = cn.Child(j);
00043         if(!gn) break;
00044         if(MatchXMLName(gn, "PDP")) {
00045           policylocation_  = (std::string)(gn.Attribute("policylocation"));
00046         }
00047       }
00048     }
00049   }
00050 #endif
00051 
00052   // Assigning service description - Glue2 document should go here.
00053   infodoc.Assign(Arc::XMLNode(
00054     "<?xml version=\"1.0\"?>"
00055     "<Domains><AdminDomain><Services><Service><Endpoint><HealthState>ok</HealthState><ServingState>production</ServingState></Endpoint>ECHO</Service></Services></AdminDomain></Domains>"
00056   ),true);
00057 }
00058 
00059 Service_Echo::~Service_Echo(void) {
00060 }
00061 
00062 Arc::MCC_Status Service_Echo::make_fault(Arc::Message& outmsg,const std::string& txtmsg) {
00063   Arc::PayloadSOAP* outpayload = new Arc::PayloadSOAP(ns_,true);
00064   Arc::SOAPFault* fault = outpayload->Fault();
00065   if(fault) {
00066     fault->Code(Arc::SOAPFault::Sender);
00067     if(txtmsg.empty()) {
00068       fault->Reason("Failed processing request");
00069     } else {
00070       logger.msg(Arc::ERROR, txtmsg);
00071       fault->Reason(txtmsg);
00072     };
00073   };
00074   outmsg.Payload(outpayload);
00075   return Arc::MCC_Status(Arc::STATUS_OK);
00076 }
00077 
00078 Arc::MCC_Status Service_Echo::process(Arc::Message& inmsg,Arc::Message& outmsg) {
00079   //Store policy location into message attribute
00080   //inmsg.Attributes()->add("PDP:POLICYLOCATION", policylocation_);
00081   // Check authorization
00082   if(!ProcessSecHandlers(inmsg, "incoming")) {
00083     logger.msg(Arc::ERROR, "echo: Unauthorized");
00084     return Arc::MCC_Status(Arc::GENERIC_ERROR);
00085   };
00086   // Both input and output are supposed to be SOAP 
00087   // Extracting payload
00088   Arc::PayloadSOAP* inpayload = NULL;
00089   try {
00090     inpayload = dynamic_cast<Arc::PayloadSOAP*>(inmsg.Payload());
00091   } catch(std::exception& e) { };
00092   if(!inpayload) {
00093     return make_fault(outmsg,"Input is not SOAP");
00094   };
00095   {
00096       std::string str;
00097       inpayload->GetDoc(str, true);
00098       logger.msg(Arc::VERBOSE, "process: request=%s",str);
00099   }; 
00100 
00101   Arc::PayloadSOAP* outpayload = NULL;
00102 
00103 
00105   MessageAuth* mauth = inmsg.Auth();
00106   MessageAuth* cauth = inmsg.AuthContext();
00107   if((!mauth) && (!cauth)) {
00108     logger.msg(ERROR,"Missing security object in message");
00109     return Arc::MCC_Status();
00110   };
00111   NS ns;
00112   XMLNode requestxml(ns,"");
00113   if(mauth) {
00114     if(!mauth->Export(SecAttr::ARCAuth,requestxml)) {
00115       delete mauth;
00116       logger.msg(ERROR,"Failed to convert security information to ARC request");
00117       return Arc::MCC_Status();
00118     };
00119   };
00120   if(cauth) {
00121     if(!cauth->Export(SecAttr::ARCAuth,requestxml)) {
00122       delete mauth;
00123       logger.msg(ERROR,"Failed to convert security information to ARC request");
00124       return Arc::MCC_Status();
00125     };
00126   };
00129   // Analyzing request 
00130 
00131   // Checking if it's info request
00132   if(MatchXMLNamespace(inpayload->Child(0),"http://docs.oasis-open.org/wsrf/rp-2")) {
00133     Arc::SOAPEnvelope* outxml = infodoc.Process(*inpayload);
00134     if(!outxml) {
00135       return make_fault(outmsg,"WSRF request processing failed");
00136     };
00137     outpayload = new Arc::PayloadSOAP(*outxml);
00138     delete outxml;
00139   }
00140   else if((*inpayload)["size"]){
00141     Arc::XMLNode echo_op = (*inpayload)["size"];
00142     int size = atoi(std::string(echo_op["size"]).c_str());
00143     std::string msg = "Message for you, sir";
00144     msg.resize(size,'0');
00145     std::string say = echo_op["say"];
00146     std::string hear = prefix_+say+suffix_;
00147     outpayload = new Arc::PayloadSOAP(ns_);
00148     outpayload->NewChild("echo:echoResponse").NewChild("echo:hear")=msg;
00149   }
00150   else {
00151     // Then it must be 'echo' operation requested
00152     Arc::XMLNode echo_op = (*inpayload)["echo"];
00153     if(!echo_op) {
00154       return make_fault(outmsg,"Request is not supported - "+echo_op.Name());
00155     };
00156     std::string say = echo_op["say"];
00157     std::string hear = prefix_+say+suffix_;
00158     outpayload = new Arc::PayloadSOAP(ns_);
00159     outpayload->NewChild("echo:echoResponse").NewChild("echo:hear")=hear;
00160   };
00161 
00162   outmsg.Payload(outpayload);
00163   {
00164       std::string str;
00165       outpayload->GetDoc(str, true);
00166       logger.msg(Arc::VERBOSE, "process: response=%s",str);
00167   }; 
00168   return Arc::MCC_Status(Arc::STATUS_OK);
00169 }
00170 
00171 bool Service_Echo::RegistrationCollector(Arc::XMLNode &doc) {
00172   // RegEntry element generation
00173   Arc::XMLNode empty(ns_, "RegEntry");
00174   empty.New(doc);
00175   doc.NewChild("SrcAdv").NewChild("Type") = "org.nordugrid.tests.echo";
00176   return true;
00177 }
00178 
00179 
00180 } // namespace Echo
00181