[ 
https://issues.apache.org/jira/browse/VFS-236?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12706033#action_12706033
 ] 

Gerrit Cap commented on VFS-236:
--------------------------------

Personally I think that this is a better solution:

### Eclipse Workspace Patch 1.0
#P commons_vfs
Index: 
sandbox/src/main/java/org/apache/commons/vfs/provider/smb/SmbFileObject.java
===================================================================
--- 
sandbox/src/main/java/org/apache/commons/vfs/provider/smb/SmbFileObject.java    
    (revision 771693)
+++ 
sandbox/src/main/java/org/apache/commons/vfs/provider/smb/SmbFileObject.java    
    (working copy)
@@ -82,22 +82,28 @@
                {
                        authData = 
UserAuthenticatorUtils.authenticate(getFileSystem().getFileSystemOptions(), 
SmbFileProvider.AUTHENTICATOR_TYPES);
 
-                       auth = new NtlmPasswordAuthentication(
-                               UserAuthenticatorUtils.toString(
-                                       UserAuthenticatorUtils.getData(
-                                               authData,
-                                               UserAuthenticationData.DOMAIN,
-                                               
UserAuthenticatorUtils.toChar(smbFileName.getDomain()))),
-                               UserAuthenticatorUtils.toString(
-                                       UserAuthenticatorUtils.getData(
-                                               authData,
-                                               UserAuthenticationData.USERNAME,
-                                               
UserAuthenticatorUtils.toChar(smbFileName.getUserName()))),
-                               UserAuthenticatorUtils.toString(
-                                       UserAuthenticatorUtils.getData(
-                                               authData,
-                                               UserAuthenticationData.PASSWORD,
-                                               
UserAuthenticatorUtils.toChar(smbFileName.getPassword()))));
+                       String domain = UserAuthenticatorUtils.toString(
+                               UserAuthenticatorUtils.getData(
+                                       authData,
+                                       UserAuthenticationData.DOMAIN,
+                                       
UserAuthenticatorUtils.toChar(smbFileName.getDomain())));
+                       
+                       String username = UserAuthenticatorUtils.toString(
+                               UserAuthenticatorUtils.getData(
+                                       authData,
+                                       UserAuthenticationData.USERNAME,
+                                       
UserAuthenticatorUtils.toChar(smbFileName.getUserName())));
+                       
+                       String password = UserAuthenticatorUtils.toString(
+                               UserAuthenticatorUtils.getData(
+                                       authData,
+                                       UserAuthenticationData.PASSWORD,
+                                       
UserAuthenticatorUtils.toChar(smbFileName.getPassword())));
+                       
+                       auth = username == null ? 
NtlmPasswordAuthentication.ANONYMOUS : new NtlmPasswordAuthentication(
+                               domain,
+                               username,
+                               password);
 
                        file = new SmbFile(path, auth);
                }


> SmbFileObject throws NPE when used without authentication
> ---------------------------------------------------------
>
>                 Key: VFS-236
>                 URL: https://issues.apache.org/jira/browse/VFS-236
>             Project: Commons VFS
>          Issue Type: Bug
>    Affects Versions: Nightly Builds
>            Reporter: Matt Casters
>             Fix For: 2.0
>
>
> If you have a shared folder without authentication, SmbFileObject throws an 
> NPE in method createSmbFile().
> Here is what I changed to get it to run:
>     private SmbFile createSmbFile(FileName fileName) throws 
> MalformedURLException, SmbException, FileSystemException
>     {
>         SmbFileName smbFileName = (SmbFileName) fileName;
>         String path = smbFileName.getUriWithoutAuth();
>               UserAuthenticationData authData = null;
>               SmbFile file;
>               NtlmPasswordAuthentication auth;
>               try
>               {
>                       authData = 
> UserAuthenticatorUtils.authenticate(getFileSystem().getFileSystemOptions(), 
> SmbFileProvider.AUTHENTICATOR_TYPES);
>                       if (authData!=null) {
>                               auth = new NtlmPasswordAuthentication(
>                                       UserAuthenticatorUtils.toString(
>                                               UserAuthenticatorUtils.getData(
>                                                       authData,
>                                                       
> UserAuthenticationData.DOMAIN,
>                                                       
> UserAuthenticatorUtils.toChar(smbFileName.getDomain()))),
>                                       UserAuthenticatorUtils.toString(
>                                               UserAuthenticatorUtils.getData(
>                                                       authData,
>                                                       
> UserAuthenticationData.USERNAME,
>                                                       
> UserAuthenticatorUtils.toChar(smbFileName.getUserName()))),
>                                       UserAuthenticatorUtils.toString(
>                                               UserAuthenticatorUtils.getData(
>                                                       authData,
>                                                       
> UserAuthenticationData.PASSWORD,
>                                                       
> UserAuthenticatorUtils.toChar(smbFileName.getPassword()))));
>                               file = new SmbFile(path, auth);
>                       } else {
>                               auth=null;
>                               file = new SmbFile(path);
>                       }
>               }
>               finally
>               {
>                       UserAuthenticatorUtils.cleanup(authData);
>               }
>               if (file.isDirectory() && !file.toString().endsWith("/"))
>               {
>                       if (auth!=null) {
>                               file = new SmbFile(path + "/", auth);
>                       } else {
>                               file = new SmbFile(path + "/");
>                       }
>               }
>               return file;
>     }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to