com.sshtools.j2ssh.client
Class SessionClient

java.lang.Object
  |
  +--com.sshtools.j2ssh.transport.Service
        |
        +--com.sshtools.j2ssh.connection.ConnectionProtocol
              |
              +--com.sshtools.j2ssh.client.SessionClient
All Implemented Interfaces:
java.lang.Runnable

public abstract class SessionClient
extends ConnectionProtocol

The SessionClient object provides authenticated remote session access through the SSH connection protocol.

Using the methods contained it it possible to start the execution of the users shell, execute a program or start an SSH subsystem. Users should refer to [SSH_CONN] for further technical information.

Version:
1.0
Author:
Lee David Painter ( lee@sshtools.com )

Constructor Summary
SessionClient()
          Constructor for the SessionClient object
 
Method Summary
 int authenticate(SshAuthentication auth)
          Authenticates the user using the authentication method provided.
 void changeTerminalDimensions(PseudoTerminal term)
          Changes the terminal dimensions to the newly provided settings
 void close()
          Closes the session
 void connectAsynchronously(SshConnectionProperties properties, HostKeyVerification hostVerification)
          Connects to the remote computer asynchronously.
 void connectSynchronously(SshConnectionProperties properties, HostKeyVerification hostVerification)
          Connects to the remote host synchronously
 boolean executeCommand(java.lang.String command)
          Requests that the remote server start the execution of the given command.
 java.lang.String getAuthenticationBanner()
          Returns the authenticaiton banner supplied by the remote server.
protected  java.util.List getAvailableAuthMethods()
          Provides the list of available authentication methods for the current user
protected  java.lang.Integer getExitCode()
          Gets the exit code of the comman exectued.
protected abstract  void initSession()
          Called by the framework when the service is started
protected  void onChannelClose(Channel channel)
          Called by the framework when the remote server has closed the specified channel.
protected  void onChannelData(Channel channel, byte[] data)
          Called by the framework when channel data arrives.
protected  void onChannelEOF(Channel channel)
          Called by the framework when the remote server has sent EOF for the given channel
protected  void onChannelExtData(Channel channel, byte[] data)
          Called by the framework when ext channel data arrives.
protected  boolean onChannelOpen(Channel channel, byte[] requestData)
          Called by the framework when the remote server wishes to open a channel
protected  void onChannelRequest(Channel channel, java.lang.String requestType, boolean wantReply, byte[] requestData)
          Called by the framework when a channel request arrives
protected abstract  void onConnectionComplete()
          Called after the connect has completed.
protected abstract  void onDisconnect(java.lang.String reason)
          Called when the session is disconnecting
protected  void onGlobalRequest(java.lang.String requestName, boolean wantReply, byte[] requestData)
          Called when a SSH_MSG_GLOBAL_REQUEST is received
protected  void onGlobalRequestFailure()
          Called when a SSH_MSG_REQUEST_FAILURE is recieved
protected  void onGlobalRequestSuccess(byte[] requestData)
          Called when the SSH_MSG_REQUEST_SUCCESS message is received
protected  void onServiceAccept()
          Called by the framework when the service has been accepted by the remote host
protected  void onServiceRequest()
          Called by the framwork when the remote computer requests a service.
protected abstract  void onSessionData(byte[] data)
          Called when session data arrives
protected abstract  void onSessionExtData(byte[] data)
          Called when extended session data arrives
 boolean requestPseudoTerminal(PseudoTerminal term)
          Requests that the remote server allocate a psuedo terminal for the session.
 void sendSessionData(byte[] data)
          Sends session data to the remote server
 boolean setEnvironmentVariable(java.lang.String name, java.lang.String value)
          Sets the environment variable for the command or shell to be started
 boolean startShell()
          Requests that the remote server start the users shell
 boolean startSubsystem(java.lang.String subsystem)
          Requests that the remote server executes the predefined subsystem (for example SFTP).
 
Methods inherited from class com.sshtools.j2ssh.connection.ConnectionProtocol
closeChannel, onMessageReceived, onServiceInit, openChannel, sendChannelData, sendChannelEOF, sendChannelExtData, sendChannelFailure, sendChannelOpenConfirmation, sendChannelOpenFailure, sendChannelRequest, sendChannelSuccess, sendChannelWindowAdjust, sendGlobalRequest, sendGlobalRequestFailure, sendGlobalRequestSuccess
 
