net.sf.jftp.net
Class FtpConnection

java.lang.Object
  extended bynet.sf.jftp.net.FtpConnection
All Implemented Interfaces:
BasicConnection, FtpConstants

public class FtpConnection
extends java.lang.Object
implements BasicConnection, FtpConstants

All control transactions are made here. See doc/FtpDownload.java for examples and note that this class is event-driven and not Exception-based! You might want to take a look at ConnectionListener, too.

Version:
1.30+, June 2003
Author:
David Hansmann, hansmann.d@debitel.net

Field Summary
static java.lang.String ABOR
          Internal abort command constant
static java.lang.String ASCII
           
static java.lang.String BINARY
           
static java.lang.String BLOCKED
           
static int CHDIR_FAILED
          Possible upload() / download() / exists() return code
static java.lang.String COMPRESSED
           
static int DENIED
          Possible getPermissions() / exists() return code
static java.lang.String DOWNLOAD
          Internal download constant
static java.lang.String EBCDIC
           
static int FILE_NOT_FOUND
          Possible exists() return code
static int GENERIC_FAILED
          Possible login() / remove() return code
 boolean hasUploaded
          Used to determine the type of transfer.
static java.lang.String L8
           
static int LOGIN_OK
          Possible login() return code
static int MKDIR_FAILED
          Possible upload() / download() / remove() return code
static int NEW_TRANSFER_SPAWNED
          Possible upload() / download() return code
static int OFFLINE
          Possible login() return code
static int PERMISSION_DENIED
          Possible upload() / download() return code
static int R
          Possible getPermissions() / exists() return code
static int REMOVE_FAILED
          Possible remove() return code
static int REMOVE_SUCCESSFUL
          Possible remove() return code
static int RENAME_FAILED
          Possible upload() return code
static java.lang.String STREAM
           
static int TRANSFER_FAILED
          Possible upload() / download() return code
static int TRANSFER_STOPPED
          Possible upload() / download() return code
static int TRANSFER_SUCCESSFUL
          Possible upload() / download() return code
static java.lang.String UPLOAD
          Internal upload constant
static int W
          Possible getPermissions() / exists() return code
 boolean work
           
static int WRONG_LOGIN_DATA
          Possible login() return code
 
Fields inherited from interface net.sf.jftp.net.FtpConstants
ACCT, ADAT, ALLO, APPE, AUTH, CCC, CDUP, CONF, CWD, DELE, ENC, FTP200_OK, FTP215_SYSTEM_TYPE, FTP220_SERVICE_READY, FTP221_SERVICE_CLOSING, FTP226_CLOSING_DATA_REQUEST_SUCCESSFUL, FTP227_ENTERING_PASSIVE_MODE, FTP230_LOGGED_IN, FTP250_COMPLETED, FTP257_PATH_CREATED, FTP331_USER_OK_NEED_PASSWORD, HELP, LIST, MIC, MKD, MODE, NLST, NOOP, PASS, PASV, PBSZ, PORT, PROT, PWD, QUIT, RC110, RC120, RC125, RC150, RC202, RC211, RC212, RC213, RC214, RC225, RC332, RC350, RC421, RC425, RC426, RC450, RC451, RC452, RC500, RC501, RC502, RC503, RC504, RC530, RC532, RC550, RC551, RC552, RC553, REIN, REST, RETR, RMD, RNFR, RNTO, SITE, SMNT, STAT, STOR, STOU, STRU, SYST, TYPE, USER
 
Constructor Summary
FtpConnection(java.lang.String host)
          Create an instance with a given host
FtpConnection(java.lang.String host, int port, java.lang.String initCWD)
          Create an instance with a given host, port and initial remote working directory
 
