wsdlpull  1.23
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
WsdlInvoker.h
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 //An api to examine and invoke the web service in a protocol independent fashion
21 
22 #ifndef _WSDLINVOKERH
23 #define _WSDLINVOKERH
24 
25 #include "xmlpull/XmlSerializer.h"
26 #include "wsdlparser/WsdlParser.h"
27 #include "wsdlparser/Soap.h"
29 
30 #include "xmlpull/XmlDoc.h"
31 
32 namespace WsdlPull{
33 
34  struct Parameter
35  {
36  Parameter(Schema::Type ,std::string,int m,int x,const SchemaParser* s,
37  const std::vector<std::string>& parents);
39  std::string tag_;
40  unsigned int min_;
41  unsigned int max_;
42  int n_;
43  std::vector<std::string> data_;
44  //bool str_;
46  std::vector<std::string> parents_;
47 
48  };
49 
51 {
52  public:
61  WsdlInvoker(const std::string &url, const std::string &schemaPath="");
62  WsdlInvoker(std::istream &input, const std::string &schemaPath = "");
63  WsdlInvoker();
64  ~WsdlInvoker();
66 
74  bool setWSDLUri(const std::string &url, const std::string & schemaPath="");
80  int getOperations(std::vector<std::string> & operations);
81  std::string getOpDocumentation(const std::string & n);
82  std::string getDocumentation();
88  bool setOperation(const std::string & operation,
93  std::string getServiceEndPoint(const std::string & opname) ;
95 
96 
99 
110  bool setValue(const std::string & param,void* val);
111  bool setValue(const std::string & param,void** values,unsigned int occur);
112  bool setValue(const std::string & param,std::string val);
113  bool setValue(const std::string & param,std::vector<std::string> values);//multiple occurrences
114  bool setValue(const std::vector<std::string> & parents,void* val);
120  bool invoke(long timeout = 0,bool processResponse = true);
129  void* getValue(const std::string & param,Schema::Type & t);
130 
131 
152  template <typename T>
153  T getValue(const std::string & xpath, size_t p_index = 1);
154 
155  /* identical to previous method but returns multiple matches if found in the document*/
156  template <typename T>
157  std::vector<T> getValues(const std::string & xpath);
159 
162 
177  int getNextInput(std::string & param ,Schema::Type & type,int & minimum,int & maximum);
178  int getNextInput(std::string & param ,Schema::Type & type,int & minimum,int & maximum,
179  std::vector<std::string>& parents);
185  int getNextHeaderInput(std::string & param ,Schema::Type & type,int & minimum,int & maximum);
186  int getNextHeaderInput(std::string & param ,Schema::Type & type,int & minimum,int & maximum,
187  std::vector<std::string>& parents);
191  int nInputHeaders()const;
192 
200  bool setInputValue(const int param,void* val);
201  bool setInputValue(const int id,void** values,unsigned int occur);
202 
210  bool setInputValue(const int param,std::string val);
211  bool setInputValue(const int param,std::vector<std::string> values);//multiple occurrences
212 
218  bool getNextOutput(std::string & name,TypeContainer * & tc);
219 
224  TypeContainer* getOutput(const std::string & name);
225 
231  bool getNextHeaderOutput(std::string & name,TypeContainer*& tc);
232 
234  // change location of the service
235  void setLocation(const std::string & url);
236  // set credentials for the service
237  void setCredentials(const std::string & user, const std::string & pass);
238  // set http authentication for the service
239  void setAuth(const std::string & user, const std::string & pass);
240  //ouput the soap message without invoking the service
241  std::string getSoapMessage();
242  std::string getXMLResponse();
243  void setProxy(const std::string & host,int port=80);
244  //enable logging
245  void setVerbose(bool f);
246  // prints the output along with typenames like
247  // name:value.
248  //if false only value is printed
249  void printTypeNames(bool f);
250  //get the error message
251  std::string errors();
252  //if this returns false something went wrong
253  bool status()const;
254 
255  //get SOAP fault related things
256  std::string getFaultCode() const;
257  std::string getFaultSubCode() const;
258  std::string getFaultString() const;
259  std::string getFaultActor() const;
260 
261  // Control XML Tree Generation and Xpath search fine tuning
262  void setProcessEnvAndBody( bool p_processEnvAndBody);
263  bool getProcessEnvAndBody( void) const;
264 
265  void setLazyRelativeMatch( bool p_lazyRelativeMatch);
266  bool getLazyRelativeMatch( void) const;
267 
268  void setBuildXmlTree( bool p_buildXmlTree);
269  bool getBuildXmlTree( void) const;
270  void buildXmlTree( XmlPullParser &p_xmlPullParser, XmlNode_t &p_xmlNode,
271  bool p_notScannedEventAvail = false);
272 
273  private:
274  //private stuff
275 
276  bool init(WsdlParser* parser);
277  //creaate xml request
278  void serialize();
279  void serializeType(Schema::Type typeId,
280  const std::string &tag,
281  const SchemaParser * sParser,
282  int minimum,
283  int maximum,
284  std::vector<std::string> parents,
285  const std::string nsp="",
286  bool isRoot = false);
287 
288  void serializeParam(int n,const std::string & tag,
289  const SchemaParser * sParser,
290  const std::string nsp="",
291  bool isRoot = false);
292  void serializeContentModel(ContentModel *cm,
293  const SchemaParser *sParser,
294  std::vector<std::string> parents);
299  void post(long timeout=0, std::string username="", std::string passwd="");
300  void processResults();
301  void processFault(XmlPullParser* xpp);
302  void processHeader(XmlPullParser *xpp);
303  void processBody(const Message* m,XmlPullParser* xpp);
304  void parseWsdl(const std::string & url, const std::string & schemaPath = "");
305  void serializeHeader();
306  bool isSoapArray (const ComplexType * ct,const SchemaParser * sParser);
307  //reset state information
308  void reset();
309  void getOperationDetails(const Operation* op);
310  std::string getPrefix(const std::string& nsp);
311  std::string getAsStringFromXPath(const std::string & xpath, size_t p_index, std::vector<std::string>* arr = 0);
312  WsdlParser * wParser_;
313  std::string strResults_;
314  WsdlParser * ourParser_;
315  XmlSerializer * xmlStream_;
316  Soap* soap_;
317  bool soapheaders_;
318  int hPartId_;
319  std::string hnsp_;
320  std::ostringstream * soapstr_;
321  std::ostringstream logger_;
322  bool status_;
323  bool serializeMode_;
324  bool verbose_;
325  bool dontPost_;
326  int oHeaders_;
327  std::map<std::string,const Operation*> opMap_;
328  const Operation* op_;
329  Soap::Encoding use_;//literal or encoded
330  std::string encodingStyle_; // this is usually the soap encoding style
331  Soap::Style style_;//rpc or doc
332  std::string nsp_; // namespace for the operation
333  std::string location_;
334  std::string username_,password_,host_;
335  int port_;
336  std::string action_;//SOAPAction header
337  std::vector<Parameter> elems_;//the simple types
338  size_t n_;//a counter to iterate through the element params
339  int iHeaders_; //number of soap header inputs
340  std::vector<std::pair<std::string,TypeContainer*> > outputs_;
341  std::vector<const XSDType* > avoidrecurse_;
342  WsdlPull::MessageType messageType_;
343 
344  std::vector<std::string> prefixes_;
345  bool bAuth;
346  std::string sAuthUser;
347  std::string sAuthPass;
348 
349  std::string sFaultCode;
350  std::string sFaultSubCode;
351  std::string sFaultString;
352  std::string sFaultActor;
353  void* ctx;//for connection purposes
354 
355  bool m_buildXmlTree;
356  bool m_xmlTreeProduced;
357  XmlDoc_t m_xmlDoc;
358 };
359 
360 inline
361 Parameter::Parameter(Schema::Type t,std::string n,int m,int x,const SchemaParser* s,
362  const std::vector<std::string>& parents)
363  :type_(t),
364  tag_(n),
365  min_(m),
366  max_(x),
367  n_(0),
368  sParser_(s),
369  parents_(parents)
370 
371 {
372 }
373 
374 inline
375 std::string
377 {
378  return logger_.str();
379 }
380 
381 inline
382 bool
383 WsdlInvoker::setWSDLUri(const std::string &url,const std::string & schemaPath)
384 {
385  parseWsdl(url,schemaPath);
386  return status_;
387 }
388 
389 inline
390 bool
392 {
393  return status_;
394 }
395 
396 inline
397 void
398 WsdlInvoker::setLocation(const std::string & url)
399 {
400  location_ = url;
401 }
402 
403 inline
404 void
406 {
407  verbose_ = f;
408 }
409 
410 inline
411 int
413 {
414  return iHeaders_;
415 }
416 
417 inline
418 std::string
420 {
421  return sFaultCode;
422 }
423 
424 inline
425 std::string
427 {
428  return sFaultSubCode;
429 }
430 
431 inline
432 std::string
434 {
435  return sFaultString;
436 }
437 
438 inline
439 std::string
441 {
442  return sFaultActor;
443 }
444 
445 
446 template<typename T>
447 inline T
448 WsdlInvoker::getValue(const std::string & xpath, size_t p_index)
449 {
450 
451  T val;
452  std::string v = getAsStringFromXPath(xpath, p_index);
453  std::istringstream iss(v);
454  iss>>val;
455  return val;
456 }
457 
458 template<>
459 inline std::string
460 WsdlInvoker::getValue<std::string>(const std::string & xpath, size_t p_index)
461 {
462 
463  return getAsStringFromXPath(xpath, p_index);
464 
465 }
466 
467 template <typename T>
468 inline std::vector<T>
469 WsdlInvoker::getValues(const std::string & xpath)
470 {
471  std::vector<std::string> arr ;
472  std::vector<T> arrT;
473  getAsStringFromXPath(xpath, 0, &arr);
474  T val;
475  for (size_t s = 0 ;s<arr.size();s++){
476 
477 
478  std::istringstream iss(arr[s]);
479  iss>>val;
480  arrT.push_back(val);
481  }
482  return arrT;
483 }
484 
485 //specialization for string
486 template<>
487 inline
488 std::vector<std::string>
489 WsdlInvoker::getValues<std::string>(const std::string & xpath)
490 {
491  std::vector<std::string> arr ;
492  getAsStringFromXPath(xpath, 0, &arr);
493  return arr;
494 }
495 
496 
497 }
498 #endif