com.myjavaworld.ftp
Class ControlConnection

java.lang.Object
  extended by com.myjavaworld.ftp.ControlConnection
All Implemented Interfaces:
FTPConstants
Direct Known Subclasses:
ExplicitSSLControlConnection, ImplicitSSLControlConnection

public class ControlConnection
extends Object
implements FTPConstants

This class represents a Control Connection as specified in the FTP protocol specification. For more details refer to RFC 959.

Version:
2.0
Author:
Sai Pullabhotla, psai [at] jMethods [dot] com

Field Summary
protected  FTPClient client
          FTPClient object that created this ControlConnection.
protected  EventListenerList listenerList
          List of registered listeners that are willing to get notifications about the activity of this ControlConnection.
protected  BufferedReader reader
          A Reader object for receiving replies from the remote host.
protected  Socket socket
          A Socket that represents a connection to the remote host.
protected  PrintStream writer
          A Writer object that sends commands to the remote host.
 
Fields inherited from interface com.myjavaworld.ftp.FTPConstants
DEFAULT_BUFFER_SIZE, DEFAULT_IMPLICIT_SSL_PORT, DEFAULT_MODE, DEFAULT_PORT, DEFAULT_STRUCTURE, DEFAULT_TIMEOUT, DEFAULT_TYPE, EOL, MODE_BLOCK, MODE_COMPRESSED, MODE_STREAM, STRUCTURE_FILE, STRUCTURE_PAGE, STRUCTURE_RECORD, TYPE_ASCII, TYPE_BINARY, TYPE_EBCDIC, TYPE_IMAGE, TYPE_LOCAL, USE_EXPLICIT_SSL, USE_IMPLICIT_SSL, USE_NO_SSL, USE_SSL_IF_AVAILABLE
 
Constructor Summary
ControlConnection(FTPClient client)
          Constructs a ControlConnection object.
 
Method Summary
 void close()
          Closes this ControlConnection by closing the socket to the remote host and its associated strems.
 void connect(String host)
          Connects to the given remote host on the default FTP port as defined in FTPConstants.DEFAULT_PORT.
 void connect(String host, int port)
          Connects to the specified remote host on the specified port number.
 String executeCommand(String command)
          Executes the given command command.
protected  void fireCommandSent(ControlConnectionEvent evt)
          Used to notify registered listeners that a command was sent to the remote host.
protected  void fireReplyReceived(ControlConnectionEvent evt)
          Used to notify the registered listeners that a reply was received from the remote host.
 InetAddress getLocalAddress()
          Returns the InetAddress of the local host.
 String getLocalHost()
          Returns the host name of the local host.
 String getLocalIPAddress()
          Returns the IP address of the local host.
 int getLocalPort()
          Retruns the local port number to which this ControlConnection is connected to.
 InetAddress getRemoteAddress()
          Returns the remote host's InetAddress.
 String getRemoteHost()
          Returns the fully qualified domain name of the remote host.
 String getRemoteIPAddress()
          Returns the IP address of the remote host in xxx.xxx.xxx.xxx format.
 int getRemotePort()
          Returns the remote port number to which this ControlConnection is connected to.
 String getReply()
          reads a single FTP response from the remote host.
 SSLSession getSSLSession()
          Returns the SSLSession associated with this connection.
 boolean isSecured()
          Tells whether or not this ControlConnection is secured.
 void sendCommand(String command)
          Sends the given command command, to the remote host over this ControlConnection.
protected  void stderr(String message)
          Prints the given string to standard error.
protected  void stdout(String message)
          Prints a given string to the standard output.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

client

protected FTPClient client
FTPClient object that created this ControlConnection.


socket

protected Socket socket
A Socket that represents a connection to the remote host.


reader

protected BufferedReader reader
A Reader object for receiving replies from the remote host.


writer

protected PrintStream writer
A Writer object that sends commands to the remote host.


listenerList

protected EventListenerList listenerList
List of registered listeners that are willing to get notifications about the activity of this ControlConnection.

Constructor Detail

ControlConnection

