com.glub.secureftp.bean
Class FTP

java.lang.Object
  extended by com.glub.secureftp.bean.FTP
Direct Known Subclasses:
SSLFTP

public class FTP
extends Object

The FTP class is responsible for handling the basic operations of the File Transfer Protocol.

Since:
2.5.5
Version:
$Revision: 1.130 $, $Date: 2007/11/27 07:12:46 $

Field Summary
static short ACTIVE_CONNECTION_TYPE
          Used to set the data connection type to active.
static short ASCII_TRANSFER_MODE
          Used to set the data transfer mode to ascii.
static short AUTO_TRANSFER_MODE
          Used to set the data transfer mode to auto.
static short BINARY_TRANSFER_MODE
          Used to set the data transfer mode to binary; also known as image.
protected  FTPCommand command
          This handles the FTP commands.
protected  boolean isConnected
          If we are connected, this is true.
protected  boolean isLoggedIn
          If we are logged in, this is true.
protected  boolean modeZEnabled
          If mode z is enabled, this is true.
static short PASSIVE_CONNECTION_TYPE
          Used to set the data connection type to passive.
static short PASV_CONNECTION_TYPE
          Used to set the data connection type to passive.
protected  OutputStream recvCmdStream
          This stream is used to print the responses returned from the server.
protected  OutputStream sendCmdStream
          This stream is used to print the commands sent to the server.
 
Constructor Summary
FTP(HostInfo hostInfo)
          Create a new FTP object without response notification.
FTP(HostInfo hostInfo, OutputStream sendCmdStream, OutputStream recvCmdStream)
          Create a new FTP object with response notification.
FTP(String host, int port)
          Create a new FTP object without response notification.
FTP(String host, int port, OutputStream sendCmdStream, OutputStream recvCmdStream)
          Create a new FTP object with response notification.
 
Method Summary
 void abort(FTPAbortableTransfer abort)
          Abort a data transfer from the FTP server.
protected  void aboutToTransferData()
          Called before data transfers begin.
 void append(File file, RemoteFile appendTo)
          Append to a file on the FTP server.
 void append(File file, RemoteFile appendTo, FTPAbortableTransfer abort)
          Append to a file on the FTP server with the ability to abort the data transfer.
 void append(File file, RemoteFile appendTo, Progress progress)
          Append to a file on the FTP server with progress information.
 void append(File file, RemoteFile appendTo, Progress progress, FTPAbortableTransfer abort)
          Append to a file on the FTP server with progress information and the ability to abort the data transfer.
 void append(File file, String appendTo)
          Append to a file on the FTP server.
 void append(File file, String appendTo, FTPAbortableTransfer abort)
          Append to a file on the FTP server with the ability to abort the data transfer.
 void append(File file, String appendTo, Progress progress)
          Append to a file on the FTP server with progress information.
 void append(File file, String appendTo, Progress progress, FTPAbortableTransfer abort)
          Append to a file on the FTP server with progress information and the ability to abort the data transfer.
 void append(InputStream inputStream, long localFileSize, RemoteFile appendTo, Progress progress)
          Append to a file on the FTP server with progress information.
 void append(InputStream inputStream, long localFileSize, RemoteFile appendTo, Progress progress, FTPAbortableTransfer abort)
          Append to a file on the FTP server with progress information and the ability to abort the data transfer.
 void append(InputStream inputStream, long localFileSize, String appendTo, Progress progress)
          Append to a file on the FTP server with progress information.
 void append(InputStream inputStream, long localFileSize, String appendTo, Progress progress, FTPAbortableTransfer abort)
          Append to a file on the FTP server with progress information and the ability to abort the data transfer.
 void append(InputStream inputStream, RemoteFile appendTo)
          Append to a file on the FTP server.
 void append(InputStream inputStream, RemoteFile appendTo, FTPAbortableTransfer abort)
          Append to a file on the FTP server with the ability to abort the data transfer.
 void append(InputStream inputStream, String appendTo)
          Append to a file on the FTP server.
 void append(InputStream inputStream, String appendTo, FTPAbortableTransfer abort)
          Append to a file on the FTP server with the ability to abort the data transfer.
 void ascii()
          Set the data transfer mode to ascii.
 void auto()
          Set the data transfer mode to auto.
 void binary()
          Set the data transfer mode to binary (or image).
 void cdup()
          Change up one directory on the FTP server.
 void chdir(RemoteFile dir)
          Change to a remote directory on the FTP server.
 void chdir(String dir)
          Change to a remote directory on the FTP server.
 void connect()
          Connect to the FTP host and port.
 void delete(RemoteFile fileName)
          Delete a file from the FTP server.
 void delete(String fileName)
          Delete a file from the FTP server.
 void forcePasvToUseControlIP(boolean on)
          Forces passive data transfers to use the control socket IP address.
 String getAccount()
          Get the account name.
 int getConnectionType()
          The data connection type is either passive or active.
 Socket getControlSocket()
          Get the control socket.
