@@ -1109,7 +1109,7 @@ protected void doProppatch(HttpServletRequest req, HttpServletResponse resp) thr
11091109 return ;
11101110 }
11111111
1112- if (isLocked (path , req )) {
1112+ if (isLocked (path , req , false )) {
11131113 resp .sendError (WebdavStatus .SC_LOCKED );
11141114 return ;
11151115 }
@@ -1291,7 +1291,7 @@ protected void doMkcol(HttpServletRequest req, HttpServletResponse resp) throws
12911291 return ;
12921292 }
12931293
1294- if (isLocked (path , req )) {
1294+ if (isLocked (path , req , true )) {
12951295 resp .sendError (WebdavStatus .SC_LOCKED );
12961296 return ;
12971297 }
@@ -1340,7 +1340,7 @@ protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws Se
13401340 return ;
13411341 }
13421342
1343- if (isLocked (path , req )) {
1343+ if (isLocked (path , req , ! resource . exists () )) {
13441344 resp .sendError (WebdavStatus .SC_LOCKED );
13451345 return ;
13461346 }
@@ -1393,7 +1393,7 @@ protected void doMove(HttpServletRequest req, HttpServletResponse resp) throws I
13931393
13941394 String path = getRelativePath (req );
13951395
1396- if (isLocked (path , req )) {
1396+ if (isLocked (path , req , true )) {
13971397 resp .sendError (WebdavStatus .SC_LOCKED );
13981398 return ;
13991399 }
@@ -2020,13 +2020,15 @@ private String getPathFromHref(String href, HttpServletRequest req) {
20202020 * Check to see if a resource is currently write locked. The method will look at the "If" header to make sure the
20212021 * client has demonstrated knowledge of the appropriate lock tokens.
20222022 *
2023- * @param path The relative path
2024- * @param req Servlet request
2023+ * @param path The relative path
2024+ * @param req Servlet request
2025+ * @param memberChange {@code true} if the request adds or removes the resource as an internal member of its parent
2026+ * collection
20252027 *
20262028 * @return <code>true</code> if the resource is locked (and no appropriate lock token has been found for at least
20272029 * one of the non-shared locks which are present on the resource).
20282030 */
2029- private boolean isLocked (String path , HttpServletRequest req ) {
2031+ private boolean isLocked (String path , HttpServletRequest req , boolean memberChange ) {
20302032
20312033 if (path == null ) {
20322034 path = getRelativePath (req );
@@ -2037,33 +2039,37 @@ private boolean isLocked(String path, HttpServletRequest req) {
20372039 ifHeader = "" ;
20382040 }
20392041
2040- return isLocked (path , req .getRemoteUser (), ifHeader );
2042+ return isLocked (path , req .getRemoteUser (), ifHeader , memberChange );
20412043 }
20422044
20432045
20442046 /**
20452047 * Check to see if a resource is currently write locked.
20462048 *
2047- * @param path Path of the resource
2048- * @param principal The authenticated principal name
2049- * @param ifHeader "If" HTTP header which was included in the request
2049+ * @param path Path of the resource
2050+ * @param principal The authenticated principal name
2051+ * @param ifHeader "If" HTTP header which was included in the request
2052+ * @param memberChange {@code true} if the request adds or removes the resource as an internal member of its parent
2053+ * collection
20502054 *
20512055 * @return <code>true</code> if the resource is locked (and no appropriate lock token has been found for at least
20522056 * one of the non-shared locks which are present on the resource).
20532057 */
2054- private boolean isLocked (String path , String principal , String ifHeader ) {
2058+ private boolean isLocked (String path , String principal , String ifHeader , boolean memberChange ) {
20552059
20562060 boolean unmatchedSharedLock = false ;
20572061 // Check if the resource or a parent is already locked
20582062 String parentPath = path ;
2063+ // RFC 4918, section 7.4: a write lock on a collection, including a depth 0 lock, protects its internal members
2064+ boolean directParent = false ;
20592065 do {
20602066 LockInfo parentLock = resourceLocks .get (parentPath );
20612067 if (parentLock != null ) {
20622068 if (parentLock .hasExpired ()) {
20632069 resourceLocks .remove (parentPath );
20642070 } else {
20652071 // parentPath == path is a check for the first loop
2066- if (parentPath == path || parentLock .depth > 0 ) {
2072+ if (parentPath == path || parentLock .depth > 0 || directParent ) {
20672073 if (parentLock .isExclusive ()) {
20682074 return !ifHeader .contains (":" + parentLock .token + ">" ) ||
20692075 (parentLock .principal != null && !parentLock .principal .equals (principal ));
@@ -2072,7 +2078,7 @@ private boolean isLocked(String path, String principal, String ifHeader) {
20722078 LockInfo lock = sharedLocks .get (token );
20732079 if (lock != null ) {
20742080 // The shared lock might not have the same depth
2075- if (parentPath == path || lock .depth > 0 ) {
2081+ if (parentPath == path || lock .depth > 0 || directParent ) {
20762082 if (ifHeader .contains (":" + token + ">" ) &&
20772083 (lock .principal == null || lock .principal .equals (principal ))) {
20782084 return false ;
@@ -2091,6 +2097,7 @@ private boolean isLocked(String path, String principal, String ifHeader) {
20912097 if (slash < 0 ) {
20922098 break ;
20932099 }
2100+ directParent = memberChange && parentPath == path ;
20942101 parentPath = parentPath .substring (0 , slash );
20952102 } while (true );
20962103
@@ -2213,7 +2220,7 @@ private boolean copyResource(String path, HttpServletRequest req, HttpServletRes
22132220 }
22142221
22152222 // Check if destination is locked
2216- if (isLocked (destinationPath , req )) {
2223+ if (isLocked (destinationPath , req , true )) {
22172224 resp .sendError (WebdavStatus .SC_LOCKED );
22182225 return false ;
22192226 }
@@ -2381,7 +2388,7 @@ private boolean copyResource(Map<String,Integer> errorList, String source, Strin
23812388 private boolean deleteResource (String path , HttpServletRequest req , HttpServletResponse resp , boolean setStatus )
23822389 throws IOException {
23832390
2384- if (isLocked (path , req )) {
2391+ if (isLocked (path , req , true )) {
23852392 resp .sendError (WebdavStatus .SC_LOCKED );
23862393 return false ;
23872394 }
@@ -2469,7 +2476,7 @@ private void deleteCollection(HttpServletRequest req, String path, Map<String,In
24692476 }
24702477 childName += entry ;
24712478
2472- if (isLocked (childName , req .getRemoteUser (), ifHeader )) {
2479+ if (isLocked (childName , req .getRemoteUser (), ifHeader , false )) {
24732480
24742481 errorList .put (childName , Integer .valueOf (WebdavStatus .SC_LOCKED ));
24752482
0 commit comments