src/wsdlparser/WsdlInvoker.h

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 
00031 namespace WsdlPull{
00032 
00033  struct Parameter
00034  {
00035    Parameter(Schema::Type ,std::string,int m,int x,const SchemaParser* s,
00036              const std::vector<std::string>& parents);
00037    Schema::Type type_;
00038    std::string tag_;
00039    unsigned int min_;
00040    unsigned int max_;
00041    int n_;
00042    std::vector<std::string> data_;
00043    //bool str_;
00044    const SchemaParser* sParser_;
00045    std::vector<std::string> parents_;
00046 
00047  };
00048 
00049 class WSDLPULL_EXPORT WsdlInvoker
00050 {
00051  public:
00052   /** @name Constructors and Destructors */
00053   //@{
00054   /**
00055    * The default constructor for WsdlInvoker
00056    * @param wsdl url
00057    * @param stream for logging errors
00058    */
00059   WsdlInvoker(const std::string &url);
00060   WsdlInvoker(const std::string &url, const std::string &schemaPath);
00061   WsdlInvoker();
00062   ~WsdlInvoker();
00063   //@}
00064 
00065   /** @name WSDL Inspection*/
00066   //@{
00067   /** setWSDLUri
00068    * set the url of the wsdl file to be processed
00069    * @param url for the wsdl file
00070    * @param optional path to schema files required to parse wsdl extensions (soap,http) 
00071    */
00072   bool setWSDLUri(const std::string &url, const std::string & schemaPath="");
00073   /** getOperations
00074    *  @param reference to vector<string>
00075    *  @brief return names of operations (only for the SOAP binding portType)
00076    *  @return int ,number of operations
00077    */
00078   int getOperations(std::vector<std::string> & operations);
00079   std::string getOpDocumentation(const std::string & n);
00080   std::string getDocumentation();
00081   /** setOperation
00082    *  @param operation name to invoke
00083    *  @brief set the operation to invoke
00084    *  @return bool ,true if successful
00085    */
00086   bool setOperation(const std::string & operation,
00087                     WsdlPull::MessageType mType = WsdlPull::Input);
00088   /** getServiceEndPoint
00089    *  returns the url to be invoked for the operation
00090    */
00091   std::string getServiceEndPoint(const std::string & opname) ;
00092   //@}
00093 
00094 
00095   /** @name Simple Invocation usage*/
00096   //@{ 
00097   
00098   /** setValue
00099    *  @brief sets the param value for an operation by name of the parameter
00100    *  @param name of the operation's parameter
00101    *  @param string/void*  representation of the parameter
00102    *  @return true if successful,false if the values didnt match or occurrence constraint failed
00103    *  example invoker.setInputValue("symbol","ABC") or 
00104    *  int zip = 94018; invoker.setInputValue("zip",(void*)(&zip));
00105    *  Note that "symbol" or "zip" above can be a message part or a constituent particle in a complex type
00106    *  The API takes care of it.
00107    */
00108   bool setValue(const std::string & param,void* val);
00109   bool setValue(const std::string & param,void** values,unsigned int occur);
00110   bool setValue(const std::string & param,std::string val);
00111   bool setValue(const std::string & param,std::vector<std::string> values);//multiple occurrences
00112   bool setValue(const std::vector<std::string> & parents,void* val);
00113   /** invoke
00114    *  invoke the web service operation
00115    @param timeout set the timeout for the request in seconds
00116    *  @return true if successful,false otherwise
00117    */
00118   bool invoke(long timeout = 0);
00119   /** getValue
00120    *  return the value of the output whose name is 'param'
00121    *  @param type is set by reference to enable type casting in client code
00122    *  @return pointer to the value .0 if there is no simple type or part name 
00123    *  whose name is 'param' in  the web service output.
00124    *  example  float * val = (int*) invoker.getOutput("Result",t);//stock quotes
00125    *  t would be Schema::FLOAT
00126    */
00127   void* getValue(const std::string & param,Schema::Type & t); 
00128   
00129   //@} 
00130   
00131   /** @name A more complex but  powerful usage*/
00132   //@{ 
00133   
00134   /** getNextInput
00135    *  Calling this method repeatedly informs the caller
00136    *   of the input types the web service operation expects.
00137    *   Each call returns a unique id which must be used while setting the
00138    *   value using setInputValue.This method exposes only atomic types.Even if 
00139    *   a web service needs a complex type,the api exposes only the constituent
00140    *   particles.The only exception is when a complex content model needs multiple
00141    *   occurrences which is still a TODO
00142    *  @param ref:param name  Name of the param
00143    *  @param ref:Schema::Type ,the schema type 
00144    *  @param ref: minimum and maximum (occurrences)
00145    *  @param ref:parents, parent list of type hierarchy for this parameter.
00146    *  @return unique id of this parameter
00147    */
00148   int getNextInput(std::string & param ,Schema::Type & type,int & minimum,int & maximum);
00149   int getNextInput(std::string & param ,Schema::Type & type,int & minimum,int & maximum,
00150                    std::vector<std::string>& parents);
00151   /**
00152    * getNextHeaderInput
00153    * Similar to the previous method except that it gets the SOAP headers if any
00154    * Set methods are same as for regular inputs
00155    */
00156   int getNextHeaderInput(std::string & param ,Schema::Type & type,int & minimum,int & maximum);
00157   int getNextHeaderInput(std::string & param ,Schema::Type & type,int & minimum,int & maximum,
00158                          std::vector<std::string>& parents);
00159   /**
00160    * returns the number of input headers you may need to set
00161    */
00162   int nInputHeaders()const;
00163   
00164   /** setInputValue
00165    *  sets the param value for an operation
00166    *         only simple types can be set.
00167    *  @param id return by getNextInput()
00168    *  @param void ** pointer to array of pointers to the the values
00169    *  @return true if successful,false if the values didnt match or occurrence constraint failed
00170    */
00171   bool setInputValue(const int param,void* val);
00172   bool setInputValue(const int id,void** values,unsigned int occur);
00173 
00174   /** setInputValue
00175    *  sets the param value for an operation
00176    *         only simple types can be set.
00177    *  @param id return by getNextInput()
00178    *  @param string representation of the parameter
00179    *  @return true if successful,false if the values didnt match or occurrence constraint failed
00180    */
00181   bool setInputValue(const int param,std::string val);
00182   bool setInputValue(const int param,std::vector<std::string> values);//multiple occurrences
00183 
00184   /** getNextOutput
00185    *  get the part/elem name and type container
00186    * @return false when we finished iterating through all outputs
00187    *         after which it rewinds and you can start again
00188    */
00189   bool getNextOutput(std::string  & name,TypeContainer * & tc);
00190   
00191   /** getOutput
00192    *   return the type container which stores the output
00193    *   for the output part/elem name
00194    */
00195   TypeContainer* getOutput(const std::string  & name);
00196   
00197   /** getNextHeaderOutput
00198    *  get the type container which stores the SOAP header
00199    * @return false when we finished iterating through all outputs
00200    *         after which it rewinds and you can start again
00201    */
00202    bool getNextHeaderOutput(std::string & name,TypeContainer*& tc);
00203 
00204   //@} 
00205    // change location of the service
00206   void setLocation(const std::string  & url);
00207   // set credentials for the service
00208   void setCredentials(const std::string & user, const std::string & pass);
00209   // set http authentication for the service
00210   void setAuth(const std::string & user, const std::string & pass);
00211   //ouput the soap message without invoking the service
00212   std::string getSoapMessage();
00213   std::string getXMLResponse();
00214   void setProxy(const std::string & host,int  port=80);
00215   //enable logging
00216   void setVerbose(bool f);
00217   // prints the output along with typenames like
00218   // name:value.
00219   //if false only value is printed
00220   void printTypeNames(bool f);
00221   //get the error message
00222   std::string errors();
00223   //if this returns false something went wrong
00224   bool status()const;
00225 
00226   //get SOAP fault related things
00227   std::string getFaultCode() const;
00228   std::string getFaultString() const;
00229   std::string getFaultActor() const;
00230 
00231 
00232  private:
00233   //private stuff 
00234 
00235   bool init(WsdlParser* parser);
00236   //creaate xml request
00237   void serialize();
00238   void serializeType(Schema::Type typeId,
00239                      const std::string &tag,
00240                      const SchemaParser * sParser,
00241                      int minimum,
00242                      int maximum,
00243                      std::vector<std::string> parents,
00244                      const std::string nsp="",
00245                      bool isRoot = false);
00246 
00247   void serializeParam(int n,const std::string & tag,
00248                       const SchemaParser * sParser,
00249                       const std::string nsp="",
00250                       bool isRoot = false);
00251   void serializeContentModel(ContentModel *cm,
00252                              const SchemaParser *sParser,
00253                              std::vector<std::string> parents);
00254   /** create xml request 
00255     and do the http post request via curl
00256     @param timeout in seconds default 0 -> no timeout
00257     **/
00258   void post(long timeout=0, std::string username="", std::string passwd="");
00259   void processResults();
00260   void processFault(XmlPullParser* xpp);
00261   void processHeader(XmlPullParser *xpp);
00262   void processBody(const Message* m,XmlPullParser* xpp);
00263   void parseWsdl(const std::string & url, const std::string & schemaPath = "");
00264   void serializeHeader();
00265   bool isSoapArray (const ComplexType * ct,const SchemaParser * sParser);
00266   //reset state information
00267   void reset();
00268   void getOperationDetails(const Operation* op);
00269   std::string getPrefix(const std::string& nsp);
00270 
00271   WsdlParser * wParser_;
00272   WsdlParser * ourParser_;
00273   XmlSerializer * xmlStream_;
00274   Soap* soap_;
00275   bool soapheaders_;
00276   int hPartId_;
00277   std::string hnsp_;
00278   std::ostringstream * soapstr_;
00279   std::ostringstream logger_;
00280   bool status_;
00281   bool serializeMode_;
00282   bool verbose_;
00283   bool dontPost_;
00284   int oHeaders_;
00285   std::map<std::string,const Operation*> opMap_;
00286   const Operation* op_;
00287   Soap::Encoding use_;//literal or encoded
00288   std::string encodingStyle_; // this is usually the soap encoding style
00289   Soap::Style style_;//rpc or doc
00290   std::string nsp_; // namespace for the operation
00291   std::string location_;
00292   std::string username_,password_,host_;
00293   int port_;
00294   std::string action_;//SOAPAction header
00295   std::vector<Parameter> elems_;//the simple types
00296   size_t n_;//a counter to iterate through the element params
00297   int iHeaders_; //number of soap header inputs
00298   std::vector<std::pair<std::string,TypeContainer*> > outputs_;
00299   WsdlPull::MessageType messageType_;
00300 
00301   std::vector<std::string> prefixes_;
00302   bool bAuth;
00303   std::string sAuthUser;
00304   std::string sAuthPass;
00305 
00306   std::string sFaultCode;
00307   std::string sFaultString;
00308   std::string sFaultActor;
00309 };
00310 
00311 inline
00312 Parameter::Parameter(Schema::Type t,std::string n,int m,int x,const SchemaParser* s,
00313                      const std::vector<std::string>& parents)
00314   :type_(t),
00315      tag_(n),
00316      min_(m),
00317      max_(x),
00318      n_(0),
00319      sParser_(s),
00320      parents_(parents)
00321      
00322 {
00323 }
00324 
00325 inline
00326 std::string 
00327 WsdlInvoker::errors()
00328 {
00329   return logger_.str();
00330 }
00331 
00332 inline
00333 bool
00334 WsdlInvoker::setWSDLUri(const std::string &url,const std::string & schemaPath)
00335 {
00336   parseWsdl(url,schemaPath);
00337   return status_;
00338 }
00339 
00340 inline
00341 bool
00342 WsdlInvoker::status()const
00343 {
00344   return status_;
00345 }
00346 
00347 inline 
00348 void
00349 WsdlInvoker::setLocation(const std::string  & url)
00350 {
00351   location_ = url;
00352 }
00353 
00354 inline
00355 void
00356 WsdlInvoker::setVerbose(bool f)
00357 {
00358   verbose_ = f;
00359 }
00360 
00361 inline
00362 int
00363 WsdlInvoker::nInputHeaders()const
00364 {
00365   return iHeaders_;
00366 }
00367 
00368 inline
00369 std::string
00370 WsdlInvoker::getFaultCode() const
00371 {
00372   return sFaultCode;
00373 }
00374 
00375 inline
00376 std::string 
00377 WsdlInvoker::getFaultString() const
00378 {
00379   return sFaultString;
00380 }
00381 
00382 inline
00383 std::string 
00384 WsdlInvoker::getFaultActor() const
00385 {
00386   return sFaultActor;
00387 }
00388 
00389 
00390 }
00391 #endif

Generated on Sun Aug 10 00:07:39 2008 for wsdlpull by  doxygen 1.4.6