SamplesConnect.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. /**
  2. Licensed Materials - Property of IBM
  3. IBM Cognos Products: DOCS
  4. (C) Copyright IBM Corp. 2005
  5. US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with
  6. IBM Corp.
  7. */
  8. using System;
  9. using System.Drawing;
  10. using System.Collections;
  11. using System.ComponentModel;
  12. using System.Windows.Forms;
  13. using System.Web.Services.Protocols;
  14. using SamplesCommon;
  15. using cognosdotnet_10_2;
  16. namespace SamplesCommon
  17. {
  18. /// <summary>
  19. /// Summary description for SamplesConnect.
  20. /// </summary>
  21. public class SamplesConnect : System.Windows.Forms.Form
  22. {
  23. private System.Windows.Forms.Label label1;
  24. private System.Windows.Forms.TextBox serverUrlTB;
  25. private System.Windows.Forms.Button buttonOK;
  26. private System.Windows.Forms.Button buttonCancel;
  27. private System.Windows.Forms.Label connectLBL;
  28. /// <summary>
  29. /// Required designer variable.
  30. /// </summary>
  31. private System.ComponentModel.Container components = null;
  32. // sn_dg_prm_smpl_connect_start_0
  33. private agentService cBIAS = null;
  34. // sn_dg_prm_smpl_connect_end_0
  35. private batchReportService1 cBIBRS = null;
  36. private contentManagerService1 cBICMS = null;
  37. private dataIntegrationService1 cBIDIS = null;
  38. private deliveryService1 cBIDS = null;
  39. private eventManagementService1 cBIEMS = null;
  40. private jobService1 cBIJS = null;
  41. private monitorService1 cBIMS = null;
  42. private reportService1 cBIRS = null;
  43. private systemService1 cBISS = null;
  44. private string cBIUrl = "";
  45. private string errorText = null;
  46. private bool connectedToServer = false;
  47. private static string savedUserName = "";
  48. private static string savedUserPassword = "";
  49. private static string savedNamespace = "";
  50. private static string savedAccountSearchPath = "";
  51. public SamplesConnect()
  52. {
  53. //
  54. // Required for Windows Form Designer support
  55. //
  56. InitializeComponent();
  57. //
  58. // TODO: Add any constructor code after InitializeComponent call
  59. //
  60. }
  61. /// <summary>
  62. /// Clean up any resources being used.
  63. /// </summary>
  64. protected override void Dispose( bool disposing )
  65. {
  66. if( disposing )
  67. {
  68. if(components != null)
  69. {
  70. components.Dispose();
  71. }
  72. }
  73. base.Dispose( disposing );
  74. }
  75. #region Windows Form Designer generated code
  76. /// <summary>
  77. /// Required method for Designer support - do not modify
  78. /// the contents of this method with the code editor.
  79. /// </summary>
  80. private void InitializeComponent()
  81. {
  82. System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(SamplesConnect));
  83. this.label1 = new System.Windows.Forms.Label();
  84. this.serverUrlTB = new System.Windows.Forms.TextBox();
  85. this.buttonOK = new System.Windows.Forms.Button();
  86. this.buttonCancel = new System.Windows.Forms.Button();
  87. this.connectLBL = new System.Windows.Forms.Label();
  88. this.SuspendLayout();
  89. //
  90. // label1
  91. //
  92. this.label1.Location = new System.Drawing.Point(10, 23);
  93. this.label1.Name = "label1";
  94. this.label1.Size = new System.Drawing.Size(69, 16);
  95. this.label1.TabIndex = 0;
  96. this.label1.Text = "Server URL";
  97. this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
  98. //
  99. // serverUrlTB
  100. //
  101. this.serverUrlTB.Location = new System.Drawing.Point(87, 23);
  102. this.serverUrlTB.Name = "serverUrlTB";
  103. this.serverUrlTB.Size = new System.Drawing.Size(312, 20);
  104. this.serverUrlTB.TabIndex = 1;
  105. this.serverUrlTB.Text = "http://localhost:9300/p2pd/servlet/dispatch";
  106. this.serverUrlTB.KeyDown += new System.Windows.Forms.KeyEventHandler(this.serverUrlTB_OnKeyPress);
  107. //
  108. // buttonOK
  109. //
  110. this.buttonOK.Location = new System.Drawing.Point(239, 55);
  111. this.buttonOK.Name = "buttonOK";
  112. this.buttonOK.Size = new System.Drawing.Size(75, 23);
  113. this.buttonOK.TabIndex = 2;
  114. this.buttonOK.Text = "OK";
  115. this.buttonOK.Click += new System.EventHandler(this.buttonOK_Click);
  116. this.buttonOK.KeyDown += new System.Windows.Forms.KeyEventHandler(this.buttonOK_OnKeyPress);
  117. //
  118. // buttonCancel
  119. //
  120. this.buttonCancel.Location = new System.Drawing.Point(327, 55);
  121. this.buttonCancel.Name = "buttonCancel";
  122. this.buttonCancel.Size = new System.Drawing.Size(75, 23);
  123. this.buttonCancel.TabIndex = 3;
  124. this.buttonCancel.Text = "Cancel";
  125. this.buttonCancel.Click += new System.EventHandler(this.buttonCancel_Click);
  126. this.buttonCancel.KeyDown += new System.Windows.Forms.KeyEventHandler(this.buttonCancel_OnKeyPress);
  127. //
  128. // connectLBL
  129. //
  130. this.connectLBL.Location = new System.Drawing.Point(24, 55);
  131. this.connectLBL.Name = "connectLBL";
  132. this.connectLBL.Size = new System.Drawing.Size(152, 23);
  133. this.connectLBL.TabIndex = 4;
  134. //
  135. // SamplesConnect
  136. //
  137. this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  138. this.ClientSize = new System.Drawing.Size(414, 86);
  139. this.Controls.Add(this.connectLBL);
  140. this.Controls.Add(this.buttonCancel);
  141. this.Controls.Add(this.buttonOK);
  142. this.Controls.Add(this.serverUrlTB);
  143. this.Controls.Add(this.label1);
  144. this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
  145. this.Name = "SamplesConnect";
  146. this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
  147. this.Text = "Connect to server";
  148. this.ResumeLayout(false);
  149. this.PerformLayout();
  150. }
  151. #endregion
  152. private void buttonOK_Click(object sender, System.EventArgs e)
  153. {
  154. connectLBL.Visible = true;
  155. connectLBL.Text = "Connecting...";
  156. cBICMS = new contentManagerService1();
  157. if (cBICMS != null)
  158. {
  159. //Test for Anonymous Authentication
  160. Boolean bTestAnonymous = false;
  161. try
  162. {
  163. cBIUrl = serverUrlTB.Text;
  164. cBICMS.Url = cBIUrl;
  165. searchPathMultipleObject homeDirSearchPath = new searchPathMultipleObject();
  166. homeDirSearchPath.Value = "~";
  167. baseClass[] bc = CBICMS.query ( homeDirSearchPath, new propEnum[]{} , new sort[]{}, new queryOptions () );
  168. if (bc != null)
  169. {
  170. bTestAnonymous = true;
  171. }
  172. }
  173. catch(Exception ex)
  174. {
  175. if (ex == null) { }
  176. //if security is enabled, we will end up here
  177. }
  178. if (bTestAnonymous == true)
  179. {
  180. connectedToServer = true;
  181. connectLBL.Visible = true;
  182. connectLBL.Text = "Connected.";
  183. savedAccountSearchPath = "CAMID(\"::Anonymous\")";
  184. }
  185. else
  186. {
  187. // Attempt to log on.
  188. SamplesLogon logon = new SamplesLogon( this );
  189. logon.setUserName(savedUserName);
  190. logon.setUserPassword(savedUserPassword);
  191. logon.setNamespace(savedNamespace);
  192. logon.ShowDialog( );
  193. if( !logon.loggedOn )
  194. {
  195. errorText = ( "Connection to server: Unable to log on." );
  196. connectedToServer = false;
  197. connectLBL.Visible = false;
  198. return;
  199. }
  200. else
  201. {
  202. connectedToServer = true;
  203. savedUserName = logon.getUserName();
  204. savedUserPassword = logon.getUserPassword();
  205. savedNamespace = logon.getNamespace();
  206. connectLBL.Visible = true;
  207. connectLBL.Text = "Connected.";
  208. savedAccountSearchPath = getLogonAccount(this).searchPath.value;;
  209. }
  210. }
  211. }
  212. this.Close();
  213. }
  214. private void buttonCancel_Click(object sender, System.EventArgs e)
  215. {
  216. connectedToServer = false;
  217. this.Close();
  218. }
  219. public bool IsConnectedToCBI()
  220. {
  221. return connectedToServer;
  222. }
  223. public string GetAccountPath()
  224. {
  225. return savedAccountSearchPath;
  226. }
  227. public string GetErrorText()
  228. {
  229. return errorText;
  230. }
  231. public string getUserName()
  232. {
  233. return savedUserName;
  234. }
  235. public string getUserPassword()
  236. {
  237. return savedUserPassword;
  238. }
  239. public string getNamespace()
  240. {
  241. return savedNamespace;
  242. }
  243. private void serverUrlTB_OnKeyPress(object sender, KeyEventArgs e)
  244. {
  245. if (e.KeyCode == Keys.Enter)
  246. {
  247. buttonOK_Click(sender, e);
  248. }
  249. else if (e.KeyCode == Keys.Escape)
  250. {
  251. this.Close();
  252. }
  253. }
  254. private void buttonOK_OnKeyPress(object sender, KeyEventArgs e)
  255. {
  256. if (e.KeyCode == Keys.Escape)
  257. {
  258. this.Close();
  259. }
  260. }
  261. private void buttonCancel_OnKeyPress(object sender, KeyEventArgs e)
  262. {
  263. if (e.KeyCode == Keys.Escape)
  264. {
  265. this.Close();
  266. }
  267. }
  268. public bool makeCLConnection(ref string cBIUrl,
  269. ref string userName,
  270. ref string userPassword,
  271. ref string userNamespace,
  272. ref bool isAnonymous)
  273. {
  274. Console.WriteLine("Connecting...");
  275. cBICMS = new contentManagerService1();
  276. if (cBICMS == null)
  277. {
  278. Console.WriteLine("Unable to get a contentManagerService1 instance.");
  279. return false;
  280. }
  281. //Test for Anonymous Authentication
  282. try
  283. {
  284. // sn_dg_prm_smpl_connect_start_1
  285. cBIUrl = "http://localhost:9300/p2pd/servlet/dispatch";
  286. cBICMS.Url = cBIUrl;
  287. // sn_dg_prm_smpl_connect_end_1
  288. searchPathMultipleObject homeDirSearchPath = new searchPathMultipleObject();
  289. homeDirSearchPath.Value = "~";
  290. baseClass[] bc = cBICMS.query ( homeDirSearchPath, new propEnum[]{} , new sort[]{}, new queryOptions () );
  291. if (bc != null)
  292. {
  293. isAnonymous = true;
  294. }
  295. }
  296. catch(Exception ex)
  297. {
  298. // Anonymous if OFF
  299. isAnonymous = false;
  300. if (0 != ex.Message.CompareTo("The client did something wrong"))
  301. {
  302. SamplesException.ShowExceptionMessage("\n" + ex.Message, false, "Error connecting to server");
  303. return false;
  304. }
  305. }
  306. if (isAnonymous == true)
  307. {
  308. // Anonymous if ON
  309. connectedToServer = true;
  310. Console.WriteLine("Connected.");
  311. return true;
  312. }
  313. else
  314. {
  315. // Attempt to log on.
  316. if ( (userName == null) || (0 == userName.CompareTo("")) )
  317. {
  318. Console.Write("userName: ");
  319. userName = Console.ReadLine();
  320. }
  321. if ( (userPassword == null) || (0 == userPassword.CompareTo("")) )
  322. {
  323. Console.Write("userPassword: ");
  324. userPassword = Console.ReadLine();
  325. }
  326. if ( (userNamespace == null) || (0 == userNamespace.CompareTo("")) )
  327. {
  328. Console.Write("userNamespace: ");
  329. userNamespace = Console.ReadLine();
  330. }
  331. SamplesLogon logon = new SamplesLogon(this);
  332. logon.specificUserLogon(false, userName, userPassword, userNamespace);
  333. if( !logon.loggedOn )
  334. {
  335. Console.WriteLine("Unable to connect to server." );
  336. connectedToServer = false;
  337. return false;
  338. }
  339. else
  340. {
  341. connectedToServer = true;
  342. savedUserName = logon.getUserName();
  343. savedUserPassword = logon.getUserPassword();
  344. savedNamespace = logon.getNamespace();
  345. Console.WriteLine("Connected.");
  346. return true;
  347. }
  348. }
  349. }
  350. public static account getLogonAccount(SamplesConnect connectedAs)
  351. {
  352. propEnum[] props =
  353. new propEnum[] { propEnum.searchPath, propEnum.defaultName, propEnum.policies };
  354. account myAccount = null;
  355. if (connectedAs.CBICMS == null)
  356. {
  357. Console.WriteLine("Invalid parameter passed to function logon.");
  358. return myAccount;
  359. }
  360. try
  361. {
  362. searchPathMultipleObject dummy = new searchPathMultipleObject();
  363. dummy.Value = "~";
  364. baseClass[] bc =
  365. connectedAs.CBICMS.query(dummy, props, new sort[] {}, new queryOptions());
  366. if ((bc != null) && (bc.Length == 1))
  367. {
  368. myAccount = (account)bc[0];
  369. }
  370. }
  371. catch (Exception ex)
  372. {
  373. //An exception here likely indicates the client is not currently
  374. //logged in, so the query fails.
  375. Console.WriteLine("Caught Exception:\n" + ex.Message);
  376. }
  377. return myAccount;
  378. }
  379. public credential getCredential()
  380. {
  381. baseClass[] results;
  382. searchPathMultipleObject credSearchPath = new searchPathMultipleObject();
  383. credSearchPath.Value = "~/credential[@name='Credential']";
  384. results = CBICMS.query(credSearchPath, new propEnum[] {}, new sort[] {}, new queryOptions());
  385. for (int i = 0; i < results.Length; i++)
  386. {
  387. if (results[i] is credential)
  388. {
  389. return (credential) results[i];
  390. }
  391. }
  392. return addCredential();
  393. }
  394. public credential addCredential()
  395. {
  396. credential mCredential = new credential();
  397. anyTypeProp mCredentials = new anyTypeProp();
  398. mCredentials.value = credentialString;
  399. baseClass[] addedObjects;
  400. //Prepare the name property for the new credential object
  401. multilingualToken[] names = new multilingualToken[1];
  402. names[0] = new multilingualToken();
  403. locale[] locales = getLocale();
  404. names[0].locale = locales[0].locale1;
  405. names[0].value = "Credential";
  406. multilingualTokenProp credNameTokenProp = new multilingualTokenProp();
  407. credNameTokenProp.value = names;
  408. //Add the searchPath, name and defaultname to the new credential object
  409. mCredential.name = credNameTokenProp;
  410. mCredential.credentials = mCredentials;
  411. baseClass[] addArray = new baseClass[1];
  412. addArray[0] = mCredential;
  413. //Add the credential object to the content store
  414. searchPathSingleObject parentPath = new searchPathSingleObject();
  415. parentPath.Value = getLogonAccount(this).searchPath.value;
  416. addOptions ao = new addOptions();
  417. ao.updateAction = updateActionEnum.replace;
  418. try
  419. {
  420. addedObjects = CBICMS.add(parentPath, addArray, ao);
  421. for (int i = 0; i < addedObjects.Length; i++)
  422. {
  423. if (addedObjects[i] is credential)
  424. {
  425. return (credential) addedObjects[i];
  426. }
  427. }
  428. }
  429. catch (Exception ex)
  430. {
  431. Console.WriteLine("Caught Exception:\n" + ex.Message);
  432. }
  433. return null;
  434. }
  435. public locale[] getLocale()
  436. {
  437. configurationData data = null;
  438. locale[] locales = null;
  439. configurationDataEnum[] config = new configurationDataEnum[1];
  440. config[0] = configurationDataEnum.serverLocale;
  441. try
  442. {
  443. /**
  444. * Use this method to retrieve global configuration data.
  445. *
  446. * Input Parameters
  447. *
  448. * config
  449. * Specifies the set of properties to be returned.
  450. *
  451. * Return Value
  452. * This method returns the server locale.
  453. */
  454. data = CBISS.getConfiguration(config);
  455. locales = data.serverLocale;
  456. if (locales == null)
  457. {
  458. Console.WriteLine("No serverLocale configured!");
  459. }
  460. }
  461. catch(Exception ex)
  462. {
  463. Console.WriteLine("Caught Exception:\n" + ex.Message);
  464. }
  465. return locales;
  466. }
  467. //property definition for the server URL
  468. public string CBIURL
  469. {
  470. get
  471. {
  472. return cBIUrl;
  473. }
  474. }
  475. public string credentialString
  476. {
  477. get
  478. {
  479. string credentialXML = "<credential>";
  480. //Namespace
  481. if (savedNamespace != "")
  482. {
  483. credentialXML += "<namespace>";
  484. credentialXML += savedNamespace;
  485. credentialXML += "</namespace>";
  486. }
  487. //Username
  488. credentialXML += "<username>";
  489. if (savedUserName == "")
  490. {
  491. credentialXML += "Anonymous";
  492. }
  493. else
  494. {
  495. credentialXML += savedUserName;
  496. }
  497. credentialXML += "</username>";
  498. //Password
  499. if (savedUserPassword != "")
  500. {
  501. credentialXML += "<password>";
  502. credentialXML += savedUserPassword;
  503. credentialXML += "</password>";
  504. }
  505. credentialXML += "</credential>";
  506. return credentialXML;
  507. }
  508. }
  509. //property definitions to give read access the various service objects
  510. //
  511. //header and url information is retrieved from the contentManagerService1 object
  512. //as this it the service used to connect and logon.
  513. public agentService CBIAS
  514. {
  515. get
  516. {
  517. // sn_dg_prm_smpl_connect_start_2
  518. if (cBIAS == null)
  519. {
  520. cBIAS = new agentService();
  521. cBIAS.Url = cBICMS.Url;
  522. }
  523. if (cBIAS.biBusHeaderValue == null)
  524. {
  525. cBIAS.biBusHeaderValue = cBICMS.biBusHeaderValue;
  526. }
  527. // sn_dg_prm_smpl_connect_end_2
  528. return cBIAS;
  529. }
  530. }
  531. public batchReportService1 CBIBRS
  532. {
  533. get
  534. {
  535. if (cBIBRS == null)
  536. {
  537. cBIBRS = new batchReportService1();
  538. cBIBRS.Url = cBICMS.Url;
  539. }
  540. if (cBIBRS.biBusHeaderValue == null)
  541. {
  542. cBIBRS.biBusHeaderValue = cBICMS.biBusHeaderValue;
  543. }
  544. return cBIBRS;
  545. }
  546. }
  547. public contentManagerService1 CBICMS
  548. {
  549. //This service is the initial connect and logon point.
  550. //It _must_ be initialized before calling this accessor.
  551. get
  552. {
  553. return cBICMS;
  554. }
  555. }
  556. public dataIntegrationService1 CBIDIS
  557. {
  558. get
  559. {
  560. if (cBIDIS == null)
  561. {
  562. cBIDIS = new dataIntegrationService1();
  563. cBIDIS.Url = cBICMS.Url;
  564. }
  565. if (cBIDIS.biBusHeaderValue == null)
  566. {
  567. cBIDIS.biBusHeaderValue = cBICMS.biBusHeaderValue;
  568. }
  569. return cBIDIS;
  570. }
  571. }
  572. public deliveryService1 CBIDS
  573. {
  574. get
  575. {
  576. if (cBIDS == null)
  577. {
  578. cBIDS = new deliveryService1();
  579. cBIDS.Url = cBICMS.Url;
  580. }
  581. if (cBIDS.biBusHeaderValue == null)
  582. {
  583. cBIDS.biBusHeaderValue = cBICMS.biBusHeaderValue;
  584. }
  585. return cBIDS;
  586. }
  587. }
  588. public eventManagementService1 CBIEMS
  589. {
  590. get
  591. {
  592. if (cBIEMS == null)
  593. {
  594. cBIEMS = new eventManagementService1();
  595. cBIEMS.Url = cBICMS.Url;
  596. }
  597. if (cBIEMS.biBusHeaderValue == null)
  598. {
  599. cBIEMS.biBusHeaderValue = cBICMS.biBusHeaderValue;
  600. }
  601. return cBIEMS;
  602. }
  603. }
  604. public jobService1 CBIJS
  605. {
  606. get
  607. {
  608. if (cBIJS == null)
  609. {
  610. cBIJS = new jobService1();
  611. cBIJS.Url = cBICMS.Url;
  612. }
  613. if (cBIJS.biBusHeaderValue == null)
  614. {
  615. cBIJS.biBusHeaderValue = cBICMS.biBusHeaderValue;
  616. }
  617. return cBIJS;
  618. }
  619. }
  620. public monitorService1 CBIMS
  621. {
  622. get
  623. {
  624. if (cBIMS == null)
  625. {
  626. cBIMS = new monitorService1();
  627. cBIMS.Url = cBICMS.Url;
  628. }
  629. if (cBIMS.biBusHeaderValue == null)
  630. {
  631. cBIMS.biBusHeaderValue = cBICMS.biBusHeaderValue;
  632. }
  633. return cBIMS;
  634. }
  635. }
  636. public reportService1 CBIRS
  637. {
  638. get
  639. {
  640. if (cBIRS == null)
  641. {
  642. cBIRS = new reportService1();
  643. cBIRS.Url = cBICMS.Url;
  644. }
  645. if (cBIRS.biBusHeaderValue == null)
  646. {
  647. cBIRS.biBusHeaderValue = cBICMS.biBusHeaderValue;
  648. }
  649. return cBIRS;
  650. }
  651. }
  652. public systemService1 CBISS
  653. {
  654. get
  655. {
  656. if (cBISS == null)
  657. {
  658. cBISS = new systemService1();
  659. cBISS.Url = cBICMS.Url;
  660. }
  661. if (cBISS.biBusHeaderValue == null)
  662. {
  663. cBISS.biBusHeaderValue = cBICMS.biBusHeaderValue;
  664. }
  665. return cBISS;
  666. }
  667. }
  668. }
  669. }