wsdlpull svntrunk
wsdl.cpp
Go to the documentation of this file.
1/*
2 * wsdlpull - A C++ parser for WSDL (Web services description language)
3 * Copyright (C) 2005-2007 Vivek Krishna
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the Free
17 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20
21//A generic web service invocation tool which uses the invocation API
23//
24#include <cstdlib>
25
26using namespace std;
27using namespace WsdlPull;
28
29#ifdef _WIN32
30#define PACKAGE_VERSION "1.x"
31#endif
32
33void
34usage(void)
35{
36 std::cout<<"Usage wsdl [options] wsdl-uri [operation name] [method parameters] [xpath expression for response]"<<std::endl;
37 std::cout<<"Version "<<PACKAGE_VERSION<<std::endl;
38 std::cout<<"Options: "<<std::endl;
39 std::cout<<" -h Display this message"<<std::endl;
40 std::cout<<" -x host[:port] Use HTTP proxy on given port"<<std::endl;
41 std::cout<<" -U user[:password] Specify Proxy authentication"<<std::endl;
42 std::cout<<" -v Verbose mode,SOAP request and response are logged"<<std::endl;
43 std::cout<<" -d display WSDL operation's documentation"<<std::endl;
44 std::cout<<" -p display WSDL port types and their operations"<<std::endl;
45 std::cout<<" -l list all the WSDL operations "<<std::endl;
46 std::cout<<" -o Allow setting occurrence constraint (default is 1)"<<std::endl;
47 std::cout<<" -s Suppress printing type/element names in the output"<<std::endl;
48 std::cout<<" -t requesttimeout in seconds"<<std::endl;
49 std::cout<<" -e set SOAP headers in input"<<std::endl;
50 std::cout<<" -g generate sample SOAP message for the invocation"<<std::endl;
51 std::cout<<" -w get soap,encoding and wsdl schemas from the web"<<std::endl;
52 std::cout<<" -f get soap,encoding and wsdl schemas from the filesystem"<<std::endl;
53 std::cout<<" -r Validate the response message with schema even when xpath selector is used(default is off)"<<std::endl;
54 std::cout<<"With no arguments,wsdl starts in the interactive mode accepting operation name and parameters from the standard input."<<std::endl<<std::endl;
55 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 expression will match all occurrences in the xml tree"<<std::endl;
56
57
58}
59
60bool
61printPortTypes(std::string uri)
62{
63
64 WsdlParser wp (uri, cout);
65 while (wp.getEventType () != WsdlParser::END){
66
67 if(wp.getNextElement () == WsdlParser::PORT_TYPE){
68
69
70 const PortType * p = wp.getPortType ();
71 cout << "Port Type :" << p->getName () << " has " <<
72 p->getNumOps () << " operations "<<endl;
74 p->getOperations(from,to);
75 while(from!=to){
76
77 const Message* in = (*from)->getMessage(Input);
78 const Message* out = (*from)->getMessage(Output);
79 MessageList * faults = (*from)->getFaults();
80 cout<<(*from)->getName()<<endl;
81 cout <<" Input Message:"<<in->getName()<<endl;
82 if (out)
83 cout <<" Output Message:"<<out->getName()<<endl;
84 if (faults) {
85 for (MessageList::iterator mli = faults->begin();
86 mli != faults->end();
87 mli++) {
88
89 cout<<" Fault :"<<(*mli)->getName()<<endl;
90 }
91 }
92 from++;
93 }
94
95 }
96 }
97 return true;
98}
99
100
101
102int
103main (int argc, char *argv[])
104{
105 WsdlInvoker invoker;
106 bool brkloop =false;
107 bool showDoc = false;
108 bool verbose = false;
109 bool occurs = false;
110 bool listops = false;
111 bool generateSoapMsg = false;
112 bool accept_password =false;
113 bool accept_headers = false;
114 bool processResponse = false;
115 long timeout = 0;
116
117#ifdef _WIN32
119#endif
120
121 int i =1;
122 for (;i<argc && !brkloop;){
123 switch(argv[i][0]){
124 case '-'://option
125 {
126 std::string options(argv[i]+1);
127 char n = options.length();
128 while(n--) {
129
130 std::string opt(1,options[n]);
131
132 if (opt=="v"){
133 invoker.setVerbose(true);
134 verbose = true;
135 showDoc = true;
136
137 }
138 else if (opt == "s"){
139
140 invoker.printTypeNames(false);
141
142 }
143 else if (opt == "d"){
144
145 showDoc = true;
146
147 }
148 else if (opt == "e"){
149
150 accept_headers = true;
151
152 }
153 else if (opt == "l"){
154
155 listops=true;
156
157 }
158 else if (opt == "x"){
159 opt = argv[i+1];
160 size_t pos=opt.find(':');
162 if(pos==std::string::npos){
163
165 }
166 XmlUtils::setProxy (true);
167 i+=1;
168 break;
169 }
170 else if (opt == "U"){
171 opt = argv[i+1];
172 size_t pos=opt.find(':');
173 XmlUtils::setProxyUser (opt.substr(0,pos));
174 if(pos!=std::string::npos)
175 XmlUtils::setProxyPass (opt.substr(pos+1));
176 else
177 accept_password = true;
178 i+=1;
179 XmlUtils::setProxy (true);
180 break;
181 }
182 else if (opt =="p"){
183
184 if(printPortTypes(argv[i+1]))
185 return 0;
186 else
187 return 1;
188 }
189 else if (opt =="h"){
190 usage();
191 return 0;
192 }
193 else if (opt == "g"){
194
195 generateSoapMsg = true;
196 }
197 else if(opt == "o"){
198
199 occurs = true;//ask for occurrence constraints
200
201 }
202 else if(opt == "t"){
203 opt = argv[i+1];
204 timeout=atoi(opt.c_str());
205 i+=1;
206 break;
207 }
208 else if(opt == "r"){
209 processResponse = true;
210 }
211 else if (opt == "w"){
212
214 }
215 else if (opt == "f"){
216
218 }
219 else{
220 std::cerr<<"Unknown option "<<argv[i]<<std::endl;
221 usage();
222 return 2;
223 }
224
225 }
226 i++;
227 break;
228
229 }
230 default:
231 brkloop = true;
232 //end of options
233 break;
234 }
235 }
236
237 if (XmlUtils::getProxy () && accept_password){
238
240 std::cout<<endl;
241 }
242
243 if (i < argc){
244 if(!invoker.setWSDLUri(argv[i])) {
245
246 std::cerr<<"Error processing "<<argv[i]<<std::endl;
247 std::cerr<<invoker.errors()<<std::endl;
248 return 1;
249 }
250#ifdef LOGGING
251 std::cerr<<invoker.errors()<<std::endl;
252#endif
253 i++;
254 }
255 else{
256
257 usage();
258 return 2;
259 }
260
261 if (verbose)
262 std::cout<<invoker.errors()<<std::endl;
263
264 if (i<argc && !listops){
265
266 if(!invoker.setOperation(argv[i])){
267
268 std::cerr<<"Unkown operation name "<<argv[i]<<std::endl;
269 return 2;
270 }
271 i++;
272 }
273 else{
274
275 std::vector<std::string> ops;
276 unsigned int choice = 0;
277 if (invoker.getOperations(ops)){
278
279 for (size_t s = 0;s<ops.size();s++){
280
281 std::cout<<s+1<<"."<<ops[s];
282
283 if (showDoc) {
284
285 std::string doc = invoker.getOpDocumentation(ops[s]);
286 if (!doc.empty())
287 std::cout<<"("<<doc<<")";
288 }
289 std::cout<<endl;
290 }
291 if (listops == true){
292
293 return 0;
294 }
295 while (choice==0){
296
297 std::cout<<"Choose one of the above operations [1-"<<ops.size()<<"] :";
298 std::cin>>choice;
299 if (choice>0 && choice<=ops.size())
300 break;
301 else
302 choice=0;
303 }
304 }
305 else {
306
307 std::cerr<<"No operation found or missing <binding> section"<<std::endl;
308 return 2;
309 }
310 if (!invoker.setOperation(ops[choice-1])){
311
312 std::cerr<<"Couldn't invoke operation "<<std::endl<<invoker.errors()<<std::endl;
313 return 1;
314 }
315 }
316 if(!accept_headers && invoker.nInputHeaders()>0){
317
318 std::cout<<"Warning:This operation has some SOAP headers in its inputs!(use -e)"<<std::endl;
319 }
320
321 if (invoker.status()){
322
323 int id =0,minimum,maximum,n;
324 Schema::Type t;
325 std::string param;
326 std::string val;
327 std::vector<std::string> values;
328 std::vector<std::string> parents;
329
330 do{
331
332 if (accept_headers && invoker.nInputHeaders()>0){
333
334 id = invoker.getNextHeaderInput(param,t,minimum,maximum,parents);
335 if (id == -1){
336 accept_headers=false;//done with headers
337 continue;
338 }
339 }
340 else{
341
342 id = invoker.getNextInput(param,t,minimum,maximum,parents);
343 }
344 if (id == -1)
345 break;
346 n = minimum;
347 if (occurs && minimum < maximum) {
348 values.clear();
349 std::cout<<param<<"["<<minimum<<","<<maximum<<"] Enter number of occurrences:";
350 cin>>n;
351
352 if (n<minimum || n>maximum){
353
354 std::cerr<<"Didnt match occurrence constraints"<<std::endl;
355 return 2;
356 }
357 while(n--) {
358
359 if (i <argc) {
360 val = argv[i++];
361 }
362 else {
363 std::cout<<param<<": ";
364 cin>>val;
365 }
366 values.push_back(val);
367 }
368 if (!invoker.setInputValue(id,values)){
369
370 std::cerr<<"Incorrect input values "<<std::endl;
371 return 2;
372 }
373 }
374 else{
375
376 if (i <argc) {
377
378 val = argv[i++];
379 }
380 else{
381 size_t j = 0;
382 for (j=0;j<parents.size()-1;j++){
383
384 std::cout<<parents[j]<<".";
385 }
386 std::cout<<parents[j]<<": ";
387 cin>>val;
388 }
389 if (!invoker.setInputValue(id,val)){
390
391 std::cerr<<"Incorrect input value "<<val<<std::endl;
392 return 2;
393 }
394 }
395 }while(1);
396
397
398 if (generateSoapMsg) {
399
400 //output the soap message and exit
401 std::cout <<invoker.getSoapMessage()<<std::endl;
402 return 0;
403
404 }
405
406#ifndef WITH_CURL
407#ifndef _WIN32
408 std::cerr<<"libcurl needs to be installed to proceed with invocation"<<std::endl;
409 std::cerr<<"Try using the -g option to just print the soap message"<<std::endl;
410 return 2;
411#endif
412#endif
413
414 if (invoker.invoke(timeout,(i>=argc || processResponse))){
415
416 TypeContainer* tc = 0;
417 std::string name;
418
419 if (i <argc) {
420
421 try {
422 //the last argument is an xpath expression to get the output
423 std::vector<std::string> arr=invoker.getValues<std::string>(argv[i++]);
424 for (size_t s = 0;s<arr.size();s++)
425 std::cout<<arr[s]<<std::endl;
426
427 return 0;
428 }
429 catch (WsdlException we) {
430
431 std::cerr<<we.description<<std::endl;
432 }
433 catch (XmlPullParserException xpe) {
434
435 std::cerr<<xpe.description<<std::endl;
436 }
437 return 2;
438 }
439 while(invoker.getNextHeaderOutput(name,tc)) {
440
441
442 tc->print(std::cout);
443 std::cout<<std::endl;
444 }
445
446 while (invoker.getNextOutput(name,tc)){
447
448 tc->print(std::cout);
449 std::cout<<std::endl;
450 }
451 return 0;
452 }
453 else{
454 cerr<<invoker.errors()<<std::endl;
455 cerr<<"Run with -v option and see request.log and response.log"<<endl;
456 }
457 }
458 return 1;
459}
460
461
462
463
void print(std::ostream &os)
std::vector< Operation * >::const_iterator cOpIterator
Definition: Operation.h:57
bool getOperations(Operation::cOpIterator &start, Operation::cOpIterator &finish) const
Definition: PortType.h:140
int getNumOps(void) const
Definition: PortType.h:102
std::string getName() const
Definition: WsdlElement.h:110
bool setInputValue(const int param, void *val)
int getOperations(std::vector< std::string > &operations)
return names of operations (only for the SOAP binding portType)
bool getNextOutput(std::string &name, TypeContainer *&tc)
std::string errors()
Definition: WsdlInvoker.h:377
bool setWSDLUri(const std::string &url, const std::string &schemaPath="")
Definition: WsdlInvoker.h:384
void printTypeNames(bool f)
int getNextHeaderInput(std::string &param, Schema::Type &type, int &minimum, int &maximum)
std::vector< T > getValues(const std::string &xpath)
sets the param value for an operation by name of the parameter
Definition: WsdlInvoker.h:470
int getNextInput(std::string &param, Schema::Type &type, int &minimum, int &maximum)
void setVerbose(bool f)
Definition: WsdlInvoker.h:406
bool invoke(long timeout=0, bool processResponse=true)
int nInputHeaders() const
Definition: WsdlInvoker.h:413
bool getNextHeaderOutput(std::string &name, TypeContainer *&tc)
std::string getOpDocumentation(const std::string &n)
std::string getSoapMessage()
bool setOperation(const std::string &operation, WsdlPull::MessageType mType=WsdlPull::Input)
set the operation to invoke
bool status() const
Definition: WsdlInvoker.h:392
const PortType * getPortType()
Definition: WsdlParser.cpp:259
static bool useLocalSchema_
Definition: WsdlParser.h:259
Type
Definition: Schema.h:60
@ Output
Definition: Operation.h:45
std::list< const Message * > MessageList
Definition: Operation.h:49
bool WSDLPULL_EXPORT getProxy()
Definition: XmlUtils.cpp:510
std::string WSDLPULL_EXPORT getProxyHost()
Definition: XmlUtils.cpp:524
void WSDLPULL_EXPORT setProxyPass(const std::string &sProxyPass)
Definition: XmlUtils.cpp:559
std::string WSDLPULL_EXPORT acceptSecretKey(const std::string &field)
Definition: XmlUtils.cpp:434
void WSDLPULL_EXPORT setProxy(const bool bProxy)
Definition: XmlUtils.cpp:517
void WSDLPULL_EXPORT setProxyUser(const std::string &sProxyUser)
Definition: XmlUtils.cpp:545
void WSDLPULL_EXPORT setProxyHost(const std::string &sProxyHost)
Definition: XmlUtils.cpp:531
int main(int argc, char *argv[])
Definition: wsdl.cpp:103
bool printPortTypes(std::string uri)
Definition: wsdl.cpp:61
void usage(void)
Definition: wsdl.cpp:34