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

Generated on Sun Aug 5 20:01:34 2007 for wsdlpull by  doxygen 1.4.6