00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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] [xpath expression for response]"<<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<<" -r Validate the response message with schema even when xpath selector is used(default is off)"<<std::endl;
00049 std::cout<<"With no arguments,wsdl starts in the interactive mode accepting operation name and parameters from the standard input."<<std::endl<<std::endl;
00050 std::cout<<"An xpath expression can be used to extract elements from web service response.If the expression points to an element or an attribute,the element's text or attribute value will be returned.The descendent selector // will also match all occurrences in the xml tree"<<std::endl;
00051
00052
00053 }
00054
00055 bool
00056 printPortTypes(std::string uri)
00057 {
00058
00059 WsdlParser wp (uri, cout);
00060 while (wp.getEventType () != WsdlParser::END){
00061
00062 if(wp.getNextElement () == WsdlParser::PORT_TYPE){
00063
00064
00065 const PortType * p = wp.getPortType ();
00066 cout << "Port Type :" << p->getName () << " has " <<
00067 p->getNumOps () << " operations "<<endl;
00068 Operation::cOpIterator from,to;
00069 p->getOperations(from,to);
00070 while(from!=to){
00071
00072 const Message* in = (*from)->getMessage(Input);
00073 const Message* out = (*from)->getMessage(Output);
00074 MessageList * faults = (*from)->getFaults();
00075 cout<<(*from)->getName()<<endl;
00076 cout <<" Input Message:"<<in->getName()<<endl;
00077 if (out)
00078 cout <<" Output Message:"<<out->getName()<<endl;
00079 if (faults) {
00080 for (MessageList::iterator mli = faults->begin();
00081 mli != faults->end();
00082 mli++) {
00083
00084 cout<<" Fault :"<<(*mli)->getName()<<endl;
00085 }
00086 }
00087 from++;
00088 }
00089
00090 }
00091 }
00092 return true;
00093 }
00094
00095
00096
00097 int
00098 main (int argc, char *argv[])
00099 {
00100 WsdlInvoker invoker;
00101 bool brkloop =false;
00102 bool showDoc = false;
00103 bool verbose = false;
00104 bool occurs = false;
00105 bool listops = false;
00106 bool generateSoapMsg = false;
00107 bool accept_password =false;
00108 bool accept_headers = false;
00109 bool processResponse = false;
00110 long timeout = 0;
00111
00112 #ifdef _WIN32
00113 WsdlPull::WsdlParser::useLocalSchema_ = false;
00114 #endif
00115
00116
00117 int i =1;
00118 for (;i<argc && !brkloop;){
00119 switch(argv[i][0]){
00120 case '-':
00121 {
00122 std::string options(argv[i]+1);
00123 char n = options.length();
00124 while(n--) {
00125
00126 std::string opt(1,options[n]);
00127
00128 if (opt=="v"){
00129 invoker.setVerbose(true);
00130 verbose = true;
00131 showDoc = true;
00132
00133 }
00134 else if (opt == "s"){
00135
00136 invoker.printTypeNames(false);
00137
00138 }
00139 else if (opt == "d"){
00140
00141 showDoc = true;
00142
00143 }
00144 else if (opt == "e"){
00145
00146 accept_headers = true;
00147
00148 }
00149 else if (opt == "l"){
00150
00151 listops=true;
00152
00153 }
00154 else if (opt == "x"){
00155 opt = argv[i+1];
00156 size_t pos=opt.find(':');
00157 XmlUtils::setProxyHost (opt);
00158 if(pos==std::string::npos){
00159
00160 XmlUtils::setProxyHost (XmlUtils::getProxyHost () + ":80");
00161 }
00162 XmlUtils::setProxy (true);
00163 i+=1;
00164 break;
00165 }
00166 else if (opt == "U"){
00167 opt = argv[i+1];
00168 size_t pos=opt.find(':');
00169 XmlUtils::setProxyUser (opt.substr(0,pos));
00170 if(pos!=std::string::npos)
00171 XmlUtils::setProxyPass (opt.substr(pos+1));
00172 else
00173 accept_password = true;
00174 i+=1;
00175 XmlUtils::setProxy (true);
00176 break;
00177 }
00178 else if (opt =="p"){
00179
00180 if(printPortTypes(argv[i+1]))
00181 exit(0);
00182 else
00183 exit(1);
00184 }
00185 else if (opt =="h"){
00186 usage();
00187 exit(0);
00188 }
00189 else if (opt == "g"){
00190
00191 generateSoapMsg = true;
00192 }
00193 else if(opt == "o"){
00194
00195 occurs = true;
00196
00197 }
00198 else if(opt == "t"){
00199 opt = argv[i+1];
00200 timeout=atoi(opt.c_str());
00201 i+=1;
00202 break;
00203 }
00204 else if(opt == "r"){
00205 processResponse = true;
00206 }
00207 else{
00208 std::cerr<<"Unknown option "<<argv[i]<<std::endl;
00209 usage();
00210 exit(2);
00211 }
00212
00213 }
00214 i++;
00215 break;
00216
00217 }
00218 default:
00219 brkloop = true;
00220
00221 break;
00222 }
00223 }
00224
00225 if (XmlUtils::getProxy () && accept_password){
00226
00227 XmlUtils::setProxyPass (XmlUtils::acceptSecretKey("Proxy Password"));
00228 std::cout<<endl;
00229 }
00230
00231 if (i < argc){
00232 if(!invoker.setWSDLUri(argv[i])) {
00233
00234 std::cerr<<"Error processing "<<argv[i]<<std::endl;
00235 std::cerr<<invoker.errors()<<std::endl;
00236 return 1;
00237 }
00238 #ifdef LOGGING
00239 std::cerr<<invoker.errors()<<std::endl;
00240 #endif
00241 i++;
00242 }
00243 else{
00244
00245 usage();
00246 exit (2);
00247 }
00248
00249 if (verbose)
00250 std::cout<<invoker.errors()<<std::endl;
00251
00252 if (i<argc && !listops){
00253
00254 if(!invoker.setOperation(argv[i])){
00255
00256 std::cerr<<"Unkown operation name "<<argv[i]<<std::endl;
00257 return 2;
00258 }
00259 i++;
00260 }
00261 else{
00262
00263 std::vector<std::string> ops;
00264 unsigned int choice = 0;
00265 if (invoker.getOperations(ops)){
00266
00267 for (size_t s = 0;s<ops.size();s++){
00268
00269 std::cout<<s+1<<"."<<ops[s];
00270
00271 if (showDoc) {
00272
00273 std::string doc = invoker.getOpDocumentation(ops[s]);
00274 if (!doc.empty())
00275 std::cout<<"("<<doc<<")";
00276 }
00277 std::cout<<endl;
00278 }
00279 if (listops == true){
00280
00281 return 0;
00282 }
00283 while (choice==0){
00284
00285 std::cout<<"Choose one of the above operations [1-"<<ops.size()<<"] :";
00286 std::cin>>choice;
00287 if (choice>0 && choice<=ops.size())
00288 break;
00289 else
00290 choice=0;
00291 }
00292 }
00293 else {
00294
00295 std::cerr<<"No operation found or missing <binding> section"<<std::endl;
00296 return 2;
00297 }
00298 if (!invoker.setOperation(ops[choice-1])){
00299
00300 std::cerr<<"Couldn't invoke operation "<<std::endl<<invoker.errors()<<std::endl;
00301 return 1;
00302 }
00303 }
00304 if(!accept_headers && invoker.nInputHeaders()>0){
00305
00306 std::cout<<"Warning:This operation has some SOAP headers in its inputs!(use -e)"<<std::endl;
00307 }
00308
00309 if (invoker.status()){
00310
00311 int id =0,minimum,maximum,n;
00312 Schema::Type t;
00313 std::string param;
00314 std::string val;
00315 std::vector<std::string> values;
00316 std::vector<std::string> parents;
00317
00318 do{
00319
00320 if (accept_headers && invoker.nInputHeaders()>0){
00321
00322 id = invoker.getNextHeaderInput(param,t,minimum,maximum,parents);
00323 if (id == -1){
00324 accept_headers=false;
00325 continue;
00326 }
00327 }
00328 else{
00329
00330 id = invoker.getNextInput(param,t,minimum,maximum,parents);
00331 }
00332 if (id == -1)
00333 break;
00334 n = minimum;
00335 if (occurs && minimum < maximum) {
00336 values.clear();
00337 std::cout<<param<<"["<<minimum<<","<<maximum<<"] Enter number of occurrences:";
00338 cin>>n;
00339
00340 if (n<minimum || n>maximum){
00341
00342 std::cerr<<"Didnt match occurrence constraints"<<std::endl;
00343 return 2;
00344 }
00345 while(n--) {
00346
00347 if (i <argc) {
00348 val = argv[i++];
00349 }
00350 else {
00351 std::cout<<param<<": ";
00352 cin>>val;
00353 }
00354 values.push_back(val);
00355 }
00356 if (!invoker.setInputValue(id,values)){
00357
00358 std::cerr<<"Incorrect input values "<<std::endl;
00359 return 2;
00360 }
00361 }
00362 else{
00363
00364 if (i <argc) {
00365
00366 val = argv[i++];
00367 }
00368 else{
00369 size_t j = 0;
00370 for (j=0;j<parents.size()-1;j++){
00371
00372 std::cout<<parents[j]<<".";
00373 }
00374 std::cout<<parents[j]<<": ";
00375 cin>>val;
00376 }
00377 if (!invoker.setInputValue(id,val)){
00378
00379 std::cerr<<"Incorrect input value "<<val<<std::endl;
00380 return 2;
00381 }
00382 }
00383 }while(1);
00384
00385
00386 if (generateSoapMsg) {
00387
00388
00389 std::cout <<invoker.getSoapMessage()<<std::endl;
00390 return 0;
00391
00392 }
00393
00394 #ifndef WITH_CURL
00395 #ifndef _WIN32
00396 std::cerr<<"libcurl needs to be installed to proceed with invocation"<<std::endl;
00397 std::cerr<<"Try using the -g option to just print the soap message"<<std::endl;
00398 exit(2);
00399 #endif
00400 #endif
00401
00402 if (invoker.invoke(timeout,(i>=argc || processResponse))){
00403
00404 TypeContainer* tc = 0;
00405 std::string name;
00406
00407 if (i <argc) {
00408
00409 try {
00410
00411 std::vector<std::string> arr=invoker.getValues<std::string>(argv[i++]);
00412 for (size_t s = 0;s<arr.size();s++)
00413 std::cout<<arr[s]<<std::endl;
00414
00415 return 0;
00416 }
00417 catch (WsdlException we) {
00418
00419 std::cerr<<we.description<<std::endl;
00420 }
00421 catch (XmlPullParserException xpe) {
00422
00423 std::cerr<<xpe.description<<std::endl;
00424 }
00425 return 2;
00426 }
00427 while(invoker.getNextHeaderOutput(name,tc)) {
00428
00429
00430 tc->print(std::cout);
00431 std::cout<<std::endl;
00432 }
00433
00434 while (invoker.getNextOutput(name,tc)){
00435
00436 tc->print(std::cout);
00437 std::cout<<std::endl;
00438 }
00439 return 0;
00440 }
00441 else{
00442 cerr<<invoker.errors()<<std::endl;
00443 cerr<<"Run with -v option and see request.log and response.log"<<endl;
00444 }
00445 }
00446 return 1;
00447 }
00448
00449
00450
00451