60 # include <winsock2.h>
69 using namespace dca_wac;
71 const std::string S_ToolName =
"wacsample";
72 const std::string S_ToolVersion =
"1.3";
79 "<redist-folder> <ticket> <product> <input-file>\n"
80 " redist-folder - the folder where the DCA is installed to\n"
81 " ticket - a valid ticket\n"
82 " product - the product associated with your ticket\n\n"
83 " input-file - a file with sample input data in the following format:\n"
84 " 1st line: requestdata\n"
86 " 3rd line: response\n"
87 " 4th line: requestdata...\n"
88 " Leave line blank if no request data URL or response data should be used\n"
89 " Note: At least one of the 3 lines must be non blank.\n"
90 " Note: A $(CR), $(LF) or $(CRLF) will be substituted with \\r, \\n and \\r\\n\n\n"
98 # define DCA_BINDIR "bin/Win32"
100 # define DCA_BINDIR "bin/linux"
107 #define DCA_INITDIR "init"
112 #define DCA_LOGDIR "./logs"
140 static bool StartupLibraries()
145 WORD wVersionRequested = MAKEWORD( 2, 2 );
147 int err = WSAStartup( wVersionRequested, &wsaData );
149 std::cout <<
"Error on WSAStartup (" << err <<
") occured, aborting" <<
167 static void ShutdownLibraries()
186 static void SetupLicense(
const std::string& ticket,
const std::string& product,
189 licenseData.
ticket = ticket;
222 static void PrintDbConnectionInfo(
const DbConnection& aDbConnection )
227 std::cout <<
"WAC Database Version: " << databaseInformation.
versionString
240 struct tm *expirationTime = localtime( &expirationDate );
242 std::cout <<
"License Info:" << std::endl;
243 std::cout <<
" DCA is " << ( aLicense.
isLicensed() ?
"licensed." :
244 "not licensed." ) << std::endl;
245 std::cout <<
" MaxUsers:" << aLicense.
getMaxUsers() <<
249 std::cout <<
" Ticket:" << aLicense.
getTicket() <<
251 std::cout <<
" Session:" << aLicense.
getSession() <<
255 std::cout <<
" Expiration Date:" << asctime( expirationTime ) <<
265 std::cout <<
"IBM DCA Sample: " << S_ToolName <<
" (" << S_ToolVersion <<
")" << std::endl;
275 std::cout << name <<
" usage:" << std::endl;
288 std::cout <<
"No results returned for the given request data" << std::endl;
292 std::cout <<
"Result from WAC Classification:" << std::endl;
295 std::cout <<
"Returned Application name=" <<
300 if( actionId >= 0 ) {
301 std::cout <<
"Returned Action name=" <<
320 std::cout <<
"Error returned from WAC Classification call. RC=" << myFR.
getReturnCode() <<
329 void SubstituteCRLF( std::string& s )
335 const std::string subst =
"$(CR)";
336 const std::string newst =
"\r";
337 size_t substlen = subst.length();
338 size_t newstlen = newst.length();
339 std::string::size_type i = 0, pos = 0;
340 while( ( i = s.find( subst, pos ) ) != std::string::npos ) {
341 s.replace( i, substlen, newst );
346 const std::string subst =
"$(LF)";
347 const std::string newst =
"\n";
348 size_t substlen = subst.length();
349 size_t newstlen = newst.length();
350 std::string::size_type i = 0, pos = 0;
351 while( ( i = s.find( subst, pos ) ) != std::string::npos ) {
352 s.replace( i, substlen, newst );
357 const std::string subst =
"$(CRLF)";
358 const std::string newst =
"\r\n";
359 size_t substlen = subst.length();
360 size_t newstlen = newst.length();
361 std::string::size_type i = 0, pos = 0;
362 while( ( i = s.find( subst, pos ) ) != std::string::npos ) {
363 s.replace( i, substlen, newst );
386 const std::string& aFile )
388 std::ifstream fstream( aFile.c_str(), std::ios::in );
389 if ( !fstream.is_open() )
return;
395 while ( std::getline(fstream, line) )
401 while( line[line.length()-1] ==
'\r' || line[line.length()-1] ==
'\n' )
402 line.erase( line.length() - 1 );
404 std::string requestLine = line;
406 if( !std::getline(fstream, line) ) {
409 std::string urlLine = line;
411 if( !std::getline(fstream, line) ) {
414 std::string responseLine = line;
416 if( requestLine.empty() &&
418 responseLine.empty() )
425 if( !urlLine.empty() ) {
432 std::cout <<
"Retrieved error on UrlClassification call rc=" <<
438 std::cout <<
"Given URL '" << urlLine <<
"' has no WAC related results available so continue with next entry"
446 if( !requestLine.empty() ) {
447 SubstituteCRLF( requestLine );
448 myInputData.
setRequestData( requestLine.c_str(), requestLine.length() );
451 if ( !responseLine.empty() ) {
452 SubstituteCRLF( responseLine );
453 myInputData.
setResponse( responseLine.c_str(), responseLine.length() );
456 std::cout <<
"Starting WAC Classification #" << i <<
457 " (URL='" << urlLine <<
"', request length=" << requestLine.length() <<
458 ", response length=" << responseLine.length() <<
463 DoCallWacClassification( aWacClassifier, myInputData, myWacClassificationResult );
465 PrintResults( myWacClassificationResult, aCategoriesInfo );
466 std::cout << std::endl;
477 int main(
int argc,
char *argv[] )
489 std::string myRedistFolder = argv[ 1 ];
490 const std::string myTicket = argv[ 2 ];
491 const std::string myProduct = argv[ 3 ];
492 const std::string myFile = argv[ 4 ];
494 if( myRedistFolder.empty() || myTicket.empty() ||
495 myProduct.empty() || myFile.empty() ) {
501 const char c = myRedistFolder[ myRedistFolder.length() - 1 ];
502 if( c !=
'/' && c !=
'\\' )
503 myRedistFolder +=
"/";
506 if( !StartupLibraries() )
525 if( myLicense.
isLicensed( WacClassification::ID ) ) {
533 SetupWacConnectionData( myWacConnectionData );
539 PrintDbConnectionInfo( myDbConnection );
542 myWacClassification = WacClassification::create( myDca, myLicense );
549 myWacClassifier = myWacClassification.
createClassifier( myDbConnection, myWacClassifierOptions );
558 SetupUrlConnectionData( myUrlConnectionData );
562 PrintDbConnectionInfo( myUrlConnection );
573 myUrlClassifier = myUrlClassification.
createDbClassifier( myUrlConnection, myUrlDbClassifierOptions );
577 TestWacClassification( myDca, myUrlClassifier, myWacClassification,
578 myWacClassifier, myWacCategoriesInfo, myFile );
584 catch(
const ExDca& ex ) {
585 std::cerr <<
"DCA Exception occured. Details: " << ex.
getDescription() <<
589 catch(
const std::exception& s ) {
590 std::cerr <<
"std::exception occured. Details: " << s.what() <<
"." << std::endl;
594 std::cerr <<
"Unknown exception caught." << std::endl;
bool enable_Feedback
This switches the Feedback feature on or off. This feature collects number of matched applications/ac...
WacAction byId(DCA_ACTION_ID_TYPE actionId) const
Returns the action with the given action id.
Is used to create a License object. A license first must be created with DcaInstance::createLicense t...
Exception class used in the DCA.
static Url create(const DcaInstance &aDcaInstance, const std::string &urlString)
Standard Url creation function.
const WacClassificationResult NullWacClassificationResult
Defines a constant unassigned WacClassificationResult you can use for checks. if ( myWacClassificatio...
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
std::string name(const std::string &localeString=std::string()) const
Returns the localized (display) name of the application.
int DCA_ACTION_ID_TYPE
Type for action ids.
bool wacDetailsAvailable(const dca::UrlClassificationResults &res) const
Returns whether or not a subsequent WAC classification may provide useful WAC information....
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 the WacClassificationResult class.
DatabaseInformation getDatabaseInformation() const
Returns information about the underlying database.
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.
DCA_ACTION_ID_TYPE actionId() const
If the classification was successful this is the matched action.
std::string binDir
the directory in which the DCA binary (*.dca) files are stored
This header includes initialization/deinitialization support functions for the 3rd party libraries us...
Sets up options for embedded URL detection and provided Feedback mechanism.
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.
WacCategoriesInfo getCategoriesInfo() const
Retrieve the WacCategoriesInfo class.
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.
Package header file for the WAC (Web Application Classification) Package.
#define DCA_INITDIR
DCA subdirectory of the DCA initialization data.
dca::FunctionResult classify(const WacInputData &input, WacClassificationResult &result) const
Classification function that takes the given input and return its results (if any) in the provided re...
bool enable_Feedback
This switches the Feedback feature on or off. This is switched off by default.
DCA_RESULT_TYPE getReturnCode() const
Gets the code of the error.
Defintion of the WacClassifier class.
std::string name(const std::string &localeString=std::string()) const
Returns the localized (display) name of the action.
Results of an URL classification.
bool detect_EmbeddedUrlsInUrlPath
If set to true, you can specify that embedded URL detection is additionally performed in the path par...
URL database classifier class.
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.
WacActions getActions() const
Returns the contained WacActions.
const DbType DBT_Wac
Used for dca::DbConnection classes for Web Application Classification databases.
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.
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.
const DbType DBT_Url
Used for DbConnection classes for URL classification.
WacClassifier createClassifier(const dca::DbConnection &aDbConnection, const WacClassifierOptions &aWacClassifierOptions=WacClassifierOptions()) const
Creates a WacClassifier that is used to classify WacInputData objects.
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
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...
DbType dbType
The type of the database.
std::string getDescription() const
Returns the description for the error or warning.
int DCA_APPLICATION_ID_TYPE
Type for application ids.
WacApplication byId(DCA_APPLICATION_ID_TYPE appid) const
Returns the application with the given application id.
Sets up options for the WAC classification.
std::string getTicket() const
Returns the ticket of the license as string.
bool enable_EmbeddedUrlDetection
If set to true embedded URL detection will be used in URL classification. This is switched on by defa...
#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.
DCA_APPLICATION_ID_TYPE applicationId() const
If the classification was successful this is the matched application.
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.
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.