dca_interface  6.3.4
wac_samples/wacenumsample/main.cpp
1 /* IBM Source Code */
2 /* (C) Copyright IBM Corp. 2009, 2012 */
3 /* Licensed Materials - Property of IBM */
4 /* US Government Users Restricted Rights - Use duplication or disclosure restricted by GSA Schedule Contract with IBM Corp. */
5 
38 #include <string>
39 #include <vector>
40 #include <iostream>
41 #include <fstream>
42 #include <ctime>
43 
44 #include "dca/dca_base.h"
46 #include "dca/dca_callbacks.h"
47 
48 const std::string S_ToolName = "wacenumsample";
49 const std::string S_ToolVersion = "1.2";
50 
55 const std::string S_UsageString =
56  "<redist-folder> <ticket> <product> [<locale>]\n"
57  " redist-folder - the folder where the DCA is installed to\n"
58  " ticket - a valid ticket\n"
59  " product - the product associated with your ticket\n"
60  " locale - optional locale for the WAC category info names and descriptions, default = en_US\n\n"
61  ;
62 
67 #ifdef WIN32
68 # define DCA_BINDIR "bin/Win32"
69 #else
70 # define DCA_BINDIR "bin/linux"
71 #endif
72 
77 #define DCA_INITDIR "init"
78 
82 #define DCA_LOGDIR "./logs"
83 
92 static void SetupInitData( const std::string& redist_folder, dca::InitData& initData )
93 {
94  initData.binDir = redist_folder + DCA_BINDIR;
95  initData.initDir = redist_folder + DCA_INITDIR;
96  initData.logDir = DCA_LOGDIR;
97 }
98 
106 static void SetupLicense( const std::string& ticket, const std::string& product,
107  dca::LicenseData& licenseData )
108 {
109  licenseData.ticket = ticket;
110  licenseData.product = product;
111 }
112 
118 static void PrintLicenseInfo( const dca::License& aLicense )
119 {
120  const time_t expirationDate = aLicense.getExpirationDate();
121  struct tm *expirationTime = localtime( &expirationDate );
122 
123  std::cout << "License Info:" << std::endl;
124  std::cout << " DCA is " << ( aLicense.isLicensed() ? "licensed." :
125  "not licensed." ) << std::endl;
126  std::cout << " MaxUsers:" << aLicense.getMaxUsers() <<
127  std::endl;
128  std::cout << " MaxSessions:" << aLicense.getMaxSessions() <<
129  std::endl;
130  std::cout << " Ticket:" << aLicense.getTicket() <<
131  std::endl;
132  std::cout << " Session:" << aLicense.getSession() <<
133  std::endl;
134  std::cout << " Last Message:" << aLicense.getLastMessage() <<
135  std::endl;
136  std::cout << " Expiration Date:" << asctime( expirationTime ) <<
137  std::endl;
138 }
139 
144 static void PrintToolHeader()
145 {
146  std::cout << "IBM DCA Sample: " << S_ToolName << " (" << S_ToolVersion << ")" << std::endl;
147 }
148 
154 static void PrintUsage( const char *name )
155 {
156  std::cout << name << " usage:" << std::endl;
157  std::cout << S_UsageString << std::endl;
158 }
159 
169 void EnumApplications( const dca::CategoriesInfo& aDcaCategoriesInfo, const dca_wac::WacApplications& aApplications,
170  const std::string& localeString )
171 {
172  if( aApplications.size() == 0 ) {
173  std::cout << "Given WacApplications are empty." << std::endl;
174  return;
175  }
176 
177  std::cout << "Enumerating Applications:" << std::endl;
178  for( DCA_INDEX_TYPE i = 0; i < aApplications.size(); ++i ) {
179  const dca_wac::WacApplication myApplication = aApplications[ i ];
180  const dca::Category myCategory = aDcaCategoriesInfo.getCategories().byId( myApplication.categoryId() );
181 
182  std::string myCategoryName = "Not available";
183  if( myCategory != dca::NullCategory )
184  myCategoryName = myCategory.name( localeString );
185 
186  std::cout << "Application #" << (i+1) << ")\t" <<
187  "name=" << myApplication.name( localeString ) <<
188  ", description=" << myApplication.description( localeString ) <<
189  ", categoryid=" << myApplication.categoryId()
190  << " [" << myCategoryName << "]" <<
191  ", id=" << myApplication.id() <<
192  std::endl;
193 
194  const DCA_SIZE_TYPE numOfActions = myApplication.getActions().size();
195  if( numOfActions == 0 ) {
196  std::cout << "\tNo Actions available." << std::endl;
197  }
198  else {
199  std::cout << "\tAvailable Actions:" << std::endl;
200  for( DCA_INDEX_TYPE u = 0; u < numOfActions; ++u ) {
201  const dca_wac::WacAction myAction = myApplication.getActions().at( u );
202  std::cout << "\tAction #" << (u+1) << ")\t" <<
203  "name=" << myAction.name( localeString ) <<
204  ", id=" << myAction.id() << std::endl;
205  }
206  }
207 
208  const DCA_SIZE_TYPE numOfTags = myApplication.getTags().size();
209  if( numOfTags == 0 ) {
210  std::cout << "\tNo Tags available." << std::endl;
211  }
212  else {
213  std::cout << "\tAvailable Tags:" << std::endl;
214  for( DCA_INDEX_TYPE j = 0; j < numOfTags; ++j ) {
215  const dca_wac::WacTag myTag = myApplication.getTags().at( j );
216  std::cout << "\tTag #" << (j+1) << ")\t" <<
217  "name=" << myTag.name( localeString ) <<
218  ", id=" << myTag.id() << std::endl;
219  }
220  }
221  std::cout << std::endl;
222  }
223  std::cout << std::endl;
224 }
225 
233 void EnumActions( const dca_wac::WacActions& aWacActions, const std::string& localeString )
234 {
235  if( aWacActions.size() == 0 ) {
236  std::cout << "Given WacActions are empty." << std::endl;
237  return;
238  }
239 
240  std::cout << "Enumerating Actions:" << std::endl;
241  for( DCA_INDEX_TYPE i = 0; i < aWacActions.size(); ++i ) {
242  const dca_wac::WacAction myAction = aWacActions[ i ];
243  std::cout << "Action #" << (i+1) << ")\t" <<
244  "name=" << myAction.name( localeString ) <<
245  ", description=" << myAction.description( localeString ) <<
246  ", id=" << myAction.id() <<
247  std::endl;
248  }
249  std::cout << std::endl;
250 }
251 
259 void EnumTags( const dca_wac::WacTags& aWacTags, const std::string& localeString )
260 {
261  if( aWacTags.size() == 0 ) {
262  std::cout << "Given WacTags are empty." << std::endl;
263  return;
264  }
265 
266  std::cout << "Enumerating Tags:" << std::endl;
267  for( DCA_INDEX_TYPE i = 0; i < aWacTags.size(); ++i ) {
268  const dca_wac::WacTag myTag = aWacTags[ i ];
269  std::cout << "Tag #" << (i+1) << ")\t" <<
270  "name=" << myTag.name( localeString ) <<
271  ", description=" << myTag.description( localeString ) <<
272  ", id=" << myTag.id() <<
273  std::endl;
274  }
275  std::cout << std::endl;
276 }
277 
285 void EnumWacCategoriesInfo( const dca::CategoriesInfo& aDcaCategoriesInfo,
286  const dca_wac::WacCategoriesInfo& aWacCategoriesInfo,
287  const std::string& localeString )
288 {
289  std::cout << "Entered WAC Enumeration routine." << std::endl;
290  std::cout << std::endl;
291 
292  const dca_wac::WacActions myActions = aWacCategoriesInfo.getActions();
293  EnumActions( myActions, localeString );
294 
295  const dca_wac::WacTags myTags = aWacCategoriesInfo.getTags();
296  EnumTags( myTags, localeString );
297 
298  const dca_wac::WacApplications myApplications = aWacCategoriesInfo.getApplications();
299  EnumApplications( aDcaCategoriesInfo, myApplications, localeString );
300 
301  std::cout << "Leaving WAC Enumeration routine." << std::endl;
302  std::cout << std::endl;
303 }
304 
310 void EnumLocales( const dca::Locales& aLocales )
311 {
312  if( aLocales.size() == 0 ) {
313  std::cout << "Given Locales are empty." << std::endl;
314  return;
315  }
316 
317  std::cout << "Enumerating Locales:" << std::endl;
318  for( DCA_INDEX_TYPE i = 0; i < aLocales.size(); ++i ) {
319  const dca::Locale myLocale = aLocales[ i ];
320  std::cout << "Locale #" << (i+1) << ")\t" <<
321  "languageId=" << myLocale.languageId() <<
322  ", displayName=" << myLocale.displayName() <<
323  std::endl;
324  }
325  std::cout << std::endl;
326 }
327 
334 void EnumCategories( const dca::Categories& aCategories, const std::string& localeString )
335 {
336  if( aCategories.size() == 0 ) {
337  std::cout << "Given Categories are empty." << std::endl;
338  return;
339  }
340 
341  std::cout << "Enumerating Categories:" << std::endl;
342  for( DCA_INDEX_TYPE i = 0; i < aCategories.size(); ++i ) {
343  const dca::Category myCategory = aCategories[ i ];
344  std::cout << "Category #" << (i+1) << ")\t" <<
345  "name=" << myCategory.name( localeString ) <<
346  ", id=" << myCategory.id() <<
347  std::endl;
348  }
349  std::cout << std::endl;
350 }
351 
358 void EnumDcaCategoriesInfo( const dca::CategoriesInfo& aCategoriesInfo, const std::string& localeString )
359 {
360  std::cout << "Entered DCA Enumeration routine." << std::endl;
361  std::cout << std::endl;
362 
363  dca::Locales myLocales = aCategoriesInfo.getLocales();
364  EnumLocales( myLocales );
365 
366  dca::Categories myCategories = aCategoriesInfo.getCategories();
367  EnumCategories( myCategories, localeString );
368 
369  std::cout << "Leaving DCA Enumeration routine." << std::endl;
370  std::cout << std::endl;
371 }
372 
380 int main( int argc, char *argv[] )
381 {
382  PrintToolHeader();
383 
384  int rc = 5;
385 
386  try {
387  if( argc < 4 ) {
388  PrintUsage( argv[0] );
389  return 5;
390  }
391 
392  std::string myRedistFolder = argv[ 1 ];
393  const std::string myTicket = argv[ 2 ];
394  const std::string myProduct = argv[ 3 ];
395  std::string myLocale = "en_US"; // Default locale
396 
397  if (argc > 4)
398  {
399  myLocale = argv[4];
400  }
401 
402  if( myRedistFolder.empty() || myTicket.empty() ||
403  myProduct.empty() ) {
404  PrintUsage( argv[0] );
405  return 5;
406  }
407 
408  // check for trailing fileslash - and add if necessary
409  const char c = myRedistFolder[ myRedistFolder.length() - 1 ];
410  if( c != '/' && c != '\\' )
411  myRedistFolder += "/";
412 
413  // init the 3rd party libraries
414  dca::InitCUrl();
416 
417  {
418  // setup DCA directories
419  dca::InitData myInitData;
420  SetupInitData( myRedistFolder, myInitData );
421 
422  // instantiate DCA API
423  dca::DcaInstance myDca;
424  myDca = dca::DcaInstance::create( myInitData );
425 
426  // setup license data
427  dca::LicenseData myLicenseData;
428  SetupLicense( myTicket, myProduct, myLicenseData );
429  const dca::License myLicense = myDca.createLicense( myLicenseData );
430 
431  PrintLicenseInfo( myLicense );
432 
433  if( myLicense.isLicensed( dca_wac::WacClassification::ID ) ) {
434  // create DCA categories info for printing out available locales and categories
435  const dca::CategoriesInfo myDcaCategoriesInfo = myDca.getCategoriesInfo( dca::DCA_CAT_INFO_TYPE_URL );
436 
437  // call enumeration routine for locales and categories
438  EnumDcaCategoriesInfo( myDcaCategoriesInfo, myLocale );
439 
440  // initialize the WAC Classification module and create a WacCategoriesInfo instance
441  const dca_wac::WacClassification myWacClassification = dca_wac::WacClassification::create( myDca, myLicense );
442 
443  // create a WAC categories info for printing out the available actions, tags and applications
444  const dca_wac::WacCategoriesInfo myCategoriesInfo = myWacClassification.getCategoriesInfo();
445 
446  // call WAC enumeration routine
447  EnumWacCategoriesInfo( myDcaCategoriesInfo, myCategoriesInfo, myLocale );
448 
449  rc = 0;
450  }
451  }
452  }
453  catch( const dca::ExDca& ex ) {
454  std::cerr << "DCA Exception occured. Details: " << ex.getDescription() <<
455  " (" << ex.getReturnCode() << ")." << std::endl;
456  rc = 10;
457  }
458  catch( const std::exception& s ) {
459  std::cerr << "std::exception occured. Details: " << s.what() << "." << std::endl;
460  rc = 10;
461  }
462  catch(...) {
463  std::cerr << "Unknown exception caught." << std::endl;
464  rc = 10;
465  }
466 
467  // deinit the 3rd party libraries
469  dca::DeinitCUrl();
470 
471  return rc;
472 }
473 
474 
475 
Is used to create a License object. A license first must be created with DcaInstance::createLicense t...
Definition: base_classes.h:547
std::string description(const std::string &localeString=std::string()) const
Returns the localized (brief) description of the application.
DCA_ACTION_ID_TYPE id() const
Returns the internal id of a WacAction class.
Exception class used in the DCA.
Definition: base_classes.h:237
const DCA_CATEGORIES_INFO_TYPE DCA_CAT_INFO_TYPE_URL
Refers to the internal categories info for URL classification.
std::string name(const std::string &localeString=std::string()) const
Returns the localized (display) name of the tag.
void InitCUrl()
Initializes libcurl. Do not use any DCA function before initializing libcurl.
std::string initDir
the directory in which the DCA init files are stored
Definition: base_classes.h:266
DCA_SIZE_TYPE size() const
Returns the number of categories in the container.
std::string name(const std::string &localeString=std::string()) const
Returns the localized (display) name of the application.
int getMaxSessions() const
Returns the maximum allowed sessions associated with your ticket/license.
time_t getExpirationDate() const
Returns the expiration date of the license in UTC.
std::string getDescription() const
Returns a description of the error.
Definition of a container class for Category objects.
CategoriesInfo getCategoriesInfo(DCA_CATEGORIES_INFO_TYPE categoryType) const
Returns the DCA internal categories, groups and locales.
Encapsulation of a locale class, which allows access to the language id and the display name of local...
Definition: base_locale.h:28
static void SetupInitData(const std::string &redist_folder, InitData &initData)
Sets up the given initData by substituting the given redist_folder with DCA subdirectories.
WacTag at(DCA_INDEX_TYPE index) const
Returns the WacTag with given index.
DCA_CATEGORY_ID_TYPE categoryId() const
Returns the id of the associated dca::Category. Each application is a member of only one category.
std::string binDir
the directory in which the DCA binary (*.dca) files are stored
Definition: base_classes.h:265
This header includes initialization/deinitialization support functions for the 3rd party libraries us...
A container class that allows access to the contained Categories, Groups and Locales.
void SetOpenSslCallbacks()
Initializes the required callbacks for OpenSSL when using HTTPS or SSL connections in a multi-threade...
Definition: wac_tags.h:30
The WacAction class represents a given action of an application e.g. UPLOAD FILE, ADD ATTACHMENT,...
Definition: wac_action.h:30
void UnsetOpenSslCallbacks()
Unsets the openssl callbacks. Do not call any DCA function after you have called this function.
DCA_CATEGORY_ID_TYPE id() const
The category id.
WacActions getActions() const
Returns all actions available for the application. If an application does not support actions,...
int getMaxUsers() const
Returns the maximum allowed users associated with your ticket/license.
void DeinitCUrl()
Deinitializes libcurl. Do not call any DCA function after you have called this function.
std::string description(const std::string &localeString=std::string()) const
Returns the localized (brief) description of the action.
DCA_SIZE_TYPE size() const
Returns the number of categories in the container.
WacCategoriesInfo getCategoriesInfo() const
Retrieve the WacCategoriesInfo class.
#define DCA_LOGDIR
Relative directory for logfile(s).
std::string description(const std::string &localeString=std::string()) const
Returns the localized (brief) description of the tag.
Package header file for the WAC (Web Application Classification) Package.
#define DCA_INITDIR
DCA subdirectory of the DCA initialization data.
Definition of a container class for WacAction objects.
Definition: wac_actions.h:32
DCA_RESULT_TYPE getReturnCode() const
Gets the code of the error.
WacTags getTags() const
Returns the contained WacTags.
std::string name(const std::string &localeString=std::string()) const
Returns the localized (display) name of the action.
The WacApplication class represents a given application e.g. HOTMAIL, GMAIL etc.
Locales getLocales() const
Returns the contained Locales.
static void PrintToolHeader()
Prints out the name and the version of this sample.
Defintion of the WacClassification class.
Use a License to initialize a classification package or a toolbox package.
Definition: base_classes.h:560
Category byId(DCA_CATEGORY_ID_TYPE id) const
Returns the category with the given category id.
WacActions getActions() const
Returns the contained WacActions.
Container class for all WAC enumeration objects.
bool isLicensed(DCA_MODULE_ID_TYPE id=0, bool force=false) const
Checks whether the given License is valid for the given module id.
static void PrintLicenseInfo(const License &aLicense)
Prints out the information about the provided License.
std::string ticket
The ticket as provided in the license.
Definition: base_classes.h:548
This header includes all header files of the DCA Base Package.
DCA_APPLICATION_ID_TYPE id() const
Returns the internal id of a WacApplication class.
Encapsulates the init and deinit of the DCA API.
Definition: base_classes.h:315
Category NullCategory
Defines a constant unassigned Category you can use for checks. if( myCat == NullCategory ) --> myCat ...
std::string product
The product code used with the license.
Definition: base_classes.h:549
Definition of a container class for WacApplication objects.
WacAction at(DCA_INDEX_TYPE index) const
Returns the WacAction with the given index.
DCA_TAG_ID_TYPE id() const
Returns the internal id of a WacTag class.
static WacClassification create(const dca::DcaInstance &aDca, const dca::License &aLicense)
Initializes the WacClassification module.
static DCA_MODULE_ID_TYPE ID
The unique ID of the WAC classification module.
std::string logDir
the directory in which the DCA log file should be created
Definition: base_classes.h:267
WacApplications getApplications() const
Returns the contained WacApplications.
static void PrintUsage(const char *name)
Prints out the syntax of the sample.
std::string getLastMessage() const
Returns the last message received from our license server or if none available the last available mes...
size_t DCA_INDEX_TYPE
Type for index access (used for arrays and collections).
Definition: base_types.h:66
Categories getCategories() const
Returns the contained Categories.
WacTags getTags() const
Returns all tags that are defined for a given application e.g. "RISKLEVEL_1", "HIGH BANDWIDTH" etc.
size_t DCA_SIZE_TYPE
Type for size (used for size of array and collections).
Definition: base_types.h:72
std::string name(const std::string &localeString=std::string()) const
Returns the localized (display) name of the category.
std::string displayName() const
Returns the display name of the locale.
std::string getTicket() const
Returns the ticket of the license as string.
#define DCA_BINDIR
DCA subdirectory of the DCA binaries.
std::string getSession() const
Returns the session of the license as string.
DCA_SIZE_TYPE size() const
Returns the number of tags in the container.
Definition of a container class for Locale objects.
Definition: base_locales.h:34
std::string languageId() const
Returns the language id as defined in the related categories.xml.
DCA_SIZE_TYPE size() const
Returns the number of applications in the container.
DCA_SIZE_TYPE size() const
Returns the number of actions in the container.
const std::string S_UsageString
Usage string, displayed if a parameter is missing.
The WacTag class represents a given tag that is associated with an application e.g....
Definition: wac_tag.h:29
This structure is used to initialize the DcaInstance.
Definition: base_classes.h:264
Encapsulates a category as defined in the categories XML schema (see Categories XML: Categories).
Definition: base_category.h:26
static DcaInstance create(const InitData &initData)
Creates a DcaInstance, starts up the DCA API and initializes the required main module.
static void SetupLicense(const std::string &ticket, const std::string &product, LicenseData &licenseData)
Sets up the given licenseData by copying the given ticket and product strings.
License createLicense(const LicenseData &licData, const ProxySettings &proxySettings=ProxySettings(), LogLevel aLogLevel=LOG_Initial) const
Creates a License object using the given LicenseData.
int main(int argc, char *argv[])
The main routine.