static String getDateStamp()
          The datestamp of the bean.
 FTPCommand getFTPCommand()
          Get the FTPCommand object.
 String getHostName()
          Get the hostname of the FTP server.
 short getListStyle()
          Get the server listing style of the FTP server.
 String getPassword()
          Get the password.
 int getPort()
          Get the port of the FTP server.
 short getTransferMode()
          The data transfer mode is either auto, ascii, or binary (image).
 String getUser()
          Get the username.
static String getVersion()
          The version of the bean.
 String help(String item)
          Get the server's remote help.
 boolean isConnected()
          Whether or not currently connected.
 boolean isLoggedIn()
          Whether or not currently logged in.
 boolean isTransferRestartable()
          Whether or not the server supports restarting broken data transfers.
 RemoteFileList list()
          List the current remote directory.
 RemoteFileList list(FTPAbortableTransfer abort)
          List the current remote directory with the ability to abort the listing.
 RemoteFileList list(RemoteFile file)
          List items on the remote FTP server.
 RemoteFileList list(RemoteFile file, FTPAbortableTransfer abort, boolean showHidden)
          List items on the remote FTP server with the ability to abort the listing.
 RemoteFileList list(String itemsToList)
          List items on the remote FTP server.
 RemoteFileList list(String itemsToList, FTPAbortableTransfer abort, boolean showHidden)
          List items on the remote FTP server with the ability to abort the listing.
 RemoteFileList listAll()
          List the current remote directory, including hidden files.
 RemoteFileList listAll(FTPAbortableTransfer abort)
          List the current remote directory including hidden files with the ability to abort the listing.
 void login(String user, String pass)
          Login to the FTP server.
 void login(String user, String pass, String acct)
          Login to the FTP server.
 void logout()
          Logout from the FTP server.
protected  Socket makeControlSocket(HostInfo hostInfo)
          Make a new control socket.
protected  ServerSocket makeDataServerSocket(HostInfo hostInfo)
          Make a new data server socket.
protected  Socket makeDataSocket(HostInfo hostInfo)
          Make a new data socket.
protected  FTPCommand makeFTPCommand(BufferedReader inputReader, PrintWriter outputWriter)
          Set the FTPCommand object.
 void mkdir(String newDir)
          Make a new directory on the FTP server.
 void modeZ()
          Set mode z (on-the-fly compression) data transfer.
 void noop()
          Send a noop command to the FTP server.
protected  HostInfo pasv()
          Setup a pasv data connection.
