/** * Return sif the node is shared or has any children that are shared. * * @return if the node is shared or has a shared sub-section */ public boolean isSharedOrHasSharedChildren() { if (isShared()) { // this is a shared file or a shared (sub) folder return true; } if (this instanceof FileIndex) { // is not shared and is of type 'file' return false; } else { // is of type 'folder', check all subfolders List<Index> children = getIndexList(this); for (Index child : children) { if (child.isFolder()) { FolderIndex subfolder = (FolderIndex) child; if (subfolder.getSharedFlag()) { return true; } } } } // no case above matches return false; }
@Test public void testPermissions() { Assert.assertTrue(root.getCalculatedUserList().contains(userId)); Assert.assertEquals(1, root.getCalculatedUserList().size()); // add permission to sub-folder dir1.share( EncryptionUtil.generateRSAKeyPair(H2HConstants.KEYLENGTH_META_FILE), new UserPermission("UserB", PermissionType.READ)); // check the sub-folder and the sub-files permission Assert.assertEquals(2, dir1.getCalculatedUserList().size()); Assert.assertEquals(2, child3.getCalculatedUserList().size()); Assert.assertEquals(2, dir2.getCalculatedUserList().size()); Assert.assertFalse(dir1.canWrite("UserB")); Assert.assertFalse(dir2.canWrite("UserB")); // validate that the root still has only one user Assert.assertTrue(root.getCalculatedUserList().contains(userId)); Assert.assertEquals(1, root.getCalculatedUserList().size()); // add a third permission to the dir1 dir1.addUserPermissions(new UserPermission("UserC", PermissionType.WRITE)); // check again Assert.assertEquals(3, dir1.getCalculatedUserList().size()); Assert.assertEquals(3, child3.getCalculatedUserList().size()); Assert.assertEquals(3, dir2.getCalculatedUserList().size()); Assert.assertTrue(dir1.canWrite("UserC")); Assert.assertTrue(dir2.canWrite("UserC")); }
@Override public void run() { logger.debug("Update file notification message received."); H2HSession session; try { session = networkManager.getSession(); } catch (NoSessionException e) { logger.error("No user seems to be logged in."); return; } UserProfileManager profileManager = session.getProfileManager(); UserProfile userProfile; try { userProfile = profileManager.readUserProfile(); } catch (GetFailedException e) { logger.error("Couldn't load user profile.", e); return; } Index updatedFile = userProfile.getFileById(fileKey); if (updatedFile == null) { logger.error("Got notified about a file we don't know."); return; } // trigger event getEventBus() .publish( new FileUpdateEvent(updatedFile.asFile(session.getRootFile()), updatedFile.isFile())); }
@Test public void testFullPath() { Assert.assertEquals("", root.getFullPath().toString()); Assert.assertEquals("1f1", child1.getFullPath().toString()); Assert.assertEquals("1f2", child2.getFullPath().toString()); Assert.assertEquals("1d", dir1.getFullPath().toString()); Assert.assertEquals(Paths.get("1d", "2f").toString(), child3.getFullPath().toString()); Assert.assertEquals(Paths.get("1d", "2d").toString(), dir2.getFullPath().toString()); }
/** * Walks recursively through the file tree to build, sort and return the whole file list. * * @param node The root node from which the digest is started. * @return The digest in sorted order. */ public static List<Path> getFilePathList(Index node) { List<Index> fileNodes = getIndexList(node); List<Path> digest = new ArrayList<Path>(); for (Index fileNode : fileNodes) { digest.add(fileNode.getFullPath()); } // sort by full path Collections.sort(digest); return digest; }
public static List<FolderIndex> getIndexList(Index node) { List<FolderIndex> digest = new ArrayList<FolderIndex>(); if (node.isFolder()) { // add self digest.add((FolderIndex) node); // add children for (Index child : ((FolderIndex) node).getChildren()) { if (child.isFolder()) digest.addAll(getIndexList(child)); } } return digest; }
@Test public void testShare() { // set 1d to be shared dir1.share(EncryptionUtil.generateRSAKeyPair(H2HConstants.KEYLENGTH_META_FILE)); // 1d, 2f and 2d should return to be shared, others not Assert.assertTrue(dir1.isShared()); Assert.assertTrue(dir2.isShared()); Assert.assertTrue(child3.isShared()); Assert.assertFalse(root.isShared()); Assert.assertFalse(child1.isShared()); Assert.assertFalse(child2.isShared()); // set 1d to be not shared dir1.unshare(); // root, 1f1, 1f2, 1d, 2f and 2d should return to be not shared Assert.assertFalse(root.isShared()); Assert.assertFalse(dir1.isShared()); Assert.assertFalse(dir2.isShared()); Assert.assertFalse(child1.isShared()); Assert.assertFalse(child2.isShared()); Assert.assertFalse(child3.isShared()); }
@Test public void testHasShared() { // set 2d to be shared dir2.share(EncryptionUtil.generateRSAKeyPair(H2HConstants.KEYLENGTH_META_FILE)); // root, 1d and 2d should show that they contain a shared folder Assert.assertTrue(root.isSharedOrHasSharedChildren()); Assert.assertTrue(dir1.isSharedOrHasSharedChildren()); Assert.assertTrue(dir2.isSharedOrHasSharedChildren()); Assert.assertFalse(child1.isSharedOrHasSharedChildren()); Assert.assertFalse(child2.isSharedOrHasSharedChildren()); Assert.assertFalse(child3.isSharedOrHasSharedChildren()); // set 2d to be not shared dir2.unshare(); // root, 1f1, 1f2, 1d, 2f and 2d should not contain a shared folder Assert.assertFalse(root.isSharedOrHasSharedChildren()); Assert.assertFalse(dir1.isSharedOrHasSharedChildren()); Assert.assertFalse(dir2.isSharedOrHasSharedChildren()); Assert.assertFalse(child1.isSharedOrHasSharedChildren()); Assert.assertFalse(child2.isSharedOrHasSharedChildren()); Assert.assertFalse(child3.isSharedOrHasSharedChildren()); }
@Override protected void doExecute() throws InvalidProcessStateException { FolderIndex fileNode = (FolderIndex) context.consumeIndex(); // create a subtree containing all children FolderIndex sharedNode = new FolderIndex(fileNode.getParent(), fileNode.getFileKeys(), fileNode.getName()); for (Index child : fileNode.getChildren()) { sharedNode.addChild(child); child.setParent(sharedNode); } // copy all user permissions List<UserPermission> userPermissions = fileNode.getUserPermissions(); UserPermission[] permissionArray = new UserPermission[userPermissions.size() + 1]; permissionArray = userPermissions.toArray(permissionArray); // add the own permission permissionArray[permissionArray.length - 1] = new UserPermission(userId, PermissionType.WRITE); // if the friend receives write access, he gets the protection key if (context.getPermissionType() == PermissionType.WRITE) { sharedNode.share(context.consumeNewProtectionKeys(), permissionArray); } else { sharedNode.share(null, permissionArray); } // remove the parent and only send the sub-tree sharedNode.setParent(null); // notify all users of the shared node Set<String> friends = new HashSet<String>(); friends.addAll(fileNode.getCalculatedUserList()); friends.remove(userId); // skip to notify myself logger.debug("Sending a notification message to the friend and all other sharers."); BaseNotificationMessageFactory messageFactory = new ShareFolderNotificationMessageFactory(sharedNode, context.getUserPermission()); context.provideMessageFactory(messageFactory); context.provideUsersToNotify(friends); }
/** * Walks recursively through the file tree and returns a preorder list * * @param node The root node from which the digest is started. * @return The digest in preorder */ public static List<Index> getIndexList(Index node) { List<Index> digest = new ArrayList<Index>(); // add self digest.add(node); // add children if (node.isFolder()) { FolderIndex folder = (FolderIndex) node; for (Index child : folder.getChildren()) { digest.addAll(getIndexList(child)); } } return digest; }
@Override public int compareTo(Index other) { return this.getFullPath().compareTo(other.getFullPath()); }