src/tools/wsdl.cpp

00001 /* 
00002  * wsdlpull - A C++ parser  for WSDL  (Web services description language)
00003  * Copyright (C) 2005-2007 Vivek Krishna
00004  *
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Library General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2 of the License, or (at your option) any later version.
00009  *
00010  * This library is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Library General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Library General Public
00016  * License along with this library; if not, write to the Free
00017  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018  */
00019 
00020 
00021 //A generic web service invocation tool which uses the invocation API
00022 #include "wsdlparser/WsdlInvoker.h"
00023 using namespace std;
00024 using namespace WsdlPull;
00025 
00026 #ifdef _WIN32
00027 #define PACKAGE_VERSION "1.x"
00028 #endif
00029 
00030 void
00031 usage(void)
00032 {
00033   std::cout<<"Usage wsdl [options] wsdl-uri [operation name] [method parameters]"<<std::endl;
00034   std::cout<<"Version "<<PACKAGE_VERSION<<std::endl;
00035   std::cout<<"Options: "<<std::endl;
00036   std::cout<<"   -h  Display this message"<<std::endl;
00037   std::cout<<"   -x host[:port] Use HTTP proxy on given port"<<std::endl;
00038   std::cout<<"   -U user[:password] Specify Proxy authentication"<<std::endl;
00039   std::cout<<"   -v Verbose mode,SOAP request and response are logged"<<std::endl;
00040   std::cout<<"   -d display WSDL operation's documentation"<<std::endl;
00041   std::cout<<"   -p display WSDL port types and their operations"<<std::endl;
00042   std::cout<<"   -l list all the WSDL operations "<<std::endl;
00043   std::cout<<"   -o Allow setting occurrence constraint (default is 1)"<<std::endl;
00044   std::cout<<"   -s Suppress printing type/element names in the output"<<std::endl;
00045   std::cout<<"   -t requesttimeout in seconds"<<std::endl;
00046   std::cout<<"   -e set SOAP headers in input"<<std::endl;
00047   std::cout<<"   -g generate sample SOAP message for the invocation"<<std::endl;
00048   std::cout<<"With no arguments,wsdl starts in the interactive mode accepting"<<std::endl;  
00049   std::cout<<"operation name and parameters from the standard input"<<std::endl;
00050 }
00051 
00052 bool
00053 printPortTypes(std::string uri)
00054 {
00055 
00056   WsdlParser wp (uri, cout);
00057   while (wp.getEventType () != WsdlParser::END){
00058 
00059       if(wp.getNextElement () == WsdlParser::PORT_TYPE){
00060 
00061           
00062         const PortType * p = wp.getPortType ();
00063         cout << "Port Type :" << p->getName () << "  has  " << 
00064           p->getNumOps () << " operations "<<endl;
00065         Operation::cOpIterator from,to;
00066         p->getOperations(from,to);
00067         while(from!=to){
00068           
00069           const Message* in = (*from)->getMessage(Input); 
00070           const Message* out = (*from)->getMessage(Output); 
00071           MessageList * faults = (*from)->getFaults();
00072           cout<<(*from)->getName()<<endl;
00073           cout <<"  Input Message:"<<in->getName()<<endl;
00074           if (out)
00075             cout <<"  Output Message:"<<out->getName()<<endl;
00076           if (faults) {
00077             for (MessageList::iterator mli = faults->begin();
00078                  mli != faults->end();
00079                  mli++) {
00080               
00081               cout<<"  Fault :"<<(*mli)->getName()<<endl;
00082             }
00083           }
00084           from++;
00085         }
00086 
00087       }
00088   }
00089   return true;
00090 }
00091 
00092 
00093 
00094 int 
00095 main (int argc, char *argv[]) 
00096 {
00097   WsdlInvoker invoker;
00098   bool brkloop =false;
00099   bool showDoc = false;
00100   bool verbose = false;
00101   bool occurs = false;
00102   bool listops = false;
00103   bool generateSoapMsg = false;
00104   bool accept_password =false;
00105   bool accept_headers = false;
00106   long timeout = 0;
00107 
00108   int i =1;
00109   for (;i<argc && !brkloop;){
00110     switch(argv[i][0]){
00111     case '-'://option
00112       {
00113         std::string options(argv[i]+1);
00114         char n = options.length();
00115         while(n--) {
00116           
00117           std::string opt(1,options[n]);
00118           
00119           if (opt=="v"){
00120             invoker.setVerbose(true);
00121             verbose = true;
00122             showDoc = true;
00123 
00124           }
00125           else if (opt == "s"){
00126           
00127             invoker.printTypeNames(false);
00128 
00129           }
00130           else if (opt == "d"){
00131           
00132             showDoc = true;
00133 
00134           }
00135           else if (opt == "e"){
00136           
00137             accept_headers = true;
00138 
00139           }
00140           else if (opt == "l"){
00141           
00142             listops=true;
00143 
00144           }
00145           else if (opt == "x"){
00146             opt = argv[i+1];
00147             size_t pos=opt.find(':');
00148             XmlUtils::setProxyHost (opt);
00149             if(pos==std::string::npos){
00150             
00151               XmlUtils::setProxyHost (XmlUtils::getProxyHost () + ":80");
00152             }
00153             XmlUtils::setProxy (true);
00154             i+=1;
00155             break;
00156           }
00157           else if (opt == "U"){
00158             opt = argv[i+1];
00159             size_t pos=opt.find(':');
00160             XmlUtils::setProxyUser (opt.substr(0,pos));
00161             if(pos!=std::string::npos)
00162               XmlUtils::setProxyPass (opt.substr(pos+1));
00163             else
00164               accept_password = true;
00165             i+=1;
00166             XmlUtils::setProxy (true);
00167             break;
00168           }
00169           else if (opt =="p"){
00170             
00171             if(printPortTypes(argv[i+1]))
00172               exit(0);
00173             else 
00174               exit(1);
00175           }
00176           else if (opt =="h"){
00177             usage();
00178             exit(0);
00179           }
00180           else if (opt == "g"){
00181 
00182             generateSoapMsg = true;
00183           }
00184           else if(opt == "o"){
00185           
00186             occurs = true;//ask for occurrence constraints
00187 
00188           }
00189           else if(opt == "t"){
00190             opt = argv[i+1];
00191             timeout=atoi(opt.c_str());
00192             i+=1;
00193             break;
00194           }
00195           else{
00196             std::cerr<<"Unknown option "<<argv[i]<<std::endl;
00197             usage();
00198             exit(2);
00199           }
00200 
00201         }
00202         i++;
00203         break;
00204 
00205       }
00206     default:
00207       brkloop = true;
00208       //end of options
00209       break;
00210     }
00211   }
00212 
00213   if (XmlUtils::getProxy () && accept_password){
00214      
00215     XmlUtils::setProxyPass (XmlUtils::acceptSecretKey("Proxy Password"));
00216     std::cout<<endl;
00217   }
00218 
00219   if (i < argc){
00220     if(!invoker.setWSDLUri(argv[i])) {
00221 
00222       std::cerr<<"Error processing "<<argv[i]<<std::endl;
00223       std::cerr<<invoker.errors()<<std::endl;
00224       return 1;
00225     }
00226 #ifdef LOGGING
00227       std::cerr<<invoker.errors()<<std::endl;
00228 #endif
00229     i++;
00230   }
00231   else{
00232     
00233     usage();
00234     exit (2);
00235   }
00236 
00237   if (verbose)
00238     std::cout<<invoker.errors()<<std::endl;
00239   
00240   if (i<argc && !listops){
00241     
00242     if(!invoker.setOperation(argv[i])){
00243       
00244       std::cerr<<"Unkown operation name "<<argv[i]<<std::endl;
00245       return 2;
00246     }
00247     i++;
00248   }
00249   else{
00250   
00251     std::vector<std::string> ops;
00252     unsigned int choice = 0;
00253     if (invoker.getOperations(ops)){
00254     
00255       for (size_t s = 0;s<ops.size();s++){
00256       
00257         std::cout<<s+1<<"."<<ops[s];
00258         
00259         if (showDoc) {
00260          
00261           std::string doc = invoker.getOpDocumentaion(ops[s]);
00262           if (!doc.empty())
00263             std::cout<<"("<<doc<<")";
00264         }
00265         std::cout<<endl;
00266       }
00267       if (listops == true){
00268         
00269         return 0;
00270       }
00271       while (choice==0){
00272         
00273         std::cout<<"Choose one of the above operations [1-"<<ops.size()<<"] :";
00274         std::cin>>choice;
00275         if (choice>0 && choice<=ops.size())
00276           break;
00277         else
00278           choice=0;
00279       }
00280     }
00281     else {
00282 
00283       std::cerr<<"No operation found or missing <binding> section"<<std::endl;
00284       return 2;
00285     }
00286     if (!invoker.setOperation(ops[choice-1])){
00287       
00288       std::cerr<<"Couldn't invoke operation "<<std::endl;
00289       return 1;
00290     }
00291   }
00292   if(!accept_headers && invoker.nInputHeaders()>0){
00293     
00294     std::cout<<"Warning:This operation has some SOAP headers in its inputs!(use -e)"<<std::endl;
00295   }
00296 
00297   if (invoker.status()){
00298   
00299     int id =0,minimum,maximum,n;
00300     Schema::Type t;
00301     std::string param;
00302     std::string val;
00303     std::vector<std::string> values;
00304     std::vector<std::string> parents;
00305     
00306     do{    
00307 
00308       if (accept_headers && invoker.nInputHeaders()>0){
00309         
00310         id = invoker.getNextHeaderInput(param,t,minimum,maximum,parents);
00311         if (id == -1){
00312           accept_headers=false;//done with headers
00313           continue;
00314         }
00315       }
00316       else{
00317         
00318         id = invoker.getNextInput(param,t,minimum,maximum,parents);
00319       }
00320       if (id == -1)
00321         break;
00322       n = minimum;
00323       if (occurs && minimum < maximum) {
00324         values.clear();
00325         std::cout<<param<<"["<<minimum<<","<<maximum<<"] Enter number of occurrences:";
00326         cin>>n;
00327         
00328         if (n<minimum || n>maximum){
00329           
00330           std::cerr<<"Didnt match occurrence constraints"<<std::endl;
00331           return 2;
00332         }
00333         while(n--) {
00334 
00335           if (i <argc) {
00336             val = argv[i++];
00337           }
00338           else {
00339             std::cout<<param<<": ";
00340             cin>>val;
00341           }
00342           values.push_back(val);
00343         }
00344         if (!invoker.setInputValue(id,values)){
00345 
00346           std::cerr<<"Incorrect input values "<<std::endl;
00347           return 2;
00348         }
00349       }
00350       else{
00351 
00352         if (i <argc) {
00353           
00354           val = argv[i++];
00355         }
00356         else{
00357           size_t j = 0;
00358           for (j=0;j<parents.size()-1;j++){
00359 
00360             std::cout<<parents[j]<<".";
00361           }
00362           std::cout<<parents[j]<<": ";
00363           cin>>val;
00364         }
00365         if (!invoker.setInputValue(id,val)){
00366 
00367           std::cerr<<"Incorrect input value "<<val<<std::endl;
00368           return 2;
00369         }
00370       }
00371     }while(1);
00372   
00373 
00374     if (generateSoapMsg) {
00375 
00376       //output the soap message and exit
00377       std::cout <<invoker.getSoapMessage()<<std::endl;
00378       return 0;
00379 
00380     }
00381 
00382 #ifndef WITH_CURL
00383 #ifndef _WIN32
00384     std::cerr<<"libcurl needs to be installed to proceed with invocation"<<std::endl;
00385     std::cerr<<"Try using the -g option to just print the soap message"<<std::endl;
00386     exit(2);
00387 #endif
00388 #endif 
00389 
00390     if (invoker.invoke(timeout)){
00391 
00392       TypeContainer* tc = 0;
00393       std::string name;
00394       while(invoker.getNextHeaderOutput(name,tc)) {
00395         
00396         tc->print(std::cout);
00397       }
00398       
00399       while (invoker.getNextOutput(name,tc)){
00400             
00401         tc->print(std::cout);
00402       }
00403       return 0;
00404     }
00405     else{
00406       cerr<<invoker.errors()<<endl;
00407     }
00408   }
00409   return 1;
00410 }
00411 
00412 
00413 
00414 

Generated on Sun Aug 5 20:01:34 2007 for wsdlpull by  doxygen 1.4.6