From 17f8b0841171db5e0470a8bdb736d1cdd36be70a Mon Sep 17 00:00:00 2001
From: srosse <none@none>
Date: Thu, 19 Feb 2015 13:32:27 +0100
Subject: [PATCH] OO-1444: use only the lock system built in OpenOLAT

---
 .../util/vfs/lock/VFSLockManagerImpl.java     | 49 +++++++++--------
 .../services/webdav/WebDAVCommandsTest.java   | 55 ++++++++++++++++++-
 2 files changed, 79 insertions(+), 25 deletions(-)

diff --git a/src/main/java/org/olat/core/util/vfs/lock/VFSLockManagerImpl.java b/src/main/java/org/olat/core/util/vfs/lock/VFSLockManagerImpl.java
index 842edc28687..a55f0cc5a7a 100644
--- a/src/main/java/org/olat/core/util/vfs/lock/VFSLockManagerImpl.java
+++ b/src/main/java/org/olat/core/util/vfs/lock/VFSLockManagerImpl.java
@@ -53,7 +53,6 @@ public class VFSLockManagerImpl implements VFSLockManager {
      * Key : path <br>
      * Value : LockInfo
      */
-    private Map<String,LockInfo> resourceLocks = new ConcurrentHashMap<String,LockInfo>();
     private Map<File,LockInfo> fileLocks = new ConcurrentHashMap<File,LockInfo>();
 
     /**
@@ -89,7 +88,7 @@ public class VFSLockManagerImpl implements VFSLockManager {
     	if(file != null && fileLocks.containsKey(file)) {
     		LockInfo lock = fileLocks.get(file);
     		if (lock != null && lock.hasExpired()) {
-                resourceLocks.remove(lock.getWebPath());
+                //LOCK resourceLocks.remove(lock.getWebPath());
                 fileLocks.remove(file);
             } else {
             	return true;
@@ -109,7 +108,7 @@ public class VFSLockManagerImpl implements VFSLockManager {
     	if(file != null && fileLocks.containsKey(file)) {
     		LockInfo lock = fileLocks.get(file);
     		if (lock != null && lock.hasExpired()) {
-                resourceLocks.remove(lock.getWebPath());
+    			//LOCK resourceLocks.remove(lock.getWebPath());
                 fileLocks.remove(file);
             } else {
         		Long lockedBy = lock.getLockedBy();
@@ -189,7 +188,7 @@ public class VFSLockManagerImpl implements VFSLockManager {
 					lock.setVfsLock(false);
 				} else {
 					if(lock.getWebPath() != null) {
-						resourceLocks.remove(lock.getWebPath());
+						//LOCK resourceLocks.remove(lock.getWebPath());
 					}
 					fileLocks.remove(file);
 					unlocked = true;
@@ -203,13 +202,13 @@ public class VFSLockManagerImpl implements VFSLockManager {
 	}
 
 	public Iterator<LockInfo> getResourceLocks() {
-		return resourceLocks.values().iterator();
+		return fileLocks.values().iterator();
 	}
 	
     public LockInfo getResourceLock(WebResource resource) {
-    	if(resourceLocks.containsKey(resource.getPath())) {
+    	/* LOCK if(resourceLocks.containsKey(resource.getPath())) {
     		return resourceLocks.get(resource.getPath());
-    	}
+    	}*/
     	
     	File file = extractFile(resource);
     	if(file != null && fileLocks.containsKey(file)) {
@@ -285,7 +284,7 @@ public class VFSLockManagerImpl implements VFSLockManager {
     }
     
     public void putResourceLock(WebResource resource, LockInfo lock) {
-		resourceLocks.put(resource.getPath(), lock);
+		//LOCK resourceLocks.put(resource.getPath(), lock);
 		File file = extractFile(resource);
 		if(file != null) {
 			fileLocks.put(file, lock);
@@ -317,18 +316,18 @@ public class VFSLockManagerImpl implements VFSLockManager {
     }
     
     public void removeResourceLock(WebResource resource) {
-    	LockInfo lock = resourceLocks.get(resource.getPath());
-    	if(lock != null) {
-    		if(lock.isVfsLock()) {
-    			lock.setWebDAVLock(false);
-    		} else {
-		    	resourceLocks.remove(resource.getPath());
-		    	File file = extractFile(resource);
-				if(file != null) {
+    	//LOCK
+		File file = extractFile(resource);
+		if(file != null) {
+	    	LockInfo lock = fileLocks.get(file);
+	    	if(lock != null) {
+	    		if(lock.isVfsLock()) {
+	    			lock.setWebDAVLock(false);
+	    		} else {
 					fileLocks.remove(file);
-				}
-    		}
-    	}
+	    		}
+	    	}
+		}
 	}
 
 	public Vector<String> getLockNullResource(WebResource resource) {
@@ -376,11 +375,15 @@ public class VFSLockManagerImpl implements VFSLockManager {
     			return true;
     		}
     	}
-
-        LockInfo lock = resourceLocks.get(path);
-
+    	
+    	File file = extractFile(resource);
+    	if(file == null) {
+    		return false;//lock only file
+    	}
+    	
+        LockInfo lock = fileLocks.get(file);
         if (lock != null && lock.hasExpired()) {
-            resourceLocks.remove(path);
+            fileLocks.remove(file);
         } else if (lock != null) {
             // At least one of the tokens of the locks must have been given
         	Iterator<String> tokenList = lock.tokens();
diff --git a/src/test/java/org/olat/core/commons/services/webdav/WebDAVCommandsTest.java b/src/test/java/org/olat/core/commons/services/webdav/WebDAVCommandsTest.java
index 9a74b0c2f25..ab3118bd216 100644
--- a/src/test/java/org/olat/core/commons/services/webdav/WebDAVCommandsTest.java
+++ b/src/test/java/org/olat/core/commons/services/webdav/WebDAVCommandsTest.java
@@ -51,6 +51,8 @@ import org.olat.core.commons.modules.bc.vfs.OlatRootFolderImpl;
 import org.olat.core.commons.persistence.DB;
 import org.olat.core.id.Identity;
 import org.olat.core.id.Roles;
+import org.olat.core.logging.OLog;
+import org.olat.core.logging.Tracing;
 import org.olat.core.util.FileUtils;
 import org.olat.core.util.vfs.VFSContainer;
 import org.olat.core.util.vfs.VFSItem;
@@ -75,6 +77,8 @@ import org.springframework.beans.factory.annotation.Autowired;
  */
 public class WebDAVCommandsTest extends WebDAVTestCase {
 	
+	private static final OLog log = Tracing.createLoggerFor(WebDAVCommandsTest.class);
+	
 	@Autowired
 	private DB dbInstance;
 	@Autowired
@@ -520,7 +524,7 @@ public class WebDAVCommandsTest extends WebDAVTestCase {
 		//author check file 
 		URI textUri = conn.getBaseURI().path("webdav").path("home").path("public").path("test.txt").build();
 		String textPropfind = conn.propfind(textUri, 0);
-		System.out.println(textPropfind);
+		log.info(textPropfind);
 		
 		//author lock the file
 		String lockToken = conn.lock(textUri, UUID.randomUUID().toString());
@@ -559,6 +563,52 @@ public class WebDAVCommandsTest extends WebDAVTestCase {
 		IOUtils.closeQuietly(conn);
 	}
 	
+	@Test
+	public void testLock_public_samePathLock()
+	throws IOException, URISyntaxException {
+		//create a user
+		Identity user1 = JunitTestHelper.createAndPersistIdentityAsRndUser("webdav-2d");
+		Identity user2 = JunitTestHelper.createAndPersistIdentityAsRndUser("webdav-2e");
+
+		//create a file
+		String publicPath1 = FolderConfig.getUserHomes() + "/" + user1.getName() + "/public";
+		VFSContainer vfsPublic1 = new OlatRootFolderImpl(publicPath1, null);
+		VFSItem item1 = createFile(vfsPublic1, "test.txt");
+		Assert.assertNotNull(item1);
+		
+		String publicPath2 = FolderConfig.getUserHomes() + "/" + user2.getName() + "/public";
+		VFSContainer vfsPublic2 = new OlatRootFolderImpl(publicPath2, null);
+		VFSItem item2 = createFile(vfsPublic2, "test.txt");
+		Assert.assertNotNull(item2);
+		
+		//lock the item with WebDAV
+		WebDAVConnection conn1 = new WebDAVConnection();
+		conn1.setCredentials(user1.getName(), "A6B7C8");
+
+		//user 1 lock the file
+		URI textUri = conn1.getBaseURI().path("webdav").path("home").path("public").path("test.txt").build();
+		String textPropfind1 = conn1.propfind(textUri, 0);
+		Assert.assertNotNull(textPropfind1);
+		
+		// lock the path /webdav/home/public/test.txt
+		String lockToken1 = conn1.lock(textUri, UUID.randomUUID().toString());
+		Assert.assertNotNull(lockToken1);
+
+		//user 2 lock its own file
+		WebDAVConnection conn2 = new WebDAVConnection();
+		conn2.setCredentials(user2.getName(), "A6B7C8");
+		String textPropfind2 = conn2.propfind(textUri, 0);
+		Assert.assertNotNull(textPropfind2);
+		
+		// lock the path /webdav/home/public/test.txt
+		String lockToken2 = conn2.lock(textUri, UUID.randomUUID().toString());
+		Assert.assertNotNull(lockToken2);
+		
+		//closes
+		conn1.close();
+		conn2.close();
+	}
+	
 	@Test
 	public void testDelete()
 	throws IOException, URISyntaxException {
@@ -679,7 +729,8 @@ public class WebDAVCommandsTest extends WebDAVTestCase {
 		Assert.assertTrue(customizingXml.contains("<D:href>/webdav/customizing/</D:href>"));
 
 		//PUT in the folder
-		URI textUri = conn.getBaseURI().path("webdav").path("customizing").path("infos.txt").build();
+		String randomFilename = "infos" + UUID.randomUUID() + ".txt";
+		URI textUri = conn.getBaseURI().path("webdav").path("customizing").path(randomFilename).build();
 		HttpPut put = conn.createPut(textUri);
 		InputStream dataStream = WebDAVCommandsTest.class.getResourceAsStream("text.txt");
 		InputStreamEntity entity = new InputStreamEntity(dataStream, -1);
-- 
GitLab