Method Summary
 void abort()
          Try to abort the transfer.
 void abortTransfer()
          Abort the last spawned transfer.
 void addConnectionListener(ConnectionListener l)
          Add a ConnectionListener.
 void ascii()
          Tell server we want ascii data connections.
 void binary()
          Tell server we want binary data connections.
 boolean cdup()
          Change to the parent of the current working directory.
 boolean chdir(java.lang.String p)
          Parses directory and does a chdir().
 boolean chdirNoRefresh(java.lang.String p)
          Parses directory and does a chdir(), but does not send an update signal
 boolean chdirRaw(java.lang.String dirName)
          Change directory unparsed.
 void disconnect()
          Disconnect from the server.
 int download(java.lang.String file)
          Download a file or directory, block until finished.
 int exists(java.lang.String file)
          Checks wheter a file exists.
 void fireActionFinished(FtpConnection con)
          Transfer is done
 void fireConnectionFailed(FtpConnection con, java.lang.String why)
          We are not logged in for some reason
 void fireConnectionInitialized(FtpConnection con)
          Connection is there and user logged in
 void fireDirectoryUpdate(FtpConnection con)
          Remote directory has changed.
 void fireProgressUpdate(java.lang.String file, java.lang.String type, int bytes)
          Progress update.
 java.lang.String getCachedPWD()
          Returns current remote directory (cached)
 ConnectionHandler getConnectionHandler()
          Return the ConnectionHandler.
 DataConnection getDataConnection()
          Return the DataConnection.
 java.io.InputStream getDownloadInputStream(java.lang.String file)
          Get download InputStream.
 java.lang.String getHost()
          Return the host used.
 FtpTransfer getLastInitiatedTransfer()
          Get the last FtpTransfer object spawned.
 java.lang.String getLine(java.lang.String until)
          Reads the response until line found or error.
 java.lang.String getLine(java.lang.String[] until)
          Reads the response until line found or error.
 java.lang.String getLocalPath()
          Return the local working directory
 java.lang.String getOsType()
          Try to determine the os-type.
 java.lang.String getPassword()
          Return the password.
 int[] getPermissions(java.lang.String file)
          Sort the permissions an return an array.
 int getPort()
          Get the port.
 java.lang.String getPWD()
          Get the current remote directory.
 java.lang.String getTypeNow()
          Returns the type used.
 java.lang.String getUsername()
          Return the username.
 int handleDownload(java.lang.String file)
          Download a file or directory.
 int handleUpload(java.lang.String file)
          Upoad a file or directory.
 boolean isConnected()
          Check if login() was successful.
 void list(java.lang.String outfile)
          List remote directory.
 int login(java.lang.String username, java.lang.String password)
          Connect to the server an log in.
 boolean mkdir(java.lang.String dirName)
          Create a directory with the given name.
 java.lang.String mode(java.lang.String code)
          Set mode manually.
 void modeBlocked()
          Unsupported at this time.
 void modeCompressed()
           
 void modeStream()
          Set mode to Stream.
 void noop()
          Do nothing, but flush buffers
 void pause(int time)
          Waits a specified amount of time
 int removeFileOrDir(java.lang.String file)
          Remove a remote file or directory.
 boolean rename(java.lang.String from, java.lang.String to)
          Moves or renames remote file.
 void sendRawCommand(java.lang.String cmd)
          Execute a remote command.
 void setConnectionHandler(ConnectionHandler h)
          Set the ConnectionHandler.
 void setConnectionListeners(java.util.Vector l)
          Add a Vector of ConnectionListeners.
 boolean setLocalPath(java.lang.String newPath)
          Set the local working directory
 java.lang.String[] sortLs(java.lang.String file)
          Sort the filenames of the current working dir an return an array.
 java.lang.String[] sortSize(java.lang.String file)
          Sort the filesizes an return an array.
 java.lang.String system()
          This command is used to find out the type of operating system at the server.
 boolean type(java.lang.String code)
          Set type (L8,I,A for example).
 int upload(java.lang.String file)
          Upload a file or directory, block until finished.
 int upload(java.lang.String file, java.io.InputStream in)
          Upload from an InputStream, block until finished.
 int upload(java.lang.String file, java.lang.String realName)
          Upload and a file or directory under a given name, block until finished.
 int upload(java.lang.String file, java.lang.String realName, java.io.InputStream in)
          Upload and a file or directory under a given name, block until finished.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TRANSFER_FAILED

public static final int TRANSFER_FAILED
Possible upload() / download() return code

See Also:
Constant Field Values

TRANSFER_STOPPED

public static final int TRANSFER_STOPPED
Possible upload() / download() return code

See Also:
Constant Field Values

MKDIR_FAILED

public static final int MKDIR_FAILED
Possible upload() / download() / remove() return code

See Also:
Constant Field Values

PERMISSION_DENIED

public static final int PERMISSION_DENIED
Possible upload() / download() return code

See Also:
Constant Field Values

CHDIR_FAILED

public static final int CHDIR_FAILED
Possible upload() / download() / exists() return code

See Also:
Constant Field Values

WRONG_LOGIN_DATA

public static final int WRONG_LOGIN_DATA
Possible login() return code

See Also:
Constant Field Values

OFFLINE

public static final int OFFLINE
Possible login() return code

See Also:
Constant Field Values

GENERIC_FAILED

public static final int GENERIC_FAILED
Possible login() / remove() return code

See Also:
Constant Field Values

REMOVE_FAILED

public static final int REMOVE_FAILED
Possible remove() return code

