From: <jbo...@li...> - 2006-02-14 22:43:04
|
Author: mnasato Date: 2006-02-14 17:42:56 -0500 (Tue, 14 Feb 2006) New Revision: 2434 Modified: trunk/labs/davcache/src/net/sf/davcache/TreeCacheStore.java trunk/labs/davcache/src/net/sf/davcache/WebdavServlet.java trunk/labs/davcache/tests/functional/net/sf/davcache/functest/WebdavServletTest.java Log: added COPY (files only, not directories) and check for Overwrite in MOVE Modified: trunk/labs/davcache/src/net/sf/davcache/TreeCacheStore.java =================================================================== --- trunk/labs/davcache/src/net/sf/davcache/TreeCacheStore.java 2006-02-14 22:26:14 UTC (rev 2433) +++ trunk/labs/davcache/src/net/sf/davcache/TreeCacheStore.java 2006-02-14 22:42:56 UTC (rev 2434) @@ -147,14 +147,18 @@ treeCache.remove(getPropsPath(path)); } - public void moveFile(String path, String destinationPath) throws CacheException + public void copyFile(String path, String destinationPath) throws CacheException { - //TODO should be atomic byte[] fileContent = getFileContent(path); - deleteFile(path); createFile(destinationPath, fileContent); } + public void moveFile(String path, String destinationPath) throws CacheException + { + copyFile(path, destinationPath); + deleteFile(path); + } + private static class FqnAndKey { private String fqn; Modified: trunk/labs/davcache/src/net/sf/davcache/WebdavServlet.java =================================================================== --- trunk/labs/davcache/src/net/sf/davcache/WebdavServlet.java 2006-02-14 22:26:14 UTC (rev 2433) +++ trunk/labs/davcache/src/net/sf/davcache/WebdavServlet.java 2006-02-14 22:42:56 UTC (rev 2434) @@ -68,12 +68,15 @@ } } - protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, - IOException + protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String method = request.getMethod().toUpperCase(); - if (method.equals("MKCOL")) + if (method.equals("COPY")) { + doCopy(request, response); + } + else if (method.equals("MKCOL")) + { doMkCol(request, response); } else if (method.equals("MOVE")) @@ -94,14 +97,73 @@ } } + protected void doCopy(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException + { + String sourcePath = getRelativePath(request); + if (!store.exists(sourcePath)) + { + response.sendError(HttpServletResponse.SC_NOT_FOUND); + return; + } + String absoluteDestinationPath = request.getHeader("Destination"); + String destinationPath = relativizePath(request, absoluteDestinationPath); + if (destinationPath.equals(sourcePath)) + { + response.sendError(HttpServletResponse.SC_FORBIDDEN, "Source and destination are the same"); + return; + } + boolean overwrite = "T".equalsIgnoreCase(request.getHeader("Overwrite")); + boolean destinationAlreadyExists = store.exists(destinationPath); + if (!overwrite && destinationAlreadyExists) + { + response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED, "Resource exists and will not be overwritten: " + destinationPath); + return; + } + try + { + if (store.isFile(sourcePath)) + { + logger.debug("copying file '" + sourcePath + "' to '" + destinationPath + "'"); + store.copyFile(sourcePath, destinationPath); + if (destinationAlreadyExists) + { + response.setStatus(HttpServletResponse.SC_NO_CONTENT); + } + else + { + response.setStatus(HttpServletResponse.SC_CREATED); + } + } + else if (store.isDirectory(sourcePath)) + { + response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "Method Not Allowed: COPY not supported for directory"); + } + } + catch (CacheException e) + { + throw new ServletException(e); + } + } + protected void doMove(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - // TODO check Overwrite header first String path = getRelativePath(request); + if (!store.exists(path)) + { + response.sendError(HttpServletResponse.SC_NOT_FOUND); + return; + } String absoluteDestinationPath = request.getHeader("Destination"); + String destinationPath = relativizePath(request, absoluteDestinationPath); + boolean overwrite = "T".equalsIgnoreCase(request.getHeader("Overwrite")); + boolean destinationAlreadyExists = store.exists(destinationPath); + if (!overwrite && destinationAlreadyExists) + { + response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED, "Resource exists and will not be overwritten: " + destinationPath); + return; + } try { - String destinationPath = relativizePath(request, absoluteDestinationPath); if (store.isFile(path)) { logger.debug("moving file from '" + path + "' to '" + destinationPath + "'"); @@ -125,7 +187,7 @@ IOException { response.addHeader("DAV", "1"); - response.addHeader("Allow", "DELETE, GET, HEAD, MKCOL, OPTIONS, PROPFIND, PROPPATCH, PUT"); + response.addHeader("Allow", "COPY, DELETE, GET, HEAD, MKCOL, OPTIONS, PROPFIND, PROPPATCH, PUT"); response.addHeader("MS-Author-Via", "DAV"); } @@ -182,8 +244,7 @@ } } - protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, - IOException + protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String relativePath = getRelativePath(request); try Modified: trunk/labs/davcache/tests/functional/net/sf/davcache/functest/WebdavServletTest.java =================================================================== --- trunk/labs/davcache/tests/functional/net/sf/davcache/functest/WebdavServletTest.java 2006-02-14 22:26:14 UTC (rev 2433) +++ trunk/labs/davcache/tests/functional/net/sf/davcache/functest/WebdavServletTest.java 2006-02-14 22:42:56 UTC (rev 2434) @@ -84,4 +84,30 @@ resources = folder.listWebdavResources(); assertEquals(0, resources.length); } + + public void testCopyFile() throws Exception + { + String testContent = "Test content."; + root.putMethod(BASE_PATH + "/test.txt", testContent); + WebdavResource[] resources = root.listWebdavResources(); + assertEquals(1, resources.length); + WebdavResource file = resources[0]; + + file.copyMethod(BASE_PATH + "/test2.txt"); + resources = root.listWebdavResources(); + assertEquals(2, resources.length); + WebdavResource file1 = resources[1]; + assertEquals("test.txt", file1.getName()); + WebdavResource file2 = resources[0]; + assertEquals("test2.txt", file2.getName()); + assertEquals(testContent.length(), file.getGetContentLength()); + assertEquals("text/plain", file.getGetContentType()); + //TODO test LastModified + assertEquals(testContent, file2.getMethodDataAsString()); + + file1.deleteMethod(); + file2.deleteMethod(); + resources = root.listWebdavResources(); + assertEquals(0, resources.length); + } } |