commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ima...@apache.org
Subject svn commit: r210204 - in /jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider: GenericFileName.java smb/SmbFileName.java smb/SmbFileNameParser.java smb/SmbFileObject.java
Date Mon, 11 Jul 2005 20:22:55 GMT
Author: imario
Date: Mon Jul 11 13:22:54 2005
New Revision: 210204

URL: http://svn.apache.org/viewcvs?rev=210204&view=rev
Log:
smb:
now uses NtlmPasswordAuthentication for credentials passing
fix for special characters in username/password
allow domains with DOMAIN\USERNAME syntax

Modified:
    jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/GenericFileName.java
    jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/smb/SmbFileName.java
    jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/smb/SmbFileNameParser.java
    jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/smb/SmbFileObject.java

Modified: jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/GenericFileName.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/GenericFileName.java?rev=210204&r1=210203&r2=210204&view=diff
==============================================================================
--- jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/GenericFileName.java
(original)
+++ jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/GenericFileName.java
Mon Jul 11 13:22:54 2005
@@ -32,8 +32,8 @@
     private final int defaultPort;
     private final String password;
     private final int port;
-    private static final char[] USERNAME_RESERVED = {':', '@'};
-    private static final char[] PASSWORD_RESERVED = {'@'};
+    private static final char[] USERNAME_RESERVED = {':', '@', '/'};
+    private static final char[] PASSWORD_RESERVED = {'@', '/'};
 
     protected GenericFileName(final String scheme,
                               final String hostName,
@@ -117,6 +117,20 @@
     {
         buffer.append(getScheme());
         buffer.append("://");
+        appendCredentials(buffer);
+        buffer.append(hostName);
+        if (port != getDefaultPort())
+        {
+            buffer.append(':');
+            buffer.append(port);
+        }
+    }
+
+    /**
+     * append the user credentials
+     */
+    protected void appendCredentials(StringBuffer buffer)
+    {
         if (userName != null && userName.length() != 0)
         {
             UriParser.appendEncoded(buffer, userName, USERNAME_RESERVED);
@@ -126,12 +140,6 @@
                 UriParser.appendEncoded(buffer, password, PASSWORD_RESERVED);
             }
             buffer.append('@');
-        }
-        buffer.append(hostName);
-        if (port != getDefaultPort())
-        {
-            buffer.append(':');
-            buffer.append(port);
         }
     }
 }

Modified: jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/smb/SmbFileName.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/smb/SmbFileName.java?rev=210204&r1=210203&r2=210204&view=diff
==============================================================================
--- jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/smb/SmbFileName.java
(original)
+++ jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/smb/SmbFileName.java
Mon Jul 11 13:22:54 2005
@@ -16,6 +16,7 @@
 package org.apache.commons.vfs.provider.smb;
 
 import org.apache.commons.vfs.FileName;
+import org.apache.commons.vfs.FileSystemException;
 import org.apache.commons.vfs.provider.GenericFileName;
 
 /**
@@ -30,6 +31,8 @@
     private static final int DEFAULT_PORT = 139;
 
     private final String share;
+    private final String domain;
+    private String uriWithoutAuth;
 
     protected SmbFileName(
         final String scheme,
@@ -37,11 +40,13 @@
         final int port,
         final String userName,
         final String password,
+        final String domain,
         final String share,
         final String path)
     {
         super(scheme, hostName, port, DEFAULT_PORT, userName, password, path);
         this.share = share;
+        this.domain = domain;
     }
 
     /**
@@ -62,6 +67,16 @@
         buffer.append(share);
     }
 
+    protected void appendCredentials(StringBuffer buffer)
+    {
+        if (getUserName() != null && getUserName().length() != 0)
+        {
+            buffer.append(getDomain());
+            buffer.append("\\");
+        }
+        super.appendCredentials(buffer);
+    }
+
     /**
      * Factory method for creating name instances.
      */
@@ -73,7 +88,37 @@
             getPort(),
             getUserName(),
             getPassword(),
+            domain,
             share,
             path);
+    }
+
+    /**
+     * Construct the path suitable for SmbFile when used with NtlmPasswordAuthentication
+     */
+    public String getUriWithoutAuth() throws FileSystemException
+    {
+        if (uriWithoutAuth != null)
+        {
+            return uriWithoutAuth;
+        }
+
+        StringBuffer sb = new StringBuffer(120);
+        sb.append(getScheme());
+        sb.append("://");
+        sb.append(getHostName());
+        sb.append("/");
+        sb.append(getShare());
+        sb.append(getPathDecoded());
+        uriWithoutAuth = sb.toString();
+        return uriWithoutAuth;
+    }
+
+    /**
+     * returns the domain name
+     */
+    public String getDomain()
+    {
+        return domain;
     }
 }

