maven-wagon-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From car...@apache.org
Subject svn commit: r489428 - in /maven/wagon/trunk/wagon-providers/wagon-webdav/src: main/java/org/apache/maven/wagon/providers/webdav/ test/java/org/apache/maven/wagon/providers/webdav/
Date Thu, 21 Dec 2006 18:19:27 GMT
Author: carlos
Date: Thu Dec 21 10:19:26 2006
New Revision: 489428

URL: http://svn.apache.org/viewvc?view=rev&rev=489428
Log:
[WAGON-67] Improve the directory creation code and error messages

Modified:
    maven/wagon/trunk/wagon-providers/wagon-webdav/src/main/java/org/apache/maven/wagon/providers/webdav/WebDavWagon.java
    maven/wagon/trunk/wagon-providers/wagon-webdav/src/test/java/org/apache/maven/wagon/providers/webdav/ServletServer.java
    maven/wagon/trunk/wagon-providers/wagon-webdav/src/test/java/org/apache/maven/wagon/providers/webdav/WebDavWagonTest.java

Modified: maven/wagon/trunk/wagon-providers/wagon-webdav/src/main/java/org/apache/maven/wagon/providers/webdav/WebDavWagon.java
URL: http://svn.apache.org/viewvc/maven/wagon/trunk/wagon-providers/wagon-webdav/src/main/java/org/apache/maven/wagon/providers/webdav/WebDavWagon.java?view=diff&rev=489428&r1=489427&r2=489428
==============================================================================
--- maven/wagon/trunk/wagon-providers/wagon-webdav/src/main/java/org/apache/maven/wagon/providers/webdav/WebDavWagon.java
(original)
+++ maven/wagon/trunk/wagon-providers/wagon-webdav/src/main/java/org/apache/maven/wagon/providers/webdav/WebDavWagon.java
Thu Dec 21 10:19:26 2006
@@ -53,6 +53,7 @@
 import java.util.List;
 import java.util.Locale;
 import java.util.Properties;
