Java Globus API: Class FTPClient

org.globus.ftp
Class FTPClient

java.lang.Object
  |
  +--org.globus.ftp.FTPClient
Direct Known Subclasses:
GridFTPClient

public class FTPClient
extends Object

This is the main user interface for FTP operations. Use this class for client - server or third party transfers that do not require GridFTP extensions. Consult the manual for general usage.


Field Summary
protected  FTPControlChannel controlChannel
           
protected  SimpleDateFormat dateFormat
           
protected  FTPServerFacade localServer
           
protected  Session session
           
 
Constructor Summary
protected FTPClient()
           
  FTPClient(String host, int port)
          Constructs client and connects it to the remote server.
 
Method Summary
 void abort()
          Aborts the current transfer.
protected  TransferState actualGet(String remoteFileName, DataSink sink, MarkerListener mListener)
           
protected  void actualList(String filter, String modifier, DataSink sink)
           
protected  TransferState actualPut(String remoteFileName, DataSource source, MarkerListener mListener, boolean append)
           
protected  void actualSetMode(int mode, String modeStr)
           
 TransferState asynchGet(String remoteFileName, DataSink sink, MarkerListener mListener)
          Retrieves the file from the remote server.
 TransferState asynchPut(String remoteFileName, DataSource source, MarkerListener mListener)
          Stores file at the remote server.
 void authorize(String user, String password)
          Performs user authorization with specified user and password.
 void changeDir(String dir)
          Changes the remote current working directory.
protected  void checkTransferParams()
           
 void close()
          Closes connection.
 void deleteDir(String dir)
          Deletes the remote directory.
 void deleteFile(String filename)
          Deletes the remote file.
 boolean exists(String filename)
          Checks if given file/directory exists on the server.
 void get(String remoteFileName, DataSink sink, MarkerListener mListener)
          Retrieves the file from the remote server.
 void get(String remoteFileName, File localFile)
           
 String getCurrentDir()
          Returns remote current working directory.
 FeatureList getFeatureList()
          Returns list of features supported by remote server.
 Date getLastModified(String filename)
          Returns last modification time of the specifed file.
 long getSize(String filename)
          Returns the remote file size.
 void goUpDir()
          Changes remote current working directory to the higher level.
 boolean isActiveMode()
           
 boolean isFeatureSupported(String feature)
          Returns true if the given feature is supported by remote server, false otherwise.
 boolean isPassiveMode()
           
 Date lastModified(String filename)
           
 Vector list()
          Performs remote directory listing.
 Vector list(String filter)
          Performs remote directory listing with the specified filter.
 void list(String filter, String modifier, DataSink sink)
          Performs directory listing and writes the result to the supplied data sink.
protected  void listCheck()
          check performed at the beginning of list()
 void makeDir(String dir)
          Creates remote directory.
 void put(File localFile, String remoteFileName, boolean append)
           
 void put(String remoteFileName, DataSource source, MarkerListener mListener)
          Stores file at the remote server.
 void put(String remoteFileName, DataSource source, MarkerListener mListener, boolean append)
          Stores file at the remote server.
 Reply quote(String command)
          Executes arbitrary operation on the server.
 void rename(String oldName, String newName)
          Renames remote directory.
 void setActive()
          Sets remote server active, telling it to connect to the client.
 void setActive(HostPort hostPort)
          Sets remote server active, telling it to connect to the given address.
 void setClientWaitParams(int maxWait, int waitDelay)
          Changes the default client timeout parameters.
 void setLocalActive()
          Starts local server in active server mode.
 HostPort setLocalPassive()
          Starts local server in passive server mode, with default parameters.
 HostPort setLocalPassive(int port, int queue)
          Starts the local server in passive server mode.
 void setMode(int mode)
          Sets transfer mode.
 void setOptions(Options opts)
          Sets the supplied options to the server.
 HostPort setPassive()
          Sets remote server to passive server mode.
 void setPassiveMode(boolean passiveMode)
          Enables/disables passive data connections.
 void setProtectionBufferSize(int size)
          Sets protection buffer size (defined in RFC 2228)
 void setRestartMarker(RestartData restartData)
          Sets restart parameter of the next transfer.
 void setType(int type)
          Sets transfer type.
 Reply site(String args)
          Executes site-specific operation (using the SITE command).
 long size(String filename)
           
 void transfer(String remoteSrcFile, FTPClient destination, String remoteDstFile, boolean append, MarkerListener mListener)
          Performs third-party transfer between two servers.