Modified: jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/smb/SmbFileNameParser.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/smb/SmbFileNameParser.java?rev=210204&r1=210203&r2=210204&view=diff
==============================================================================
--- jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/smb/SmbFileNameParser.java
(original)
+++ jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/smb/SmbFileNameParser.java
Mon Jul 11 13:22:54 2005
@@ -46,6 +46,14 @@
         // Extract the scheme and authority parts
         final Authority auth = extractToPath(filename, name);
 
+        // extract domain
+        String username = auth.userName;
+        String domain = extractDomain(username);
+        if (domain != null)
+        {
+            username = username.substring(domain.length()+1);
+        }
+
         // Decode and adjust separators
         UriParser.canonicalizePath(name, 0, name.length(), this);
         UriParser.fixSeparators(name);
@@ -66,9 +74,23 @@
             auth.scheme,
             auth.hostName,
             auth.port,
-            auth.userName,
+            username,
             auth.password,
+            domain,
             share,
             path);
+    }
+
+    private String extractDomain(String username)
+    {
+        for (int i = 0; i < username.length(); i++)
+        {
+            if (username.charAt(i) == '\\')
+            {
+                return username.substring(0, i);
+            }
+        }
+
+        return null;
     }
 }

Modified: jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/smb/SmbFileObject.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/smb/SmbFileObject.java?rev=210204&r1=210203&r2=210204&view=diff
==============================================================================
--- jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/smb/SmbFileObject.java
(original)
+++ jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/smb/SmbFileObject.java
Mon Jul 11 13:22:54 2005
@@ -19,6 +19,7 @@
 import jcifs.smb.SmbFile;
 import jcifs.smb.SmbFileInputStream;
 import jcifs.smb.SmbFileOutputStream;
+import jcifs.smb.NtlmPasswordAuthentication;
 import org.apache.commons.vfs.FileName;
 import org.apache.commons.vfs.FileObject;
 import org.apache.commons.vfs.FileSystemException;
@@ -42,14 +43,14 @@
     extends AbstractFileObject
     implements FileObject
 {
-    private final String fileName;
+    // private final String fileName;
     private SmbFile file;
 
     protected SmbFileObject(final FileName name,
                             final SmbFileSystem fileSystem) throws FileSystemException
     {
         super(name, fileSystem);
-        this.fileName = UriParser.decode(name.getURI());
+        // this.fileName = UriParser.decode(name.getURI());
     }
 
     /**
@@ -60,7 +61,7 @@
         // Defer creation of the SmbFile to here
         if (file == null)
         {
-            file = createSmbFile(fileName);
+            file = createSmbFile(getName());
         }
     }
 
@@ -70,19 +71,21 @@
         file = null;
     }
 
-    private SmbFile createSmbFile(String path) throws MalformedURLException, SmbException
+    private SmbFile createSmbFile(FileName fileName) throws MalformedURLException, SmbException,
FileSystemException
     {
-        SmbFile f = new SmbFile(path);
-        return createSmbFile(f);
-    }
+        SmbFileName smbFileName = (SmbFileName) fileName;
+
+        String path = smbFileName.getUriWithoutAuth();
+
+        NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(
+            smbFileName.getDomain(), smbFileName.getUserName(), smbFileName.getPassword());
+        SmbFile file = new SmbFile(path, auth);
 
-    private SmbFile createSmbFile(SmbFile file) throws MalformedURLException, SmbException
-    {
         if (file.isDirectory() && !file.toString().endsWith("/"))
         {
-            String listDirPath = file.toString() + "/";
-            file = new SmbFile(listDirPath);
+            file = new SmbFile(path + "/", auth);
         }
+
         return file;
     }
 
@@ -135,7 +138,7 @@
 
     protected void doRename(FileObject newfile) throws Exception
     {
-        file.renameTo(createSmbFile(newfile.getName().getURI()));
+        file.renameTo(createSmbFile(newfile.getName()));
     }
 
     /**
@@ -144,7 +147,7 @@
     protected void doCreateFolder() throws Exception
     {
         file.mkdir();
-        file = createSmbFile(file);
+        file = createSmbFile(getName());
     }
 
     /**



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message