+import java.util.Stack;
 import java.util.TimeZone;
 
 /**
@@ -210,8 +211,6 @@
     {
         Repository repository = getRepository();
 
-        String basedir = repository.getBasedir();
-
         resourceName = StringUtils.replace( resourceName, "\\", "/" );
         String dir = PathUtils.dirname( resourceName );
         dir = StringUtils.replace( dir, "\\", "/" );
@@ -229,54 +228,8 @@
         }
 
         firePutInitiated( resource, source );
-        String oldpath = webdavResource.getPath();
 
-        String relpath = getPath( basedir, dir );
-
-        try
-        {
-            // Test if dest resource path exist.
-            String cdpath = checkUri( relpath + "/" );
-            webdavResource.setPath( cdpath );
-
-            if ( webdavResource.exists() && !webdavResource.isCollection() )
-            {
-                throw new TransferFailedException(
-                    "Destination path exists and is not a WebDAV collection (directory):
" + cdpath );
-            }
-
-            webdavResource.setPath( oldpath );
-
-            // if dest resource path does not exist, create it
-            if ( !webdavResource.exists() )
-            {
-                // mkcolMethod() cannot create a directory hierarchy at once,
-                // it has to create each directory one at a time
-                try
-                {
-                    String[] dirs = relpath.split( "/" );
-                    String createDir = "/";
-
-                    // start at 1 because first element of dirs[] from split() is ""
-                    for ( int count = 1; count < dirs.length; count++ )
-                    {
-                        createDir = createDir + dirs[count] + "/";
-                        webdavResource.mkcolMethod( createDir );
-                    }
-                    webdavResource.setPath( oldpath );
-                }
-                catch ( IOException ioe )
-                {
-                    throw new TransferFailedException( "Failed to create destination WebDAV
collection (directory): "
-                        + relpath, ioe );
-                }
-            }
-        }
-        catch ( IOException e )
-        {
-            throw new TransferFailedException(
-                "Failed to create destination WebDAV collection (directory): " + relpath,
e );
-        }
+        mkdirs( dir );
 
         try
         {
@@ -299,10 +252,11 @@
                     throw new AuthorizationException( "Access denied to: " + dest );
 
                 case HttpStatus.SC_NOT_FOUND:
-                    throw new ResourceDoesNotExistException( "File: " + dest + " does not
exist" );
+                    // should never happen as the destination is created before or fail
+                    throw new TransferFailedException( "Destination does not exist: " + repository.getUrl()
);
 
                 case HttpStatus.SC_LENGTH_REQUIRED:
-                    throw new ResourceDoesNotExistException( "Transfer failed, server requires
Content-Length." );
+                    throw new TransferFailedException( "Transfer failed, server requires
Content-Length." );
 
                     //add more entries here
                 default:
@@ -329,6 +283,103 @@
     }
 
     /**
+     * Create directories in server as needed.
+     * They are created one at a time until the whole path exists.
+     *
+     * @param dir path to be created in server from repository basedir
+     * @throws TransferFailedException
+     */
+    private void mkdirs( String dir )
+        throws TransferFailedException
+    {
+        Repository repository = getRepository();
+        String basedir = repository.getBasedir();
+        String destinationPath = webdavResource.getPath();
+
+        String relpath = FileUtils.normalize( getPath( basedir, dir ) + "/" );
+        String currentPath = relpath;
+
+        Stack directoriesToBeCreated = new Stack();
+
+        try
+        {
+            while ( currentPath != null )
+            {
+                webdavResource.setPath( currentPath );
+
+                /* needed to call webdavResource.exists() later */
+                try
+                {
+                    webdavResource.setProperties( WebdavResource.NAME, DepthSupport.DEPTH_0
);
+                }
+                catch ( HttpException e )
+                {
+                    // ignore exceptions thrown when the path does not exist ( 404 errors
)
+                }
+
+                if ( webdavResource.exists() )
+                {
+                    if ( webdavResource.isCollection() )
+                    {
+                        /* path exists and it's a directory */
+                        break;
+                    }
+                    else
+                    {
+                        throw new TransferFailedException(
+                                                           "Destination path exists and is
not a WebDAV collection (directory): "
+                                                               + webdavResource.toString()
);
+                    }
+                }
+
+                /* if dest resource path does not exist, create it later */
+                directoriesToBeCreated.push( currentPath );
+
+                /* go down a folder */
+                currentPath += "/../";
+                currentPath = FileUtils.normalize( currentPath );
+            }
+
+            // mkcolMethod() cannot create a directory hierarchy at once,
+            // it has to create each directory one at a time
+            while ( !directoriesToBeCreated.empty() )
+            {
+                currentPath = (String) directoriesToBeCreated.pop();
+                webdavResource.setPath( currentPath );
+
+                try
+                {
+                    boolean destinationCreated = webdavResource.mkcolMethod( currentPath
);
+                    if ( !destinationCreated )
+                    {
+                        throw new TransferFailedException( "Destination folder could not
be created: "
+                            + webdavResource.toString() );
+                    }
+                }
+                catch ( IOException e )
+                {
+                    throw new TransferFailedException( "Failed to create destination WebDAV
collection (directory): "
+                        + webdavResource.toString(), e );
+                }
+            }
+
+            webdavResource.setPath( destinationPath );
+
+        }
+        catch ( HttpException e )
+        {
+            throw new TransferFailedException( "Unknown error creating destination WebDAV
collection (directory): "
+                + webdavResource.toString() + ". Server returned error: "
+                + HttpStatus.getStatusText( e.getReasonCode() ), e );
+        }
+        catch ( IOException e )
+        {
+            throw new TransferFailedException( "Unknown error creating destination WebDAV
collection (directory): "
+                + webdavResource.toString(), e );
+        }
+    }
+
+    /**
      * Converts a String url to an HttpURL
      *
      * @param url String url to convert to an HttpURL
@@ -350,9 +401,10 @@
 
     /**
      * Determine which URI to use at the prompt.
+     * @see FileUtils#normalize(String)
      *
      * @param uri the path to be set.
-     * @return the absolute path.
+     * @return the normalized path.
      */
     private String checkUri( String uri )
         throws IOException