protected  TransferState transferBegin(BasicClientControlChannel other, MarkerListener mListener)
           
protected  void transferRun(BasicClientControlChannel other, MarkerListener mListener)
          Actual transfer management.
protected  TransferState transferStart(BasicClientControlChannel other, MarkerListener mListener)
           
protected  void transferWait(TransferState transferState)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

session

protected Session session

controlChannel

protected FTPControlChannel controlChannel

localServer

protected FTPServerFacade localServer

dateFormat

protected SimpleDateFormat dateFormat
Constructor Detail

FTPClient

protected FTPClient()

FTPClient

public FTPClient(String host,
                 int port)
          throws IOException,
                 ServerException
Constructs client and connects it to the remote server.
Parameters:
host - remote server host
port - remote server port
Method Detail

getSize

public long getSize(String filename)
             throws IOException,
                    ServerException
Returns the remote file size.
Parameters:
filename - filename get the size for.
Returns:
size of the file.
Throws:
FTPException - if the file does not exist or an error occured.

getLastModified

public Date getLastModified(String filename)
                     throws IOException,
                            ServerException
Returns last modification time of the specifed file.
Parameters:
filename - filename get the last modification time for.
Returns:
the time and date of the last modification.
Throws:
FTPException - if the file does not exist or an error occured.

exists

public boolean exists(String filename)
               throws IOException,
                      ServerException
Checks if given file/directory exists on the server.
Parameters:
filename - file or directory name
Returns:
true if the file exists, false otherwise.

changeDir

public void changeDir(String dir)
               throws IOException,
                      ServerException
Changes the remote current working directory.

deleteDir

public void deleteDir(String dir)
               throws IOException,
                      ServerException
Deletes the remote directory.

deleteFile

public void deleteFile(String filename)
                throws IOException,
                       ServerException
Deletes the remote file.

makeDir

public void makeDir(String dir)
             throws IOException,
                    ServerException
Creates remote directory.

rename

public void rename(String oldName,
                   String newName)
            throws IOException,
                   ServerException
Renames remote directory.

getCurrentDir

public String getCurrentDir()
                     throws IOException,
                            ServerException
Returns remote current working directory.
Returns:
remote current working directory.

goUpDir

public void goUpDir()
             throws IOException,
                    ServerException
Changes remote current working directory to the higher level.

list

public Vector list()
            throws ServerException,
                   ClientException,
                   IOException
Performs remote directory listing. Sends 'LIST -d *' command.

list

public Vector list(String filter)
            throws ServerException,
                   ClientException,
                   IOException
Performs remote directory listing with the specified filter. Sends 'LIST -d ' command. Note: This function can only parse Unix ls -d output. If on remote server 'ls' does accept -d option, this method might fail. Also note that 'ls -d' output might be different on each architecture, in which case the parsing might fail. In that case, you may choose to use the parametrized list().
Returns:
Vector list of FileInfo objects, representing remote files

list

public void list(String filter,
                 String modifier,
                 DataSink sink)
          throws ServerException,
                 ClientException,
                 IOException
Performs directory listing and writes the result to the supplied data sink. This method is allowed in ASCII mode only.
Parameters:
filter - remote list command file filter, eg. "*"
modifier - remote list command modifier, eg. "-d"
sink - data destination

checkTransferParams

protected void checkTransferParams()
                            throws ServerException,
                                   IOException,
                                   ClientException

actualList

protected void actualList(String filter,
                          String modifier,
                          DataSink sink)
                   throws ServerException,
                          ClientException,
                          IOException

listCheck

protected void listCheck()
                  throws ClientException
check performed at the beginning of list()

setType

public void setType(int type)
             throws IOException,
                    ServerException
Sets transfer type.
Parameters:
type - requested representation type, should be equal to one of the corresponding public variables defined in Session.

setMode