See Also:
Constant Field Values

RENAME_FAILED

public static final int RENAME_FAILED
Possible upload() return code

See Also:
Constant Field Values

FILE_NOT_FOUND

public static final int FILE_NOT_FOUND
Possible exists() return code

See Also:
Constant Field Values

NEW_TRANSFER_SPAWNED

public static final int NEW_TRANSFER_SPAWNED
Possible upload() / download() return code

See Also:
Constant Field Values

TRANSFER_SUCCESSFUL

public static final int TRANSFER_SUCCESSFUL
Possible upload() / download() return code

See Also:
Constant Field Values

LOGIN_OK

public static final int LOGIN_OK
Possible login() return code

See Also:
Constant Field Values

REMOVE_SUCCESSFUL

public static final int REMOVE_SUCCESSFUL
Possible remove() return code

See Also:
Constant Field Values

R

public static final int R
Possible getPermissions() / exists() return code

See Also:
Constant Field Values

W

public static final int W
Possible getPermissions() / exists() return code

See Also:
Constant Field Values

DENIED

public static final int DENIED
Possible getPermissions() / exists() return code

See Also:
Constant Field Values

DOWNLOAD

public static final java.lang.String DOWNLOAD
Internal download constant

See Also:
Constant Field Values

UPLOAD

public static final java.lang.String UPLOAD
Internal upload constant

See Also:
Constant Field Values

ABOR

public static final java.lang.String ABOR
Internal abort command constant

See Also:
Constant Field Values

hasUploaded

public boolean hasUploaded
Used to determine the type of transfer.


work

public boolean work

ASCII

public static final java.lang.String ASCII
See Also:
Constant Field Values

BINARY

public static final java.lang.String BINARY
See Also:
Constant Field Values

EBCDIC

public static final java.lang.String EBCDIC
See Also:
Constant Field Values

L8

public static final java.lang.String L8
See Also:
Constant Field Values

STREAM

public static final java.lang.String STREAM
See Also:
Constant Field Values

BLOCKED

public static final java.lang.String BLOCKED
See Also:
Constant Field Values

COMPRESSED

public static final java.lang.String COMPRESSED
See Also:
Constant Field Values
Constructor Detail

FtpConnection

public FtpConnection(java.lang.String host)
Create an instance with a given host

Parameters:
host - The host to connect to

FtpConnection

public FtpConnection(java.lang.String host,
                     int port,
                     java.lang.String initCWD)
Create an instance with a given host, port and initial remote working directory

Parameters:
host - The host to connect to
port - The port used for the control connection, usually 21
initCWD - The initial remote working directory
Method Detail

login

public int login(java.lang.String username,
                 java.lang.String password)
Connect to the server an log in. Does also fire directory update and connection initialized event if successful or connection failed event if failed.

Parameters:
username - The username
password - The password
Returns:
WRONG_LOGIN_DATA, OFFLINE, GENERIC_FAILED or LOGIN_OK status code

sortSize

public java.lang.String[] sortSize(java.lang.String file)
Sort the filesizes an return an array. The Array should be in sync with the other sort*-Methods

Specified by:
sortSize in interface BasicConnection
Parameters:
file - The file containing the raw server listing, usually Settings.ls_out
Returns:
An String-array of sizes containing the size or -1 if failed or 0 for directories

getPermissions

public int[] getPermissions(java.lang.String file)
Sort the permissions an return an array. The Array should be in sync with the other sort*-Methods

Specified by:
getPermissions in interface BasicConnection
Parameters:
file - The file containing the raw server listing, usually Settings.ls_out
Returns:
An int-array of sizes containing W, R or DENIED for each file

sortLs

public java.lang.String[] sortLs(java.lang.String file)
Sort the filenames of the current working dir an return an array. The Array should be in sync with the other sort*-Methods

Specified by:
sortLs in interface BasicConnection
Parameters:
file - The file containing the raw server listing, usually Settings.ls_out
Returns:
An String-array of sizes containing the name of the file (symlinks are resolved)

getOsType

public java.lang.String getOsType()
Try to determine the os-type. (Used internally to check how to parse the ls-output)

Returns:
A Part of the SYST comman server response

handleDownload

public int handleDownload(java.lang.String file)
Download a file or directory. Uses multithreading if enabled and does not block.

Specified by:
handleDownload in interface BasicConnection
Parameters:
file - The file to download
Returns:
An int-statuscode, see constants defined in this class for details

download

public int download(java.lang.String file)
Download a file or directory, block until finished.

Specified by:
download in interface BasicConnection
Parameters:
file - The file to download
Returns:
An int returncode