public ControlConnection(FTPClient client)
Constructs a ControlConnection object.

Parameters:
client - The FTPClient that created this control connection.
Method Detail

connect

public void connect(String host)
             throws ConnectionException,
                    FTPException
Connects to the given remote host on the default FTP port as defined in FTPConstants.DEFAULT_PORT.

Parameters:
host - Host name or IP address of the remote host.
Throws:
ConnectionException - if unable to connect to the specified host.
FTPException

connect

public void connect(String host,
                    int port)
             throws ConnectionException,
                    FTPException
Connects to the specified remote host on the specified port number.

Parameters:
host - Host name or IP address of the remote host.
port - Port number to connect to.
Throws:
ConnectionException - If unable to connect to the specified host.
FTPException

sendCommand

public void sendCommand(String command)
                 throws ConnectionException
Sends the given command command, to the remote host over this ControlConnection. All commands will be appended with telnet end of line (\r\n) characters before sending to the remote host.

Parameters:
command - Command to send to the remote host.
Throws:
ConnectionException - if a network or IO error occurs while sending the command.

getReply

public String getReply()
                throws ConnectionException
reads a single FTP response from the remote host. If the reponse is a multi-line reponse, all lines will be read until the response is completely retrieved.

Returns:
response from the remote host.
Throws:
ConnectionException - if a network or IO error occurs while reading the response.

executeCommand

public String executeCommand(String command)
                      throws ConnectionException
Executes the given command command. This method is similar to calling the sendCommand and getReply methods sequentially.

Parameters:
command - The command to be sent to the remote host.
Returns:
Reply from the remote host.
Throws:
ConnectionException - if a network or IO error occurs.

close

public void close()
           throws IOException
Closes this ControlConnection by closing the socket to the remote host and its associated strems.

Throws:
IOException - if a network or IO error occurs while closing this ControlConnection.

getRemoteAddress

public InetAddress getRemoteAddress()
Returns the remote host's InetAddress.

Returns:
InetAddress of the remote host.

getRemoteHost

public String getRemoteHost()
Returns the fully qualified domain name of the remote host.

Returns:
Fully qualified domain name of the remote host.

getRemoteIPAddress

public String getRemoteIPAddress()
Returns the IP address of the remote host in xxx.xxx.xxx.xxx format.

Returns:
IP address of the remote host.

getRemotePort

public int getRemotePort()
Returns the remote port number to which this ControlConnection is connected to.

Returns:
Remote port number to which this ControlConnection is connected to.

getLocalAddress

public InetAddress getLocalAddress()
Returns the InetAddress of the local host.

Returns:
InetAddress of the local host.

getLocalHost

public String getLocalHost()
Returns the host name of the local host.

Returns:
Host name of the local host.

getLocalIPAddress

public String getLocalIPAddress()
Returns the IP address of the local host.

Returns:
IP address of the local host.

getLocalPort

public int getLocalPort()
Retruns the local port number to which this ControlConnection is connected to.

Returns:
Local port.

isSecured

public boolean isSecured()
Tells whether or not this ControlConnection is secured.

Returns:
true, if this connection is secured. false, otherwise.

getSSLSession

public SSLSession getSSLSession()
Returns the SSLSession associated with this connection.

Returns:
SSL Session associated with this connection. Returns null, if this connection is not a secured connection.

fireCommandSent

protected void fireCommandSent(ControlConnectionEvent evt)
Used to notify registered listeners that a command was sent to the remote host. The command is wrapped in the ControlConnectionEvent object.

Parameters:
evt - ControlConnectionEvent.

fireReplyReceived

protected void fireReplyReceived(ControlConnectionEvent evt)
Used to notify the registered listeners that a reply was received from the remote host.

Parameters:
evt - ControlConnectionEvent.

stdout

protected void stdout(String message)
Prints a given string to the standard output.

Parameters:
message - The message to print.

stderr

protected void stderr(String message)
Prints the given string to standard error.

Parameters:
message - The message to print.


Copyright © 2000-2012 jMethods, Inc.. All Rights Reserved.