/**
   * Helper function to create the Kaltura client object once and then reuse a static instance.
   *
   * @return a singleton of <code>KalturaClient</code> used in this case
   * @throws KalturaApiException if failed to generate session
   */
  private static KalturaClient getKalturaClient() throws KalturaApiException {
    if (client != null) {
      return client;
    }

    // Set Constants
    int partnerId = testConfig.getPartnerId();
    String adminSecret = testConfig.getAdminSecret();
    String userId = testConfig.getUserId();

    // Generate configuration
    KalturaConfiguration config = new KalturaConfiguration();
    config.setEndpoint(testConfig.getServiceUrl());

    try {
      // Create the client and open session
      client = new KalturaClient(config);
      String ks = client.generateSession(adminSecret, userId, KalturaSessionType.ADMIN, partnerId);
      client.setSessionId(ks);
    } catch (Exception ex) {
      client = null;
      throw new KalturaApiException("Failed to generate session");
    }

    System.out.println("Generated KS locally: [" + client.getSessionId() + "]");
    return client;
  }
  /** uploads a video file to Kaltura and assigns it to a given Media Entry object */
  private static void uploadMediaFileAndAttachToEmptyEntry(KalturaMediaEntry entry)
      throws KalturaApiException {
    KalturaClient client = getKalturaClient();
    System.out.println("Uploading a video file...");

    // upload upload token
    KalturaUploadToken upToken = client.getUploadTokenService().add();
    KalturaUploadedFileTokenResource fileTokenResource = new KalturaUploadedFileTokenResource();

    // Connect to media entry and update name
    fileTokenResource.token = upToken.id;
    entry = client.getMediaService().addContent(entry.id, fileTokenResource);

    // Upload actual data
    try {
      InputStream fileData = TestUtils.getTestVideo();
      int fileSize = fileData.available();

      client
          .getUploadTokenService()
          .upload(upToken.id, fileData, testConfig.getUploadVideo(), fileSize);

      System.out.println("Uploaded a new Video file to entry: " + entry.id);
    } catch (FileNotFoundException e) {
      System.out.println("Failed to open test video file");
    } catch (IOException e) {
      System.out.println("Failed to read test video file");
    }
  }
  /** uploads a video file to Kaltura and assigns it to a given Media Entry object */
  private static void uploadMediaFileByChunkAndAttachToEmptyEntry(KalturaMediaEntry entry)
      throws KalturaApiException {
    KalturaClient client = getKalturaClient();
    System.out.println("Uploading a video file...");

    // upload upload token
    KalturaUploadToken upToken = client.getUploadTokenService().add();

    try {
      String TAG = "test-upload-large-files";
      UploadToken uploadToken = new UploadToken(TAG, 5, client);
      String path = testConfig.getUploadVideo();
      System.out.println("Trying to upload " + path);
      uploadToken.setStartUpload(true);
      uploadToken.uploadMediaFileAndAttachToEmptyEntry(TAG, entry, path);

      System.out.println("Uploaded a new Video file to entry: " + entry.id);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }