reportrunner.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /**
  2. Licensed Materials - Property of IBM
  3. IBM Cognos Products: DOCS
  4. (C) Copyright IBM Corp. 2005, 2008
  5. US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with
  6. IBM Corp.
  7. */
  8. /**
  9. * reportrunner.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. */
  15. import java.io.FileOutputStream;
  16. import java.net.MalformedURLException;
  17. import java.net.URL;
  18. import javax.xml.rpc.ServiceException;
  19. import org.apache.axis.AxisFault;
  20. import org.apache.axis.client.Stub;
  21. import org.apache.axis.message.SOAPHeaderElement;
  22. import com.cognos.developer.schemas.bibus._3.AsynchDetailReportOutput;
  23. import com.cognos.developer.schemas.bibus._3.AsynchOptionEnum;
  24. import com.cognos.developer.schemas.bibus._3.AsynchOptionInt;
  25. import com.cognos.developer.schemas.bibus._3.AsynchReply;
  26. import com.cognos.developer.schemas.bibus._3.AsynchReplyStatusEnum;
  27. import com.cognos.developer.schemas.bibus._3.BiBusHeader;
  28. import com.cognos.developer.schemas.bibus._3.CAM;
  29. import com.cognos.developer.schemas.bibus._3.FormFieldVar;
  30. import com.cognos.developer.schemas.bibus._3.FormatEnum;
  31. import com.cognos.developer.schemas.bibus._3.HdrSession;
  32. import com.cognos.developer.schemas.bibus._3.OutputEncapsulationEnum;
  33. import com.cognos.developer.schemas.bibus._3.ParameterValue;
  34. import com.cognos.developer.schemas.bibus._3.Option;
  35. import com.cognos.developer.schemas.bibus._3.ReportService_PortType;
  36. import com.cognos.developer.schemas.bibus._3.ReportService_ServiceLocator;
  37. import com.cognos.developer.schemas.bibus._3.RunOptionBoolean;
  38. import com.cognos.developer.schemas.bibus._3.RunOptionEnum;
  39. import com.cognos.developer.schemas.bibus._3.RunOptionOutputEncapsulation;
  40. import com.cognos.developer.schemas.bibus._3.RunOptionStringArray;
  41. import com.cognos.developer.schemas.bibus._3.SearchPathSingleObject;
  42. /**
  43. * Run a report from the samples deployment archive, and save its output
  44. * as HTML.
  45. *
  46. * Note that this application does no error handling; errors will cause
  47. * ugly exception stack traces to appear on the console, and the
  48. * application will exit ungracefully.
  49. */
  50. public class reportrunner
  51. {
  52. /**
  53. * This function will prepare the biBusHeader.
  54. *
  55. * @param repService An initialized report service object.
  56. * @param user Log in as this User ID in the specified namespace.
  57. * @param pass Password for user in the specified namespace.
  58. * @param name The security namespace to log on to.
  59. */
  60. static void setUpHeader(
  61. ReportService_PortType repService,
  62. String user,
  63. String pass,
  64. String name)
  65. {
  66. // Scrub the header to remove the conversation context.
  67. BiBusHeader bibus =
  68. BIBusHeaderHelper.getHeaderObject(((Stub)repService).getResponseHeader("", "biBusHeader"));
  69. if (bibus != null)
  70. {
  71. if (bibus.getTracking() != null)
  72. {
  73. if (bibus.getTracking().getConversationContext() != null)
  74. {
  75. bibus.getTracking().setConversationContext(null);
  76. }
  77. }
  78. return;
  79. }
  80. // Set up a new biBusHeader for the "logon" action.
  81. bibus = new BiBusHeader();
  82. bibus.setCAM(new CAM());
  83. bibus.getCAM().setAction("logonAs");
  84. bibus.setHdrSession(new HdrSession());
  85. FormFieldVar ffs[] = new FormFieldVar[3];
  86. ffs[0] = new FormFieldVar();
  87. ffs[0].setName("CAMUsername");
  88. ffs[0].setValue(user);
  89. ffs[0].setFormat(FormatEnum.not_encrypted);
  90. ffs[1] = new FormFieldVar();
  91. ffs[1].setName("CAMPassword");
  92. ffs[1].setValue(pass);
  93. ffs[1].setFormat(FormatEnum.not_encrypted);
  94. ffs[2] = new FormFieldVar();
  95. ffs[2].setName("CAMNamespace");
  96. ffs[2].setValue(name);
  97. ffs[2].setFormat(FormatEnum.not_encrypted);
  98. bibus.getHdrSession().setFormFieldVars(ffs);
  99. ((Stub)repService).setHeader("http://developer.cognos.com/schemas/bibus/3/", "biBusHeader", bibus);
  100. }
  101. static void showUsage()
  102. {
  103. String usage = "";
  104. usage += "Run a report and save the output as HTML.\n\n";
  105. usage += "usage:\n\n";
  106. usage += "-host hostName\n\tHost name of an IBM Cognos server.\n";
  107. usage += "\t Default: localhost\n";
  108. usage += "-port portNumber\n\tPort number of the server.\n";
  109. usage += "\t Default: 9300\n";
  110. usage
  111. += "-report searchPath\n\tSearch path to a report.\n";
  112. usage += "\t Default: "
  113. + "/content/folder[@name='Samples']/folder[@name='Models']/package[@name='GO Data Warehouse (query)']"
  114. + "/folder[@name='SDK Report Samples']"
  115. + "/report[@name='Product Introduction List']\n";
  116. usage
  117. += "-output outputPath\n\tFile name for the output HTML document.\n";
  118. usage += "\t Default: reportrunner.html\n";
  119. usage += "-user userName\n\tUser to log on as. "
  120. + "Must exist in 'userNamespace'.\n";
  121. usage += "\t Default: none\n";
  122. usage += "-password userPassword\n\tPassword for 'userName' ";
  123. usage += "in 'userNamespace'.\n";
  124. usage += "\t Default: none\n";
  125. usage
  126. += "-namespace userNamespace\n\tSecurity namespace to log on to.\n";
  127. usage += "\t Default: none\n";
  128. System.out.println(usage);
  129. }
  130. /**
  131. * Replace all occurrences of "pattern" in "str" with "replace".
  132. *
  133. * @param str The source string.
  134. * @param pattern The pattern to search for.
  135. * @param replace The replacement for pattern.
  136. *
  137. * @return Identical to the original string with all instances
  138. * of "pattern" replaced by "replace"
  139. */
  140. public static String replaceSubstring(
  141. String str,
  142. String pattern,
  143. String replace)
  144. {
  145. int strLen = str.length();
  146. int patternLen = pattern.length();
  147. int start = 0, end = 0;
  148. StringBuffer result = new StringBuffer(strLen);
  149. char[] chars = new char[strLen];
  150. while ((end = str.indexOf(pattern, start)) >= 0)
  151. {
  152. str.getChars(start, end, chars, 0);
  153. result.append(chars, 0, end - start).append(replace);
  154. start = end + patternLen;
  155. }
  156. str.getChars(start, strLen, chars, 0);
  157. result.append(chars, 0, strLen - start);
  158. return result.toString();
  159. }
  160. /*
  161. * The main entry point for the application.
  162. */
  163. public static void main(String[] args)
  164. {
  165. // Change this URL if your server is in another location or a
  166. // different port.
  167. String serverHost = "localhost";
  168. String serverPort = "9300";
  169. // Change this search path if you want to run a different report.
  170. String reportPath =
  171. "/content/folder[@name='Samples']/folder[@name='Models']/package[@name='GO Data Warehouse (query)']"
  172. + "/folder[@name='SDK Report Samples']"
  173. + "/report[@name='Product Introduction List']";
  174. // Output filename
  175. String outputPath = "reportrunner.html";
  176. // Specify a user to log in as.
  177. String userName = "";
  178. String userPassword = "";
  179. String userNamespace = "";
  180. // Parse the command-line arguments.
  181. for (int idx = 0; idx < args.length; idx++)
  182. {
  183. if (args[idx].compareTo("-host") == 0)
  184. {
  185. serverHost = args[idx + 1];
  186. }
  187. else if (args[idx].compareTo("-port") == 0)
  188. {
  189. serverPort = args[idx + 1];
  190. }
  191. else if (args[idx].compareTo("-report") == 0)
  192. {
  193. reportPath = args[idx + 1];
  194. }
  195. else if (args[idx].compareTo("-output") == 0)
  196. {
  197. outputPath = args[idx + 1];
  198. }
  199. else if (args[idx].compareTo("-user") == 0)
  200. {
  201. userName = args[idx + 1];
  202. }
  203. else if (args[idx].compareTo("-password") == 0)
  204. {
  205. userPassword = args[idx + 1];
  206. }
  207. else if (args[idx].compareTo("-namespace") == 0)
  208. {
  209. userNamespace = args[idx + 1];
  210. }
  211. else
  212. {
  213. System.out.println("Unknown argument: " + args[idx]);
  214. showUsage();
  215. System.exit(0);
  216. }
  217. idx++;
  218. }
  219. // Create a connection to a report server.
  220. String Cognos_URL =
  221. "http://"
  222. + serverHost
  223. + ":"
  224. + serverPort
  225. + "/p2pd/servlet/dispatch";
  226. System.out.println(
  227. "Creating connection to "
  228. + serverHost
  229. + ":"
  230. + serverPort
  231. + "...");
  232. System.out.println("Server URL: " + Cognos_URL);
  233. ReportService_ServiceLocator reportServiceLocator =
  234. new ReportService_ServiceLocator();
  235. ReportService_PortType repService = null;
  236. try
  237. {
  238. repService = reportServiceLocator.getreportService(new URL(Cognos_URL));
  239. }
  240. catch (MalformedURLException ex)
  241. {
  242. System.out.println(
  243. "Caught a MalformedURLException:\n" + ex.getMessage());
  244. System.out.println("Server URL was: " + Cognos_URL);
  245. System.exit(-1);
  246. }
  247. catch (ServiceException ex)
  248. {
  249. System.out.println("Caught a ServiceException: " + ex.getMessage());
  250. ex.printStackTrace();
  251. System.exit(-1);
  252. }
  253. System.out.println("... done.");
  254. // Set up the biBusHeader for a logon.
  255. if ((userName.length() > 0)
  256. && (userPassword.length() > 0)
  257. && (userNamespace.length() > 0))
  258. {
  259. System.out.println(
  260. "Logging on as "
  261. + userName
  262. + " in the "
  263. + userNamespace
  264. + " namespace.");
  265. setUpHeader(repService, userName, userPassword, userNamespace);
  266. }
  267. // Set up the report parameters.
  268. ParameterValue parameters[] = new ParameterValue[] {};
  269. Option runOptions[] = new Option[5];
  270. RunOptionBoolean saveOutput = new RunOptionBoolean();
  271. saveOutput.setName(RunOptionEnum.saveOutput);
  272. saveOutput.setValue(false);
  273. runOptions[0] = saveOutput;
  274. // TODO: Output format should be specified on the command-line
  275. RunOptionStringArray outputFormat = new RunOptionStringArray();
  276. outputFormat.setName(RunOptionEnum.outputFormat);
  277. outputFormat.setValue(new String[] { "HTML" });
  278. runOptions[1] = outputFormat;
  279. RunOptionOutputEncapsulation outputEncapsulation =
  280. new RunOptionOutputEncapsulation();
  281. outputEncapsulation.setName(RunOptionEnum.outputEncapsulation);
  282. outputEncapsulation.setValue(OutputEncapsulationEnum.none);
  283. runOptions[2] = outputEncapsulation;
  284. AsynchOptionInt primaryWait = new AsynchOptionInt();
  285. primaryWait.setName(AsynchOptionEnum.primaryWaitThreshold);
  286. primaryWait.setValue(0);
  287. runOptions[3] = primaryWait;
  288. AsynchOptionInt secondaryWait = new AsynchOptionInt();
  289. secondaryWait.setName(AsynchOptionEnum.secondaryWaitThreshold);
  290. secondaryWait.setValue(0);
  291. runOptions[4] = secondaryWait;
  292. // Now, run the report.
  293. try
  294. {
  295. System.out.println("Running the report...");
  296. System.out.println("Report search path is:");
  297. System.out.println(reportPath);
  298. AsynchReply res =
  299. repService.run(new SearchPathSingleObject(reportPath), parameters, runOptions);
  300. System.out.println("... done.");
  301. // The report is finished, let's fetch the results and save them to
  302. // a file.
  303. if (res.getStatus() == AsynchReplyStatusEnum.complete)
  304. {
  305. AsynchDetailReportOutput reportOutput = null;
  306. for (int i = 0; i < res.getDetails().length; i++)
  307. {
  308. if (res.getDetails()[i]
  309. instanceof AsynchDetailReportOutput)
  310. {
  311. reportOutput =
  312. (AsynchDetailReportOutput)res.getDetails()[i];
  313. break;
  314. }
  315. }
  316. String[] data = reportOutput.getOutputPages();
  317. System.out.println(
  318. "Writing report output to " + outputPath + "...");
  319. FileOutputStream fs = new FileOutputStream(outputPath);
  320. // URIs in the output are relative on the server; we need to
  321. // make them absolute so they reference the server properly.
  322. String uriFix =
  323. "http://" + serverHost + ":" + serverPort + "/ibmcognos/";
  324. for (int idx = 0; idx < data.length; idx++)
  325. {
  326. String fixed_hunk =
  327. replaceSubstring(data[idx], "../", uriFix);
  328. fs.write(fixed_hunk.getBytes());
  329. }
  330. fs.close();
  331. System.out.println("... done.");
  332. }
  333. }
  334. catch (AxisFault ex)
  335. {
  336. String ex_str = CognosBIException.convertToString(ex);
  337. System.out.println("SOAP exception!\n");
  338. System.out.println(ex_str);
  339. }
  340. catch (Exception ex)
  341. {
  342. System.out.println("Unhandled exception!");
  343. System.out.println("Message: \n" + ex.getMessage());
  344. System.out.println("Stack trace:");
  345. ex.printStackTrace();
  346. }
  347. }
  348. }