@Test
  public void testFileHistoryInitEmpty() {
    PartialFileHistory fileHistory = new PartialFileHistory(FileHistoryId.parseFileId("1234"));

    assertEquals(FileHistoryId.parseFileId("1234"), fileHistory.getFileHistoryId());
    assertNotSame(FileHistoryId.parseFileId("9999"), fileHistory.getFileHistoryId());
    assertNull(fileHistory.getLastVersion());
    assertNotNull(fileHistory.toString());
    assertTrue(fileHistory.toString().contains("1234"));
    assertNotNull(fileHistory.getFileVersions());
    assertEquals(0, fileHistory.getFileVersions().size());
  }
  @Test
  public void testFileHistoryClone() {
    FileVersion fileVersion1 = new FileVersion();
    fileVersion1.setVersion(1L);
    fileVersion1.setPath("/somepath");

    FileVersion fileVersion2 = new FileVersion();
    fileVersion2.setVersion(2L);
    fileVersion2.setPath("/somepath");

    PartialFileHistory fileHistory = new PartialFileHistory(FileHistoryId.parseFileId("1234"));
    fileHistory.addFileVersion(fileVersion1);
    fileHistory.addFileVersion(fileVersion2);

    PartialFileHistory fileHistoryClone = fileHistory.clone();

    assertEquals(fileHistory, fileHistoryClone);
    assertEquals(fileHistory.getFileVersions().size(), fileHistoryClone.getFileVersions().size());
    assertEquals(fileHistory.getFileVersions(), fileHistoryClone.getFileVersions());

    FileVersion fileVersion3 = new FileVersion();
    fileVersion3.setVersion(3L);
    fileVersion3.setPath("/somepath");

    fileHistoryClone.addFileVersion(fileVersion3);
    assertEquals(
        fileHistory.getFileVersions().size() + 1, fileHistoryClone.getFileVersions().size());
  }
Example #3
0
  private void handleGetFileRequest(GetFileRequest fileRequest) {
    try {
      FileHistoryId fileHistoryId = FileHistoryId.parseFileId(fileRequest.getFileHistoryId());
      long version = fileRequest.getVersion();

      FileVersion fileVersion = localDatabase.getFileVersion(fileHistoryId, version);
      FileContent fileContent = localDatabase.getFileContent(fileVersion.getChecksum(), true);
      Map<ChunkChecksum, MultiChunkId> multiChunks =
          localDatabase.getMultiChunkIdsByChecksums(fileContent.getChunks());

      TransferManager transferManager =
          config.getTransferPlugin().createTransferManager(config.getConnection());
      Downloader downloader = new Downloader(config, transferManager);
      Assembler assembler = new Assembler(config, localDatabase);

      downloader.downloadAndDecryptMultiChunks(new HashSet<MultiChunkId>(multiChunks.values()));

      File tempFile = assembler.assembleToCache(fileVersion);
      String tempFileToken = StringUtil.toHex(ObjectId.secureRandomBytes(40));

      GetFileResponse fileResponse =
          new GetFileResponse(fileRequest.getId(), fileRequest.getRoot(), tempFileToken);
      GetFileResponseInternal fileResponseInternal =
          new GetFileResponseInternal(fileResponse, tempFile);

      eventBus.post(fileResponseInternal);
    } catch (Exception e) {
      logger.log(Level.WARNING, "Cannot reassemble file.", e);
      eventBus.post(new BadRequestResponse(fileRequest.getId(), "Cannot reassemble file."));
    }
  }
Example #4
0
 private PartialFileHistory createNewFileHistory(PartialFileHistory lastFileHistory) {
   if (lastFileHistory == null) {
     FileHistoryId newFileHistoryId = FileHistoryId.secureRandomFileId();
     return new PartialFileHistory(newFileHistoryId);
   } else {
     return new PartialFileHistory(lastFileHistory.getFileHistoryId());
   }
 }
Example #5
0
  private void handleGetFileHistoryRequest(GetFileHistoryRequest fileHistoryRequest) {
    FileHistoryId fileHistoryId = FileHistoryId.parseFileId(fileHistoryRequest.getFileHistoryId());
    List<FileVersion> fileHistory = localDatabase.getFileHistory(fileHistoryId);
    GetFileHistoryResponse fileHistoryRespose =
        new GetFileHistoryResponse(
            fileHistoryRequest.getId(), fileHistoryRequest.getRoot(), fileHistory);

    eventBus.post(fileHistoryRespose);
  }
