1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.millscript.commons.vfs.protocols.ftp;
22
23 import org.millscript.commons.alert.alerts.IOAlert;
24 import org.millscript.commons.vfs.VFS;
25 import org.millscript.commons.vfs.VFolder;
26 import org.millscript.commons.vfs.alerts.VHostNotFoundAlert;
27 import org.millscript.commons.vfs.protocols.AbstractProtocolVVolume;
28
29 import gnu.inet.ftp.FTPConnection;
30
31 import java.io.IOException;
32 import java.net.URI;
33 import java.net.UnknownHostException;
34
35 /**
36 * The class implements a virtual filesystem volume for an FTP connection.
37 */
38 public class FtpVolume extends AbstractProtocolVVolume {
39
40 /**
41 * The underlying FTP connection we will be using.
42 */
43 final FTPConnection ftpConnection;
44
45 /**
46 * Constructs a new FTP volume
47 *
48 * @param v the VFS that was used to create this volume
49 * @param root
50 */
51 public FtpVolume( final VFS v, final URI root ) {
52 super( v, root );
53 try {
54 this.ftpConnection = new FTPConnection( root.getHost(), root.getPort() );
55 final String userinfo = root.getUserInfo();
56 final int n = userinfo.indexOf( ':' );
57 if ( n < 0 ) {
58
59
60 this.ftpConnection.authenticate( userinfo, null );
61 } else {
62 final String username = userinfo.substring( 0, n );
63 final String password = userinfo.substring( n + 1 );
64 this.ftpConnection.authenticate( username, password );
65 }
66 } catch ( UnknownHostException ex ) {
67 throw VHostNotFoundAlert.hostCannotBeResolved( root ).setParentThrowable( ex ).mishap();
68 } catch ( IOException ex ) {
69 throw new IOAlert(
70 "A problem occured trying to make connection to FTP server"
71 ).setParentThrowable( ex ).mishap();
72 }
73 }
74
75 /**
76 * @see org.millscript.commons.vfs.protocols.AbstractProtocolVVolume#makeRootVFolder()
77 */
78 @Override
79 protected VFolder makeRootVFolder() {
80 return new FtpFolder( this, null, null );
81 }
82
83 }