Modified: maven/wagon/trunk/wagon-providers/wagon-webdav/src/test/java/org/apache/maven/wagon/providers/webdav/ServletServer.java
URL: http://svn.apache.org/viewvc/maven/wagon/trunk/wagon-providers/wagon-webdav/src/test/java/org/apache/maven/wagon/providers/webdav/ServletServer.java?view=diff&rev=489428&r1=489427&r2=489428
==============================================================================
--- maven/wagon/trunk/wagon-providers/wagon-webdav/src/test/java/org/apache/maven/wagon/providers/webdav/ServletServer.java
(original)
+++ maven/wagon/trunk/wagon-providers/wagon-webdav/src/test/java/org/apache/maven/wagon/providers/webdav/ServletServer.java
Thu Dec 21 10:19:26 2006
@@ -100,11 +100,15 @@
             context.addHandler( new SecurityHandler() );
         }
 
-        Iterator itpaths = wdc.getServlets().iterator();
-        while ( itpaths.hasNext() )
+        List servlets = wdc.getServlets();
+        if ( servlets != null )
         {
-            Servlet servlet = (Servlet) itpaths.next();
-            initServlet( context, servlet );
+            Iterator itpaths = servlets.iterator();
+            while ( itpaths.hasNext() )
+            {
+                Servlet servlet = (Servlet) itpaths.next();
+                initServlet( context, servlet );
+            }
         }
     }
 

Modified: maven/wagon/trunk/wagon-providers/wagon-webdav/src/test/java/org/apache/maven/wagon/providers/webdav/WebDavWagonTest.java
URL: http://svn.apache.org/viewvc/maven/wagon/trunk/wagon-providers/wagon-webdav/src/test/java/org/apache/maven/wagon/providers/webdav/WebDavWagonTest.java?view=diff&rev=489428&r1=489427&r2=489428
==============================================================================
--- maven/wagon/trunk/wagon-providers/wagon-webdav/src/test/java/org/apache/maven/wagon/providers/webdav/WebDavWagonTest.java
(original)
+++ maven/wagon/trunk/wagon-providers/wagon-webdav/src/test/java/org/apache/maven/wagon/providers/webdav/WebDavWagonTest.java
Thu Dec 21 10:19:26 2006
@@ -20,12 +20,14 @@
 import java.io.IOException;
 
 import org.apache.maven.wagon.FileTestUtils;
+import org.apache.maven.wagon.TransferFailedException;
 import org.apache.maven.wagon.WagonTestCase;
 
 /**
  * WebDAV Wagon Test 
  * 
  * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
+ * @author <a href="mailto:carlos@apache.org">Carlos Sanchez</a>
  */
 public class WebDavWagonTest
     extends WagonTestCase
@@ -66,4 +68,32 @@
     {
         release( server );
     }
+
+    public void testWagonPutToNonWebdav()
+        throws Exception
+    {
+        setupRepositories();
+
+        String url = "http://localhost:10007";
+
+        testRepository.setUrl( "dav:" + url );
+
+        setupWagonTestingFixtures();
+
+        try
+        {
+            putFile();
+            fail( "Expected and not thrown " + TransferFailedException.class.getName() );
+        }
+        catch ( TransferFailedException e )
+        {
+            assertEquals( "Exception message doesn't match expected", "Destination folder
could not be created: " + url
+                + "/", e.getMessage() );
+        }
+
+        testRepository.setUrl( getTestRepositoryUrl() );
+
+        tearDownWagonTestingFixtures();
+    }
+
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: wagon-commits-unsubscribe@maven.apache.org
For additional commands, e-mail: wagon-commits-help@maven.apache.org


Mime
View raw message