Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

com.yaftp.ftp
Class FtpClientSession  view FtpClientSession download FtpClientSession.java

java.lang.Object
  extended bycom.yaftp.ftp.FtpClientSession
All Implemented Interfaces:
FtpConnection

public class FtpClientSession
extends java.lang.Object
implements FtpConnection

FtpClientSession is the main Ftp access class


Nested Class Summary
(package private)  class FtpClientSession.CommandSocket
          CommandSocket INNER CLASS is used to deal with FTP COMMAND port
(package private)  class FtpClientSession.DataSocket
          DataSocket INNER CLASS is a threaded data transfert class used to listen on FTP data port
 
Field Summary
private static java.lang.String _CLOSEPAR_
           
private  FtpClientSession.CommandSocket _cmdSock
           
private static java.lang.String _COMMA_
           
private  int _commandPort
           
private  java.lang.String _currentPsw
           
private  java.lang.String _currentUser
           
private  FtpDataNotifier _dataEvt
           
private  java.lang.String _directoryMessage
           
private static java.lang.String _DOT_
           
private static java.lang.String _EMPTY_
           
private  java.lang.String _ftpSrvName
           
private  java.lang.String _lastCommandMessage
           
private static java.lang.String _OPENPAR_
           
private  Ftp _parent
           
private  FtpSessionOs _serverOS
           
private  FtpTraces _traces
           
private static int FTP_COMMAND_OK
           
private static int FTP_DEFAULT_COMMAND_PORT
           
private static int FTP_DEFAULT_DATA_PORT
           
private static int FTP_FILE_NOT_AVAILABLE
           
private static int FTP_PASV_CLOSE
           
private static int FTP_PASV_CLOSE_ALTERNATE
          this code is used by MVS FTP server
private static int FTP_PASV_OPEN
           
private static int FTP_PERMANENT_NEGATIVE_COMPLETION
           
private static int FTP_POSITIVE_COMPLETION_REPLY
           
private static int FTP_POSITIVE_INTERMEDIATE_REPLY
           
private static int FTP_POSITIVE_PRELIMINARY_REPLY
           
private static int FTP_SUCCESSFULL_CONNECTION
           
private static int FTP_SYSTEM_TYPE
           
private static int FTP_TRANSIENT_NEGATIVE_COMPLETION
           
private static short MAX_UNSIGNED_BYTE
           
 
Constructor Summary
FtpClientSession()
          Bean compliant class constructor
 
Method Summary
 void changeDirectory(java.lang.String dirName)
          proceed with standard change directory command
 void changeFtpSrvDir(java.lang.String dirName)
          proceed with a change directory command on server side
 void deleFtpSrvDir(java.lang.String dirName)
          proceed with a remove directory command on server side
 boolean delete(java.lang.String candidate)
          send a DELE Command
 java.util.Vector filteredList()
          Filtered file list operation
 void ftpAscii()
          change transfert mode to ASCII
 void ftpBinary()
          change transfert mode to binary
 java.lang.String ftpCommand(java.lang.String command)
          execute a user entered FTP command
 void ftpInit(java.lang.String ftpSrvName)
           
 java.lang.String[] get_column_names()
          returns the names of expanded mode list view
 java.lang.String get_ftpSrvName()
          return a well known name identifying this FTP
 java.lang.String get_lastCommandMessage()
           
 FtpTraces get_traces()
           
 java.lang.String getDirectoryMessage()
          return message appropriate to directory change
 void getFile(java.lang.String fName, FtpClientListener callback)
          FTP receive File Operation
 java.lang.String getOsDetails()
          returns OS detail string if needed
 java.lang.String getOSFtp()
          request info about the server OS hosting the FTP service
 java.lang.String getWorkingDirectory()
          return current FTP server working directory
 boolean isLocal()
          return true if FTP implementors deasl with local drive session
 boolean isMVS()
          returns true if connection is an MVS FTP session
 void list(FtpClientListener callback)
          list command with callback
 void login(java.lang.String userID, java.lang.String passwd)
          Log in into FTP server
 void login(java.lang.String userID, java.lang.String passwd, FtpSessionOs ftpOs)
          Log in into FTP server
