rheise.jftpd
Class ServerPI

java.lang.Object
  |
  +--rheise.jftpd.ServerPI

public class ServerPI
extends java.lang.Object
implements java.lang.Runnable

This is the server protocol interpreter. The main Server object creates one of these for each user connection to interpret user commands. This class implements the Runnable interface so that it can be run as a thread or invoked directly.


Field Summary
private  java.lang.String baseDir
          This is effectively the root directory as seen from outside.
private  java.net.Socket clientSocket
          The client we are talking to.
private  java.lang.Class[] commandHandlerArgTypes
          Reflection is used to invoke a command handler for a given string command.
private  java.lang.String currentDir
          The current directory as seen externally.
private  ServerDTP dtp
          The data transfer process responsible for transferring files to and from the user.
private  java.lang.String password
          Stores the password of the user.
private  java.io.BufferedReader reader
          A Reader for reading from the client socket.
private  java.lang.String username
          Stores the username of the user.
private  java.io.PrintWriter writer
          A Writer for writing to the client socket.
 
Constructor Summary
ServerPI(java.net.Socket clientSocket)
          Creates a server protocol interpreter for the specified client socket.
 
Method Summary
(package private)  void checkLogin()
           
private  void clientLoop()
          The loops on input from the client socket, reading each command and using the reflection API to invoke the appropriate command handler method for that command.
(package private)  java.lang.String createNativePath(java.lang.String ftpPath)
          Creates a native absolute path from a path string sent from the client.
 int handle_abor(java.lang.String line, java.util.StringTokenizer st)
          Command handler for the ABOR command.
 int handle_acct(java.lang.String line, java.util.StringTokenizer st)
          Command handler for the ACCT command.
 int handle_allo(java.lang.String line, java.util.StringTokenizer st)
          Command handler for the ALLO command.
 int handle_appe(java.lang.String line, java.util.StringTokenizer st)
          Command handler for the APPE command.
 int handle_cdup(java.lang.String line, java.util.StringTokenizer st)
          Command handler for the CDUP command.
 int handle_cwd(java.lang.String line, java.util.StringTokenizer st)
          Command handler for the CWD command.
 int handle_dele(java.lang.String line, java.util.StringTokenizer st)
          Command handler for the DELE command.
 int handle_help(java.lang.String line, java.util.StringTokenizer st)
          Command handler for the HELP command.
 int handle_list(java.lang.String line, java.util.StringTokenizer st)
          Command handler for the LIST command.
 int handle_mdtm(java.lang.String line, java.util.StringTokenizer st)
          Command handler for the MDTM command.
 int handle_mkd(java.lang.String line, java.util.StringTokenizer st)
          Command handler for the MKD command.
 int handle_mode(java.lang.String line, java.util.StringTokenizer st)
          Command handler for the MODE command.
 int handle_nlst(java.lang.String line, java.util.StringTokenizer st)
          Command handler for the NLST command.
 int handle_noop(java.lang.String line, java.util.StringTokenizer st)
          Command handler for the NOOP command.
 int handle_pass(java.lang.String line, java.util.StringTokenizer st)
          Command handler for the PASS command.
 int handle_pasv(java.lang.String line, java.util.StringTokenizer st)
          Command handler for the PASV command.
 int handle_port(java.lang.String line, java.util.StringTokenizer st)
          Command handler for the PORT command.
 int handle_pwd(java.lang.String line, java.util.StringTokenizer st)
          Command handler for the PWD command.
 int handle_quit(java.lang.String line, java.util.StringTokenizer st)
          Command handler for the QUIT command.
 int handle_rein(java.lang.String line, java.util.StringTokenizer st)
          Command handler for the REIN command.
 int handle_rest(java.lang.String line, java.util.StringTokenizer st)
          Command handler for the REST command.
 int handle_retr(java.lang.String line, java.util.StringTokenizer st)
          Command handler for the RETR command.
 int handle_rmd(java.lang.String line, java.util.StringTokenizer st)
          Command handler for the RMD command.
 int handle_rnfr(java.lang.String line, java.util.StringTokenizer st)
          Command handler for the RNFR command.
 int handle_rnto(java.lang.String line, java.util.StringTokenizer st)
          Command handler for the RNTO command.
 int handle_site(java.lang.String line, java.util.StringTokenizer st)
          Command handler for the SITE command.
 int handle_size(java.lang.String line, java.util.StringTokenizer st)
          Command handler for the SIZE command.
 int handle_smnt(java.lang.String line, java.util.StringTokenizer st)
          Command handler for the SMNT command.
 int handle_stat(java.lang.String line, java.util.StringTokenizer st)
          Command handler for the STAT command.
 int handle_stor(java.lang.String line, java.util.StringTokenizer st)
          Command handler for the STOR command.
 int handle_stou(java.lang.String line, java.util.StringTokenizer st)
          Command handler for the STOU command.
 int handle_stru(java.lang.String line, java.util.StringTokenizer st)
          Command handler for the STRU command.
 int handle_syst(java.lang.String line, java.util.StringTokenizer st)
          Command handler for the SYST command.
 int handle_type(java.lang.String line, java.util.StringTokenizer st)
          Command handler for the TYPE command.
 int handle_user(java.lang.String line, java.util.StringTokenizer st)
          Command handler for the USER command.
