ozeigermann    2004/04/26 05:20:03

  Modified:    webdavclient/clientlib/src/java/org/apache/webdav/lib
                        WebdavResource.java
               webdavclient/commandline/src/java/org/apache/webdav/cmd
                        Client.java
  Log:
  Applied patch by Thomas Bernert to address issue #28536
  
  Revision  Changes    Path
  1.15      +28 -18    
jakarta-slide/webdavclient/clientlib/src/java/org/apache/webdav/lib/WebdavResource.java
  
  Index: WebdavResource.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-slide/webdavclient/clientlib/src/java/org/apache/webdav/lib/WebdavResource.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- WebdavResource.java       26 Apr 2004 09:49:23 -0000      1.14
  +++ WebdavResource.java       26 Apr 2004 12:20:03 -0000      1.15
  @@ -2378,23 +2378,33 @@
           GetMethod method = new GetMethod(URIUtil.encodePathQuery(path));
   
           int statusCode = client.executeMethod(method);
  +        
  +        setStatusCode(statusCode);
  +        
  +        // get the file only if status is any kind of OK
  +        if (statusCode >= 200 && statusCode < 300) {
  +            
  +            // Do a simple little loop to read the response back into the passed
  +            // file parameter.
  +            InputStream inStream = method.getResponseBodyAsStream();
   
  -        // Do a simple little loop to read the response back into the passed
  -        // file parameter.
  -        InputStream inStream = method.getResponseBodyAsStream();
  -
  -        FileOutputStream fos = new FileOutputStream(file);
  -        byte buffer[] = new byte[2048];
  -        int bytesRead;
  -        while ((bytesRead = inStream.read(buffer)) >= 0) {
  -            fos.write(buffer, 0, bytesRead);
  +            FileOutputStream fos = new FileOutputStream(file);
  +            byte buffer[] = new byte[65535];
  +            int bytesRead;
  +            while ((bytesRead = inStream.read(buffer)) >= 0) {
  +                fos.write(buffer, 0, bytesRead);
  +            }
  +            inStream.close();
  +            fos.close();
  +            
  +            return true;
  +            
  +        } else {
  +            return false;
           }
  -        inStream.close();
  -        fos.close();
  +        
   
  -        setStatusCode(statusCode);
  -        return (statusCode >= 200 && statusCode < 300) ? true : false;
  -    }
  +   }
   
   
       /**
  
  
  
  1.13      +121 -34   
jakarta-slide/webdavclient/commandline/src/java/org/apache/webdav/cmd/Client.java
  
  Index: Client.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-slide/webdavclient/commandline/src/java/org/apache/webdav/cmd/Client.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- Client.java       21 Apr 2004 08:27:10 -0000      1.12
  +++ Client.java       26 Apr 2004 12:20:03 -0000      1.13
  @@ -37,6 +37,7 @@
   import java.text.SimpleDateFormat;
   import java.util.Date;
   import java.util.Enumeration;
  +import java.util.StringTokenizer;
   import java.util.Vector;
   import org.apache.commons.httpclient.HttpException;
   import org.apache.commons.httpclient.HttpStatus;
  @@ -803,9 +804,40 @@
               handleException(ex);
           }
       }
  +    
  +    String getLocalTragetFileName(String path, String filename) {
  +        
  +        String srcFileName = null;
  +        String tarFileName = null;
  + 
  +
  +        // get traget filename from last portion of path
  +        StringTokenizer st = new StringTokenizer(path, "/\\");
  +        while (st.hasMoreTokens()) {
  +            srcFileName = st.nextToken();
  +        }
  +                
  +        File targetFile = getFileByPath((filename != null) ? filename : 
srcFileName); 
  +        
  +        try {           
  +            if (targetFile.isDirectory()) {
  +                tarFileName = targetFile.getCanonicalPath() + "/"+ srcFileName;    
  +            } else {
  +                tarFileName = targetFile.getCanonicalPath();    
  +            }         
  +        } catch (IOException e) {
  +            System.err.println(e.toString());
  +            return null;
  +        }           
  +
  +        return tarFileName;
  +    }
   
       void get(String path, String filename)
       {
  +        
  +        filename = getLocalTragetFileName( path,  filename);
  +        
           try {
               // The resource on the remote.
               String src = checkUri(path);
  @@ -826,7 +858,7 @@
   
                   // FIXME: interactive ?
                   out.print("Aleady exists. " +
  -                    "Do you want to overwrite it(Y/n)? ");
  +                    "Do you want to overwrite it(y/n)? ");
                   BufferedReader in =
                       new BufferedReader(new InputStreamReader(System.in));
                   y = in.readLine();
  @@ -847,43 +879,98 @@
               handleException(ex);
           }
       }
  +    
  +    String getRemoteTragetFileName(String filename, String path) {
  +        
  +        String srcPathName = null;
  +        String target = null;
  +       
  + 
  +
  +        // get traget filename from last portion of filename
  +        StringTokenizer st = new StringTokenizer(filename, "/\\");
  +        while (st.hasMoreTokens()) {
  +            srcPathName = st.nextToken();
  +        }
  +        
  +        
  +        try {
  +                
  +            if (path != null) {
  +                target = checkUri(path);
  +                
  +                // check is path a collection ?
  +                String currentPath = webdavResource.getPath();
  +                
  +                webdavResource.setPath(target);
  +                
  +                if (webdavResource.exists()) {
  +                    if (webdavResource.isCollection()) {
  +                        target += "/" + srcPathName;
  +                    } 
  +                } 
  +                
  +                webdavResource.setPath(currentPath);
  +                
  +            } else {
  +                target = checkUri(getPath() + "/" + srcPathName);
  +            }
  +                
  +                            
  +        } catch (Exception ex) {
  +        }
  +        
  +        return target;
  +               
  +
  +    }
  +    
  +    
  +
   
       void put(String filename, String path)
       {
  -        out.println("put " + filename + " " + path);
  +        String y = "y";
   
  -// FIXME upload URLs !!!!
  -//                if ((count == 2 && src.indexOf(":") > 1) ||
  -//                    count == 1 && dest.indexOf(":") > 1) {
  -//                    URL url = new URL(count == 1 ? dest : src);
  -//                    out.print("Uploading  '" +
  -//                                     ((count == 2) ? src : dest) +
  -//                                     "' to '" + ((count == 2) ?
  -//                                     dest : src) + "': ");
  -//                    if (webdavResource.putMethod(dest, url)) {
  -//                        out.println("succeeded.");
  -//                    } else {
  -//                        out.println("failed.");
  -//                    }
  -//                    continue;
  -//                }
  -
  -        try {
  -            String src = filename;
  -            String dest =  checkUri(path);
  -            File file = new File(dir.getCanonicalPath(), src);
  -            if (file.exists()) {
  -                out.print("Uploading  '" + src + "' to '" + dest + "': ");
  -                if (webdavResource.putMethod(dest, file)) {
  -                    out.println("succeeded.");
  +        try {
  +            String src  = filename;
  +            String dest = getRemoteTragetFileName( filename,  path);
  +            
  +            String currentPath = webdavResource.getPath();
  +            
  +            try {
  +                webdavResource.setPath(dest);
  +                if (webdavResource.exists()) {
  +                    out.print("Aleady exists. " +
  +                              "Do you want to overwrite it(y/n)? ");
  +                    BufferedReader in =
  +                          new BufferedReader(new InputStreamReader(System.in));
  +                    y = in.readLine();
  +                }
  +                webdavResource.setPath(currentPath);
  +            } catch (Exception ex) {
  +            } 
  +            
  +            if (y.trim().equalsIgnoreCase("y") ||
  +                  (y != null && y.length() == 0)) {
  + 
  +            
  +                File file = getFileByPath(src);
  +                
  +                if (file.exists()) {
  +                    out.print("Uploading  '" + file.getCanonicalPath() + "' to '" + 
dest + "'");
  +                    
  +                    if (webdavResource.putMethod(dest, file)) {
  +                        out.println("succeeded.");
  +                    }
  +                    else {
  +                        out.println("failed.");
  +                        out.println(webdavResource.getStatusMessage());
  +                    }
                   }
                   else {
  -                    out.println("failed.");
  -                    out.println(webdavResource.getStatusMessage());
  +                    out.println("Warning: File not exists");
                   }
  -            }
  -            else {
  -                out.println("Warning: File not exists");
               }
           }
           catch (Exception ex) {
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to