Methods inherited from class com.sshtools.j2ssh.transport.Service
getExchangeHash, getMessageStore, getServiceName, getTransportLayer, onThreadException, run, start, stop, waitForSingleMessage
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SessionClient

public SessionClient()
Constructor for the SessionClient object

Method Detail

initSession

protected abstract void initSession()
                             throws TransportProtocolException,
                                    ServiceOperationException
Called by the framework when the service is started

Throws:
ServiceOperationException - Throw if any service exception occurs
TransportProtocolException - May be thrown by transport protocol methods used

getAvailableAuthMethods

protected java.util.List getAvailableAuthMethods()
Provides the list of available authentication methods for the current user

Returns:
A list containing the method names as specified in [SSH-USERAUTH]

getExitCode

protected java.lang.Integer getExitCode()
Gets the exit code of the comman exectued. This may be null if nothing has been returned from the remote side.

Returns:

startShell

public boolean startShell()
                   throws TransportProtocolException,
                          ServiceOperationException
Requests that the remote server start the users shell

Returns:
The success of the operation
Throws:
ServiceOperationException - Throw if any service exception occurs
TransportProtocolException - May be thrown by transport protocol methods used

setEnvironmentVariable

public boolean setEnvironmentVariable(java.lang.String name,
                                      java.lang.String value)
                               throws TransportProtocolException,
                                      ServiceOperationException
Sets the environment variable for the command or shell to be started

Parameters:
name - The environment variable name
value - The environment variable value
Returns:
The success of the request
Throws:
ServiceOperationException - Throw if any service exception occurs
TransportProtocolException - May be thrown by transport protocol methods used

changeTerminalDimensions

public void changeTerminalDimensions(PseudoTerminal term)
                              throws TransportProtocolException,
                                     ServiceOperationException
Changes the terminal dimensions to the newly provided settings

Parameters:
term - The pseudo terminal instance
Throws:
ServiceOperationException - Throw if any service exception occurs
TransportProtocolException - May be thrown by transport protocol methods used

requestPseudoTerminal

public boolean requestPseudoTerminal(PseudoTerminal term)
                              throws TransportProtocolException,
                                     ServiceOperationException
Requests that the remote server allocate a psuedo terminal for the session.

Parameters:
term - The terminal answerback mode
Returns:
The success of the operation
Throws:
ServiceOperationException - Throw if any service exception occurs
TransportProtocolException - May be thrown by transport protocol methods used

executeCommand

public boolean executeCommand(java.lang.String command)
                       throws TransportProtocolException,
                              ServiceOperationException
Requests that the remote server start the execution of the given command.

Parameters:
command - The command to execute
Returns:
The success of the operation
Throws:
ServiceOperationException - Throw if any service exception occurs
TransportProtocolException - May be thrown by transport protocol methods used

startSubsystem

public boolean startSubsystem(java.lang.String subsystem)
                       throws TransportProtocolException,
                              ServiceOperationException
Requests that the remote server executes the predefined subsystem (for example SFTP).

Parameters:
subsystem - The predefined subsystem to start
Returns:
The success of the operation
Throws:
ServiceOperationException - Throw if any service exception occurs
TransportProtocolException - May be thrown by transport protocol methods used

onChannelEOF

protected void onChannelEOF(Channel channel)
Called by the framework when the remote server has sent EOF for the given channel

Specified by:
onChannelEOF in class ConnectionProtocol
Parameters:
channel - The channel for which EOF was received

onChannelClose

protected void onChannelClose(Channel channel)
Called by the framework when the remote server has closed the specified channel. At this point the channel must be closed and the session is disconnected (calling onDisconnect)

Specified by:
onChannelClose in class ConnectionProtocol
Parameters:
channel - The channel to be closed

onDisconnect

protected abstract void onDisconnect(java.lang.String reason)
Called when the session is disconnecting

Specified by:
onDisconnect in class Service
Parameters:
reason - The disconnection reason

onChannelOpen

protected boolean onChannelOpen(Channel channel,
                                byte[] requestData)
Called by the framework when the remote server wishes to open a channel

Specified by:
onChannelOpen in class ConnectionProtocol
Parameters:
channel - The channel for the request
requestData - The extended channel open data
Returns:
Return true if the channel is to bo opened, otherwise false

connectSynchronously