protected  void port(HostInfo hostInfo)
          Setup an active data connection.
 String pwd()
          Get the current directory on the FTP server.
 void raw(String rawCmd)
          Send a raw command to the FTP server.
 void rename(String from, String to)
          Rename a file on the FTP server.
 void retrieve(RemoteFile remoteFile, File outputFile, boolean restartXfer)
          Retrieve a file from the FTP server.
 void retrieve(RemoteFile remoteFile, File outputFile, boolean restartXfer, FTPAbortableTransfer abort)
          Retrieve a file from the FTP server with the ability to abort the transfer.
 void retrieve(RemoteFile remoteFile, File outputFile, boolean restartXfer, Progress progress)
          Retrieve a file from the FTP server with progress information.
 void retrieve(RemoteFile remoteFile, File outputFile, boolean restartXfer, Progress progress, FTPAbortableTransfer abort)
          Retrieve a file from the FTP server with progress information and the ability to abort the transfer.
 void retrieve(RemoteFile remoteFile, OutputStream outputStream)
          Retrieve a file from the FTP server.
 void retrieve(RemoteFile remoteFile, OutputStream outputStream, FTPAbortableTransfer abort)
          Retrieve a file from the FTP server with the ability to abort the transfer.
 void retrieve(RemoteFile remoteFile, OutputStream outputStream, Progress progress)
          Retrieve a file from the FTP server with progress information.
 void retrieve(RemoteFile remoteFile, OutputStream outputStream, Progress progress, FTPAbortableTransfer abort)
          Retrieve a file from the FTP server with progress information and the ability to abort the transfer.
 void retrieve(String remoteFile, File outputFile, boolean restartXfer)
          Retrieve a file from the FTP server.
 void retrieve(String remoteFile, File outputFile, boolean restartXfer, FTPAbortableTransfer abort)
          Retrieve a file from the FTP server with the ability to abort the transfer.
 void retrieve(String remoteFile, File outputFile, boolean restartXfer, Progress progress)
          Retrieve a file from the FTP server with progress information.
 void retrieve(String remoteFile, File outputFile, boolean restartXfer, Progress progress, FTPAbortableTransfer abort)
          Retrieve a file from the FTP server with progress information and the ability to abort the transfer.
 void retrieve(String remoteFile, OutputStream outputStream)
          Retrieve a file from the FTP server.
 void retrieve(String remoteFile, OutputStream outputStream, FTPAbortableTransfer abort)
          Retrieve a file from the FTP server with the ability to abort the transfer.
 void retrieve(String remoteFile, OutputStream outputStream, Progress progress)
          Retrieve a file from the FTP server with progress information.
 void retrieve(String remoteFile, OutputStream outputStream, Progress progress, FTPAbortableTransfer abort)
          Retrieve a file from the FTP server with progress information and the ability to abort the transfer.
 void rmdir(RemoteFile dir)
          Remote a directory from the FTP server.
 void rmdir(String dir)
          Remote a directory from the FTP server.
 void sendAccount(String acct)
          Sends the account to the FTP server.
 void sendPassword(String pass)
          Sends the password to the FTP server.
 void sendUserName(String user)
          Sends the username to the FTP server.
 void setAccount(String account)
          Set the account name.
 void setConnectionType(short type)
          Set the data connection type.
