AppendFileExample.java 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import java.io.IOException;
  2. import com.ibm.bi.platform.modeling.sdk.examples.FileImportHelper;
  3. import com.ibm.bi.platform.modeling.sdk.examples.internal.ModelingHelper;
  4. import com.ibm.bi.platform.modeling.sdk.examples.internal.UnexpectedHTTPResponseException;
  5. import com.ibm.json.java.JSONObject;
  6. /*
  7. * IBM Confidential
  8. *
  9. * OCO Source Materials
  10. *
  11. * IBM Cognos Products: Moser
  12. *
  13. * (C) Copyright IBM Corp. 2020
  14. *
  15. * The source code for this program is not published or otherwise
  16. * divested of its trade secrets, irrespective of what has been
  17. * deposited with the U.S. Copyright Office.
  18. */
  19. /**
  20. * Append more rows to uploaded file
  21. *
  22. */
  23. public class AppendFileExample {
  24. /**
  25. * @param args
  26. * args[0] ca server url origin, e.g. http://localhost:9300/
  27. * args[1] login credentials relative file path
  28. * args[2] store id of the file to be appended to
  29. * args[3] directory containing file relative to current , e.g. "resources/"
  30. * args[4] file to upload name, e.g. "pizza.xlsx"
  31. */
  32. public static void main(String[] args) {
  33. if (args.length < 5) {
  34. System.err.println("Correct arguments: origin fileWithCredentials fileStoreId sourceDir filename");
  35. System.err.println("origin - ca server url origin, e.g. http://localhost:9300/");
  36. System.err.println("fileWithCredentials - login credentials relative file path");
  37. System.err.println("fileStoreId - store id of the file to be appended to");
  38. System.err.println("sourceDir - directory containing file relative to current , e.g. \"resources\"");
  39. System.err.println("filename - file to upload name, e.g. \"pizza.xlsx\"");
  40. return;
  41. }
  42. String origin = args[0];
  43. try {
  44. FileImportHelper helper = new FileImportHelper(origin);
  45. String credentials = ModelingHelper.stringFromFile(args[1]);
  46. helper.authenticate(credentials);
  47. String uploadedFileId = args[2], dir = args[3], appendFileName = args[4];
  48. // Post the file.
  49. String response = helper.appendFile(uploadedFileId, dir, appendFileName);
  50. JSONObject json = JSONObject.parse(response);
  51. String uploadedId = (String) json.get("id");
  52. System.out.println("File is uploaded : " + uploadedId);
  53. } catch (UnexpectedHTTPResponseException e) {
  54. try {
  55. System.out.println("Response : " + ModelingHelper.httpEntityToString(e.getResponse().getEntity()));
  56. } catch (IOException io) {
  57. }
  58. e.printStackTrace();
  59. } catch (IOException e) {
  60. e.printStackTrace();
  61. }
  62. }
  63. }