(package private)  int reply(int code, java.lang.String text)
          Writes a reply line to the client socket.
(package private)  java.lang.String resolvePath(java.lang.String path)
          Resolves an FTP given by the client.
 void run()
          Runs the protocol interpreter for a client.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

clientSocket

private java.net.Socket clientSocket
The client we are talking to.

reader

private java.io.BufferedReader reader
A Reader for reading from the client socket.

writer

private java.io.PrintWriter writer
A Writer for writing to the client socket.

dtp

private ServerDTP dtp
The data transfer process responsible for transferring files to and from the user.

commandHandlerArgTypes

private java.lang.Class[] commandHandlerArgTypes
Reflection is used to invoke a command handler for a given string command. This variable caches the argument types for command handler methods.

username

private java.lang.String username
Stores the username of the user.

password

private java.lang.String password
Stores the password of the user.

baseDir

private final java.lang.String baseDir
This is effectively the root directory as seen from outside.

currentDir

private java.lang.String currentDir
The current directory as seen externally.
Constructor Detail

ServerPI

public ServerPI(java.net.Socket clientSocket)
         throws java.io.IOException
Creates a server protocol interpreter for the specified client socket. Commands will be read from the socket, and replies will be written to the socket. Data transfers, however, require a separate data socket to be created via the ServerDTP.
Method Detail

run

public void run()
Runs the protocol interpreter for a client.
Specified by:
run in interface java.lang.Runnable

clientLoop

private void clientLoop()
                 throws java.lang.Exception
The loops on input from the client socket, reading each command and using the reflection API to invoke the appropriate command handler method for that command.

handle_user

public int handle_user(java.lang.String line,
                       java.util.StringTokenizer st)
                throws CommandException
Command handler for the USER command.
USER %SP% %username% %CRLF%

handle_pass

public int handle_pass(java.lang.String line,
                       java.util.StringTokenizer st)
                throws CommandException
Command handler for the PASS command. A USER command must have been read first.
PASS %SP% %password% %CRLF%

handle_acct

public int handle_acct(java.lang.String line,
                       java.util.StringTokenizer st)
                throws CommandException
Command handler for the ACCT command.
ACCT %SP% %account-information% %CRLF%

handle_cwd

public int handle_cwd(java.lang.String line,
                      java.util.StringTokenizer st)
               throws CommandException
Command handler for the CWD command.
CWD %SP% %pathname% %CRLF%

handle_cdup

public int handle_cdup(java.lang.String line,
                       java.util.StringTokenizer st)
                throws CommandException
Command handler for the CDUP command.
CDUP %CRLF%

handle_smnt

public int handle_smnt(java.lang.String line,
                       java.util.StringTokenizer st)
                throws CommandException
Command handler for the SMNT command.
SMNT %SP% %pathname% %CRLF%

handle_quit

public int handle_quit(java.lang.String line,
                       java.util.StringTokenizer st)
                throws CommandException
Command handler for the QUIT command.
QUIT %CRLF%

handle_rein

public int handle_rein(java.lang.String line,
                       java.util.StringTokenizer st)
                throws CommandException
Command handler for the REIN command.
REIN %CRLF%

handle_port

public int handle_port(java.lang.String line,
                       java.util.StringTokenizer st)
                throws CommandException
Command handler for the PORT command.
PORT %SP% %host-port% %CRLF%

handle_pasv

public int handle_pasv(java.lang.String line,
                       java.util.StringTokenizer st)
                throws CommandException
Command handler for the PASV command.
PASV %CRLF%

handle_type

public int handle_type(java.lang.String line,
                       java.util.StringTokenizer st)
                throws CommandException
Command handler for the TYPE command. Supported arguments are 'A' for ASCII and 'I' for IMAGE.
TYPE %SP% %type-code% %CRLF%

handle_stru

public int handle_stru(java.lang.String line,
                       java.util.StringTokenizer st)
                throws CommandException
Command handler for the STRU command. The only supported argument is 'F' for file structure.
STRU %SP% %structure-code% %CRLF%

handle_mode

public int handle_mode(java.lang.String line,
                       java.util.StringTokenizer st)
                throws CommandException
Command handler for the MODE command. The only supported argument is 'S' for STREAM.
MODE %SP% %mode-code% %CRLF%

handle_retr

public int handle_retr(java.lang.String line,
                       java.util.StringTokenizer st)
                throws CommandException
Command handler for the RETR command. The ServerDTP is used to send the data to the user.
RETR %SP% %pathname% %CRLF%

handle_stor

public int handle_stor(java.lang.String line,
                       java.util.StringTokenizer st)
                throws CommandException