public void setMode(int mode)
             throws IOException,
                    ServerException
Sets transfer mode.
Parameters:
mode - requested transfer mode, should be equal to one of the corresponding public variables defined in Session.

actualSetMode

protected void actualSetMode(int mode,
                             String modeStr)
                      throws IOException,
                             ServerException

setProtectionBufferSize

public void setProtectionBufferSize(int size)
                             throws IOException,
                                    ServerException
Sets protection buffer size (defined in RFC 2228)
Parameters:
size - the size of buffer

abort

public void abort()
           throws IOException,
                  ServerException
Aborts the current transfer. FTPClient is not thread safe so be careful with using this procedure, which will typically happen in multi threaded environment. Especially during client-server two party transfer, calling abort() may result with exceptions being thrown in the thread that currently perform the transfer.

close

public void close()
           throws IOException,
                  ServerException
Closes connection. Sends QUIT and closes connection even if the server reply was not positive. Also, closes the local server.

isFeatureSupported

public boolean isFeatureSupported(String feature)
                           throws IOException,
                                  ServerException
Returns true if the given feature is supported by remote server, false otherwise.
Returns:
true if the given feature is supported by remote server, false otherwise.

getFeatureList

public FeatureList getFeatureList()
                           throws IOException,
                                  ServerException
Returns list of features supported by remote server.
Returns:
list of features supported by remote server.

setPassive

public HostPort setPassive()
                    throws IOException,
                           ServerException
Sets remote server to passive server mode.
Returns:
the address at which the server is listening.

setActive

public void setActive(HostPort hostPort)
               throws IOException,
                      ServerException
Sets remote server active, telling it to connect to the given address.
Parameters:
hostPort - the address to which the server should connect

setActive

public void setActive()
               throws IOException,
                      ServerException,
                      ClientException
Sets remote server active, telling it to connect to the client. setLocalPassive() must be called beforehand.

setLocalActive

public void setLocalActive()
                    throws ClientException,
                           IOException
Starts local server in active server mode.

setLocalPassive

public HostPort setLocalPassive()
                         throws IOException
Starts local server in passive server mode, with default parameters. In other words, behaves like setLocalPassive(FTPServerFacade.ANY_PORT, FTPServerFacade.DEFAULT_QUEUE)

setLocalPassive

public HostPort setLocalPassive(int port,
                                int queue)
                         throws IOException
Starts the local server in passive server mode.
Parameters:
port - port at which local server should be listening; can be set to FTPServerFacade.ANY_PORT
queue - max size of queue of awaiting new connection requests
Returns:
the server address

setClientWaitParams

public void setClientWaitParams(int maxWait,
                                int waitDelay)
Changes the default client timeout parameters. In the beginning of the transfer, the critical moment is the wait for the initial server reply. If it does not arrive after timeout, client assumes that the transfer could not start for some reason and aborts the operation. Default timeout in miliseconds is Session.DEFAULT_MAX_WAIT. During the waiting period, client polls the control channel once a certain period, which is by default set to Session.DEFAULT_WAIT_DELAY.
Use this method to change these parameters.
Parameters:
maxWait - timeout in miliseconds
waitDelay - polling period

setOptions

public void setOptions(Options opts)
                throws IOException,
                       ServerException
Sets the supplied options to the server.

setRestartMarker

public void setRestartMarker(RestartData restartData)
                      throws IOException,
                             ServerException
Sets restart parameter of the next transfer.
Parameters:
restart - marker to use
Throws:
FTPException - if the file does not exist or an error occured.

authorize

public void authorize(String user,
                      String password)
               throws IOException,
                      ServerException
Performs user authorization with specified user and password.
Parameters:
user - username
pwd - user password
Throws:
ServerException - on server refusal

get

public void get(String remoteFileName,
                DataSink sink,
                MarkerListener mListener)
         throws IOException,
                ClientException,
                ServerException
Retrieves the file from the remote server.
Parameters:
remoteFileName - remote file name
sink - sink to which the data will be written
mListener - restart marker listener (currently not used)

asynchGet

public TransferState asynchGet(String remoteFileName,
                               DataSink sink,
                               MarkerListener mListener)
                        throws IOException,
                               ClientException,
                               ServerException