getDownloadInputStream

public java.io.InputStream getDownloadInputStream(java.lang.String file)
Get download InputStream.

Specified by:
getDownloadInputStream in interface BasicConnection
Parameters:
file - The file to download
Returns:
An InputStream

handleUpload

public int handleUpload(java.lang.String file)
Upoad a file or directory. Uses multithreading if enabled and does not block.

Specified by:
handleUpload in interface BasicConnection
Parameters:
file - The file to upload
Returns:
An int-statuscode, NEW_TRANSFER_SPAWNED,TRANSFER_FAILED or TRANSFER_SUCCESSFUL

upload

public int upload(java.lang.String file)
Upload a file or directory, block until finished.

Specified by:
upload in interface BasicConnection
Parameters:
file - The file to upload
Returns:
An int returncode

upload

public int upload(java.lang.String file,
                  java.lang.String realName)
Upload and a file or directory under a given name, block until finished. Note that setting realName does not affect directory transfers

Parameters:
file - The file to upload
realName - The file to rename the uploaded file to
Returns:
An int responsecode

upload

public int upload(java.lang.String file,
                  java.io.InputStream in)
Upload from an InputStream, block until finished.

Specified by:
upload in interface BasicConnection
Parameters:
file - The file to upload
in - InputStream to read from
Returns:
An int responsecode

upload

public int upload(java.lang.String file,
                  java.lang.String realName,
                  java.io.InputStream in)
Upload and a file or directory under a given name, block until finished. Note that setting realName does not affect directory transfers

Parameters:
file - The file to upload
realName - The file to rename the uploaded file to
in - InputStream to read from
Returns:
An int responsecode

removeFileOrDir

public int removeFileOrDir(java.lang.String file)
Remove a remote file or directory.

Specified by:
removeFileOrDir in interface BasicConnection
Parameters:
file - The file to remove
Returns:
REMOVE_SUCCESSFUL, REMOVE_FAILED, PERMISSION_DENIED or GENERIC_FAILED

disconnect

public void disconnect()
Disconnect from the server. The connection is marked ad closed even if the server does not respond correctly - if it fails for any reason the connection should just time out

Specified by:
disconnect in interface BasicConnection

sendRawCommand

public void sendRawCommand(java.lang.String cmd)
Execute a remote command. Sends noops before and after the command

Specified by:
sendRawCommand in interface BasicConnection
Parameters:
cmd - The raw command that is to be sent to the server

getLine

public java.lang.String getLine(java.lang.String until)
Reads the response until line found or error. (Used internally)

Parameters:
until - The String the response line hast to start with, 2 for succesful return codes for example

getLine

public java.lang.String getLine(java.lang.String[] until)
Reads the response until line found or error. (Used internally)


isConnected

public boolean isConnected()
Check if login() was successful.

Specified by:
isConnected in interface BasicConnection
Returns:
True if connected, false otherwise

getPWD

public java.lang.String getPWD()
Get the current remote directory.

Specified by:
getPWD in interface BasicConnection
Returns:
The server's CWD

getCachedPWD

public java.lang.String getCachedPWD()
Returns current remote directory (cached)

Returns:
The cached CWD

chdirRaw

public boolean chdirRaw(java.lang.String dirName)
Change directory unparsed. Use chdir instead if you do not parse the dir correctly...

Parameters:
dirName - The raw directory name
Returns:
True if successful, false otherwise

cdup

public boolean cdup()
Change to the parent of the current working directory. The CDUP command is a special case of CWD, and is included to simplify the implementation of programs for transferring directory trees between operating systems having different syntaxes for naming the parent directory.

Specified by:
cdup in interface BasicConnection
Returns:
True if successful, false otherwise

mkdir

public boolean mkdir(java.lang.String dirName)
Create a directory with the given name.

Specified by:
mkdir in interface BasicConnection
Parameters:
dirName - The name of the directory to create
Returns:
True if successful, false otherwise

list

public void list(java.lang.String outfile)
          throws java.io.IOException
List remote directory. Note that you have to get the output using the sort*-methods.

Specified by:
list in interface BasicConnection
Parameters:
outfile - The file to save the output to, usually Settings.ls_out
Throws:
java.io.IOException

chdir

public boolean chdir(java.lang.String p)
Parses directory and does a chdir(). Does also fire a directory update event.

Specified by:
chdir in interface BasicConnection
Returns:
True is successful, false otherwise

chdirNoRefresh

public boolean chdirNoRefresh(java.lang.String p)
Parses directory and does a chdir(), but does not send an update signal

