cmQuery.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. * cmQuery.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: Simple Content Manager query sample.
  15. */
  16. import java.net.MalformedURLException;
  17. import java.net.URL;
  18. import java.rmi.RemoteException;
  19. import javax.xml.rpc.ServiceException;
  20. import org.apache.axis.AxisFault;
  21. import org.apache.axis.client.Stub;
  22. import org.apache.axis.message.SOAPHeaderElement;
  23. import com.cognos.developer.schemas.bibus._3.BaseClass;
  24. import com.cognos.developer.schemas.bibus._3.BiBusHeader;
  25. import com.cognos.developer.schemas.bibus._3.CAM;
  26. import com.cognos.developer.schemas.bibus._3.ContentManagerService_PortType;
  27. import com.cognos.developer.schemas.bibus._3.ContentManagerService_ServiceLocator;
  28. import com.cognos.developer.schemas.bibus._3.FormFieldVar;
  29. import com.cognos.developer.schemas.bibus._3.FormatEnum;
  30. import com.cognos.developer.schemas.bibus._3.HdrSession;
  31. import com.cognos.developer.schemas.bibus._3.OrderEnum;
  32. import com.cognos.developer.schemas.bibus._3.PropEnum;
  33. import com.cognos.developer.schemas.bibus._3.QueryOptions;
  34. import com.cognos.developer.schemas.bibus._3.SearchPathMultipleObject;
  35. import com.cognos.developer.schemas.bibus._3.Sort;
  36. import com.cognos.developer.schemas.bibus._3.StringProp;
  37. import com.cognos.developer.schemas.bibus._3.TokenProp;
  38. public class cmQuery
  39. {
  40. public static void main(String[] args)
  41. {
  42. // Attempt a simple CM query.
  43. String searchPath = new String("/*");
  44. String userName = null;
  45. String userPassword = null;
  46. String userNamespace = null;
  47. // Process command-line arguments.
  48. //
  49. // cmQuery accepts these arguments:
  50. //
  51. // --search=searchPath
  52. // --uid=userName
  53. // --pwd=userPassword
  54. // --namespace=userNamespace
  55. try
  56. {
  57. for (int i = 0; i < args.length; i++)
  58. {
  59. String[] command =
  60. {
  61. args[i].substring(0, args[i].indexOf('=')),
  62. args[i].substring(args[i].indexOf('=') + 1)};
  63. if (command[0].compareTo("--search") == 0)
  64. {
  65. searchPath = command[1];
  66. }
  67. else if (command[0].compareTo("--uid") == 0)
  68. {
  69. userName = command[1];
  70. }
  71. else if (command[0].compareTo("--pwd") == 0)
  72. {
  73. userPassword = command[1];
  74. }
  75. else if (command[0].compareTo("--namespace") == 0)
  76. {
  77. userNamespace = command[1];
  78. }
  79. else
  80. {
  81. throw new Exception("Unknown argument: " + args[i]);
  82. }
  83. }
  84. }
  85. catch (Exception ex)
  86. {
  87. System.out.println(ex.getMessage());
  88. System.exit(-1);
  89. }
  90. // Concatenate the read filter to the searchPath this way we
  91. // ask CM to only return the objects we have read acces on.
  92. // searchPath += "[permission('read')]";
  93. try
  94. {
  95. // Create the connection to Content Manager Service.
  96. URL endPointUrl =
  97. new URL("http://localhost:9300/p2pd/servlet/dispatch");
  98. ContentManagerService_ServiceLocator service =
  99. new ContentManagerService_ServiceLocator();
  100. ContentManagerService_PortType cms =
  101. service.getcontentManagerService(endPointUrl);
  102. // Search properties: we need the defaultName and the searchPath.
  103. PropEnum[] properties =
  104. { PropEnum.defaultName, PropEnum.searchPath };
  105. // Sort options: ascending sort on the defaultName property.
  106. //
  107. // The cmQuery.pl sample doesn't do this, it returns the default unsorted response.
  108. Sort[] sortBy = { new Sort()};
  109. sortBy[0].setOrder(OrderEnum.ascending);
  110. sortBy[0].setPropName(PropEnum.defaultName);
  111. // Query options; use the defaults.
  112. QueryOptions options = new QueryOptions();
  113. // Add the authentication information, if any.
  114. //
  115. // Another option would be to use the logon() and logonAs() methods...
  116. CAM cam = new CAM();
  117. cam.setAction("logonAs");
  118. HdrSession header = new HdrSession();
  119. if (userName != null)
  120. {
  121. FormFieldVar[] vars = new FormFieldVar[3];
  122. vars[0] = new FormFieldVar();
  123. vars[0].setName("CAMNamespace");
  124. vars[0].setValue(userNamespace);
  125. vars[0].setFormat(FormatEnum.not_encrypted);
  126. vars[1] = new FormFieldVar();
  127. vars[1].setName("CAMUsername");
  128. vars[1].setValue(userName);
  129. vars[1].setFormat(FormatEnum.not_encrypted);
  130. vars[2] = new FormFieldVar();
  131. vars[2].setName("CAMPassword");
  132. vars[2].setValue(userPassword);
  133. vars[2].setFormat(FormatEnum.not_encrypted);
  134. header.setFormFieldVars(vars);
  135. }
  136. else
  137. {
  138. cam.setAction("logon");
  139. }
  140. BiBusHeader bibus = new BiBusHeader();
  141. bibus.setCAM(cam);
  142. bibus.setHdrSession(header);
  143. ((Stub)cms).setHeader("http://developer.cognos.com/schemas/bibus/3/", "biBusHeader", bibus);
  144. // Make the query.
  145. try
  146. {
  147. BaseClass[] results =
  148. cms.query(
  149. new SearchPathMultipleObject(searchPath),
  150. properties,
  151. sortBy,
  152. options);
  153. // Display the results.
  154. System.out.println("Results:");
  155. for (int i = 0; i < results.length; i++)
  156. {
  157. TokenProp theDefaultName = results[i].getDefaultName();
  158. StringProp theSearchPath = results[i].getSearchPath();
  159. System.out.print("\t");
  160. System.out.print(theDefaultName.getValue());
  161. System.out.print("\t");
  162. System.out.println(theSearchPath.getValue());
  163. }
  164. }
  165. catch (AxisFault ex)
  166. {
  167. // Fault details can be found via ex.getFaultDetails(),
  168. // which returns an Element array.
  169. System.out.println("SOAP Fault:");
  170. System.out.println(ex.toString());
  171. }
  172. catch (RemoteException ex)
  173. {
  174. SOAPHeaderElement theException =
  175. ((Stub)cms).getHeader(
  176. "",
  177. "biBusHeader");
  178. // You can now use theException to find out more information
  179. // about the problem.
  180. System.out.println(theException.toString());
  181. System.out.println("The request threw an RMI exception:");
  182. System.out.println(ex.getMessage());
  183. System.out.println("Stack trace:");
  184. ex.printStackTrace();
  185. }
  186. }
  187. catch (MalformedURLException ex)
  188. {
  189. System.out.println("Malformed URL exception:");
  190. System.out.println(ex.getMessage());
  191. System.out.println("Stack trace:");
  192. ex.printStackTrace();
  193. }
  194. catch (ServiceException ex)
  195. {
  196. System.out.println("Remote service exception:");
  197. System.out.println(ex.getMessage());
  198. System.out.println("Stack trace:");
  199. ex.printStackTrace();
  200. }
  201. }
  202. }