Author: markt
Date: Thu Nov  1 18:54:14 2012
New Revision: 1404724

URL: http://svn.apache.org/viewvc?rev=1404724&view=rev
Log:
Restore ability for HTTP PUT to overwrite an existing resource

Modified:
    tomcat/trunk/java/org/apache/catalina/WebResourceRoot.java
    tomcat/trunk/java/org/apache/catalina/WebResourceSet.java
    tomcat/trunk/java/org/apache/catalina/servlets/DefaultServlet.java
    tomcat/trunk/java/org/apache/catalina/servlets/WebdavServlet.java
    tomcat/trunk/java/org/apache/catalina/webresources/DirResourceSet.java
    tomcat/trunk/java/org/apache/catalina/webresources/FileResourceSet.java
    tomcat/trunk/java/org/apache/catalina/webresources/JarResourceSet.java
    tomcat/trunk/java/org/apache/catalina/webresources/StandardRoot.java
    
tomcat/trunk/test/org/apache/catalina/webresources/AbstractTestResourceSet.java
    
tomcat/trunk/test/org/apache/catalina/webresources/AbstractTestResourceSetMount.java
    
tomcat/trunk/test/org/apache/catalina/webresources/TesterWebResourceRoot.java

Modified: tomcat/trunk/java/org/apache/catalina/WebResourceRoot.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/WebResourceRoot.java?rev=1404724&r1=1404723&r2=1404724&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/WebResourceRoot.java (original)
+++ tomcat/trunk/java/org/apache/catalina/WebResourceRoot.java Thu Nov  1 
18:54:14 2012
@@ -161,14 +161,18 @@ public interface WebResourceRoot extends
      * Create a new resource at the requested path using the provided
      * InputStream.
      *
-     * @param path  The path to be used for the new Resource. It is relative to
-     *              the root of the web application and must start with '/'.
-     * @param is    The InputStream that will provide the content for the new
-     *              Resource.
+     * @param path      The path to be used for the new Resource. It is 
relative
+     *                  to the root of the web application and must start with
+     *                  '/'.
+     * @param is        The InputStream that will provide the content for the
+     *                  new Resource.
+     * @param overwrite If <code>true</code> and the resource already exists it
+     *                  will be overwritten. If <code>false</code> and the
+     *                  resource already exists the write will fail.
      *
      * @return  <code>true</code> if and only if the new Resource is written
      */
-    boolean write(String path, InputStream is);
+    boolean write(String path, InputStream is, boolean overwrite);
 
     /**
      * Creates a new {@link WebResourceSet} for this {@link WebResourceRoot}

Modified: tomcat/trunk/java/org/apache/catalina/WebResourceSet.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/WebResourceSet.java?rev=1404724&r1=1404723&r2=1404724&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/WebResourceSet.java (original)
+++ tomcat/trunk/java/org/apache/catalina/WebResourceSet.java Thu Nov  1 
18:54:14 2012
@@ -76,14 +76,18 @@ public interface WebResourceSet extends 
      * InputStream. If a resource already exists at the provided path it will
      * not be overwritten.
      *
-     * @param path  The path to be used for the new Resource. It is relative to
-     *              the root of the web application and must start with '/'.
-     * @param is    The InputStream that will provide the content for the new
-     *              Resource.
+     * @param path      The path to be used for the new Resource. It is 
relative
+     *                  to the root of the web application and must start with
+     *                  '/'.
+     * @param is        The InputStream that will provide the content for the
+     *                  new Resource.
+     * @param overwrite If <code>true</code> and the resource already exists it
+     *                  will be overwritten. If <code>false</code> and the
+     *                  resource already exists the write will fail.
      *
      * @return  <code>true</code> if and only if the new Resource is written
      */
-    boolean write(String path, InputStream is);
+    boolean write(String path, InputStream is, boolean overwrite);
 
     void setRoot(WebResourceRoot root);
 }

Modified: tomcat/trunk/java/org/apache/catalina/servlets/DefaultServlet.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/servlets/DefaultServlet.java?rev=1404724&r1=1404723&r2=1404724&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/servlets/DefaultServlet.java 
(original)
+++ tomcat/trunk/java/org/apache/catalina/servlets/DefaultServlet.java Thu Nov  
1 18:54:14 2012
@@ -511,7 +511,7 @@ public class DefaultServlet
                 resourceInputStream = req.getInputStream();
             }
 