protected  void setControlSocket(Socket controlSocket)
          Set the control socket.
 void setHostName(String hostName)
          Set the hostname of the FTP server.
 void setListStyle(short listStyle)
          Set the server type to help with the listing style of the FTP server.
 void setPassword(String password)
          Set the password.
 void setPort(int port)
          Set the port of the FTP server.
 void setRecvCmdStream(OutputStream recvCmdStream)
          Set the stream responsible for handling respones from the FTP server.
 void setSendCmdStream(OutputStream sendCmdStream)
          Set the stream responsible for handling commands sent to the FTP server.
 void setSocksIVProxy(String host, int port)
          Set the Socks IV server proxy.
 void setSocksVProxy(String host, int port, String username, String password)
          Set the Socks V server proxy.
 void setStringDataAsUTF8(boolean on)
          Set server to treat string data in UTF8 format
 void setUser(String user)
          Set the username.
 long size(RemoteFile file)
          Get the size of a file on the FTP server.
 long size(String file)
          Get the size of a file on the FTP server.
 void store(File file, boolean restartXfer)
          Store a file to the FTP server.
 void store(File file, boolean restartXfer, FTPAbortableTransfer abort)
          Store a file to the FTP server with the ability to abort the data transfer.
 void store(File file, boolean restartXfer, Progress progress)
          Store a file to the FTP server with progress information.
 void store(File file, boolean restartXfer, Progress progress, FTPAbortableTransfer abort)
          Store a file to the FTP server with progress information and the ability to abort the data transfer.
 void store(File file, String name, boolean restartXfer)
          Store a file to the FTP server with a specific filename.
 void store(File file, String name, boolean restartXfer, FTPAbortableTransfer abort)
          Store a file to the FTP server with a specific filename and with the ability to abort the data transfer.
 void store(File file, String name, boolean restartXfer, Progress progress)
          Store a file to the FTP server with a specific filename and with progress information.
 void store(File file, String name, boolean restartXfer, Progress progress, FTPAbortableTransfer abort)
          Store a file to the FTP server with a specific filename and progress information and the ability to abort the data transfer.
 void store(InputStream inputStream)
          Store a file to the FTP server.
 void store(InputStream inputStream, FTPAbortableTransfer abort)
          Store a file to the FTP server with the ability to abort the data transfer.
 void store(InputStream inputStream, long localFileSize, Progress progress)
          Store a file to the FTP server with progress information.
 void store(InputStream inputStream, long localFileSize, Progress progress, FTPAbortableTransfer abort)
          Store a file to the FTP server with progress information and the ability to abort the data transfer.
 void store(InputStream inputStream, long localFileSize, String name, Progress progress)
          Store a file to the FTP server with a specific filename and with progress information.
 void store(InputStream inputStream, long localFileSize, String name, Progress progress, FTPAbortableTransfer abort)
          Store a file to the FTP server with a specific filename and progress information and the ability to abort the data transfer.
 void store(InputStream inputStream, String name)
          Store a file to the FTP server with a specific filename.
 void store(InputStream inputStream, String name, FTPAbortableTransfer abort)
          Store a file to the FTP server with a specific filename and with the ability to abort the data transfer.
 boolean stringDataAsUTF8()
          Whether or not the server is treating the string data as UTF8
 Date time(RemoteFile file)
          Get the modification time of a file on the FTP server.
 Date time(String file)
          Get the modification time of a file on the FTP server.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ASCII_TRANSFER_MODE

public static final short ASCII_TRANSFER_MODE
Used to set the data transfer mode to ascii.

See Also:
Constant Field Values

BINARY_TRANSFER_MODE

public static final short BINARY_TRANSFER_MODE
Used to set the data transfer mode to binary; also known as image.

See Also:
Constant Field Values

AUTO_TRANSFER_MODE

public static final short AUTO_TRANSFER_MODE
Used to set the data transfer mode to auto. This mode attempts to guess which transfer mode (ascii or binary) to set based on file type.

See Also:
Constant Field Values

PASV_CONNECTION_TYPE

public static final short PASV_CONNECTION_TYPE
Used to set the data connection type to passive.

See Also:
Constant Field Values

PASSIVE_CONNECTION_TYPE

public static final short PASSIVE_CONNECTION_TYPE
Used to set the data connection type to passive.

See Also:
Constant Field Values

ACTIVE_CONNECTION_TYPE

public static final short ACTIVE_CONNECTION_TYPE
Used to set the data connection type to active.

See Also:
Constant Field Values

modeZEnabled

protected boolean modeZEnabled
If mode z is enabled, this is true.


isConnected

protected boolean isConnected
If we are connected, this is true.


isLoggedIn

protected boolean isLoggedIn
If we are logged in, this is true.


recvCmdStream

