com.radinks.net
Class FTPConnection

java.lang.Object
  extended bycom.radinks.net.FTPConnection
Direct Known Subclasses:
FTPConnection

public class FTPConnection
extends java.lang.Object


Nested Class Summary
 class FTPConnection.DataConnection
           Inner class that acts as an abstraction layer for Socket and ServerSocket.
 
Field Summary
static int ACTIVE_MODE
           
protected  int connectMode
          flag - defines whether to connect in active or passive modes.
protected  int contentLength
           
protected static byte[] CRLF
           
protected  java.io.InputStream in
           
protected  java.lang.String lastMessage
           
protected  java.net.URL location
          The FTP server that we are connecting to, the username and password that is being used for authentication and the starting location on the folder tree are all defined by this URL.
protected  java.io.OutputStream out
           
static int PASV_MODE
           
protected  java.net.Socket sock_control
           
protected  java.net.Socket sock_data
           
protected  int timeout
          socket timeout
protected  java.lang.String welcome
           
protected  java.io.Writer writer
           
 
Constructor Summary
FTPConnection()
           
FTPConnection(java.lang.String location)
          Open a connection to the given FTP URL.
 
Method Summary
 boolean cdhome()
          Set current working directory to the path defined in the url.
 boolean check_reply(java.lang.String code)
          Returns true if the server returns the expected code.
 boolean chmod(int perms, java.lang.String path)
          Chmod is implemened via the site command.
 boolean cwd(java.lang.String dir)
          Change Working Directory
 java.io.InputStream getIn()
           
 java.lang.String getLastMessage()
          Returns the last response from the server.
 java.net.URL getLocation()
           
 java.io.PrintStream getLogWriter()
           
 java.io.OutputStream getOut()
           
 java.io.Writer getWriter()
          Returns the writer associated with the control socket.
 void initStream()
          The server usually sends a 220 reply when your first connect to it.
 boolean isOk()
          Error messages are typically 500 status codes.
protected  void log(java.lang.String mes)
          logs the communitcation.
 boolean login()
          Login, if the location includes a username and password use them, else login annonymousy.
 boolean login(java.lang.String username, java.lang.String password)
          Logs into the system with the given username and password.
 FTPConnection.DataConnection makeDataConnection()
          open a new active or passive data connection.
 boolean mkdir(java.lang.String dir)
          Create a new directory on the server, will not attempt to determine if the directory already exists.
 void openConnection()
          Open the connection to the previously defined URL given in location.
 boolean pass(java.lang.String pass)
          sends the password as part of the user authentication procedure and reads the welcome message if one is available.
protected  java.net.Socket pasv()
          Open a passive mode data connection.
 java.net.ServerSocket port()
          Open a data connection in active mode.
 void setLocation(java.net.URL location)
           
 void setLogWriter(java.io.PrintStream logWriter)
          Where to log.
 int switchMode()
          Swtich between Active and Passive modes.
 boolean testConnection()
          Are you there?
 boolean user(java.lang.String user)
          sends the USER command to the server, you can call this method directory if you want more control than given by the login() method.
protected  void writeln(java.lang.String s)
          Centralize all the write operations for ease of maintenance.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ACTIVE_MODE

public static int ACTIVE_MODE

PASV_MODE

public static int PASV_MODE

connectMode

protected int connectMode
flag - defines whether to connect in active or passive modes.


timeout

protected int timeout
socket timeout


CRLF

protected static final byte[] CRLF

contentLength

protected int contentLength

location

protected java.net.URL location
The FTP server that we are connecting to, the username and password that is being used for authentication and the starting location on the folder tree are all defined by this URL.


out

protected java.io.OutputStream out

writer

protected java.io.Writer writer

in

protected java.io.InputStream in

sock_control

protected java.net.Socket sock_control

sock_data

protected java.net.Socket sock_data

welcome

protected java.lang.String welcome

lastMessage

protected java.lang.String lastMessage
Constructor Detail

FTPConnection

public FTPConnection()

FTPConnection

public FTPConnection(java.lang.String location)
              throws java.net.MalformedURLException
Open a connection to the given FTP URL. example URL: ftp://user:[email protected]/path/info/ All the information needed to establish the connection including username and password should be included in the URL. If anonymous login is used the username and password can be omitted.

Parameters:
location - a url on the ftp server.
Throws:
java.net.MalformedURLException
Method Detail

login

public boolean login()
              throws java.io.IOException
Login, if the location includes a username and password use them, else login annonymousy.

Returns:
did the server accept you?
Throws:
java.io.IOException