-            if (resources.write(path, resourceInputStream)) {
+            if (resources.write(path, resourceInputStream, true)) {
                 if (resource.exists()) {
                     resp.setStatus(HttpServletResponse.SC_NO_CONTENT);
                 } else {

Modified: tomcat/trunk/java/org/apache/catalina/servlets/WebdavServlet.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/servlets/WebdavServlet.java?rev=1404724&r1=1404723&r2=1404724&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/servlets/WebdavServlet.java (original)
+++ tomcat/trunk/java/org/apache/catalina/servlets/WebdavServlet.java Thu Nov  
1 18:54:14 2012
@@ -1672,7 +1672,8 @@ public class WebdavServlet
                 copyResource(errorList, childSrc, childDest);
             }
         } else if (sourceResource.isFile()) {
-            if (!resources.write(dest, sourceResource.getInputStream())) {
+            if (!resources.write(dest, sourceResource.getInputStream(),
+                    false)) {
                 errorList.put(source,
                         new Integer(WebdavStatus.SC_INTERNAL_SERVER_ERROR));
                 return false;

Modified: tomcat/trunk/java/org/apache/catalina/webresources/DirResourceSet.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/webresources/DirResourceSet.java?rev=1404724&r1=1404723&r2=1404724&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/webresources/DirResourceSet.java 
(original)
+++ tomcat/trunk/java/org/apache/catalina/webresources/DirResourceSet.java Thu 
Nov  1 18:54:14 2012
@@ -169,7 +169,7 @@ public class DirResourceSet extends Abst
     }
 
     @Override
-    public boolean write(String path, InputStream is) {
+    public boolean write(String path, InputStream is, boolean overwrite) {
         checkPath(path);
 
         if (is == null) {
@@ -189,7 +189,13 @@ public class DirResourceSet extends Abst
         }
 
         if (dest.exists()) {
-            return false;
+            if (overwrite) {
+                if (!dest.delete()) {
+                    return false;
+                }
+            } else {
+                return false;
+            }
         }
 
         try (FileOutputStream fos = new FileOutputStream(dest)) {

Modified: 
tomcat/trunk/java/org/apache/catalina/webresources/FileResourceSet.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/webresources/FileResourceSet.java?rev=1404724&r1=1404723&r2=1404724&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/webresources/FileResourceSet.java 
(original)
+++ tomcat/trunk/java/org/apache/catalina/webresources/FileResourceSet.java Thu 
Nov  1 18:54:14 2012
@@ -137,7 +137,7 @@ public class FileResourceSet extends Abs
     }
 
     @Override
-    public boolean write(String path, InputStream is) {
+    public boolean write(String path, InputStream is, boolean overwrite) {
         checkPath(path);
         return false;
     }

Modified: tomcat/trunk/java/org/apache/catalina/webresources/JarResourceSet.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/webresources/JarResourceSet.java?rev=1404724&r1=1404723&r2=1404724&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/webresources/JarResourceSet.java 
(original)
+++ tomcat/trunk/java/org/apache/catalina/webresources/JarResourceSet.java Thu 
Nov  1 18:54:14 2012
@@ -217,7 +217,7 @@ public class JarResourceSet extends Abst
     }
 
     @Override
-    public boolean write(String path, InputStream is) {
+    public boolean write(String path, InputStream is, boolean overwrite) {
         checkPath(path);
 
         if (is == null) {

Modified: tomcat/trunk/java/org/apache/catalina/webresources/StandardRoot.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/webresources/StandardRoot.java?rev=1404724&r1=1404723&r2=1404724&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/webresources/StandardRoot.java 
(original)
+++ tomcat/trunk/java/org/apache/catalina/webresources/StandardRoot.java Thu 
Nov  1 18:54:14 2012
@@ -137,14 +137,14 @@ public class StandardRoot extends Lifecy
     }
 
     @Override
-    public boolean write(String path, InputStream is) {
+    public boolean write(String path, InputStream is, boolean overwrite) {
         checkState();
 
-        if (preResourceExists(path)) {
+        if (!overwrite && preResourceExists(path)) {
             return false;
         }
 
-        return main.write(path, is);
+        return main.write(path, is, overwrite);
     }
 
     private boolean preResourceExists(String path) {

Modified: 
tomcat/trunk/test/org/apache/catalina/webresources/AbstractTestResourceSet.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/webresources/AbstractTestResourceSet.java?rev=1404724&r1=1404723&r2=1404724&view=diff
==============================================================================
--- 
tomcat/trunk/test/org/apache/catalina/webresources/AbstractTestResourceSet.java 
(original)
+++ 
tomcat/trunk/test/org/apache/catalina/webresources/AbstractTestResourceSet.java 
Thu Nov  1 18:54:14 2012
@@ -256,48 +256,51 @@ public abstract class AbstractTestResour
     @Test(expected = IllegalArgumentException.class)
     public final void testWriteEmpty() {
         InputStream is = new ByteArrayInputStream("test".getBytes());
-        resourceRoot.write("", is);
+        resourceRoot.write("", is, false);
     }
 
     @Test
     public final void testWriteRoot() {
         InputStream is = new ByteArrayInputStream("test".getBytes());
-        Assert.assertFalse(resourceRoot.write(getMount() + "/", is));
+        Assert.assertFalse(resourceRoot.write(getMount() + "/", is, false));
     }
 
     @Test
     public final void testWriteDirA() {
         InputStream is = new ByteArrayInputStream("test".getBytes());
-        Assert.assertFalse(resourceRoot.write(getMount() + "/d1", is));
+        Assert.assertFalse(resourceRoot.write(getMount() + "/d1", is, false));
     }
 
     @Test
     public final void testWriteDirB() {
         InputStream is = new ByteArrayInputStream("test".getBytes());
-        Assert.assertFalse(resourceRoot.write(getMount() + "/d1/", is));
+        Assert.assertFalse(resourceRoot.write(getMount() + "/d1/", is, false));
     }
 
     @Test
     public final void testWriteFile() {
         InputStream is = new ByteArrayInputStream("test".getBytes());
-        Assert.assertFalse(resourceRoot.write(getMount() + "/d1/d1-f1.txt", 
is));
+        Assert.assertFalse(resourceRoot.write(
+                getMount() + "/d1/d1-f1.txt", is, false));
     }
 
     @Test(expected = NullPointerException.class)
     public final void testWriteNew() {
-        resourceRoot.write(getMount() + "/new-test", null);
+        resourceRoot.write(getMount() + "/new-test", null, false);
     }
 
     @Test
     public final void testWrite() {
         InputStream is = new ByteArrayInputStream("test".getBytes());
         if (isWriteable()) {
-            Assert.assertTrue(resourceRoot.write(getMount() + "/new-test", 
is));
+            Assert.assertTrue(resourceRoot.write(
+                    getMount() + "/new-test", is, false));
             File file = new File(getBaseDir(), "new-test");
             Assert.assertTrue(file.exists());
             Assert.assertTrue(file.delete());
         } else {
-            Assert.assertFalse(resourceRoot.write(getMount() + "/new-test", 
is));
+            Assert.assertFalse(resourceRoot.write(
+                    getMount() + "/new-test", is, false));
         }
     }
 }

Modified: 
tomcat/trunk/test/org/apache/catalina/webresources/AbstractTestResourceSetMount.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/webresources/AbstractTestResourceSetMount.java?rev=1404724&r1=1404723&r2=1404724&view=diff
==============================================================================
--- 
tomcat/trunk/test/org/apache/catalina/webresources/AbstractTestResourceSetMount.java
 (original)
+++ 
tomcat/trunk/test/org/apache/catalina/webresources/AbstractTestResourceSetMount.java
 Thu Nov  1 18:54:14 2012
@@ -62,6 +62,6 @@ public abstract class AbstractTestResour
     @Test
     public void testWriteAbove() {
         InputStream is = new ByteArrayInputStream("test".getBytes());
-        Assert.assertFalse(resourceRoot.write("/", is));
+        Assert.assertFalse(resourceRoot.write("/", is, false));
     }
 }

Modified: 
tomcat/trunk/test/org/apache/catalina/webresources/TesterWebResourceRoot.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/webresources/TesterWebResourceRoot.java?rev=1404724&r1=1404723&r2=1404724&view=diff
==============================================================================
--- 
tomcat/trunk/test/org/apache/catalina/webresources/TesterWebResourceRoot.java 
(original)
+++ 
tomcat/trunk/test/org/apache/catalina/webresources/TesterWebResourceRoot.java 
Thu Nov  1 18:54:14 2012
@@ -151,12 +151,12 @@ public class TesterWebResourceRoot imple
     }
 
     @Override
-    public boolean write(String path, InputStream is) {
+    public boolean write(String path, InputStream is, boolean overwrite) {
         if (getResource(path).exists()) {
             return false;
         }
 
-        return main.write(path, is);
+        return main.write(path, is, false);
     }
 
     @Override



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to