public void connectSynchronously(SshConnectionProperties properties,
                                 HostKeyVerification hostVerification)
                          throws TransportProtocolException,
                                 ServiceOperationException
Connects to the remote host synchronously

Parameters:
properties - The connection properties
hostVerification - The host verification instance
Throws:
ServiceOperationException - Throw if any service exception occurs
TransportProtocolException - May be thrown by transport protocol methods used

connectAsynchronously

public void connectAsynchronously(SshConnectionProperties properties,
                                  HostKeyVerification hostVerification)
Connects to the remote computer asynchronously. Calls the abstract method onConnectionComplete when the connection has been completed.

Parameters:
properties - The properties to connect
hostVerification - The host verification instance

onConnectionComplete

protected abstract void onConnectionComplete()
Called after the connect has completed. At this point there is a list of suitable authenticaiton methods available for the user and possibly an authenticaiton banner message to display


authenticate

public int authenticate(SshAuthentication auth)
                 throws TransportProtocolException,
                        AuthenticationProtocolException,
                        ServiceOperationException
Authenticates the user using the authentication method provided.

Parameters:
auth - The authenticaiton method instance
Returns:
The authentication result
Throws:
AuthenticationProtocolException
ServiceOperationException
TransportProtocolException

close

public void close()
           throws TransportProtocolException,
                  ServiceOperationException
Closes the session

Throws:
ServiceOperationException
TransportProtocolException

getAuthenticationBanner

public java.lang.String getAuthenticationBanner()
Returns the authenticaiton banner supplied by the remote server.

Returns:

onGlobalRequest

protected void onGlobalRequest(java.lang.String requestName,
                               boolean wantReply,
                               byte[] requestData)
Called when a SSH_MSG_GLOBAL_REQUEST is received

Specified by:
onGlobalRequest in class ConnectionProtocol
Parameters:
requestName - The request name
wantReply - Does the remote computer wnat a reply?
requestData - The request data

onGlobalRequestFailure

protected void onGlobalRequestFailure()
Called when a SSH_MSG_REQUEST_FAILURE is recieved

Specified by:
onGlobalRequestFailure in class ConnectionProtocol

onGlobalRequestSuccess

protected void onGlobalRequestSuccess(byte[] requestData)
Called when the SSH_MSG_REQUEST_SUCCESS message is received

Specified by:
onGlobalRequestSuccess in class ConnectionProtocol
Parameters:
requestData - The request data

onChannelData

protected void onChannelData(Channel channel,
                             byte[] data)
Called by the framework when channel data arrives. This then proceeds to validate window space and then call the correct handler for the channel

Specified by:
onChannelData in class ConnectionProtocol
Parameters:
channel - The channel receiving the data
data - The data received

onSessionData

protected abstract void onSessionData(byte[] data)
Called when session data arrives

Parameters:
data - The unprocessed session data

onChannelExtData

protected void onChannelExtData(Channel channel,
                                byte[] data)
Called by the framework when ext channel data arrives.

Specified by:
onChannelExtData in class ConnectionProtocol
Parameters:
channel - The channel for which the data has arrived
data - The extended data

onSessionExtData

protected abstract void onSessionExtData(byte[] data)
Called when extended session data arrives

Parameters:
data - The extended session data

onChannelRequest

protected void onChannelRequest(Channel channel,
                                java.lang.String requestType,
                                boolean wantReply,
                                byte[] requestData)
Called by the framework when a channel request arrives

Specified by:
onChannelRequest in class ConnectionProtocol
Parameters:
channel - The channel for which the request was made
requestType - The request type
wantReply - Whether the remote side wants an explicit reply
requestData - The request data

onServiceAccept

protected void onServiceAccept()
                        throws ServiceOperationException
Called by the framework when the service has been accepted by the remote host

Specified by:
onServiceAccept in class Service
Throws:
ServiceOperationException - Thrown

onServiceRequest

protected void onServiceRequest()
Called by the framwork when the remote computer requests a service. This method ignores the requests on a client implementation

Specified by:
onServiceRequest in class Service

sendSessionData

public void sendSessionData(byte[] data)
                     throws WindowSpaceNotAvailableException,
                            TransportProtocolException
Sends session data to the remote server

Parameters:
data - The data to send
Throws:
WindowSpaceNotAvailableException
TransportProtocolException


Copyright © 2002 Sshtools.com. All Rights Reserved.