View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   // MillScript-Commons: an Open Spice interpreter and batch website creation tool
3   // Copyright (C) 2005 Kevin Rogers
4   //
5   // This file is part of MillScript-Commons.
6   //
7   // MillScript-Commons is free software; you can redistribute it and/or modify it under
8   // the terms of the GNU General Public License as published by the Free
9   // Software Foundation; either version 2 of the License, or (at your option)
10  // any later version.
11  //
12  // MillScript-Commons is distributed in the hope that it will be useful, but WITHOUT
13  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  // more details.
16  //
17  // You should have received a copy of the GNU General Public License along with
18  // MillScript-Commons; if not, write to the Free Software Foundation, Inc., 59 Temple
19  // Place, Suite 330, Boston, MA  02111-1307  USA
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                  // no password in user info, lets try and authenticate without
59                  // a password
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  }