Command handler for the STOR command. The ServerDTP is used to receive the data on the data port.
STOR %SP% %pathname% %CRLF%

handle_stou

public int handle_stou(java.lang.String line,
                       java.util.StringTokenizer st)
                throws CommandException
Command handler for the STOU command.
STOU %CRLF%

handle_appe

public int handle_appe(java.lang.String line,
                       java.util.StringTokenizer st)
                throws CommandException
Command handler for the APPE command.
APPE %SP% %pathname% %CRLF%

handle_allo

public int handle_allo(java.lang.String line,
                       java.util.StringTokenizer st)
                throws CommandException
Command handler for the ALLO command.
ALLO %SP% %decimal-integer% [%SP% R %SP% %decimal-integer%] %CRLF%

handle_rest

public int handle_rest(java.lang.String line,
                       java.util.StringTokenizer st)
                throws CommandException
Command handler for the REST command.
REST %SP% %marker% %CRLF%

handle_rnfr

public int handle_rnfr(java.lang.String line,
                       java.util.StringTokenizer st)
                throws CommandException
Command handler for the RNFR command.
RNFR %SP% %pathname% %CRLF%

handle_rnto

public int handle_rnto(java.lang.String line,
                       java.util.StringTokenizer st)
                throws CommandException
Command handler for the RNTO command.
RNTO %SP% %pathname% %CRLF%

handle_abor

public int handle_abor(java.lang.String line,
                       java.util.StringTokenizer st)
                throws CommandException
Command handler for the ABOR command.
ABOR %CRLF%

handle_dele

public int handle_dele(java.lang.String line,
                       java.util.StringTokenizer st)
                throws CommandException
Command handler for the DELE command.
DELE %SP% %pathname% %CRLF%

handle_rmd

public int handle_rmd(java.lang.String line,
                      java.util.StringTokenizer st)
               throws CommandException
Command handler for the RMD command.
RMD %SP% %pathname% %CRLF%

handle_mkd

public int handle_mkd(java.lang.String line,
                      java.util.StringTokenizer st)
               throws CommandException
Command handler for the MKD command.
MKD %SP% %pathname% %CRLF%

handle_pwd

public int handle_pwd(java.lang.String line,
                      java.util.StringTokenizer st)
               throws CommandException
Command handler for the PWD command.
PWD %CRLF%

handle_list

public int handle_list(java.lang.String line,
                       java.util.StringTokenizer st)
                throws CommandException
Command handler for the LIST command. If pathname is not specified, the current durectory will be used.
LIST [%SP% %pathname%] %CRLF%

handle_nlst

public int handle_nlst(java.lang.String line,
                       java.util.StringTokenizer st)
                throws CommandException
Command handler for the NLST command. If pathname is not specified, the current directory will be used.
NLST [%SP% %pathname%] %CRLF%

handle_site

public int handle_site(java.lang.String line,
                       java.util.StringTokenizer st)
                throws CommandException
Command handler for the SITE command.
SITE %SP% %string% %CRLF%

handle_syst

public int handle_syst(java.lang.String line,
                       java.util.StringTokenizer st)
                throws CommandException
Command handler for the SYST command.
SYST %CRLF%

handle_stat

public int handle_stat(java.lang.String line,
                       java.util.StringTokenizer st)
                throws CommandException
Command handler for the STAT command.
STAT [%SP% %pathname%] %CRLF%

handle_help

public int handle_help(java.lang.String line,
                       java.util.StringTokenizer st)
                throws CommandException
Command handler for the HELP command.
HELP [%SP% %string%] %CRLF%

handle_noop

public int handle_noop(java.lang.String line,
                       java.util.StringTokenizer st)
                throws CommandException
Command handler for the NOOP command.
NOOP %CRLF%

handle_size

public int handle_size(java.lang.String line,
                       java.util.StringTokenizer st)
                throws CommandException
Command handler for the SIZE command.
SIZE %SP% %pathname% %CRLF%

handle_mdtm

public int handle_mdtm(java.lang.String line,
                       java.util.StringTokenizer st)
                throws CommandException
Command handler for the MDTM command.
MDTM %SP% %pathname% %CRLF%

reply

int reply(int code,
          java.lang.String text)
Writes a reply line to the client socket.
Parameters:
code - the reply code.
text - the string message to include in the reply.
Returns:
the reply code for this reply.

createNativePath

java.lang.String createNativePath(java.lang.String ftpPath)
Creates a native absolute path from a path string sent from the client. The absolute path constructed will always be prefixed with baseDir. If ftpPath does not begin with a '/', the constructed path will also be relativee to currentDir.

resolvePath

java.lang.String resolvePath(java.lang.String path)
Resolves an FTP given by the client. Relative paths will be resolved relative to currentDir. '.' path segments will be removed, and '..' path segments will pop the previous segment of the path stack (if there is a previous segment).

checkLogin

void checkLogin()
          throws CommandException