Retrieves the file from the remote server.
Parameters:
remoteFileName - remote file name
sink - sink to which the data will be written
mListener - restart marker listener (currently not used)

actualGet

protected TransferState actualGet(String remoteFileName,
                                  DataSink sink,
                                  MarkerListener mListener)
                           throws IOException,
                                  ClientException,
                                  ServerException

put

public void put(String remoteFileName,
                DataSource source,
                MarkerListener mListener)
         throws IOException,
                ServerException,
                ClientException
Stores file at the remote server.
Parameters:
remoteFileName - remote file name
source - data will be read from here
mListener - restart marker listener (currently not used)

put

public void put(String remoteFileName,
                DataSource source,
                MarkerListener mListener,
                boolean append)
         throws IOException,
                ServerException,
                ClientException
Stores file at the remote server.
Parameters:
remoteFileName - remote file name
source - data will be read from here
mListener - restart marker listener (currently not used)
append - append to the end of file or overwrite

asynchPut

public TransferState asynchPut(String remoteFileName,
                               DataSource source,
                               MarkerListener mListener)
                        throws IOException,
                               ServerException,
                               ClientException
Stores file at the remote server.
Parameters:
remoteFileName - remote file name
source - data will be read from here
mListener - restart marker listener (currently not used)

actualPut

protected TransferState actualPut(String remoteFileName,
                                  DataSource source,
                                  MarkerListener mListener,
                                  boolean append)
                           throws IOException,
                                  ServerException,
                                  ClientException

transfer

public void transfer(String remoteSrcFile,
                     FTPClient destination,
                     String remoteDstFile,
                     boolean append,
                     MarkerListener mListener)
              throws IOException,
                     ServerException,
                     ClientException
Performs third-party transfer between two servers.
Parameters:
remoteSrcFile - source filename
dstServer - another client connected to destination server
remoteDstFile - destination filename
append - enables append mode; if true, data will be appened to the remote file, otherwise file will be overwritten.
mListener - marker listener. Can be set to null.

transferRun

protected void transferRun(BasicClientControlChannel other,
                           MarkerListener mListener)
                    throws IOException,
                           ServerException,
                           ClientException
Actual transfer management. Transfer is controlled by two new threads listening to the two servers.

transferBegin

protected TransferState transferBegin(BasicClientControlChannel other,
                                      MarkerListener mListener)

transferStart

protected TransferState transferStart(BasicClientControlChannel other,
                                      MarkerListener mListener)
                               throws IOException,
                                      ServerException,
                                      ClientException

transferWait

protected void transferWait(TransferState transferState)
                     throws IOException,
                            ServerException,
                            ClientException

quote

public Reply quote(String command)
            throws IOException,
                   ServerException
Executes arbitrary operation on the server. This is potentially dangerous operation. Depending on the command executed it might put the server in a different state from the state the client is expecting.
Parameters:
command - command to execute
Returns:
the Reply to the operation.
Throws:
IOException - in case of I/O error.
ServerException - if operation failed.

site

public Reply site(String args)
           throws IOException,
                  ServerException
Executes site-specific operation (using the SITE command). This is potentially dangerous operation. Depending on the command executed it might put the server in a different state from the state the client is expecting.
Parameters:
args - parameters for the SITE operation.
Returns:
the Reply to the operation.
Throws:
IOException - in case of I/O error
ServerException - if operation failed.

size

public long size(String filename)
          throws IOException,
                 ServerException

lastModified

public Date lastModified(String filename)
                  throws IOException,
                         ServerException

get

public void get(String remoteFileName,
                File localFile)
         throws IOException,
                ClientException,
                ServerException

put

public void put(File localFile,
                String remoteFileName,
                boolean append)
         throws IOException,
                ServerException,
                ClientException

setPassiveMode

public void setPassiveMode(boolean passiveMode)
                    throws IOException,
                           ClientException,
                           ServerException
Enables/disables passive data connections.
Parameters:
passiveMode, - if true passive connections will be established. If false, they will not.

isPassiveMode

public boolean isPassiveMode()

isActiveMode

public boolean isActiveMode()