Example #6
0
  private void handleRestoreRequest(RestoreRequest restoreRequest) {
    RestoreOperationOptions restoreOptions = new RestoreOperationOptions();

    restoreOptions.setFileHistoryId(FileHistoryId.parseFileId(restoreRequest.getFileHistoryId()));
    restoreOptions.setFileVersion(restoreRequest.getVersion());

    try {
      RestoreOperationResult restoreResult = new RestoreOperation(config, restoreOptions).execute();

      RestoreResponse restoreResponse =
          new RestoreResponse(restoreRequest.getId(), restoreResult.getTargetFile());
      eventBus.post(restoreResponse);
    } catch (Exception e) {
      logger.log(Level.WARNING, "BadRequestResponse: Cannot restore file.");
      eventBus.post(new BadRequestResponse(restoreRequest.getId(), "Cannot restore file."));
    }
  }
  @Test
  public void testFileHistoryIdRandom() {
    FileHistoryId secureRandomFileId1 = FileHistoryId.secureRandomFileId();
    FileHistoryId secureRandomFileId2 = FileHistoryId.secureRandomFileId();

    assertNotNull(secureRandomFileId1);
    assertEquals(20 * 2, secureRandomFileId1.toString().length());

    assertNotSame(secureRandomFileId1, secureRandomFileId2);
    assertNotNull(secureRandomFileId2);
    assertEquals(20 * 2, secureRandomFileId2.toString().length());
  }
  @Test
  public void testFileHistoryGetVersionsNonEmpty() {
    FileVersion fileVersion = new FileVersion();
    fileVersion.setVersion(5L);
    fileVersion.setPath("/somepath");

    PartialFileHistory fileHistory = new PartialFileHistory(FileHistoryId.parseFileId("1234"));
    fileHistory.addFileVersion(fileVersion);

    assertNotNull(fileHistory.getLastVersion());
    assertNotNull(fileHistory.getFileVersions());
    assertEquals(1, fileHistory.getFileVersions().size());
    assertEquals(fileVersion, fileHistory.getLastVersion());
    assertNull(fileHistory.getFileVersions().get(1L));
    assertNull(fileHistory.getFileVersion(1L));
    assertEquals(fileVersion, fileHistory.getFileVersions().get(5L));
    assertEquals(fileVersion, fileHistory.getFileVersion(5L));
  }
  private void sendLsFolderRequest(String root, FileHistoryId fileHistoryId) {
    // Create list request
    LsOperationOptions lsOptions = new LsOperationOptions();

    lsOptions.setPathExpression(fileHistoryId.toString());
    lsOptions.setFileHistoryId(true);
    lsOptions.setRecursive(false);
    lsOptions.setDeleted(true);
    lsOptions.setFetchHistories(true);
    lsOptions.setFileTypes(Sets.newHashSet(FileType.FILE, FileType.SYMLINK));

    LsFolderRequest lsRequest = new LsFolderRequest();

    lsRequest.setRoot(root);
    lsRequest.setOptions(lsOptions);

    logger.log(
        Level.INFO,
        "Detail panel: Sending LsRequest with ID #" + lsRequest.getId() + " for " + root + " ...");

    // Send request
    pendingLsFolderRequests.put(lsRequest.getId(), lsRequest);
    eventBus.post(lsRequest);
  }
Example #10
0
 @Test(expected = IllegalArgumentException.class)
 public void testFileHistoryAddFileVersionInvalidVersionNull() {
   PartialFileHistory fileHistory = new PartialFileHistory(FileHistoryId.parseFileId("1234"));
   fileHistory.addFileVersion(new FileVersion());
 }
Example #11
0
 @Test(expected = IllegalArgumentException.class)
 public void testFileHistoryIdInvalidNull() {
   FileHistoryId.parseFileId(null);
 }
Example #12
0
 @Test
 public void testFileHistoryIdParsedOk() {
   assertEquals("12", FileHistoryId.parseFileId("12").toString());
 }