com.foxsmart.ic.net
Class DefaultHttpProtocolHandler

java.lang.Object
  extended by com.foxsmart.ic.net.DefaultHttpProtocolHandler
All Implemented Interfaces:
HttpProtocolHandler, ProtocolHandler

public class DefaultHttpProtocolHandler
extends java.lang.Object
implements HttpProtocolHandler

The DefaultHttpProtocolHandler class handles the HTTP communication with the router.


Nested Class Summary
 
Nested classes/interfaces inherited from interface com.foxsmart.ic.net.ProtocolHandler
ProtocolHandler.Protocol
 
Field Summary
static java.lang.String COMMAND_PART_SEPARATOR
          Command part separator.
static int DEFAULT_HTTP_PORT
          The default telnet socket port.
protected  java.lang.Integer defaultProtocolTimeout
           
static java.lang.String ESCAPE_PREFIX
          Escape prefix.
protected  org.apache.commons.logging.Log log
           
static java.lang.String PASSWORD_MASK
           
static java.lang.String PREFIX_CONFIGURE
          The "configure" prefix.
static java.lang.String PREFIX_EXEC
          The "exec" prefix.
 
Fields inherited from interface com.foxsmart.ic.net.ProtocolHandler
CR, DEFAULT_PROTOCOL_TIMEOUT_SECONDS
 
Constructor Summary
DefaultHttpProtocolHandler()
          Default Constructor.
 
Method Summary
 void connect(java.lang.String hostname, java.lang.Integer port, java.lang.String username, java.lang.String password, java.lang.Integer timeout)
          Connects to the router using HTTP.
 void disconnect()
          This method sets the usernameUsed and passwordUsed flags to false.
 boolean getAvailable()
          Default implementation returns true.
protected  java.lang.String getBaseUrl()
          Gets the base URL for connecting to the router using HTTP.
protected  java.lang.String getBaseUrl(java.lang.String hostname, java.lang.Integer port)
          Gets the base URL for connecting to the router using HTTP with the specified hostname and port number.
 java.lang.Integer getDefaultProtocolTimeout()
          Gets the default operation timeout value.
 boolean getPasswordUsed()
          Gets whether the password was used for authentication.
 ProtocolHandler.Protocol getProtocol()
          Returns the HTTP protocol.
 boolean getUsernameUsed()
          Gets whether the username was used for authentication.
 boolean isConnected()
          Returns true if the client is currently connected to a server.
protected  java.lang.String prepareCommandPart(java.lang.String commandPart)
           
 java.lang.String sendCommandWithResults(java.lang.String command)
          Sends a command and returns the results.
 java.lang.String sendConfigCommandWithResults(java.lang.String command, boolean parent)
          Sends a configuration command and returns the results.
 void setDefaultProtocolTimeout(java.lang.Integer timeout)
          Modifies the default operation protocol timeout value.
 void setPasswordUsed(boolean passwordUsed)
          Sets whether the password was used for authentication.
 void setUsernameUsed(boolean usernameUsed)
          Sets whether the username was used for authentication.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

PREFIX_EXEC

public static final java.lang.String PREFIX_EXEC
The "exec" prefix.

See Also:
Constant Field Values

PREFIX_CONFIGURE

public static final java.lang.String PREFIX_CONFIGURE
The "configure" prefix.

See Also:
Constant Field Values

COMMAND_PART_SEPARATOR

public static final java.lang.String COMMAND_PART_SEPARATOR
Command part separator.

See Also:
Constant Field Values

ESCAPE_PREFIX

public static final java.lang.String ESCAPE_PREFIX
Escape prefix.

See Also:
Constant Field Values

log

protected org.apache.commons.logging.Log log

DEFAULT_HTTP_PORT

public static final int DEFAULT_HTTP_PORT
The default telnet socket port.

See Also:
Constant Field Values

defaultProtocolTimeout

protected java.lang.Integer defaultProtocolTimeout

PASSWORD_MASK

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

DefaultHttpProtocolHandler

public DefaultHttpProtocolHandler()
Default Constructor.

Method Detail

getProtocol

public ProtocolHandler.Protocol getProtocol()
Returns the HTTP protocol.

Specified by:
getProtocol in interface ProtocolHandler
Returns:
the HTTP protocol.

connect

public void connect(java.lang.String hostname,
                    java.lang.Integer port,
                    java.lang.String username,
                    java.lang.String password,
                    java.lang.Integer timeout)
             throws java.lang.Exception
Connects to the router using HTTP. The enable password should be specified for the password. The specified username will be supplied for authentication credentials although it is not needed by the router (i.e. only the enable password).

