WsdlInvoker.h

Go to the documentation of this file.
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 //An api to examine and invoke the web service in a protocol independent fashion
00021 
00022 #ifndef _WSDLINVOKERH
00023 #define _WSDLINVOKERH
00024 
00025 #include "xmlpull/XmlSerializer.h"
00026 #include "wsdlparser/WsdlParser.h"
00027 #include "wsdlparser/Soap.h"
00028 #include "xmlpull/wsdlpull_export.h"
00029 
00030 #include "xmlpull/XmlDoc.h"
00031 
00032 namespace WsdlPull{
00033 
00034  struct Parameter
00035  {
00036    Parameter(Schema::Type ,std::string,int m,int x,const SchemaParser* s,
00037              const std::vector<std::string>& parents);
00038    Schema::Type type_;
00039    std::string tag_;
00040    unsigned int min_;
00041    unsigned int max_;
00042    int n_;
00043    std::vector<std::string> data_;
00044    //bool str_;
00045    const SchemaParser* sParser_;
00046    std::vector<std::string> parents_;
00047 
00048  };
00049 
00050 class WSDLPULL_EXPORT WsdlInvoker
00051 {
00052  public:
00061   WsdlInvoker(const std::string &url, const std::string &schemaPath="");
00062   WsdlInvoker(std::istream &input, const std::string &schemaPath = "");
00063   WsdlInvoker();
00064   ~WsdlInvoker();
00066 
00074   bool setWSDLUri(const std::string &url, const std::string & schemaPath="");
00080   int getOperations(std::vector<std::string> & operations);
00081   std::string getOpDocumentation(const std::string & n);
00082   std::string getDocumentation();
00088   bool setOperation(const std::string & operation,
00089                     WsdlPull::MessageType mType = WsdlPull::Input);
00093   std::string getServiceEndPoint(const std::string & opname) ;
00095 
00096 
00099   
00110   bool setValue(const std::string & param,void* val);
00111   bool setValue(const std::string & param,void** values,unsigned int occur);
00112   bool setValue(const std::string & param,std::string val);
00113   bool setValue(const std::string & param,std::vector<std::string> values);//multiple occurrences
00114   bool setValue(const std::vector<std::string> & parents,void* val);
00120   bool invoke(long timeout = 0,bool processResponse = true);
00129   void* getValue(const std::string & param,Schema::Type & t); 
00130 
00131 
00152   template <typename T>
00153   T  getValue(const std::string & xpath, size_t p_index = 1);  
00154 
00155   /* identical to previous method but returns multiple matches if found in the document*/
00156   template <typename T>
00157   std::vector<T>  getValues(const std::string & xpath);  
00159   
00162   
00177   int getNextInput(std::string & param ,Schema::Type & type,int & minimum,int & maximum);
00178   int getNextInput(std::string & param ,Schema::Type & type,int & minimum,int & maximum,
00179                    std::vector<std::string>& parents);
00185   int getNextHeaderInput(std::string & param ,Schema::Type & type,int & minimum,int & maximum);
00186   int getNextHeaderInput(std::string & param ,Schema::Type & type,int & minimum,int & maximum,
00187                          std::vector<std::string>& parents);
00191   int nInputHeaders()const;
00192   
00200   bool setInputValue(const int param,void* val);
00201   bool setInputValue(const int id,void** values,unsigned int occur);
00202 
00210   bool setInputValue(const int param,std::string val);
00211   bool setInputValue(const int param,std::vector<std::string> values);//multiple occurrences
00212 
00218   bool getNextOutput(std::string  & name,TypeContainer * & tc);
00219   
00224   TypeContainer* getOutput(const std::string  & name);
00225   
00231    bool getNextHeaderOutput(std::string & name,TypeContainer*& tc);
00232 
00234    // change location of the service
00235   void setLocation(const std::string  & url);
00236   // set credentials for the service
00237   void setCredentials(const std::string & user, const std::string & pass);
00238   // set http authentication for the service
00239   void setAuth(const std::string & user, const std::string & pass);
00240   //ouput the soap message without invoking the service
00241   std::string getSoapMessage();
00242   std::string getXMLResponse();
00243   void setProxy(const std::string & host,int  port=80);
00244   //enable logging
00245   void setVerbose(bool f);
00246   // prints the output along with typenames like
00247   // name:value.
00248   //if false only value is printed
00249   void printTypeNames(bool f);
00250   //get the error message
00251   std::string errors();
00252   //if this returns false something went wrong
00253   bool status()const;
00254 
00255   //get SOAP fault related things
00256   std::string getFaultCode() const;
00257   std::string getFaultSubCode() const;
00258   std::string getFaultString() const;
00259   std::string getFaultActor() const;
00260 
00261   // Control XML Tree Generation and Xpath search fine tuning
00262   void setProcessEnvAndBody( bool p_processEnvAndBody);
00263   bool getProcessEnvAndBody( void) const;
00264 
00265   void setLazyRelativeMatch( bool p_lazyRelativeMatch);
00266   bool getLazyRelativeMatch( void) const;
00267 
00268   void setBuildXmlTree( bool p_buildXmlTree);
00269   bool getBuildXmlTree( void) const;
00270   void buildXmlTree( XmlPullParser &p_xmlPullParser, XmlNode_t &p_xmlNode,
00271                      bool p_notScannedEventAvail = false);
00272 
00273  private:
00274   //private stuff 
00275 
00276   bool init(WsdlParser* parser);
00277   //creaate xml request
00278   void serialize();
00279   void serializeType(Schema::Type typeId,
00280                      const std::string &tag,
00281                      const SchemaParser * sParser,
00282                      int minimum,
00283                      int maximum,
00284                      std::vector<std::string> parents,
00285                      const std::string nsp="",
00286                      bool isRoot = false);
00287 
00288   void serializeParam(int n,const std::string & tag,
00289                       const SchemaParser * sParser,
00290                       const std::string nsp="",
00291                       bool isRoot = false);
00292   void serializeContentModel(ContentModel *cm,
00293                              const SchemaParser *sParser,
00294                              std::vector<std::string> parents);
00299   void post(long timeout=0, std::string username="", std::string passwd="");
00300   void processResults();
00301   void processFault(XmlPullParser* xpp);
00302   void processHeader(XmlPullParser *xpp);
00303   void processBody(const Message* m,XmlPullParser* xpp);
00304   void parseWsdl(const std::string & url, const std::string & schemaPath = "");
00305   void serializeHeader();
00306   bool isSoapArray (const ComplexType * ct,const SchemaParser * sParser);
00307   //reset state information
00308   void reset();
00309   void getOperationDetails(const Operation* op);
00310   std::string getPrefix(const std::string& nsp);
00311   std::string getAsStringFromXPath(const std::string & xpath, size_t p_index, std::vector<std::string>* arr = 0);
00312   WsdlParser * wParser_;
00313   std::string strResults_;
00314   WsdlParser * ourParser_;
00315   XmlSerializer * xmlStream_;
00316   Soap* soap_;
00317   bool soapheaders_;
00318   int hPartId_;
00319   std::string hnsp_;
00320   std::ostringstream * soapstr_;
00321   std::ostringstream logger_;
00322   bool status_;
00323   bool serializeMode_;
00324   bool verbose_;
00325   bool dontPost_;
00326   int oHeaders_;
00327   std::map<std::string,const Operation*> opMap_;
00328   const Operation* op_;
00329   Soap::Encoding use_;//literal or encoded
00330   std::string encodingStyle_; // this is usually the soap encoding style
00331   Soap::Style style_;//rpc or doc
00332   std::string nsp_; // namespace for the operation
00333   std::string location_;
00334   std::string username_,password_,host_;
00335   int port_;
00336   std::string action_;//SOAPAction header
00337   std::vector<Parameter> elems_;//the simple types
00338   size_t n_;//a counter to iterate through the element params
00339   int iHeaders_; //number of soap header inputs
00340   std::vector<std::pair<std::string,TypeContainer*> > outputs_;
00341   std::vector<const XSDType* > avoidrecurse_;
00342   WsdlPull::MessageType messageType_;
00343 
00344   std::vector<std::string> prefixes_;
00345   bool bAuth;
00346   std::string sAuthUser;
00347   std::string sAuthPass;
00348 
00349   std::string sFaultCode;
00350   std::string sFaultSubCode;
00351   std::string sFaultString;
00352   std::string sFaultActor;
00353   void* ctx;//for connection purposes
00354 
00355   bool m_buildXmlTree;
00356   bool m_xmlTreeProduced;
00357   XmlDoc_t m_xmlDoc;
00358 };
00359 
00360 inline
00361 Parameter::Parameter(Schema::Type t,std::string n,int m,int x,const SchemaParser* s,
00362                      const std::vector<std::string>& parents)
00363   :type_(t),
00364      tag_(n),
00365      min_(m),
00366      max_(x),
00367      n_(0),
00368      sParser_(s),
00369      parents_(parents)
00370      
00371 {
00372 }
00373 
00374 inline
00375 std::string 
00376 WsdlInvoker::errors()
00377 {
00378   return logger_.str();
00379 }
00380 
00381 inline
00382 bool
00383 WsdlInvoker::setWSDLUri(const std::string &url,const std::string & schemaPath)
00384 {
00385   parseWsdl(url,schemaPath);
00386   return status_;
00387 }
00388 
00389 inline
00390 bool
00391 WsdlInvoker::status()const
00392 {
00393   return status_;
00394 }
00395 
00396 inline 
00397 void
00398 WsdlInvoker::setLocation(const std::string  & url)
00399 {
00400   location_ = url;
00401 }
00402 
00403 inline
00404 void
00405 WsdlInvoker::setVerbose(bool f)
00406 {
00407   verbose_ = f;
00408 }
00409 
00410 inline
00411 int
00412 WsdlInvoker::nInputHeaders()const
00413 {
00414   return iHeaders_;
00415 }
00416 
00417 inline
00418 std::string
00419 WsdlInvoker::getFaultCode() const
00420 {
00421   return sFaultCode;
00422 }
00423 
00424 inline
00425 std::string
00426 WsdlInvoker::getFaultSubCode() const
00427 {
00428   return sFaultSubCode;
00429 }
00430 
00431 inline
00432 std::string 
00433 WsdlInvoker::getFaultString() const
00434 {
00435   return sFaultString;
00436 }
00437 
00438 inline
00439 std::string 
00440 WsdlInvoker::getFaultActor() const
00441 {
00442   return sFaultActor;
00443 }
00444 
00445 
00446 template<typename T>
00447 inline T
00448 WsdlInvoker::getValue(const std::string & xpath, size_t p_index) 
00449 {
00450   
00451   T val;
00452   std::string v = getAsStringFromXPath(xpath, p_index);
00453   std::istringstream iss(v);
00454   iss>>val;
00455   return val;
00456 }
00457 
00458 template<>
00459 inline std::string
00460 WsdlInvoker::getValue<std::string>(const std::string & xpath, size_t p_index) 
00461 {
00462   
00463   return getAsStringFromXPath(xpath, p_index);
00464 
00465 }
00466 
00467 template <typename T>
00468 inline std::vector<T>
00469 WsdlInvoker::getValues(const std::string & xpath)
00470 {
00471   std::vector<std::string> arr ;
00472   std::vector<T> arrT;
00473   getAsStringFromXPath(xpath, 0, &arr);
00474   T val;
00475   for (size_t s = 0 ;s<arr.size();s++){
00476     
00477     
00478     std::istringstream iss(arr[s]);
00479     iss>>val;
00480     arrT.push_back(val);
00481   }
00482   return arrT;
00483 }
00484 
00485 //specialization for string
00486 template<>
00487 inline 
00488 std::vector<std::string>
00489 WsdlInvoker::getValues<std::string>(const std::string & xpath)
00490 {
00491   std::vector<std::string> arr ;
00492   getAsStringFromXPath(xpath, 0, &arr);
00493   return arr;
00494 }
00495 
00496 
00497 }
00498 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by  doxygen 1.6.2