protected OutputStream recvCmdStream
This stream is used to print the responses returned from the server.


sendCmdStream

protected OutputStream sendCmdStream
This stream is used to print the commands sent to the server.


command

protected FTPCommand command
This handles the FTP commands.

Constructor Detail

FTP

public FTP(HostInfo hostInfo)
Create a new FTP object without response notification.

Parameters:
hostInfo - the HostInfo to connect to.

FTP

public FTP(String host,
           int port)
Create a new FTP object without response notification.

Parameters:
host - the hostname to connect to.
port - the port to connect to.

FTP

public FTP(HostInfo hostInfo,
           OutputStream sendCmdStream,
           OutputStream recvCmdStream)
Create a new FTP object with response notification.

Parameters:
hostInfo - the HostInfo to connect to.
sendCmdStream - the commands sent to the server. Pass null if not interested in this data.
recvCmdStream - the responses returned from the server. Pass null if not interested in this data.

FTP

public FTP(String host,
           int port,
           OutputStream sendCmdStream,
           OutputStream recvCmdStream)
Create a new FTP object with response notification.

Parameters:
host - the hostname to connect to.
port - the port to connect to.
sendCmdStream - the commands sent to the server. Pass null if not interested in this data.
recvCmdStream - the responses returned from the server. Pass null if not interested in this data.
Method Detail

connect

public void connect()
             throws FTPConnectException,
                    FTPException,
                    IOException,
                    UnknownHostException,
                    IllegalArgumentException
Connect to the FTP host and port. If the port was not set, we default to 21.

Throws:
FTPConnectException - if the connection fails.
FTPException - if the FTP server returns an error code.
IOException - if there are socket problems.
UnknownHostException - if the host could not be found.
IllegalArgumentException - if hostName is null.

login

public void login(String user,
                  String pass)
           throws IOException,
                  FTPBadLoginException,
                  FTPConnectException,
                  FTPException,
                  IllegalArgumentException
Login to the FTP server.

Parameters:
user - the username to login as.
pass - the password to login as.
Throws:
IOException - if there is a socket problem.
FTPBadLoginException - if there is a problem logging in.
FTPConnectException - if this is called prior to connect
FTPException - if the FTP server returns an error code.
IllegalArgumentException - if there is a problem with any of the passed in args.
See Also:
connect()

login

public void login(String user,
                  String pass,
                  String acct)
           throws IOException,
                  FTPBadLoginException,
                  FTPConnectException,
                  FTPException,
                  IllegalArgumentException
Login to the FTP server.

Parameters:
user - the username to login as.
pass - the password to login as.
acct - the account to use.
Throws:
IOException - if there is a socket problem.
FTPBadLoginException - if there is a problem logging in.
FTPConnectException - if this is called prior to connect
FTPException - if the FTP server returns an error code.
IllegalArgumentException - if there is a problem with any of the passed in args.
See Also:
connect()

logout

public void logout()
            throws IOException,
                   FTPException
Logout from the FTP server.

Throws:
IOException - if there is a socket problem.
FTPException - if the FTP server returns an error code.

sendUserName

public void sendUserName(String user)
                  throws FTPNeedPasswordException,
                         FTPNeedAccountException,
                         FTPBadLoginException,
                         FTPException,
                         IllegalArgumentException
Sends the username to the FTP server.

Parameters:
user - the username.
Throws:
FTPNeedPasswordException - if a password is required.
FTPNeedAccountException - if an accound is required.
FTPBadLoginException - if there is a problem logging in.
FTPException - if the FTP server returns an error code.
IllegalArgumentException - if there is a problem with the username.

sendPassword

public void sendPassword(String pass)
                  throws FTPNeedAccountException,
                         FTPBadLoginException,
                         FTPException,
                         IllegalArgumentException
Sends the password to the FTP server.

