50 const std::string S_ToolName =
"createdbsample";
51 const std::string S_ToolVersion =
"1.0";
53 std::string custom_db_folder;
56 const std::string S_UrlString(
"www.an_example_custom_url.com" );
63 "<redist-folder> <ticket> <product> <custom-db-folder>\n"
64 " redist-folder - the folder where the DCA is installed to\n"
65 " ticket - a valid ticket\n"
66 " product - the product associated with your ticket\n"
67 " custom-db-folder - the folder where the Custom Database should be created in\n\n"
75 # define DCA_BINDIR "bin/Win32"
77 # define DCA_BINDIR "bin/linux"
84 #define DCA_INITDIR "init"
89 #define DCA_LOGDIR "./logs"
116 static void SetupLicense(
const std::string& ticket,
const std::string& product,
119 licenseData.
ticket = ticket;
131 static void SetupConnectionData(
const std::string& custom_db_folder,
145 std::cout <<
"IBM DCA Sample: " << S_ToolName <<
" (" << S_ToolVersion <<
156 std::cout <<
" usage:" << std::endl;
169 struct tm *expirationTime = localtime( &expirationDate );
171 std::cout <<
"License Info:" << std::endl;
172 std::cout <<
" DCA is " << ( aLicense.
isLicensed() ?
"licensed." :
173 "not licensed." ) << std::endl;
174 std::cout <<
" MaxUsers:" << aLicense.
getMaxUsers() <<
178 std::cout <<
" Ticket:" << aLicense.
getTicket() <<
180 std::cout <<
" Session:" << aLicense.
getSession() <<
184 std::cout <<
" Expiration Date:" << asctime( expirationTime ) <<
195 void AddUrlAndCategories(
UrlCustomDb& aUrlCustomDb,
const std::string& aUrlString,
198 std::ostringstream log;
199 log <<
" Added URL '" << aUrlString <<
"'";
203 for( CategorySet::const_iterator I = aCategories.begin(),
204 IEnd = aCategories.end(); I != IEnd; ++I )
207 log <<
" with categories [";
211 if( ( added_cnt == 0 ) || ( added_cnt < (
int)aCategories.size() - 1 ) )
219 log <<
" to the custom database";
222 aUrlCustomDb.
setUrl( aUrlString, aCategories );
223 std::cout << log.str() << std::endl;
232 void RemoveUrl(
UrlCustomDb& aUrlCustomDb,
const std::string& aUrlString )
235 std::cout <<
" Removed URL '" << aUrlString <<
236 "' from the custom database" << std::endl;
251 aUrlClassificationResults )
257 std::cerr <<
"Error occured while calling UrlDbClassifier::classify() RC:" <<
278 const std::string& dbfolder )
285 std::cerr <<
"Error: Database already exists" <<
289 std::cerr <<
"Creation of custom database failed: RC:" <<
318 const std::string& dbfolder,
UrlCustomDb& aUrlCustomDb )
323 SetupConnectionData( dbfolder, myDbConnectionData );
330 aUrlCustomDb = aUrlCustomDbModule.
openCustomDb( aDbConnection );
334 catch(
const ExDca& ex ) {
335 std::cerr <<
"EcDca Exception caught. Details: " << ex.
getDescription()
339 catch(
const std::exception& stdex ) {
340 std::cerr <<
"std::exception caught. Details: " << stdex.what() <<
359 bool CreateUrlClassificationInstances(
const DcaInstance& theDca,
const License& aLicense,
372 catch(
const ExDca& ex ) {
373 std::cerr <<
"EcDca Exception caught. Details: " << ex.
getDescription()
377 catch(
const std::exception& stdex ) {
378 std::cerr <<
"std::exception caught. Details: " << stdex.what() <<
383 std::cerr <<
"Unknown exception caught" << std::endl;
406 std::cout <<
"Started ProcessCustomDatabase" << std::endl;
408 std::cout <<
"* Initializing custom database module" << std::endl;
414 std::cout <<
"* Creating new custom database file in folder '" <<
415 custom_db_folder <<
"'" << std::endl;
418 if( !CreateCustomDbFile( myUrlCustomDbModule, custom_db_folder ) ) {
424 std::cout <<
"* Creating DbConnection and UrlCustomDb instance"
430 if( !CreateCustomDbInstances( theDca, aLicense, myUrlCustomDbModule,
431 myDbConnection, custom_db_folder, myUrlCustomDb ) ) {
435 std::cout <<
"* Starting Test by adding an URL and Categories to the custom database"
440 myCategories.insert( 0 );
441 myCategories.insert( 1 );
443 AddUrlAndCategories( myUrlCustomDb, S_UrlString, myCategories );
445 std::cout <<
"* Try to retrieve added URL and Categories from the custom database"
450 if( !myUrlCustomDb.
getUrl( S_UrlString, myResultCategories ) ) {
451 std::cerr <<
"Could not retrieve URL '" << S_UrlString <<
452 "' from UrlCustomDb instance" << std::endl;
456 if( myResultCategories.size() != 2 ) {
457 std::cerr <<
"Could not retrieve the correct categories for URL '" <<
458 S_UrlString <<
"' using UrlCustomDb instance" << std::endl;
462 CategorySet::const_iterator IEnd = myResultCategories.end();
463 if( myResultCategories.find( 0 ) != IEnd &&
464 myResultCategories.find( 1 ) != IEnd )
465 std::cout <<
" Lookup URL '" << S_UrlString <<
466 "' returned category 0 and 1" << std::endl;
468 std::cout <<
"* Removing previously added URL from the custom database"
472 RemoveUrl( myUrlCustomDb, S_UrlString );
474 std::cout <<
"* Verifying that it isn't in the custom database anymore"
478 if( myUrlCustomDb.
getUrl( S_UrlString, myResultCategories ) ) {
479 std::cerr <<
"URL '" << S_UrlString <<
"' is still in the " <<
480 "custom database." << std::endl;
484 std::cout <<
"* Re-adding URL and Categories to the custom database for the next test"
488 AddUrlAndCategories( myUrlCustomDb, S_UrlString, myCategories );
496 std::cout <<
"* Creating UrlClassification and UrlDbClassifier instance"
499 if( !CreateUrlClassificationInstances( theDca, aLicense, myDbConnection,
500 myUrlClassification, myUrlDbClassifier ) )
507 std::cout <<
"* Retrieving URL and Categories by using the UrlClassification instances"
511 if( !LookupUrl( theDca, myUrlDbClassifier, S_UrlString, myResults ) ) {
512 std::cerr <<
"Could not retrieve URL '" << S_UrlString <<
513 "' from UrlDbClassifier instance" << std::endl;
517 if( myResults.
size() != 2 ) {
518 std::cerr <<
"Could not retrieve the correct categories for URL '" <<
519 S_UrlString <<
"' using UrlDbClassifier instance" << std::endl;
524 std::cout <<
" Lookup URL '" << S_UrlString <<
525 "' returned category 0 and 1" << std::endl;
528 std::cout <<
"* Done, leaving test function, cleanup of local instances " <<
529 "is done automatically by going out-of-scope" << std::endl;
545 int main(
int argc,
char *argv[] )
557 std::string myRedistFolder = argv[ 1 ];
558 const std::string myTicket = argv[ 2 ];
559 const std::string myProduct = argv[ 3 ];
560 custom_db_folder = argv[ 4 ];
562 if( myRedistFolder.empty() || myTicket.empty() ||
563 myProduct.empty() || custom_db_folder.empty() ) {
569 char c = *(myRedistFolder.rbegin());
570 if( c !=
'/' && c !=
'\\' )
571 myRedistFolder +=
"/";
573 c = *(custom_db_folder.rbegin());
574 if( c !=
'/' && c !=
'\\' )
575 custom_db_folder +=
"/";
601 if( ProcessCustomDatabase( myDca, myLicense ) )
606 catch(
const ExDca& ex ) {
607 std::cerr <<
"DCA Exception occured. Details: " << ex.
getDescription()
611 catch(
const std::exception& s ) {
612 std::cerr <<
"std::exception occured. Details: " << s.what() <<
The Custom Database module, used to create new custom databases or open existing custom databases.
Is used to create a License object. A license first must be created with DcaInstance::createLicense t...
Exception class used in the DCA.
UrlCustomDb openCustomDb(const DbConnection &aDbConnection) const
Creates a UrlCustomDb instance that is connected to the database defined by the given DbConnection.
static Url create(const DcaInstance &aDcaInstance, const std::string &urlString)
Standard Url creation function.
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
int getMaxSessions() const
Returns the maximum allowed sessions associated with your ticket/license.
static UrlCustomDbModule create(const DcaInstance &aDcaInstance, const License &aLicense)
Loads and initializes the custom database module.
time_t getExpirationDate() const
Returns the expiration date of the license in UTC.
std::string getDescription() const
Returns a description of the error.
std::string configDir
Specifies the complete folder path where the custom database is located, or the folder in which it sh...
DbConnectionCustomData customData
Fill out this structure only if you are using a custom database.
const int ERR_DATABASE_ALREADY_EXISTING
Error code: The specified database already exists.
DCA_RESULT_TYPE getReturnCode() const
Returns the last error code (if any).
FunctionResult classify(const Url &aUrl, UrlClassificationResults &urlResults) const
Performs the URL classification and returns the results.
static void SetupInitData(const std::string &redist_folder, InitData &initData)
Sets up the given initData by substituting the given redist_folder with DCA subdirectories.
std::string binDir
the directory in which the DCA binary (*.dca) files are stored
void removeUrl(const std::string &aUrlString)
Removes the given URL string from the database, if present.
This header includes initialization/deinitialization support functions for the 3rd party libraries us...
Encapsulates the Custom Database maintenance interface.
void SetOpenSslCallbacks()
Initializes the required callbacks for OpenSSL when using HTTPS or SSL connections in a multi-threade...
Stores the connection data for a database.
void UnsetOpenSslCallbacks()
Unsets the openssl callbacks. Do not call any DCA function after you have called this function.
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.
Database connection class for a local or remote database.
void setUrl(const std::string &aUrlString, const CategorySet &categories)
Updates or creates categories for the given URL string.
This header includes all header files of the URL Classification Package.
#define DCA_LOGDIR
Relative directory for logfile(s).
Main class for the URL classification.
#define DCA_INITDIR
DCA subdirectory of the DCA initialization data.
DCA_RESULT_TYPE getReturnCode() const
Gets the code of the error.
Results of an URL classification.
URL database classifier class.
static void PrintToolHeader()
Prints out the name and the version of this sample.
bool contains(DCA_CATEGORY_ID_TYPE catid) const
Fast method to see whether the results contain a given category or not.
Use a License to initialize a classification package or a toolbox package.
FunctionResult createCustomDb(const std::string &aDatabaseFolder) const
Creates a new custom database file in the given folder.
const DbType DBT_Custom
Used for DbConnection classes of custom databases.
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.
This header includes all header files of the DCA Base Package.
bool useLocalDatabase
Set to true to connect to a local or custom database, set to false to use a remote database.
Encapsulates the init and deinit of the DCA API.
std::string product
The product code used with the license.
static UrlClassification create(const DcaInstance &aDcaInstance, const License &aLicense)
Creates the URL classification module by using the given DcaInstance and License.
std::string logDir
the directory in which the DCA log file should be created
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...
DbType dbType
The type of the database.
std::string getDescription() const
Returns the description for the error or warning.
std::set< int > CategorySet
Basic container used for categorizations of URLs. This represents a set of category ids.
bool getUrl(const std::string &aUrlString, CategorySet &foundCategories) const
Retrieves the categories of URL string (if found).
std::string getTicket() const
Returns the ticket of the license as string.
#define DCA_BINDIR
DCA subdirectory of the DCA binaries.
Standard function result.
std::string getSession() const
Returns the session of the license as string.
Encapsulates a URL object.
UrlDbClassifier createDbClassifier(const DbConnection &aDbConnection, const UrlDbClassifierOptions &options=UrlDbClassifierOptions()) const
Create a URL database classifier. The classifier is created by using the provided database connection...
const std::string S_UsageString
Usage string, displayed if a parameter is missing.
DbConnection createDbConnection(const License &aLicense, const DbConnectionData &dbcData, const ProxySettings &proxySettings=ProxySettings(), LogLevel aLogLevel=LOG_Initial) const
Creates a DbConnection object using the given DbConnectionData.
This structure is used to initialize the DcaInstance.
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.
static DCA_MODULE_ID_TYPE ID
The unique ID of the Custom DB module.
License createLicense(const LicenseData &licData, const ProxySettings &proxySettings=ProxySettings(), LogLevel aLogLevel=LOG_Initial) const
Creates a License object using the given LicenseData.
DCA_SIZE_TYPE size() const
Returns the number of results in the container.
int main(int argc, char *argv[])
The main routine.