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.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
71 if ( vol.ftpConnection.changeWorkingDirectory( uri.toString() ) ) {
72
73 return false;
74 } else {
75
76
77
78
79 final InputStream listStream = vol.ftpConnection.list( uri.toString() );
80 listStream.close();
81
82 return true;
83 }
84 } catch ( IOException ex ) {
85
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
101 throw VCannotOpenEntryAlert.entryIsNotAFile( this ).setParentThrowable( ex ).mishap();
102 } catch ( IOException ex ) {
103
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
119 throw VCannotOpenEntryAlert.fileNotFound( this ).setParentThrowable( ex ).mishap();
120 } catch ( IOException ex ) {
121
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
137 throw VCannotOpenEntryAlert.entryIsNotAFile( this ).setParentThrowable( ex ).mishap();
138 } catch ( IOException ex ) {
139
140 throw new IOAlert(
141 "Cannot dispatch the FTP request"
142 ).culprit( "message", ex.getMessage() ).setParentThrowable( ex ).mishap();
143 }
144 }
145
146 }