ReplaceFileExample.java 2.1 KB

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