Specified by:
connect in interface HttpProtocolHandler
Parameters:
hostname - The name of the remote host.
port - The port to connect to on the remote host. If null is passed in, a default port will be used.
username - The username to connect with. Null should be passed in if no username is required.
password - The password to connect with. Null should be passed in if no password is required.
timeout - The timeout in seconds. If null is passed in, a default timeout will be used.
Throws:
AuthenticationFailedException - when a successful connection could not be made
java.io.IOException - if the URL stream could not be opened to the URL
java.net.UnknownServiceException - if the URL protocol does not support input
ThreadTimeoutException - if a timeout occurred while connecting to the router.
java.net.UnknownHostException - If the hostname cannot be resolved.
java.lang.Exception - if any other problems were encountered.
See Also:
Note that the timeout is not used.

sendCommandWithResults

public java.lang.String sendCommandWithResults(java.lang.String command)
                                        throws java.lang.Exception
Sends a command and returns the results. This method delegates to the implementation class and passes the "exec" prefix.

Specified by:
sendCommandWithResults in interface HttpProtocolHandler
Parameters:
command - The command to send to the device.
Returns:
the results of the command.
Throws:
java.lang.Exception - if any problems were encountered.

sendConfigCommandWithResults

public java.lang.String sendConfigCommandWithResults(java.lang.String command,
                                                     boolean parent)
                                              throws java.lang.Exception
Sends a configuration command and returns the results. This method delegates to the implementation class and passes the "configure" prefix for parents and null for non-parents.

Specified by:
sendConfigCommandWithResults in interface HttpProtocolHandler
Parameters:
command - The command to send to the device.
parent - True if the configuration command is a parent element or false if not.
Returns:
the results of the command.
Throws:
java.lang.Exception - if any problems were encountered.

getBaseUrl

protected java.lang.String getBaseUrl()
Gets the base URL for connecting to the router using HTTP. This method delegates to getBaseUrl(hostname, port) with the currently connected hostname and port number.

Returns:
the base URL.

getBaseUrl

protected java.lang.String getBaseUrl(java.lang.String hostname,
                                      java.lang.Integer port)
Gets the base URL for connecting to the router using HTTP with the specified hostname and port number. The URL uses the form: "http://:/".

Parameters:
hostname - the hostname
port - the port number.
Returns:
the base URL.

isConnected

public boolean isConnected()
Returns true if the client is currently connected to a server. Although HTTP is a stateless protocol, the connected state is set to true when a successful connection is made to the router. The flag is reset back to false when the disconnect method is called.

Specified by:
isConnected in interface HttpProtocolHandler
Returns:
True if the client is currently connected to a server, false otherwise.

setDefaultProtocolTimeout

public void setDefaultProtocolTimeout(java.lang.Integer timeout)
Modifies the default operation protocol timeout value.

Specified by:
setDefaultProtocolTimeout in interface HttpProtocolHandler
Specified by:
setDefaultProtocolTimeout in interface ProtocolHandler
Parameters:
timeout - The default timeout value in seconds to wait for the data to be returned. If null is passed, no timeout will be used. Beware that if an expected match string is not found or if the device doesn't respond, the expect method will not return.

getDefaultProtocolTimeout

public java.lang.Integer getDefaultProtocolTimeout()
Gets the default operation timeout value.

Specified by:
getDefaultProtocolTimeout in interface HttpProtocolHandler
Specified by:
getDefaultProtocolTimeout in interface ProtocolHandler
Returns:
the timeout in seconds.

setUsernameUsed

public void setUsernameUsed(boolean usernameUsed)
Sets whether the username was used for authentication.

Specified by:
setUsernameUsed in interface HttpProtocolHandler
Parameters:
usernameUsed - True if the username was used or false if not.

getUsernameUsed

public boolean getUsernameUsed()
Gets whether the username was used for authentication.

Specified by:
getUsernameUsed in interface HttpProtocolHandler
Returns:
True if the username was used or false if not.

setPasswordUsed

public void setPasswordUsed(boolean passwordUsed)
Sets whether the password was used for authentication.

Specified by:
setPasswordUsed in interface HttpProtocolHandler
Parameters:
passwordUsed - True if the password was used or false if not.

getPasswordUsed

public boolean getPasswordUsed()
Gets whether the password was used for authentication.

Specified by:
getPasswordUsed in interface HttpProtocolHandler
Returns:
True if the password was used or false if not.

disconnect

public void disconnect()
                throws java.io.IOException
This method sets the usernameUsed and passwordUsed flags to false. Specific handlers should override this method to perform the actual disconnection implementation and call this super method as well.

Specified by:
disconnect in interface HttpProtocolHandler
Throws:
java.io.IOException - If there is an error closing the socket.

getAvailable

public boolean getAvailable()
Default implementation returns true. Subclasses can provide a concrete implementation as needed.

Specified by:
getAvailable in interface ProtocolHandler
Returns:
true.

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

prepareCommandPart

protected java.lang.String prepareCommandPart(java.lang.String commandPart)


Copyright © 2005-2008 Fox Smart, Inc. All Rights Reserved.