static void main(java.lang.String[] argv)
           this main method is used to test and demonstrate FtpClientSession class usage to tranfert file in and out.
 void newDirectory(java.lang.String dirName)
          proceed with a make directory command on server side
 void putFile(java.lang.String fName, FtpClientListener callback)
          FTP send File Operation
 void reconnectSession()
          use this method to reconnect a timed out session ; the FTP session will be negociated using login provided userid and passwords
 void removeDirectory(java.lang.String candidate)
          proceed with remove empty directory command
 void rename(java.lang.String from, java.lang.String to)
          send a RENAME Command
 void set_jesMode(boolean jesMode)
          this idiosynchrasie is used for MVS only
 void set_parent(Ftp parent)
          Init debugging on (Should be set before ftpInit call
 java.lang.String site(java.lang.String siteValue)
          send a Site Command
 void stop()
          Loging out Ftp Client Session
 void trace(java.lang.String message)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_EMPTY_

private static final java.lang.String _EMPTY_
See Also:
Constant Field Values

FTP_DEFAULT_COMMAND_PORT

private static final int FTP_DEFAULT_COMMAND_PORT
See Also:
Constant Field Values

FTP_DEFAULT_DATA_PORT

private static final int FTP_DEFAULT_DATA_PORT
See Also:
Constant Field Values

MAX_UNSIGNED_BYTE

private static final short MAX_UNSIGNED_BYTE
See Also:
Constant Field Values

FTP_POSITIVE_PRELIMINARY_REPLY

private static final int FTP_POSITIVE_PRELIMINARY_REPLY
See Also:
Constant Field Values

FTP_POSITIVE_COMPLETION_REPLY

private static final int FTP_POSITIVE_COMPLETION_REPLY
See Also:
Constant Field Values

FTP_POSITIVE_INTERMEDIATE_REPLY

private static final int FTP_POSITIVE_INTERMEDIATE_REPLY
See Also:
Constant Field Values

FTP_TRANSIENT_NEGATIVE_COMPLETION

private static final int FTP_TRANSIENT_NEGATIVE_COMPLETION
See Also:
Constant Field Values

FTP_PERMANENT_NEGATIVE_COMPLETION

private static final int FTP_PERMANENT_NEGATIVE_COMPLETION
See Also:
Constant Field Values

FTP_COMMAND_OK

private static final int FTP_COMMAND_OK
See Also:
Constant Field Values

FTP_SYSTEM_TYPE

private static final int FTP_SYSTEM_TYPE
See Also:
Constant Field Values

FTP_SUCCESSFULL_CONNECTION

private static final int FTP_SUCCESSFULL_CONNECTION
See Also:
Constant Field Values

FTP_FILE_NOT_AVAILABLE

private static final int FTP_FILE_NOT_AVAILABLE
See Also:
Constant Field Values

FTP_PASV_OPEN

private static final int FTP_PASV_OPEN
See Also:
Constant Field Values

FTP_PASV_CLOSE

private static final int FTP_PASV_CLOSE
See Also:
Constant Field Values

FTP_PASV_CLOSE_ALTERNATE

private static final int FTP_PASV_CLOSE_ALTERNATE
this code is used by MVS FTP server

See Also:
Constant Field Values

_OPENPAR_

private static final java.lang.String _OPENPAR_
See Also:
Constant Field Values

_CLOSEPAR_

private static final java.lang.String _CLOSEPAR_
See Also:
Constant Field Values

_COMMA_

private static final java.lang.String _COMMA_
See Also:
Constant Field Values

_DOT_

private static final java.lang.String _DOT_
See Also:
Constant Field Values

_commandPort

private int _commandPort

_ftpSrvName

private java.lang.String _ftpSrvName

_cmdSock

private transient FtpClientSession.CommandSocket _cmdSock

_currentUser

private java.lang.String _currentUser

_currentPsw

private java.lang.String _currentPsw

_traces

private transient FtpTraces _traces

_dataEvt

private transient FtpDataNotifier _dataEvt

_parent

private transient Ftp _parent

_serverOS

private transient FtpSessionOs _serverOS

_lastCommandMessage

private transient java.lang.String _lastCommandMessage

_directoryMessage

private transient java.lang.String _directoryMessage
Constructor Detail

FtpClientSession

public FtpClientSession()
Bean compliant class constructor

Method Detail

get_lastCommandMessage

public java.lang.String get_lastCommandMessage()

getDirectoryMessage

public java.lang.String getDirectoryMessage()
Description copied from interface: FtpConnection
return message appropriate to directory change

Specified by:
getDirectoryMessage in interface FtpConnection

set_parent

public void set_parent(Ftp parent)
Init debugging on (Should be set before ftpInit call


get_traces

public FtpTraces get_traces()

trace

public final void trace(java.lang.String message)

ftpInit

public void ftpInit(java.lang.String ftpSrvName)
             throws ClientFtpError

get_ftpSrvName

public java.lang.String get_ftpSrvName()
Description copied from interface: FtpConnection
return a well known name identifying this FTP

Specified by:
get_ftpSrvName in interface FtpConnection

login

public void login(java.lang.String userID,
                  java.lang.String passwd)
           throws ClientFtpError
Log in into FTP server


login

public void login(java.lang.String userID,
                  java.lang.String passwd,
                  FtpSessionOs ftpOs)
           throws ClientFtpError
Log in into FTP server


reconnectSession

public void reconnectSession()
                      throws ClientFtpError
use this method to reconnect a timed out session ; the FTP session will be negociated using login provided userid and passwords


stop

public final void stop()
                throws ClientFtpError
Loging out Ftp Client Session

Specified by:
stop in interface FtpConnection

getWorkingDirectory

public final java.lang.String getWorkingDirectory()
                                           throws ClientFtpError
return current FTP server working directory


site

public final java.lang.String site(java.lang.String siteValue)
                            throws ClientFtpError
send a Site Command


delete

public final boolean delete(java.lang.String candidate)
                     throws ClientFtpError
send a DELE Command

Specified by:
delete in interface FtpConnection

rename

public final void rename(java.lang.String from,
                         java.lang.String to)
                  throws ClientFtpError
send a RENAME Command

Specified by:
rename in interface FtpConnection

removeDirectory

public final void removeDirectory(java.lang.String candidate)
                           throws ClientFtpError
Description copied from interface: FtpConnection
proceed with remove empty directory command

Specified by:
removeDirectory in interface FtpConnection

list

public void list(FtpClientListener callback)
          throws ClientFtpError
list command with callback


filteredList

public java.util.Vector filteredList()
                              throws ClientFtpError
Filtered file list operation

Specified by:
filteredList in interface FtpConnection

changeFtpSrvDir

public void changeFtpSrvDir(java.lang.String dirName)
                     throws ClientFtpError
proceed with a change directory command on server side


newDirectory

public void newDirectory(java.lang.String dirName)
                  throws ClientFtpError
proceed with a make directory command on server side

Specified by:
newDirectory in interface FtpConnection

deleFtpSrvDir

public void deleFtpSrvDir(java.lang.String dirName)
                   throws ClientFtpError
proceed with a remove directory command on server side


ftpBinary

public void ftpBinary()
               throws ClientFtpError
change transfert mode to binary


ftpCommand

public java.lang.String ftpCommand(java.lang.String command)
                            throws ClientFtpError
execute a user entered FTP command

Specified by:
ftpCommand in interface FtpConnection

ftpAscii

public void ftpAscii()
              throws ClientFtpError
change transfert mode to ASCII


getOSFtp

public final java.lang.String getOSFtp()
request info about the server OS hosting the FTP service

Specified by:
getOSFtp in interface FtpConnection

getOsDetails

public final java.lang.String getOsDetails()
Description copied from interface: FtpConnection
returns OS detail string if needed

Specified by:
getOsDetails in interface FtpConnection

get_column_names

public java.lang.String[] get_column_names()
Description copied from interface: FtpConnection
returns the names of expanded mode list view

Specified by:
get_column_names in interface FtpConnection

isLocal

public boolean isLocal()
Description copied from interface: FtpConnection
return true if FTP implementors deasl with local drive session

Specified by:
isLocal in interface FtpConnection

changeDirectory

public void changeDirectory(java.lang.String dirName)
                     throws ClientFtpError
Description copied from interface: FtpConnection
proceed with standard change directory command

Specified by:
changeDirectory in interface FtpConnection

isMVS

public boolean isMVS()
Description copied from interface: FtpConnection
returns true if connection is an MVS FTP session

Specified by:
isMVS in interface FtpConnection

set_jesMode

public void set_jesMode(boolean jesMode)
Description copied from interface: FtpConnection
this idiosynchrasie is used for MVS only

Specified by:
set_jesMode in interface FtpConnection

getFile

public void getFile(java.lang.String fName,
                    FtpClientListener callback)
             throws ClientFtpError
FTP receive File Operation


putFile

public void putFile(java.lang.String fName,
                    FtpClientListener callback)
             throws ClientFtpError
FTP send File Operation


main

public static void main(java.lang.String[] argv)
    this main method is used to test and demonstrate FtpClientSession
    class usage to tranfert file in and out.

    usage of FtpClientSession class is straightforward :

    here is the main test static class content :

 CODE STARTS *************************

    String host  = null ;
    String user  = null ;
    String passw = null ;
    String fName = null ;

    System.out.println( "Starting ftp test" ) ;
    if ( argv.length  >= 1 )
      host  = argv[0] ;
    if ( argv.length  >= 2 )
      user  = argv[1] ;
    if ( argv.length  >= 3 )
      passw = argv[2] ;
    if ( argv.length  >= 4 )
      fName = argv[3] ;

    try {
      Ftp ftp = new Ftp() ;
      ftp.set_ftpServerName( host );
      FtpClientSession myFtp = new FtpClientSession() ;
      myFtp.set_parent( ftp  ) ;

      System.out.println( "Before init " ) ;
      myFtp.ftpInit(host) ;
      System.out.println( "Init done " ) ;

      myFtp.login( user    , passw   ) ;
      System.out.println( "Login done " ) ;

      FtpVectorListener fileList = new FtpVectorListener() ;

      myFtp.list( fileList ) ;

      System.out.println( "Ending ftp test part 1 " ) ;

      if ( fName != null )
      try {
        DataOutputStream out = new DataOutputStream (
                               new BufferedOutputStream (
                               new FileOutputStream( fName )
                                                         )   ) ;


        for ( int ii = 0 ; ii < fileList.get_dataList().size() ; ii++ )
        {
          System.out.println( (String)( fileList.get_dataList().elementAt(ii))) ;
          out.writeBytes((String)( fileList.get_dataList().elementAt(ii))+"\n") ;
        }
        out.close() ;
      } catch ( IOException e ){
        System.out.println(  "Severe file access Error : " ) ;
        System.out.println(  e.getMessage() ) ;

      }

      Vector fileFilteredList = myFtp.filteredList() ;
      System.out.println( "Ending ftp test part 2 " ) ;

      for ( int ii = 0 ; ii < fileFilteredList.size() ; ii++ )
        System.out.println( (FtpOsFile)( fileFilteredList.elementAt(ii) ) ) ;

      // resend FileList local sored file back to caller

      if ( fName != null )
        myFtp.putFile(fName , fileList ) ;

      System.out.println( "Ending ftp test part 4" ) ;


      // reRead FileList once again
      if ( fName != null )
        myFtp.getFile(fName , fileList ) ;

      System.out.println( "Ending ftp test part 3 " ) ;

      for ( int ii = 0 ; ii < fileList.get_dataList().size() ; ii++ )
        System.out.println( (String)( fileList.get_dataList().elementAt(ii) ) ) ;

      myFtp.stop() ;

     } catch( ClientFtpError e )
    {
      System.out.println(  "Severe ftp error occured : " ) ;
      System.out.println(  e.getMessage() ) ;
    }
    System.out.println( "Ending ftp test :" ) ;

 CODE ENDS   *************************

    now let's explain it in details :

    String host  = null ; ==> IP name or address of the host to connect with
    String user  = null ; ==> ftp user name
    String passw = null ; ==> associated password
    String fName = null ; ==> file name to transfer in or out

    we will need 4 parameter to establish a FTP client connection, which
    are :

      Ftp ftp = new Ftp() ;
      ftp.set_ftpServerName( host );
      ftp.set_userId( user ) ;
      ftp.set_passWord( passw );
      FtpClientSession myFtp = new FtpClientSession() ;
      myFtp.set_parent( ftp  ) ;

    the two statement above proceed with FtpClientSession class initialization
    and should occur in the above specified order.

    We then establish connection with the FTP host server

    myFtp.ftpInit(host) ;

    Before any operation we should log into the system providing username
    and password :

    myFtp.login( user , passw   ) ;

    we will then list the available files on server using a provided
    callback container class : FtpVectorListener which will store the
    FileList into a String vector :

    FtpVectorListener fileList = new FtpVectorListener() ;
    myFtp.list( fileList ) ;

    We will then write the File List into a local file and send it
    ovr the ftp connection and display it on standard locl output :

      if ( fName != null )
      try {
        DataOutputStream out = new DataOutputStream (
                               new BufferedOutputStream (
                               new FileOutputStream( fName )
                                                         )   ) ;


        for ( int ii = 0 ; ii < fileList.get_dataList().size() ; ii++ )
        {
          System.out.println( (String)( fileList.get_dataList().elementAt(ii))) ;
          out.writeBytes((String)( fileList.get_dataList().elementAt(ii))+"\n") ;
        }
        out.close() ;
      } catch ( IOException e ){
        System.out.println(  "Severe file access Error : " ) ;
        System.out.println(  e.getMessage() ) ;
      }
      Vector fileFilteredList = myFtp.filteredList() ;
      System.out.println( "Ending ftp test part 2 " ) ;

      for ( int ii = 0 ; ii < fileFilteredList.size() ; ii++ )
        System.out.println( (FtpOsFile)( fileFilteredList.elementAt(ii) ) ) ;

   We now send the file over the FTP connection , the transfer messages
   beeing strored into the fileList string vector :

      if ( fName != null )
        myFtp.putFile(fName , fileList ) ;

   We after do the reverse operation transfering the file back :

   if ( fName != null )
        myFtp.getFile(fName , fileList ) ;