Specified by:
chdirNoRefresh in interface BasicConnection
Returns:
True is successful, false otherwise

pause

public void pause(int time)
Waits a specified amount of time

Parameters:
time - The time to sleep in milliseconds

getLocalPath

public java.lang.String getLocalPath()
Return the local working directory

Specified by:
getLocalPath in interface BasicConnection
Returns:
The local CWD

setLocalPath

public boolean setLocalPath(java.lang.String newPath)
Set the local working directory

Specified by:
setLocalPath in interface BasicConnection
Parameters:
newPath - The new local CWD
Returns:
Always true in the current implementation

exists

public int exists(java.lang.String file)
Checks wheter a file exists.

Returns:
An int-code: CHDIR_FAILED, PERMISSION_DENIED (no listing allowed), R, W or DENIED

rename

public boolean rename(java.lang.String from,
                      java.lang.String to)
Moves or renames remote file.

Parameters:
from - remote file to move or rename.
to - new file name.
Returns:
true if completed successfully; false otherwise.

getPort

public int getPort()
Get the port.

Returns:
The port used by the FTP control connection

binary

public void binary()
Tell server we want binary data connections.


ascii

public void ascii()
Tell server we want ascii data connections.


type

public boolean type(java.lang.String code)
Set type (L8,I,A for example).

Returns:
True if the type was changed successfully, false otherwise

getTypeNow

public java.lang.String getTypeNow()
Returns the type used.

Returns:
The type String, I, A, L8 for example

noop

public void noop()
Do nothing, but flush buffers


abort

public void abort()
Try to abort the transfer.


system

public java.lang.String system()
This command is used to find out the type of operating system at the server. The reply shall have as its first word one of the system names listed in the current version of the Assigned Numbers document (RFC 943).

Returns:
The server response of the SYST command

modeStream

public void modeStream()
Set mode to Stream. Used internally.


modeBlocked

public void modeBlocked()
Unsupported at this time.


modeCompressed

public void modeCompressed()

mode

public java.lang.String mode(java.lang.String code)
Set mode manually.

Parameters:
code - The mode code wanted, I, A, L8 for example
Returns:
The server's response to the request

getHost

public java.lang.String getHost()
Return the host used.

Returns:
The remote host of this connection

getUsername

public java.lang.String getUsername()
Return the username.

Returns:
The username used by this connection

getPassword

public java.lang.String getPassword()
Return the password.

Returns:
The password used by this connection

getDataConnection

public DataConnection getDataConnection()
Return the DataConnection. Should be necessary only for complex actions.

Returns:
The DataConnection object currently used by this conenction

addConnectionListener

public void addConnectionListener(ConnectionListener l)
Add a ConnectionListener. The Listener is notified about transfers and the connection state.

Specified by:
addConnectionListener in interface BasicConnection
Parameters:
l - A ConnectionListener object

setConnectionListeners

public void setConnectionListeners(java.util.Vector l)
Add a Vector of ConnectionListeners. Each Listener is notified about transfers and the connection state.

Specified by:
setConnectionListeners in interface BasicConnection
Parameters:
l - A Vector containig ConnectionListener objects

fireDirectoryUpdate

public void fireDirectoryUpdate(FtpConnection con)
Remote directory has changed.

Parameters:
con - The FtpConnection calling the event

fireProgressUpdate

public void fireProgressUpdate(java.lang.String file,
                               java.lang.String type,
                               int bytes)
Progress update. The transfer is active and makes progress, so a signal is send each few kb.

Parameters:
file - The file transferred
type - the type of event, DataConnection.GETDIR for example
bytes - The number of bytes transferred so far

fireConnectionInitialized

public void fireConnectionInitialized(FtpConnection con)
Connection is there and user logged in

Parameters:
con - The FtpConnection calling the event

fireConnectionFailed

public void fireConnectionFailed(FtpConnection con,
                                 java.lang.String why)
We are not logged in for some reason

Parameters:
con - The FtpConnection calling the event
why - The login() response code

fireActionFinished

public void fireActionFinished(FtpConnection con)
Transfer is done

Parameters:
con - The FtpConnection calling the event

getConnectionHandler

public ConnectionHandler getConnectionHandler()
Return the ConnectionHandler.

Returns:
The connection handler of this instance

setConnectionHandler

public void setConnectionHandler(ConnectionHandler h)
Set the ConnectionHandler.

Parameters:
h - The connection handler that is to be used by this connection

getLastInitiatedTransfer

public FtpTransfer getLastInitiatedTransfer()
Get the last FtpTransfer object spawned.


abortTransfer

public void abortTransfer()
Abort the last spawned transfer.