NCBI C++ ToolKit
soap_client_sample.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: soap_client_sample.cpp 101393 2023-12-08 15:08:26Z gouriano $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Author: Andrei Gourianov
27  *
28  * File Description:
29  * Sample SOAP HTTP client
30  *
31  */
32 
33 #include <ncbi_pch.hpp>
34 #include <corelib/ncbistd.hpp>
35 #include <corelib/ncbiapp.hpp>
36 #include <corelib/ncbienv.hpp>
37 #include <corelib/ncbiargs.hpp>
38 #include <util/util_exception.hpp>
39 
42 
45 
46 
47 // to see communication log, put the following into environment:
48 // CONN_DEBUG_PRINTOUT=data
49 // DIAG_POST_LEVEL=0
50 
51 
52 /////////////////////////////////////////////////////////////////////////////
53 //
54 // CSampleSoapClient
55 //
56 
58 {
59 public:
60  CSampleSoapClient(void);
61  ~CSampleSoapClient(void);
62 
64  CConstRef<CVersionResponse> GetVersion(const string& client_id, CConstRef<CSoapFault>* fault=0);
66 };
67 
68 
69 /////////////////////////////////////////////////////////////////////////////
70 
73  // This Server URL is valid only within NCBI
74 #if 0
75  // works either way
76  "http://intrawebdev8:6224/staff/gouriano/samplesoap/samplesoap.cgi",
77 #else
78  "https://intrawebdev8/staff/gouriano/samplesoap/samplesoap.cgi",
79 #endif
80  "http://ncbi.nlm.nih.gov/")
81 {
82  // Register incoming object types
83  // so the SOAP message parser can recognize these objects
84  // in incoming data and parse them correctly
85  RegisterObjectType(CDescriptionText::GetTypeInfo);
86  RegisterObjectType(CVersionResponse::GetTypeInfo);
87  RegisterObjectType(CMathResponse::GetTypeInfo);
88 }
89 
90 
92 {
93 }
94 
95 
98 // Request with no parameters
99 // Response is a single string
100 {
101  CSoapMessage request( GetDefaultNamespaceName() ), response;
102 
103  // Both variants are okay
104 #if 0
106  any->SetName("Description");
107 // any->SetNamespaceName(GetDefaultNamespaceName());
108 #else
110  any->Set();
111 #endif
112  request.AddObject( *any, CSoapMessage::eMsgBody);
113 
114  Invoke(response,request,fault);
115  return SOAP_GetKnownObject<CDescriptionText>(response);
116 }
117 
118 
119 // Request is a single string: ClientID
120 // Response is a sequence of 3 strings: Major, Minor, ClientID
121 // here ClientID is same as the one sent in request
123 CSampleSoapClient::GetVersion(const string& client_id, CConstRef<CSoapFault>* fault)
124 {
125  CSoapMessage request( GetDefaultNamespaceName() ), response;
126 
128  req->SetClientID(client_id);
129 // req->SetNamespaceName(GetDefaultNamespaceName());
130  request.AddObject( *req, CSoapMessage::eMsgBody);
131 
132  Invoke(response,request,fault);
133  return SOAP_GetKnownObject<CVersionResponse>(response);
134 }
135 
136 
137 // Request is a list of Operand where each Operand is a sequence of 2 strings + attribute
138 // Response is a list of result strings
141 {
142  CSoapMessage request, response;
143  // The following call is not needed, because the ops object already has
144  // a (correct) namespace name defined
145 // ops.SetNamespaceName(GetDefaultNamespaceName());
146  request.AddObject( ops, CSoapMessage::eMsgBody);
147 
148  Invoke(response,request,fault);
149  return SOAP_GetKnownObject<CMathResponse>(response);
150 }
151 
152 
153 /////////////////////////////////////////////////////////////////////////////
154 //
155 // CSampleSoapClientApplication
156 //
157 
159 {
160 public:
161  virtual void Init(void);
162  virtual int Run (void);
163 };
164 
165 
166 /////////////////////////////////////////////////////////////////////////////
167 
169 {
171  // Create
172  unique_ptr<CArgDescriptions> arg_desc(new CArgDescriptions);
173  // Program description
174  string prog_description = "Test NCBI C++ Toolkit Sample Soap Server\n";
175  arg_desc->SetUsageContext(GetArguments().GetProgramBasename(),
176  prog_description, false);
177  // Pass argument descriptions to the application
178  SetupArgDescriptions(arg_desc.release());
179 }
180 
181 
183 {
185  try {
186 
187  CConstRef<CSoapFault> fault;
189  if (v1) {
190  cout << v1->GetText() << endl;
191  } else if (fault) {
192  ERR_POST(Error << fault->GetFaultcode() << ": " << fault->GetFaultstring());
193  }
194 
195  CConstRef<CVersionResponse> v2 = ws.GetVersion("C++ SOAP client",&fault);
196  if (v2) {
197  cout << v2->GetVersionStruct().GetMajor() << "." <<
198  v2->GetVersionStruct().GetMinor() << ": " <<
199  v2->GetVersionStruct().GetClientID() << endl;
200  } else if (fault) {
201  ERR_POST(Error << fault->GetFaultcode() << ": " << fault->GetFaultstring());
202  }
203 
204  CMath ops;
205  CRef<COperand> op1(new COperand);
206  op1->SetX(1);
207  op1->SetY(2);
208  op1->SetAttlist().SetOperation( COperand::C_Attlist::eAttlist_operation_add);
209  ops.SetOperand().push_back(op1);
210  CRef<COperand> op2(new COperand);
211  op2->SetX(22);
212  op2->SetY(11);
213  op2->SetAttlist().SetOperation( COperand::C_Attlist::eAttlist_operation_subtract);
214  ops.SetOperand().push_back(op2);
215 
216  CConstRef<CMathResponse> v3 = ws.DoMath(ops,&fault);
217  if (v3) {
218  for (auto i : v3->GetMathResult()) {
219  cout << i << endl;
220  }
221  } else if (fault) {
222  ERR_POST(Error << fault->GetFaultcode() << ": " << fault->GetFaultstring());
223  }
224  } catch (CEofException&) {
225  ERR_POST(Error << "service unavailable");
226  throw;
227  } catch (CException&) {
228  ERR_POST(Error << "request failed");
229  throw;
230  }
231 // cout << "done" << endl;
232  return 0;
233 }
234 
235 
236 /////////////////////////////////////////////////////////////////////////////
237 // MAIN
238 
239 int NcbiSys_main(int argc, ncbi::TXChar* argv[])
240 {
241  return CSampleSoapClientApplication().AppMain(argc, argv);
242 }
Serializable object that stores any combination of parsable data.
Definition: serialbase.hpp:264
CArgDescriptions –.
Definition: ncbiargs.hpp:541
CConstRef –.
Definition: ncbiobj.hpp:1266
CDescription –.
Definition: Description.hpp:66
CMath –.
Definition: Math.hpp:66
COperand –.
Definition: Operand.hpp:66
CRef –.
Definition: ncbiobj.hpp:618
virtual int Run(void)
Run the application.
virtual void Init(void)
Initialize the application.
CConstRef< CDescriptionText > GetDescription(CConstRef< CSoapFault > *fault=0)
CConstRef< CMathResponse > DoMath(CMath &ops, CConstRef< CSoapFault > *fault=0)
CConstRef< CVersionResponse > GetVersion(const string &client_id, CConstRef< CSoapFault > *fault=0)
CSampleVersion –.
Definition: Version.hpp:66
const string & GetDefaultNamespaceName(void) const
Get default namespace name.
Definition: soap_client.cpp:67
void RegisterObjectType(TTypeInfoGetter type_getter)
Register incoming object type.
Definition: soap_client.cpp:81
void Invoke(CSoapMessage &response, const CSoapMessage &request, CConstRef< CSoapFault > *fault=0, const string &soap_action=kEmptyStr) const
Invoke SOAP server procedure synchronously: send 'request' and receive 'response'.
Definition: soap_client.cpp:98
void AddObject(const CSerialObject &obj, EMessagePart destination)
Include a standard set of the NCBI C++ Toolkit most basic headers.
virtual void Init(void)
Initialize the application.
Definition: ncbiapp.cpp:266
int AppMain(int argc, const char *const *argv, const char *const *envp=0, EAppDiagStream diag=eDS_Default, const char *conf=NcbiEmptyCStr, const string &name=NcbiEmptyString)
Main function (entry point) for the NCBI application.
Definition: ncbiapp.cpp:799
virtual void SetupArgDescriptions(CArgDescriptions *arg_desc)
Setup the command line argument descriptions.
Definition: ncbiapp.cpp:1175
const CNcbiArguments & GetArguments(void) const
Get the application's cached unprocessed command-line arguments.
#define ERR_POST(message)
Error posting with file, line number information but without error codes.
Definition: ncbidiag.hpp:186
void Error(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1197
const CVect2< U > & v2
Definition: globals.hpp:440
char TXChar
Definition: ncbistr.hpp:172
TOperand & SetOperand(void)
Assign a value to Operand data member.
Definition: Math_.hpp:175
int i
if(yy_accept[yy_current_state])
Defines the CNcbiApplication and CAppException classes for creating NCBI applications.
Defines command line argument related classes.
Defines unified interface to application:
USING_SCOPE(objects)
int NcbiSys_main(int argc, ncbi::TXChar *argv[])
USING_NCBI_SCOPE
Modified on Thu Feb 29 12:16:52 2024 by modify_doxy.py rev. 669887