cdhome

public boolean cdhome()
               throws java.io.IOException
Set current working directory to the path defined in the url.

Returns:
success or failure
Throws:
java.io.IOException

chmod

public boolean chmod(int perms,
                     java.lang.String path)
              throws java.io.IOException
Chmod is implemened via the site command.

Parameters:
perms - the permission to apply
path - the file name to apply the permissions to
Returns:
success or failure.
Throws:
java.io.IOException

cwd

public boolean cwd(java.lang.String dir)
            throws java.io.IOException
Change Working Directory

Parameters:
dir - to change to
Returns:
success or failure.
Throws:
java.io.IOException

login

public boolean login(java.lang.String username,
                     java.lang.String password)
              throws java.io.IOException
Logs into the system with the given username and password.

Parameters:
username - username
password - password
Returns:
succes or failure.
Throws:
java.io.IOException

openConnection

public void openConnection()
                    throws java.io.IOException,
                           java.net.UnknownHostException
Open the connection to the previously defined URL given in location.

Throws:
java.io.IOException
java.net.UnknownHostException

initStream

public void initStream()
                throws java.io.IOException
The server usually sends a 220 reply when your first connect to it.

Throws:
java.io.IOException

writeln

protected void writeln(java.lang.String s)
                throws java.io.IOException
Centralize all the write operations for ease of maintenance.

Parameters:
s - the data to be sent over the control connection.
Throws:
java.io.IOException

port

public java.net.ServerSocket port()
                           throws java.io.IOException
Open a data connection in active mode. Active mode requires that the client listens for incoming connections - effectively a reversal of the tradicational client/server relationship.

Returns:
a ServerSocket to listen on.
Throws:
java.io.IOException

pasv

protected java.net.Socket pasv()
                        throws java.io.IOException
Open a passive mode data connection.

Returns:
a client Socket
Throws:
java.io.IOException

getIn

public java.io.InputStream getIn()
Returns:
the InputStream for the control connection.

getOut

public java.io.OutputStream getOut()
Returns:
the OutputStream for the control connection.

isOk

public boolean isOk()
             throws java.io.IOException
Error messages are typically 500 status codes. This method returns true if such a status code is not encountered.

Returns:
did the server accept the last action.
Throws:
java.io.IOException

check_reply

public boolean check_reply(java.lang.String code)
                    throws java.io.IOException
Returns true if the server returns the expected code. The exact message is not interesting.

Parameters:
code - the status code that we expect.
Returns:
did we recieve the expected code.
Throws:
java.io.IOException

mkdir

public boolean mkdir(java.lang.String dir)
              throws java.io.IOException
Create a new directory on the server, will not attempt to determine if the directory already exists.

Parameters:
dir - - name of the new folder.
Returns:
success or failure
Throws:
java.io.IOException

user

public boolean user(java.lang.String user)
             throws java.io.IOException
sends the USER command to the server, you can call this method directory if you want more control than given by the login() method.

Parameters:
user - username
Returns:
true if the username is acceptable.
Throws:
java.io.IOException

pass

public boolean pass(java.lang.String pass)
             throws java.io.IOException
sends the password as part of the user authentication procedure and reads the welcome message if one is available.

Parameters:
pass - - password for the user
Returns:
Is the password accepted?
Throws:
java.io.IOException

log

protected void log(java.lang.String mes)
logs the communitcation.

Parameters:
mes - add this message to the log.

getLastMessage

public java.lang.String getLastMessage()
Returns the last response from the server. You may need to call this method if check_reply() returned false, which indicates that the expected response was not recieved. The last message should then be retrieved for closer ispection.

Returns:
last response.

switchMode

public int switchMode()
Swtich between Active and Passive modes.

Returns:
current mode

makeDataConnection

public FTPConnection.DataConnection makeDataConnection()
                                                throws java.io.IOException
open a new active or passive data connection.

Returns:
DataConnection
Throws:
java.io.IOException

setLogWriter

public void setLogWriter(java.io.PrintStream logWriter)
Where to log. The default is System.out, use null to disbale logging.

Parameters:
logWriter - use this writer for loggin.

getLogWriter

public java.io.PrintStream getLogWriter()

testConnection

public boolean testConnection()
Are you there?

Returns:
yes or no

getWriter

public java.io.Writer getWriter()
Returns the writer associated with the control socket.

Returns:
Writer

setLocation

public void setLocation(java.net.URL location)

getLocation

public java.net.URL getLocation()


Rad Inks FTP library
Copyright (c) 2003,2004 Rad Inks (Pvt) Ltd.