Logon.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. /**
  2. Licensed Materials - Property of IBM
  3. IBM Cognos Products: DOCS
  4. (C) Copyright IBM Corp. 2005, 2013
  5. US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with
  6. IBM Corp.
  7. */
  8. /**
  9. * Logon.java
  10. *
  11. * Copyright (C) 2008 Cognos ULC, an IBM Company. All rights reserved.
  12. * Cognos (R) is a trademark of Cognos ULC, (formerly Cognos Incorporated).
  13. *
  14. * Description: This code sample demonstrates how to log on
  15. * and how to log off using the following methods:
  16. *
  17. * - logon
  18. * Use this method to log on through the SDK. If authenticated
  19. * by a third party security provider, the action is
  20. * successful and a passport is created in the biBusHeader.
  21. * - logoff
  22. * Use this method to log off through the SDK. If the action is
  23. * successful, the passport is removed from the biBusHeader.
  24. * - query
  25. * Use this method to request objects from the content store.
  26. */
  27. import java.awt.BorderLayout;
  28. import java.awt.Container;
  29. import java.awt.GridLayout;
  30. import java.awt.event.ActionEvent;
  31. import java.awt.event.ActionListener;
  32. import javax.swing.JButton;
  33. import javax.swing.JComboBox;
  34. import javax.swing.JDialog;
  35. import javax.swing.JLabel;
  36. import javax.swing.JOptionPane;
  37. import javax.swing.JPanel;
  38. import javax.swing.JPasswordField;
  39. import javax.swing.JTextField;
  40. import org.apache.axis.client.Stub;
  41. import org.apache.axis.message.SOAPHeaderElement;
  42. import com.cognos.developer.schemas.bibus._3.Account;
  43. import com.cognos.developer.schemas.bibus._3.BaseClass;
  44. import com.cognos.developer.schemas.bibus._3.BiBusHeader;
  45. import com.cognos.developer.schemas.bibus._3.DisplayObject;
  46. import com.cognos.developer.schemas.bibus._3.PromptOption;
  47. import com.cognos.developer.schemas.bibus._3.PropEnum;
  48. import com.cognos.developer.schemas.bibus._3.QueryOptions;
  49. import com.cognos.developer.schemas.bibus._3.SearchPathMultipleObject;
  50. import com.cognos.developer.schemas.bibus._3.SearchPathSingleObject;
  51. import com.cognos.developer.schemas.bibus._3.Sort;
  52. import com.cognos.developer.schemas.bibus._3.XmlEncodedXML;
  53. public class Logon implements ActionListener
  54. {
  55. private static String logon = "logon";
  56. private static String cancel = "cancel";
  57. private static String userID = "";
  58. private static String password = "";
  59. private static String nameSpace = "";
  60. private static String credentialString = "";
  61. private static String anonUserID = "";
  62. private static String anonNameSpace = "";
  63. private JTextField userNameField = new JTextField(10);
  64. private JPasswordField passwordField = new JPasswordField(10);
  65. private JDialog loginDialog;
  66. private JComboBox namespaceBox;
  67. /**
  68. * Use this Java method to log on, bypassing
  69. * any prompts.
  70. *
  71. * @param connection
  72. * Connection to Server
  73. *
  74. * @param namespace
  75. * Specifies the namespace where the user ID is stored.
  76. * @param uid
  77. * Specifies the ID of the user.
  78. * @param pwd
  79. * Specifies the password of the user.
  80. * @return
  81. * Returns a string containing status information.
  82. */
  83. public String quickLogon(
  84. CRNConnect connection,
  85. String namespace,
  86. String uid,
  87. String pwd)
  88. throws Exception
  89. {
  90. // sn_dg_prm_sdk_method_contentManagerService_logon_start_1
  91. StringBuffer credentialXML = new StringBuffer();
  92. credentialXML.append("<credential>");
  93. credentialXML.append("<namespace>");
  94. credentialXML.append(namespace);
  95. credentialXML.append("</namespace>");
  96. credentialXML.append("<username>");
  97. credentialXML.append(uid);
  98. credentialXML.append("</username>");
  99. credentialXML.append("<password>");
  100. credentialXML.append(pwd);
  101. credentialXML.append("</password>");
  102. credentialXML.append("</credential>");
  103. String encodedCredentials = credentialXML.toString();
  104. credentialString = encodedCredentials;
  105. connection.getCMService().logon(new XmlEncodedXML(encodedCredentials), new SearchPathSingleObject[] {});
  106. // sn_dg_prm_sdk_method_contentManagerService_logon_end_1
  107. return ("Logon successful as " + uid);
  108. }
  109. /**
  110. * Use this Java method to logon.
  111. *
  112. * @param connection
  113. * Connection to Server
  114. *
  115. * @return Returns a string containing status information.
  116. */
  117. public String logon(CRNConnect connection)
  118. {
  119. loginDialog = new JDialog();
  120. JPanel userNamePanel = new JPanel();
  121. JPanel passwordPanel = new JPanel();
  122. JPanel namespacePanel = new JPanel();
  123. // NOTE: If you are already logged on, you must first log off
  124. // before you can log on as a different user.
  125. logoff(connection);
  126. String namespaceInfo[] = getNamespaces(connection);
  127. if (namespaceInfo == null)
  128. {
  129. JOptionPane.showMessageDialog(
  130. null,
  131. "Unable to connect",
  132. "Connect Failed",
  133. JOptionPane.ERROR_MESSAGE);
  134. return "Unable to connect to server";
  135. }
  136. //namespaceInfo is name/ID pairs -- always even
  137. String namespaces[] = new String[namespaceInfo.length / 2];
  138. String namespaceIDs[] = new String[namespaceInfo.length / 2];
  139. for (int j = 0, k = 0; k < namespaceInfo.length; j++, k++)
  140. {
  141. namespaces[j] = namespaceInfo[k++];
  142. namespaceIDs[j] = namespaceInfo[k];
  143. }
  144. // Setup the username field
  145. JLabel userNameLabel = new JLabel("User Name: ");
  146. userNameLabel.setLabelFor(userNameField);
  147. userNamePanel.add(userNameLabel, BorderLayout.WEST);
  148. userNamePanel.add(userNameField, BorderLayout.EAST);
  149. // Setup the password field
  150. passwordField.setEchoChar('*');
  151. JLabel passwordLabel = new JLabel("Password: ");
  152. passwordLabel.setLabelFor(passwordField);
  153. passwordPanel.add(passwordLabel, BorderLayout.WEST);
  154. passwordPanel.add(passwordField, BorderLayout.EAST);
  155. // Setup the namespace field
  156. namespaceBox = new JComboBox(namespaces);
  157. namespaceBox.setSelectedItem(null);
  158. JLabel namespaceLabel = new JLabel("Namespace: ");
  159. namespaceLabel.setLabelFor(namespaceBox);
  160. namespacePanel.add(namespaceLabel, BorderLayout.WEST);
  161. namespacePanel.add(namespaceBox, BorderLayout.EAST);
  162. // Add the fields to the panel
  163. JPanel loginPanel = new JPanel(new GridLayout(3, 0));
  164. loginPanel.add(userNamePanel);
  165. loginPanel.add(passwordPanel);
  166. loginPanel.add(namespacePanel);
  167. // Set up the ButtonPanel
  168. JPanel buttonPanel = createButtonPanel();
  169. // Set up and display the window
  170. loginDialog.setTitle("Logon");
  171. Container loginContentPane = loginDialog.getContentPane();
  172. loginContentPane.add(loginPanel, BorderLayout.CENTER);
  173. loginContentPane.add(buttonPanel, BorderLayout.SOUTH);
  174. loginDialog.pack();
  175. loginDialog.setResizable(false);
  176. loginDialog.setModal(true);
  177. loginDialog.setVisible(true);
  178. // Process the user input
  179. if (userID == "")
  180. {
  181. return "";
  182. }
  183. // Find NamespaceID
  184. boolean found = false;
  185. int i = 0;
  186. while(!found && i < namespaces.length)
  187. {
  188. if (nameSpace.compareToIgnoreCase(namespaces[i]) == 0)
  189. found = true;
  190. else
  191. i++;
  192. }
  193. StringBuffer credentialXML = new StringBuffer();
  194. credentialXML.append("<credential>");
  195. credentialXML.append("<namespace>");
  196. credentialXML.append(namespaceIDs[i]);
  197. credentialXML.append("</namespace>");
  198. credentialXML.append("<username>");
  199. credentialXML.append(userID);
  200. credentialXML.append("</username>");
  201. credentialXML.append("<password>");
  202. credentialXML.append(password);
  203. credentialXML.append("</password>");
  204. credentialXML.append("</credential>");
  205. String encodedCredentials = credentialXML.toString();
  206. credentialString = encodedCredentials;
  207. try
  208. {
  209. connection.getCMService().logon(new XmlEncodedXML(encodedCredentials), new SearchPathSingleObject[] {});
  210. SOAPHeaderElement x = ((Stub)connection.getCMService()).getResponseHeader("http://developer.cognos.com/schemas/bibus/3/", "biBusHeader");
  211. ((Stub)connection.getCMService()).setHeader(x);
  212. password = "";
  213. }
  214. catch (java.rmi.RemoteException remoteEx)
  215. {
  216. userID = "";
  217. password = "";
  218. nameSpace = "";
  219. credentialString = "";
  220. return "";
  221. }
  222. return ("Logon successful.");
  223. }
  224. /**
  225. * Use this Java method to log off.
  226. *
  227. * @param connection
  228. * Connection to Server
  229. *
  230. * @return Returns a string containing status information.
  231. */
  232. public String logoff(CRNConnect connection)
  233. {
  234. nameSpace = "";
  235. userID = "";
  236. password = "";
  237. try
  238. {
  239. // sn_dg_sdk_method_contentManagerService_logoff_start_1
  240. connection.getCMService().logoff();
  241. // sn_dg_sdk_method_contentManagerService_logoff_end_1
  242. }
  243. catch (java.rmi.RemoteException remoteEx)
  244. {
  245. return (remoteEx.toString());
  246. }
  247. return ("Logoff successful.");
  248. }
  249. public static boolean loggedIn(CRNConnect connection)
  250. {
  251. return (userID != "" || userID == null || doTestForAnonymous(connection));
  252. }
  253. /**
  254. * Use this Java method to get account information for the current user.
  255. *
  256. * @param connection
  257. * Specifies the object that provides the connection to
  258. * the server.
  259. * @return Returns a string containing user information.
  260. */
  261. public String logonInfo(CRNConnect connection)
  262. {
  263. String output = new String();
  264. if (connection != null)
  265. {
  266. Account myAccount = getLogonAccount(connection);
  267. if (myAccount == null)
  268. {
  269. output = "You are not currently logged on.\n";
  270. return output;
  271. }
  272. String logonName = myAccount.getDefaultName().getValue();
  273. if (logonName == null)
  274. {
  275. output = "You are not currently logged on.\n";
  276. return output;
  277. }
  278. output =
  279. output.concat(
  280. "You are currently logged on as: " + logonName + "\n");
  281. if (myAccount.getUserName().getValue() != null)
  282. {
  283. output =
  284. output.concat(
  285. "Your user name is: "
  286. + myAccount.getUserName().getValue() + "\n");
  287. }
  288. output =
  289. output.concat(
  290. "Your searchPath is: "
  291. + myAccount.getSearchPath().getValue() + "\n");
  292. if (myAccount.getNotificationEMail().getValue() == null)
  293. {
  294. output =
  295. output.concat("You do not have a notification email address defined.\n");
  296. }
  297. else
  298. {
  299. // sn_dg_sdk_task_querycontent_start_2
  300. output =
  301. output.concat(
  302. "Your alert email address is: "
  303. + myAccount.getNotificationEMail().getValue());
  304. // sn_dg_sdk_task_querycontent_end_2
  305. }
  306. }
  307. else
  308. {
  309. output =
  310. output.concat("Invalid parameter passed to function logon.");
  311. }
  312. return output;
  313. }
  314. /**
  315. * Use this Java method to find out if Anonymous access is enabled
  316. *
  317. * @param connection
  318. * Connection to Server
  319. *
  320. * @return Returns a boolean indicating whether or not
  321. * Anonymous access is enabled (true) or disabled (false).
  322. */
  323. public static boolean doTestForAnonymous(CRNConnect connection)
  324. {
  325. boolean doTestForAnonymous = false;
  326. try
  327. {
  328. BaseClass bc[] =
  329. connection.getCMService().query(
  330. new SearchPathMultipleObject("/content"),
  331. new PropEnum[] {},
  332. new Sort[] {},
  333. new QueryOptions());
  334. if (bc != null)
  335. {
  336. doTestForAnonymous = true;
  337. }
  338. else
  339. {
  340. doTestForAnonymous = false;
  341. }
  342. }
  343. catch (java.rmi.RemoteException remoteEx)
  344. {
  345. System.out.println("");
  346. //Ignore this, it means that Anonymous access is denied...
  347. }
  348. return doTestForAnonymous;
  349. }
  350. // Get account information for the current user.
  351. public static Account getLogonAccount(CRNConnect connection)
  352. {
  353. // sn_dg_sdk_task_querycontent_start_0
  354. PropEnum props[] =
  355. new PropEnum[] {PropEnum.searchPath, PropEnum.defaultName, PropEnum.policies, PropEnum.userName, PropEnum.notificationEMail };
  356. Account myAccount = null;
  357. // sn_dg_sdk_task_querycontent_end_0
  358. if (connection.getCMService() == null)
  359. {
  360. System.out.println("Invalid parameter passed to function logon.");
  361. return myAccount;
  362. }
  363. try
  364. {
  365. // sn_dg_sdk_task_querycontent_start_1
  366. BaseClass bc[] =
  367. connection.getCMService().query(new SearchPathMultipleObject("~"), props, new Sort[] {}, new QueryOptions());
  368. if ((bc != null) && (bc.length > 0))
  369. {
  370. for (int i = 0; i < bc.length; i++)
  371. {
  372. myAccount = (Account)bc[i];
  373. }
  374. }
  375. // sn_dg_sdk_task_querycontent_end_1
  376. }
  377. catch (java.rmi.RemoteException remoteEx)
  378. {
  379. //An exception here likely indicates the client is not currently
  380. //logged in, so the query fails.
  381. System.out.println(
  382. "Caught RemoteException:\n" + remoteEx.getMessage());
  383. }
  384. return myAccount;
  385. }
  386. /**
  387. * Use this Java method to retrieve the available namespaces.
  388. *
  389. * @param connection
  390. * Connection to Server
  391. *
  392. * @return Returns an array of strings containing all available namespaces.
  393. */
  394. public String[] getNamespaces(CRNConnect connection)
  395. //throws Exception
  396. {
  397. // This call to the query method provides the logon information.
  398. // The authentication will fail and the SOAP:Header
  399. // will contain all the information required to log on.
  400. try
  401. {
  402. connection.getCMService().query(
  403. new SearchPathMultipleObject("/content"),
  404. new PropEnum[] {},
  405. new Sort[] {},
  406. new QueryOptions());
  407. }
  408. catch (java.rmi.RemoteException remoteEx)
  409. {
  410. // Ignore this exception because the query was expected to fail.
  411. }
  412. // Retrieve the biBusHeader SOAP:Header that contains
  413. // the logon information.
  414. BiBusHeader bibus =
  415. BIBusHeaderHelper.getHeaderObject(((Stub)connection.getCMService()).getResponseHeader("http://developer.cognos.com/schemas/bibus/3/", "biBusHeader"));
  416. // Initialize the return values container
  417. String[] namespaces = new String[] {};
  418. // Look in the displayObjects for namespace prompt options and capture
  419. // all the namespaces defined there.
  420. try
  421. {
  422. DisplayObject[] dob =
  423. bibus
  424. .getCAM()
  425. .getException()
  426. .getPromptInfo()
  427. .getDisplayObjects();
  428. for (int i = 0; i < dob.length; i++)
  429. {
  430. if (dob[i].getName().equalsIgnoreCase("CAMNamespace"))
  431. {
  432. PromptOption[] pop = dob[i].getPromptOptions();
  433. // Check to see how many namespaces exist.
  434. // If there is an array, there are many namespaces.
  435. // Otherwise there is only one namespace.
  436. if (pop != null)
  437. {
  438. namespaces = new String[pop.length * 2];
  439. for (int j = 0, k = 0; k < pop.length; j++, k++)
  440. {
  441. namespaces[j] = pop[k].getValue();
  442. namespaces[++j] = pop[k].getId();
  443. }
  444. }
  445. else // There is only one namespace.
  446. {
  447. namespaces = new String[2];
  448. //check the next display object for the name, if there is one
  449. if((i+1)<dob.length)
  450. {
  451. if (dob[i+1].getName().equalsIgnoreCase(("CAMNamespaceDisplayName")))
  452. {
  453. namespaces[0] = dob[i+1].getValue();
  454. namespaces[1] = dob[i].getValue();
  455. }
  456. else
  457. {
  458. //re-use namespace id in place of name
  459. namespaces[0] = dob[i].getValue();
  460. namespaces[1] = dob[i].getValue();
  461. }
  462. }
  463. else
  464. {
  465. //re-use namespace id in place of name
  466. namespaces[0] = dob[i].getValue();
  467. namespaces[1] = dob[i].getValue();
  468. }
  469. }
  470. }
  471. }
  472. }
  473. catch (NullPointerException npe)
  474. {
  475. // This exception may occur if we have a malformed header.
  476. // If this happens, return an empty array.
  477. namespaces = null;
  478. }
  479. // Clear the header so no information from this call remains.
  480. ((Stub)connection.getCMService()).clearHeaders();
  481. return namespaces;
  482. }
  483. protected JPanel createButtonPanel()
  484. {
  485. JPanel panel = new JPanel();
  486. JButton logonButton = new JButton("Logon");
  487. JButton cancelButton = new JButton("Cancel");
  488. logonButton.setActionCommand(logon);
  489. cancelButton.setActionCommand(cancel);
  490. logonButton.addActionListener(this);
  491. cancelButton.addActionListener(this);
  492. panel.add(logonButton, BorderLayout.WEST);
  493. panel.add(cancelButton, BorderLayout.EAST);
  494. return panel;
  495. }
  496. public void actionPerformed(ActionEvent event)
  497. {
  498. String cmd = event.getActionCommand();
  499. if (logon.equals(cmd))
  500. {
  501. userID = new String(userNameField.getText());
  502. password = new String(passwordField.getPassword());
  503. passwordField.setText("");
  504. nameSpace = (String)namespaceBox.getSelectedItem();
  505. loginDialog.dispose();
  506. }
  507. else if (cancel.equals(cmd))
  508. {
  509. userID = "";
  510. userNameField.setText("");
  511. password = "";
  512. passwordField.setText("");
  513. nameSpace = "";
  514. namespaceBox.setSelectedItem(null);
  515. loginDialog.dispose();
  516. }
  517. else
  518. {
  519. loginDialog.dispose();
  520. }
  521. }
  522. public static String getCredentialString()
  523. {
  524. if (credentialString.compareTo("") == 0)
  525. {
  526. StringBuffer credentialXML = new StringBuffer();
  527. credentialXML.append("<credential>");
  528. credentialXML.append("<namespace>");
  529. credentialXML.append(anonNameSpace);
  530. credentialXML.append("</namespace>");
  531. credentialXML.append("<username>");
  532. credentialXML.append(anonUserID);
  533. credentialXML.append("</username>");
  534. credentialXML.append("</credential>");
  535. return credentialXML.toString();
  536. }
  537. return credentialString;
  538. }
  539. }