Parameters:
pass - the password.
Throws:
FTPNeedAccountException - if an accound is required.
FTPBadLoginException - if there is a problem logging in.
FTPException - if the FTP server returns an error code.
IllegalArgumentException - if there is a problem with the password.

sendAccount

public void sendAccount(String acct)
                 throws FTPBadLoginException,
                        FTPException,
                        IllegalArgumentException
Sends the account to the FTP server.

Parameters:
acct - the account.
Throws:
FTPBadLoginException - if there is a problem logging in.
FTPException - if the FTP server returns an error code.
IllegalArgumentException - if there is a problem with the account.

listAll

public RemoteFileList listAll()
                       throws FTPException,
                              IOException
List the current remote directory, including hidden files. Subject to availiblity on certain servers.

Based on what getConnectionType returns, either pasv or port will be sent in this routine.

Returns:
a list of RemoteFile objects as a RemoteFileList.
Throws:
FTPException - if the FTP server returns an error code.
IOException - if there is a socket problem.
See Also:
RemoteFile, getConnectionType(), setConnectionType(short), pasv(), port(HostInfo)

list

public RemoteFileList list()
                    throws FTPException,
                           IOException
List the current remote directory.

Based on what getConnectionType returns, either pasv or port will be sent in this routine.

Returns:
a list of RemoteFile objects as a RemoteFileList.
Throws:
FTPException - if the FTP server returns an error code.
IOException - if there is a socket problem.
See Also:
RemoteFile, getConnectionType(), setConnectionType(short), pasv(), port(HostInfo)

list

public RemoteFileList list(FTPAbortableTransfer abort)
                    throws FTPException,
                           IOException
List the current remote directory with the ability to abort the listing.

Based on what getConnectionType returns, either pasv or port will be sent in this routine.

Parameters:
abort - an Object that allows for the abortion of the list.
Returns:
a list of RemoteFile objects as a RemoteFileList.
Throws:
FTPException - if the FTP server returns an error code.
IOException - if there is a socket problem.
See Also:
RemoteFile, getConnectionType(), setConnectionType(short), pasv(), port(HostInfo)

listAll

public RemoteFileList listAll(FTPAbortableTransfer abort)
                       throws FTPException,
                              IOException
List the current remote directory including hidden files with the ability to abort the listing.

Based on what getConnectionType returns, either pasv or port will be sent in this routine.

Parameters:
abort - an Object that allows for the abortion of the list.
Returns:
a list of RemoteFile objects as a RemoteFileList.
Throws:
FTPException - if the FTP server returns an error code.
IOException - if there is a socket problem.
See Also:
RemoteFile, getConnectionType(), setConnectionType(short), pasv(), port(HostInfo)

list

public RemoteFileList list(RemoteFile file)
                    throws FTPException,
                           IOException
List items on the remote FTP server.

Based on what getConnectionType returns, either pasv or port will be sent in this routine.

Parameters:
file - a RemoteFile you want to to list from the FTP server. Pass null to list the current directory.
Returns:
a list of RemoteFile objects as a RemoteFileList.
Throws:
FTPException - if the FTP server returns an error code.
IOException - if there is a socket problem.
See Also:
RemoteFile, getConnectionType(), setConnectionType(short), pasv(), port(HostInfo)

list

public RemoteFileList list(String itemsToList)
                    throws FTPException,
                           IOException
List items on the remote FTP server.

Based on what getConnectionType returns, either pasv or port will be sent in this routine.

Parameters:
itemsToList - a space-delimited String of items you want to to list from the FTP server. Pass null to list the current directory.
Returns:
a list of RemoteFile objects as a RemoteFileList.
Throws:
FTPException - if the FTP server returns an error code.
IOException - if there is a socket problem.
See Also:
RemoteFile, getConnectionType(), setConnectionType(short), pasv(), port(HostInfo)

list

public RemoteFileList list(RemoteFile file,
                           FTPAbortableTransfer abort,
                           boolean showHidden)
                    throws FTPException,
                           IOException