Package org.apache.sshd.server.channel
Class ChannelSession
- java.lang.Object
-
- All Implemented Interfaces:
Closeable
,AutoCloseable
,Channel
,AttributeRepository
,AttributeStore
,Channel
,ChannelIdentifier
,ChannelListenerManager
,ChannelStreamWriterResolver
,ChannelStreamWriterResolverManager
,Closeable
,PropertyResolver
,SessionContextHolder
,SessionHolder<Session>
,ExecutorServiceCarrier
,ServerChannel
,ServerSessionHolder
public class ChannelSession extends AbstractServerChannel
Server side channel session- Author:
- Apache MINA SSHD Project
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description class
ChannelSession.CommandCloseable
-
Nested classes/interfaces inherited from class org.apache.sshd.common.channel.AbstractChannel
AbstractChannel.GracefulChannelCloseable, AbstractChannel.GracefulState
-
Nested classes/interfaces inherited from class org.apache.sshd.common.util.closeable.AbstractCloseable
AbstractCloseable.State
-
Nested classes/interfaces inherited from interface org.apache.sshd.common.AttributeRepository
AttributeRepository.AttributeKey<T extends Object>
-
-
Field Summary
Fields Modifier and Type Field Description protected ChannelAsyncOutputStream
asyncErr
protected ChannelAsyncOutputStream
asyncOut
protected CloseFuture
commandExitFuture
protected Command
commandInstance
protected AtomicBoolean
commandStarted
static List<ChannelRequestHandler>
DEFAULT_HANDLERS
protected StandardEnvironment
env
protected OutputStream
err
protected Buffer
extendedDataBuffer
protected ChannelDataReceiver
extendedDataWriter
protected OutputStream
out
protected ChannelDataReceiver
receiver
protected Buffer
receiverBuffer
protected String
type
-
Fields inherited from class org.apache.sshd.server.channel.AbstractServerChannel
exitStatusSent
-
Fields inherited from class org.apache.sshd.common.channel.AbstractChannel
channelListenerProxy, channelListeners, closeSignaled, eofReceived, eofSent, gracefulFuture, gracefulState, initialized, RESPONSE_BUFFER_GROWTH_FACTOR, service, unregisterSignaled
-
Fields inherited from class org.apache.sshd.common.util.closeable.AbstractCloseable
closeFuture, futureLock, state
-
Fields inherited from class org.apache.sshd.common.util.logging.AbstractLoggingBean
log
-
Fields inherited from interface org.apache.sshd.common.channel.Channel
CHANNEL_EXEC, CHANNEL_SHELL, CHANNEL_SUBSYSTEM
-
Fields inherited from interface org.apache.sshd.common.channel.throttle.ChannelStreamWriterResolver
NONE
-
Fields inherited from interface org.apache.sshd.common.PropertyResolver
EMPTY
-
-
Constructor Summary
Constructors Constructor Description ChannelSession()
ChannelSession(Collection<? extends RequestHandler<Channel>> handlers)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
addEnvVariable(String name, String value)
protected void
closeImmediately0()
protected void
closeShell(int exitValue, boolean closeImmediately)
protected void
doWriteData(byte[] data, int off, long len)
protected void
doWriteExtendedData(byte[] data, int off, long len)
StandardEnvironment
getEnvironment()
protected Closeable
getInnerCloseable()
protected int
getPtyModeValue(PtyMode mode)
ServerSession
getSession()
protected RequestHandler.Result
handleAgentForwarding(String requestType, Buffer buffer, boolean wantReply)
protected RequestHandler.Result
handleAgentForwardingParsed(String requestType)
protected RequestHandler.Result
handleBreak(Buffer buffer, boolean wantReply)
protected RequestHandler.Result
handleBreakParsed(long breakLength)
protected RequestHandler.Result
handleEnv(Buffer buffer, boolean wantReply)
protected RequestHandler.Result
handleEnvParsed(String name, String value)
void
handleEof()
Invoked whenSSH_MSG_CHANNEL_EOF
receivedprotected RequestHandler.Result
handleExec(String request, Buffer buffer, boolean wantReply)
protected RequestHandler.Result
handleExecParsed(String request, String commandLine)
protected RequestHandler.Result
handleInternalRequest(String requestType, boolean wantReply, Buffer buffer)
Called byAbstractChannel.handleUnknownChannelRequest(String, boolean, Buffer)
in order to allow channel request handling if none of the registered handlers processed the request - last chance.protected RequestHandler.Result
handlePtyReq(Buffer buffer, boolean wantReply)
protected RequestHandler.Result
handlePtyReqParsed(String term, int tColumns, int tRows, int tWidth, int tHeight, Map<PtyMode,Integer> ptyModes)
protected RequestHandler.Result
handleShell(String request, Buffer buffer, boolean wantReply)
protected RequestHandler.Result
handleShellParsed(String request)
protected RequestHandler.Result
handleSignal(Buffer buffer, boolean wantReply)
protected RequestHandler.Result
handleSignalParsed(String name)
protected RequestHandler.Result
handleSubsystem(String request, Buffer buffer, boolean wantReply)
protected RequestHandler.Result
handleSubsystemParsed(String request, String subsystem)
void
handleWindowAdjust(Buffer buffer)
Invoked whenSSH_MSG_CHANNEL_WINDOW_ADJUST
receivedprotected RequestHandler.Result
handleWindowChange(Buffer buffer, boolean wantReply)
protected RequestHandler.Result
handleWindowChangeParsed(int tColumns, int tRows, int tWidth, int tHeight)
protected RequestHandler.Result
handleX11Forwarding(String requestType, Buffer buffer, boolean wantReply)
protected RequestHandler.Result
handleX11ForwardingParsed(String requestType, ServerSession session, boolean singleConnection, String authProtocol, String authCookie, int screenId)
protected boolean
isSendChunkIfRemoteWindowIsSmallerThanPacketSize(byte cmd)
Chance for specializations to vary chunking behaviour depending on the SFTP client version.protected RequestHandler.Result
prepareChannelCommand(String request, Command cmd)
protected Command
prepareCommand(String requestType, Command command)
Called byprepareChannelCommand(String, Command)
in order to set up the command's streams, session, file-system, exit callback, etc..protected IoWriteFuture
sendResponse(Buffer buffer, String req, RequestHandler.Result result, boolean wantReply)
void
setDataReceiver(ChannelDataReceiver receiver)
ForCommand
to installChannelDataReceiver
.void
setExtendedDataWriter(ChannelDataReceiver extendedDataWriter)
A specialChannelDataReceiver
that can be used to receive data sent as "extended" - usually STDERR.-
Methods inherited from class org.apache.sshd.server.channel.AbstractServerChannel
doInit, handleOpenFailure, handleOpenSuccess, open, sendExitStatus
-
Methods inherited from class org.apache.sshd.common.channel.AbstractChannel
addChannelListener, addPendingRequest, addRequestHandler, attributeKeys, clearAttributes, computeAttributeIfAbsent, configureWindow, getAttribute, getAttributesCount, getChannelId, getChannelListenerProxy, getChannelStreamWriterResolver, getExecutorService, getLocalWindow, getParentPropertyResolver, getProperties, getRecipient, getRemoteWindow, getRequestHandlers, handleChannelRegistrationResult, handleChannelRequest, handleChannelUnregistration, handleClose, handleData, handleExtendedData, handleFailure, handleRequest, handleSuccess, handleUnknownChannelRequest, init, invokeChannelSignaller, isEofSent, isEofSignalled, isInitialized, notifyStateChanged, notifyStateChanged, preClose, removeAttribute, removeChannelListener, removePendingRequest, removeRequestHandler, resolveChannelStreamWriterResolver, sendEof, sendWindowAdjust, setAttribute, setChannelStreamWriterResolver, setRecipient, signalChannelClosed, signalChannelClosed, signalChannelInitialized, signalChannelInitialized, signalChannelOpenFailure, signalChannelOpenFailure, signalChannelOpenSuccess, signalChannelOpenSuccess, toString, validateIncomingDataSize, writePacket
-
Methods inherited from class org.apache.sshd.common.util.closeable.AbstractInnerCloseable
doCloseGracefully, doCloseImmediately
-
Methods inherited from class org.apache.sshd.common.util.closeable.AbstractCloseable
addCloseFutureListener, builder, close, getFutureLock, isClosed, isClosing, removeCloseFutureListener
-
Methods inherited from class org.apache.sshd.common.util.logging.AbstractLoggingBean
debug, debug, debug, debug, debug, error, error, error, error, error, getSimplifiedLogger, info, info, warn, warn, warn, warn, warn, warn, warn, warn
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.apache.sshd.common.AttributeRepository
attributeKeys, getAttribute, getAttributesCount
-
Methods inherited from interface org.apache.sshd.common.AttributeStore
clearAttributes, computeAttributeIfAbsent, removeAttribute, setAttribute
-
Methods inherited from interface org.apache.sshd.common.channel.Channel
addRequestHandler, addRequestHandlers, getLocalWindow, getRecipient, getRemoteWindow, getRequestHandlers, handleChannelRegistrationResult, handleChannelUnregistration, handleClose, handleData, handleExtendedData, handleFailure, handleRequest, handleSuccess, init, isEofSignalled, isInitialized, removeRequestHandler, removeRequestHandlers, resolveAttribute, writePacket
-
Methods inherited from interface org.apache.sshd.common.channel.ChannelIdentifier
getChannelId
-
Methods inherited from interface org.apache.sshd.common.channel.ChannelListenerManager
addChannelListener, getChannelListenerProxy, removeChannelListener
-
Methods inherited from interface org.apache.sshd.common.channel.throttle.ChannelStreamWriterResolverManager
getChannelStreamWriterResolver, resolveChannelStreamWriter, resolveChannelStreamWriterResolver, setChannelStreamWriterResolver
-
Methods inherited from interface org.apache.sshd.common.Closeable
addCloseFutureListener, close, close, isClosed, isClosing, isOpen, removeCloseFutureListener
-
Methods inherited from interface org.apache.sshd.common.PropertyResolver
getBoolean, getBooleanProperty, getCharset, getInteger, getIntProperty, getLong, getLongProperty, getObject, getParentPropertyResolver, getProperties, getString, getStringProperty, isEmpty
-
Methods inherited from interface org.apache.sshd.server.channel.ServerChannel
getServerSession
-
Methods inherited from interface org.apache.sshd.common.session.SessionHolder
getSessionContext
-
-
-
-
Field Detail
-
DEFAULT_HANDLERS
public static final List<ChannelRequestHandler> DEFAULT_HANDLERS
-
type
protected String type
-
asyncOut
protected ChannelAsyncOutputStream asyncOut
-
asyncErr
protected ChannelAsyncOutputStream asyncErr
-
out
protected OutputStream out
-
err
protected OutputStream err
-
commandInstance
protected Command commandInstance
-
receiver
protected ChannelDataReceiver receiver
-
extendedDataWriter
protected ChannelDataReceiver extendedDataWriter
-
receiverBuffer
protected Buffer receiverBuffer
-
extendedDataBuffer
protected Buffer extendedDataBuffer
-
commandStarted
protected final AtomicBoolean commandStarted
-
env
protected final StandardEnvironment env
-
commandExitFuture
protected final CloseFuture commandExitFuture
-
-
Constructor Detail
-
ChannelSession
public ChannelSession()
-
ChannelSession
public ChannelSession(Collection<? extends RequestHandler<Channel>> handlers)
-
-
Method Detail
-
getSession
public ServerSession getSession()
- Specified by:
getSession
in interfaceSessionHolder<Session>
- Overrides:
getSession
in classAbstractChannel
-
handleWindowAdjust
public void handleWindowAdjust(Buffer buffer) throws IOException
Description copied from interface:Channel
Invoked whenSSH_MSG_CHANNEL_WINDOW_ADJUST
received- Specified by:
handleWindowAdjust
in interfaceChannel
- Overrides:
handleWindowAdjust
in classAbstractChannel
- Parameters:
buffer
- The rest of the message dataBuffer
after decoding the channel identifiers- Throws:
IOException
- If failed to handle the message
-
getInnerCloseable
protected Closeable getInnerCloseable()
- Overrides:
getInnerCloseable
in classAbstractChannel
-
closeImmediately0
protected void closeImmediately0()
-
handleEof
public void handleEof() throws IOException
Description copied from interface:Channel
Invoked whenSSH_MSG_CHANNEL_EOF
received- Specified by:
handleEof
in interfaceChannel
- Overrides:
handleEof
in classAbstractChannel
- Throws:
IOException
- If failed to handle the message
-
doWriteData
protected void doWriteData(byte[] data, int off, long len) throws IOException
- Specified by:
doWriteData
in classAbstractChannel
- Throws:
IOException
-
doWriteExtendedData
protected void doWriteExtendedData(byte[] data, int off, long len) throws IOException
- Specified by:
doWriteExtendedData
in classAbstractChannel
- Throws:
IOException
-
handleInternalRequest
protected RequestHandler.Result handleInternalRequest(String requestType, boolean wantReply, Buffer buffer) throws IOException
Description copied from class:AbstractChannel
Called byAbstractChannel.handleUnknownChannelRequest(String, boolean, Buffer)
in order to allow channel request handling if none of the registered handlers processed the request - last chance.- Overrides:
handleInternalRequest
in classAbstractChannel
- Parameters:
requestType
- The request typewantReply
- Whether reply is requestedbuffer
- TheBuffer
containing extra request-specific data- Returns:
- The handling result - if
null
orUnsupported
and reply is required then a failure message will be sent - Throws:
IOException
- If failed to process the request internally
-
sendResponse
protected IoWriteFuture sendResponse(Buffer buffer, String req, RequestHandler.Result result, boolean wantReply) throws IOException
- Overrides:
sendResponse
in classAbstractChannel
- Throws:
IOException
-
handleEnv
protected RequestHandler.Result handleEnv(Buffer buffer, boolean wantReply) throws IOException
- Throws:
IOException
-
handleEnvParsed
protected RequestHandler.Result handleEnvParsed(String name, String value) throws IOException
- Throws:
IOException
-
handlePtyReq
protected RequestHandler.Result handlePtyReq(Buffer buffer, boolean wantReply) throws IOException
- Throws:
IOException
-
handlePtyReqParsed
protected RequestHandler.Result handlePtyReqParsed(String term, int tColumns, int tRows, int tWidth, int tHeight, Map<PtyMode,Integer> ptyModes) throws IOException
- Throws:
IOException
-
handleWindowChange
protected RequestHandler.Result handleWindowChange(Buffer buffer, boolean wantReply) throws IOException
- Throws:
IOException
-
handleWindowChangeParsed
protected RequestHandler.Result handleWindowChangeParsed(int tColumns, int tRows, int tWidth, int tHeight) throws IOException
- Throws:
IOException
-
handleSignal
protected RequestHandler.Result handleSignal(Buffer buffer, boolean wantReply) throws IOException
- Throws:
IOException
-
handleSignalParsed
protected RequestHandler.Result handleSignalParsed(String name) throws IOException
- Throws:
IOException
-
handleBreak
protected RequestHandler.Result handleBreak(Buffer buffer, boolean wantReply) throws IOException
- Throws:
IOException
-
handleBreakParsed
protected RequestHandler.Result handleBreakParsed(long breakLength) throws IOException
- Throws:
IOException
-
handleShell
protected RequestHandler.Result handleShell(String request, Buffer buffer, boolean wantReply) throws IOException
- Throws:
IOException
-
handleShellParsed
protected RequestHandler.Result handleShellParsed(String request) throws IOException
- Throws:
IOException
-
handleExec
protected RequestHandler.Result handleExec(String request, Buffer buffer, boolean wantReply) throws IOException
- Throws:
IOException
-
handleExecParsed
protected RequestHandler.Result handleExecParsed(String request, String commandLine) throws IOException
- Throws:
IOException
-
handleSubsystem
protected RequestHandler.Result handleSubsystem(String request, Buffer buffer, boolean wantReply) throws IOException
- Throws:
IOException
-
handleSubsystemParsed
protected RequestHandler.Result handleSubsystemParsed(String request, String subsystem) throws IOException
- Throws:
IOException
-
prepareChannelCommand
protected RequestHandler.Result prepareChannelCommand(String request, Command cmd) throws IOException
- Throws:
IOException
-
setDataReceiver
public void setDataReceiver(ChannelDataReceiver receiver)
ForCommand
to installChannelDataReceiver
. When you do this,CommandDirectInputStreamAware.setInputStream(java.io.InputStream)
orAsyncCommandInputStreamAware.setIoInputStream(org.apache.sshd.common.io.IoInputStream)
will no longer be invoked. If you call this method fromCommand#start(ChannelSession, Environment)
, the input stream you received inCommandDirectInputStreamAware.setInputStream(java.io.InputStream)
will not read any data.- Parameters:
receiver
- TheChannelDataReceiver
instance
-
setExtendedDataWriter
public void setExtendedDataWriter(ChannelDataReceiver extendedDataWriter)
A specialChannelDataReceiver
that can be used to receive data sent as "extended" - usually STDERR. Note: by default any such data sent to the channel session causes an exception, but specific implementations may choose to register such a receiver (e.g., for custom usage of the STDERR stream). A good place in the code to register such a writer would be in commands that also implementChannelSessionAware
.- Parameters:
extendedDataWriter
- TheChannelDataReceiver
.
-
prepareCommand
protected Command prepareCommand(String requestType, Command command) throws IOException
Called byprepareChannelCommand(String, Command)
in order to set up the command's streams, session, file-system, exit callback, etc..- Parameters:
requestType
- The request that caused the command to be createdcommand
- The createdCommand
- may benull
- Returns:
- The updated command instance - if
null
then the request that initially caused the creation of the command is failed and the original command (if any) destroyed (eventually). Note: if a different command instance than the input one is returned, then it is up to the implementor to take care of the wrapping or destruction of the original command instance. - Throws:
IOException
- If failed to prepare the command
-
getPtyModeValue
protected int getPtyModeValue(PtyMode mode)
-
handleAgentForwarding
protected RequestHandler.Result handleAgentForwarding(String requestType, Buffer buffer, boolean wantReply) throws IOException
- Throws:
IOException
-
handleAgentForwardingParsed
protected RequestHandler.Result handleAgentForwardingParsed(String requestType) throws IOException
- Throws:
IOException
-
handleX11Forwarding
protected RequestHandler.Result handleX11Forwarding(String requestType, Buffer buffer, boolean wantReply) throws IOException
- Throws:
IOException
-
handleX11ForwardingParsed
protected RequestHandler.Result handleX11ForwardingParsed(String requestType, ServerSession session, boolean singleConnection, String authProtocol, String authCookie, int screenId) throws IOException
- Throws:
IOException
-
getEnvironment
public StandardEnvironment getEnvironment()
-
closeShell
protected void closeShell(int exitValue, boolean closeImmediately) throws IOException
- Throws:
IOException
-
isSendChunkIfRemoteWindowIsSmallerThanPacketSize
protected boolean isSendChunkIfRemoteWindowIsSmallerThanPacketSize(byte cmd)
Chance for specializations to vary chunking behaviour depending on the SFTP client version.- Parameters:
cmd
- EitherSSH_MSG_CHANNEL_DATA
orSSH_MSG_CHANNEL_EXTENDED_DATA
indicating the output stream type- Returns:
true
if should chunk data sent viaChannelAsyncOutputStream
when reported remote window size is less than its packet size- Throws:
UnsupportedOperationException
- if the command is neither of the supported ones- See Also:
ChannelAsyncOutputStream(Channel, byte, boolean)
-
-