View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   // MillScript: an Open Spice interpreter and batch website creation tool
3   // Copyright (C) 2005 Kevin Rogers
4   //
5   // This file is part of MillScript.
6   //
7   // MillScript 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 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; 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.AbstractVFile;
25  import org.millscript.commons.vfs.VFolder;
26  import org.millscript.commons.vfs.alerts.VCannotOpenEntryAlert;
27  
28  import gnu.inet.ftp.FTPException;
29  
30  import java.io.IOException;
31  import java.io.InputStream;
32  import java.io.OutputStream;
33  
34  /**
35   * This class implements a virtual filesystem file for an FTP filesystem file.
36   */
37  public class FtpFile extends AbstractVFile< FtpVolume > {
38  
39      /**
40       * Constructs a new FTP virtual filesystem file with the specified name,
41       * parent folder and volume.
42       *
43       * @param vol   the volume this entry is held on
44       * @param parent    the parent folder for this file
45       * @param name  the name of the file
46       */
47      FtpFile( final FtpVolume vol, final VFolder parent, final String name ) {
48          super( vol, parent, name );
49      }
50  
51      /**
52       * Constructs a new FTP virtual filesystem file with the specified name,
53       * parent folder. This file will belong to the same volume as the specified
54       * parent folder.
55       *
56       * @param parent    the parent folder for this file
57       * @param name  the name of the file
58       */
59      FtpFile( final FtpFolder parent, final String name ) {
60          super( parent.getVolume(), parent, name );
61      }
62  
63      /**
64       * @see org.millscript.commons.vfs.VEntry#exists()
65       */
66      public boolean exists() {
67          final StringBuffer uri = this.appendAbsolutePath( new StringBuffer() );
68          final FtpVolume vol = this.getVolume();
69          try {
70              // Treat the entry as a folder and try to change into it
71              if ( vol.ftpConnection.changeWorkingDirectory( uri.toString() ) ) {
72                  // Ooo, we succeeded, so this entry is a folder
73                  return false;
74              } else {
75                  // Ok, the segment isn't a folder so let's try and find out if
76                  // it exists. We can't use STAT, so lets try LIST.
77                  // TODO - Check this does throw an FTPException if the path
78                  // doesn't exist
79                  final InputStream listStream = vol.ftpConnection.list( uri.toString() );
80                  listStream.close();
81                  // We're still here, so this entry must exist and be a file
82                  return true;
83              }
84          } catch ( IOException ex ) {
85              // Alert - an i/o problem
86              throw new IOAlert(
87                  "Cannot dispatch the FTP request"
88              ).culprit( "message", ex.getMessage() ).setParentThrowable( ex ).mishap();
89          }
90      }
91  
92      /**
93       * @see org.millscript.commons.vfs.VFile#getAppendOutputStream()
94       */
95      public OutputStream getAppendOutputStream() {
96          final StringBuffer uri = this.appendAbsolutePath( new StringBuffer() );
97          try {
98              return this.getVolume().ftpConnection.append( uri.toString() );
99          } catch ( FTPException ex ) {
100             // TODO - This should be a little bit better
101             throw VCannotOpenEntryAlert.entryIsNotAFile( this ).setParentThrowable( ex ).mishap();
102         } catch ( IOException ex ) {
103             // Alert - an i/o problem
104             throw new IOAlert(
105                 "Cannot dispatch the FTP request"
106             ).culprit( "message", ex.getMessage() ).setParentThrowable( ex ).mishap();
107         }
108     }
109 
110     /**
111      * @see org.millscript.commons.vfs.VFile#getInputStream()
112      */
113     public InputStream getInputStream() {
114         final StringBuffer uri = this.appendAbsolutePath( new StringBuffer() );
115         try {
116             return this.getVolume().ftpConnection.retrieve( uri.toString() );
117         } catch ( FTPException ex ) {
118             // TODO - This should be a little bit better
119             throw VCannotOpenEntryAlert.fileNotFound( this ).setParentThrowable( ex ).mishap();
120         } catch ( IOException ex ) {
121             // Alert - an i/o problem
122             throw new IOAlert(
123                 "Cannot dispatch the FTP request"
124             ).culprit( "message", ex.getMessage() ).setParentThrowable( ex ).mishap();
125         }
126     }
127 
128     /**
129      * @see org.millscript.commons.vfs.VFile#getOutputStream()
130      */
131     public OutputStream getOutputStream() {
132         final StringBuffer uri = this.appendAbsolutePath( new StringBuffer() );
133         try {
134             return this.getVolume().ftpConnection.store( uri.toString() );
135         } catch ( FTPException ex ) {
136             // TODO - This should be a little bit better
137             throw VCannotOpenEntryAlert.entryIsNotAFile( this ).setParentThrowable( ex ).mishap();
138         } catch ( IOException ex ) {
139             // Alert - an i/o problem
140             throw new IOAlert(
141                 "Cannot dispatch the FTP request"
142             ).culprit( "message", ex.getMessage() ).setParentThrowable( ex ).mishap();
143         }
144     }
145 
146 }