KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > AuthListFiles


1 import jcifs.netbios.NbtAddress;
2 import jcifs.util.*;
3 import jcifs.smb.*;
4 import java.util.Date JavaDoc;
5
6 public class AuthListFiles extends NtlmAuthenticator {
7
8     public static String JavaDoc readLine() throws Exception JavaDoc {
9         int c;
10         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
11         while(( c = System.in.read() ) != '\n' ) {
12             if( c == -1 ) return "";
13             sb.append( (char)c );
14         }
15         return sb.toString().trim();
16     }
17
18     public AuthListFiles( String JavaDoc[] argv ) throws Exception JavaDoc {
19         NtlmAuthenticator.setDefault( this );
20
21         SmbFile file = new SmbFile( argv[0] );
22
23         SmbFile[] files = file.listFiles();
24
25         for( int i = 0; i < files.length; i++ ) {
26             System.out.print( " " + files[i].getName() );
27         }
28         System.out.println();
29     }
30
31     protected NtlmPasswordAuthentication getNtlmPasswordAuthentication() {
32         System.out.println( getRequestingException().getMessage() + " for " + getRequestingURL() );
33         System.out.print( "username: " );
34         try {
35             int i;
36             String JavaDoc username = readLine();
37             String JavaDoc domain = null, password;
38
39             if(( i = username.indexOf( '\\' )) != -1 ) {
40                 domain = username.substring( 0, i );
41                 username = username.substring( i + 1 );
42             }
43             System.out.print( "password: " );
44             password = readLine();
45             if( password.length() == 0 ) {
46                 return null;
47             }
48             return new NtlmPasswordAuthentication( domain, username, password );
49         } catch( Exception JavaDoc e ) {
50         }
51         return null;
52     }
53
54
55     public static void main( String JavaDoc[] argv ) throws Exception JavaDoc {
56         new AuthListFiles( argv );
57     }
58 }
59
Popular Tags