Package org.apache.sshd.sftp.client.impl
Class AbstractSftpClient
java.lang.Object
org.apache.sshd.common.util.logging.AbstractLoggingBean
org.apache.sshd.client.subsystem.AbstractSubsystemClient
org.apache.sshd.sftp.client.impl.AbstractSftpClient
- All Implemented Interfaces:
Closeable
,AutoCloseable
,Channel
,ClientChannelHolder
,ClientSessionHolder
,SubsystemClient
,ChannelHolder
,NamedResource
,SessionContextHolder
,SessionHolder<ClientSession>
,FullAccessSftpClient
,RawSftpClient
,SftpClient
,SftpErrorDataHandler
- Direct Known Subclasses:
DefaultSftpClient
public abstract class AbstractSftpClient
extends AbstractSubsystemClient
implements FullAccessSftpClient, SftpErrorDataHandler
- Author:
- Apache MINA SSHD Project
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.apache.sshd.sftp.client.SftpClient
SftpClient.Attribute, SftpClient.Attributes, SftpClient.CloseableHandle, SftpClient.CopyMode, SftpClient.DirEntry, SftpClient.Handle, SftpClient.OpenMode
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final SftpErrorDataHandler
static final int
Property that can be used on theFactoryManager
to control the internal timeout used by the client to complete the buffer sending inRawSftpClient.send(int, Buffer)
.Fields inherited from class org.apache.sshd.common.util.logging.AbstractLoggingBean
log
Fields inherited from interface org.apache.sshd.common.NamedResource
BY_NAME_COMPARATOR, NAME_EXTRACTOR
Fields inherited from interface org.apache.sshd.sftp.client.SftpClient
DEFAULT_CHANNEL_MODES, EMPTY_DIR_ENTRIES, IO_BUFFER_SIZE, MIN_BUFFER_SIZE, MIN_READ_BUFFER_SIZE, MIN_WRITE_BUFFER_SIZE
Fields inherited from interface org.apache.sshd.sftp.client.SftpErrorDataHandler
EMPTY
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncanonicalPath
(String path) The effective "normalized" remote pathprotected SftpClient.Attributes
checkAttributes
(int cmd, org.apache.sshd.common.util.buffer.Buffer request) protected SftpClient.Attributes
checkAttributesResponse
(SftpResponse response) protected void
checkCommandStatus
(int cmd, org.apache.sshd.common.util.buffer.Buffer request) Sends the specified command, waits for the response and then invokescheckResponseStatus(SftpResponse)
protected int
checkData
(int cmd, org.apache.sshd.common.util.buffer.Buffer request, int dstOffset, byte[] dst, AtomicReference<Boolean> eofSignalled) protected int
checkDataResponse
(SftpResponse response, int dstoff, byte[] dst, AtomicReference<Boolean> eofSignalled) protected List<SftpClient.DirEntry>
checkDirResponse
(SftpResponse response, AtomicReference<Boolean> eolIndicator) protected byte[]
checkHandle
(int cmd, org.apache.sshd.common.util.buffer.Buffer request) protected byte[]
checkHandleResponse
(SftpResponse response) protected String
checkOneName
(int cmd, org.apache.sshd.common.util.buffer.Buffer request) protected String
checkOneNameResponse
(SftpResponse response) protected void
checkResponseStatus
(int cmd, int id, SftpStatus status) protected void
checkResponseStatus
(SftpResponse response) Checks if the incoming response is anSSH_FXP_STATUS
one, and if so whether the substatus isSSH_FX_OK
.void
close
(SftpClient.Handle handle) Close the handle obtained from one of theopen
methodsvoid
errorData
(byte[] buf, int start, int len) Receive binary data from server error stream<E extends SftpClientExtension>
EgetExtension
(Class<? extends E> extensionType) getExtension
(SftpClientExtensionFactory factory) getParsedServerExtensions
(Map<String, byte[]> extensions) protected int
protected String
getReferencedName
(int cmd, org.apache.sshd.common.util.buffer.Buffer buf, int nameIndex) protected int
protected SftpClient.Attributes
handleUnexpectedAttributesPacket
(SftpResponse response) protected byte[]
handleUnexpectedHandlePacket
(SftpResponse response) protected IOException
handleUnexpectedPacket
(int expected, SftpResponse response) protected int
handleUnknownDataPacket
(SftpResponse response) protected List<SftpClient.DirEntry>
handleUnknownDirListingPacket
(SftpResponse response) protected String
handleUnknownOneNamePacket
(SftpResponse response) void
Create a linklistDir
(SftpClient.Handle handle) void
lock
(SftpClient.Handle handle, long offset, long length, int mask) Retrieve remote path meta-data - do not follow symbolic linksvoid
Create remote directoryopen
(String path, Collection<SftpClient.OpenMode> options) Opens a remote file with the specified mode(s)Obtain a handle for a directoryopenRemoteFileChannel
(String path, Collection<SftpClient.OpenMode> modes) Opens anFileChannel
on the specified remote pathprotected <B extends org.apache.sshd.common.util.buffer.Buffer>
BputReferencedName
(int cmd, B buf, String name, int nameIndex) read
(String path, int bufferSize, Collection<SftpClient.OpenMode> mode) Read a remote file's data via an input streamint
read
(SftpClient.Handle handle, long fileOffset, byte[] dst, int dstOffset, int len, AtomicReference<Boolean> eofSignalled) Reads data from the open (file) handleprotected SftpClient.Attributes
readAttributes
(int cmd, org.apache.sshd.common.util.buffer.Buffer buffer, AtomicInteger nameIndex) readDir
(SftpClient.Handle handle, AtomicReference<Boolean> eolIndicator) Retrieve target of a linkvoid
void
rename
(String oldPath, String newPath, Collection<SftpClient.CopyMode> options) protected SftpResponse
response
(int cmd, int requestId) Receives a response buffer, validates and returns it as aSftpResponse
.void
Remove remote directoryprotected SftpResponse
rpc
(int cmd, org.apache.sshd.common.util.buffer.Buffer request) Perform an SFTP request and wait until the response has been received.void
setStat
(String path, SftpClient.Attributes attributes) Update remote node meta-datavoid
setStat
(SftpClient.Handle handle, SftpClient.Attributes attributes) Update remote node meta-dataRetrieve remote path meta-data - follow symbolic links if encounteredstat
(SftpClient.Handle handle) Retrieve file/directory handle meta-dataprotected void
throwStatusException
(int cmd, int id, SftpStatus status) void
unlock
(SftpClient.Handle handle, long offset, long length) write
(String path, int bufferSize, Collection<SftpClient.OpenMode> mode) Write to a remote file via an output streamvoid
write
(SftpClient.Handle handle, long fileOffset, byte[] src, int srcOffset, int len) Write data to (open) file handleprotected <B extends org.apache.sshd.common.util.buffer.Buffer>
BwriteAttributes
(int cmd, B buffer, SftpClient.Attributes attributes) Methods inherited from class org.apache.sshd.client.subsystem.AbstractSubsystemClient
toString
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.client.channel.ClientChannelHolder
getClientChannel
Methods inherited from interface org.apache.sshd.client.session.ClientSessionHolder
getClientSession
Methods inherited from interface org.apache.sshd.sftp.client.RawSftpClient
receive, receive, receive, send
Methods inherited from interface org.apache.sshd.common.session.SessionHolder
getSessionContext
Methods inherited from interface org.apache.sshd.sftp.client.SftpClient
getExtension, getName, getNameDecodingCharset, getServerExtensions, getVersion, isClosing, open, open, openRemoteFileChannel, openRemotePathChannel, openRemotePathChannel, read, read, read, read, read, read, read, read, readDir, readEntries, rename, rename, setNameDecodingCharset, singleSessionInstance, symLink, write, write, write, write, write, write
Methods inherited from interface org.apache.sshd.client.subsystem.SubsystemClient
getSession
-
Field Details
-
INIT_COMMAND_SIZE
public static final int INIT_COMMAND_SIZE- See Also:
-
SFTP_CLIENT_CMD_TIMEOUT
Property that can be used on theFactoryManager
to control the internal timeout used by the client to complete the buffer sending inRawSftpClient.send(int, Buffer)
. -
errorDataHandler
-
-
Constructor Details
-
AbstractSftpClient
-
-
Method Details
-
getChannel
- Specified by:
getChannel
in interfaceChannelHolder
- Specified by:
getChannel
in interfaceClientChannelHolder
-
getExtension
- Specified by:
getExtension
in interfaceSftpClient
- Type Parameters:
E
- The generic extension type- Parameters:
extensionType
- The extension type- Returns:
- The extension instance - Note: it is up to the caller to invoke
OptionalFeature.isSupported()
-null
if this extension type is not implemented by the client - See Also:
-
getExtension
- Specified by:
getExtension
in interfaceSftpClient
- Parameters:
factory
- TheSftpClientExtensionFactory
instance to use - ignored ifnull
- Returns:
- The extension instance - Note: it is up to the caller to invoke
OptionalFeature.isSupported()
-null
if this extension type is not implemented by the client
-
getParsedServerExtensions
-
getParsedServerExtensions
-
getReferencedName
protected String getReferencedName(int cmd, org.apache.sshd.common.util.buffer.Buffer buf, int nameIndex) - Parameters:
cmd
- The command that was sent whose response contains the name to be decodedbuf
- TheBuffer
containing the encoded namenameIndex
- The zero-based order of the requested names for the command - e.g.,- When listing a directory's contents each successive name will have an increasing index.
- For SFTP version 3, when retrieving a single name, short name will have index=0 and the long one index=1.
- Returns:
- The decoded referenced name
-
putReferencedName
protected <B extends org.apache.sshd.common.util.buffer.Buffer> B putReferencedName(int cmd, B buf, String name, int nameIndex) - Type Parameters:
B
- Type ofBuffer
being updated- Parameters:
cmd
- The command for which this name is being addedbuf
- The buffer instance to updatename
- The name to place in the buffernameIndex
- The zero-based order of the name for the specific command if more than one name required - e.g., rename, link/symbolic link- Returns:
- The updated buffer
-
rpc
protected SftpResponse rpc(int cmd, org.apache.sshd.common.util.buffer.Buffer request) throws IOException Perform an SFTP request and wait until the response has been received.- Parameters:
cmd
- the SFTP command coderequest
- aBuffer
containing the request data- Returns:
- the
SftpResponse
for the request - Throws:
IOException
- if an error occurs
-
response
Receives a response buffer, validates and returns it as aSftpResponse
.- Parameters:
cmd
- the command of the request the response is forrequestId
- the id of the request- Returns:
- the
SftpResponse
- Throws:
IOException
- if the received buffer is malformed
-
checkCommandStatus
protected void checkCommandStatus(int cmd, org.apache.sshd.common.util.buffer.Buffer request) throws IOException Sends the specified command, waits for the response and then invokescheckResponseStatus(SftpResponse)
- Parameters:
cmd
- The command to sendrequest
- The requestBuffer
- Throws:
IOException
- If failed to send, receive or check the returned status- See Also:
-
checkResponseStatus
Checks if the incoming response is anSSH_FXP_STATUS
one, and if so whether the substatus isSSH_FX_OK
.- Parameters:
response
- The receivedSftpResponse
- Throws:
IOException
- If response does not carry a status or carries a bad status code- See Also:
-
checkResponseStatus
- Parameters:
cmd
- The sent command opcodeid
- The request idstatus
- TheSftpStatus
- Throws:
IOException
- if!status.isOk()
- See Also:
-
throwStatusException
- Throws:
IOException
-
checkHandle
protected byte[] checkHandle(int cmd, org.apache.sshd.common.util.buffer.Buffer request) throws IOException - Parameters:
cmd
- Command to be sentrequest
- TheBuffer
containing the request- Returns:
- The received handle identifier
- Throws:
IOException
- If failed to send/receive or process the response- See Also:
-
checkHandleResponse
- Throws:
IOException
-
handleUnexpectedHandlePacket
- Throws:
IOException
-
checkAttributes
protected SftpClient.Attributes checkAttributes(int cmd, org.apache.sshd.common.util.buffer.Buffer request) throws IOException - Parameters:
cmd
- Command to be sentrequest
- RequestBuffer
- Returns:
- The decoded response
Attributes
- Throws:
IOException
- If failed to send/receive or process the response- See Also:
-
checkAttributesResponse
- Throws:
IOException
-
handleUnexpectedAttributesPacket
protected SftpClient.Attributes handleUnexpectedAttributesPacket(SftpResponse response) throws IOException - Throws:
IOException
-
checkOneName
protected String checkOneName(int cmd, org.apache.sshd.common.util.buffer.Buffer request) throws IOException - Parameters:
cmd
- Command to be sentrequest
- The requestBuffer
- Returns:
- The retrieved name
- Throws:
IOException
- If failed to send/receive or process the response- See Also:
-
checkOneNameResponse
- Throws:
IOException
-
handleUnknownOneNamePacket
- Throws:
IOException
-
readAttributes
protected SftpClient.Attributes readAttributes(int cmd, org.apache.sshd.common.util.buffer.Buffer buffer, AtomicInteger nameIndex) throws IOException - Throws:
IOException
-
writeAttributes
protected <B extends org.apache.sshd.common.util.buffer.Buffer> B writeAttributes(int cmd, B buffer, SftpClient.Attributes attributes) -
open
public SftpClient.CloseableHandle open(String path, Collection<SftpClient.OpenMode> options) throws IOException Description copied from interface:SftpClient
Opens a remote file with the specified mode(s)- Specified by:
open
in interfaceSftpClient
- Parameters:
path
- The remote pathoptions
- The desired mode - if none specified thenSftpClient.OpenMode.Read
is assumed- Returns:
- The file's
SftpClient.CloseableHandle
- Throws:
IOException
- If failed to open the remote file
-
close
Description copied from interface:SftpClient
Close the handle obtained from one of theopen
methods- Specified by:
close
in interfaceSftpClient
- Parameters:
handle
- TheHandle
to close- Throws:
IOException
- If failed to execute
-
remove
- Specified by:
remove
in interfaceSftpClient
- Parameters:
path
- The remote path to remove- Throws:
IOException
- If failed to execute
-
rename
public void rename(String oldPath, String newPath, Collection<SftpClient.CopyMode> options) throws IOException - Specified by:
rename
in interfaceSftpClient
- Throws:
IOException
-
read
public int read(SftpClient.Handle handle, long fileOffset, byte[] dst, int dstOffset, int len, AtomicReference<Boolean> eofSignalled) throws IOException Description copied from interface:SftpClient
Reads data from the open (file) handle- Specified by:
read
in interfaceSftpClient
- Parameters:
handle
- The fileSftpClient.Handle
to read fromfileOffset
- The file offset to read fromdst
- The destination bufferdstOffset
- Offset in destination buffer to place the read datalen
- Available destination buffer size to readeofSignalled
- If notnull
then upon return holds a value indicating whether EOF was reached due to the read. Ifnull
indicator value then this indication is not available- Returns:
- Number of read bytes -
-1
if EOF reached - Throws:
IOException
- If failed to read the data- See Also:
-
checkData
protected int checkData(int cmd, org.apache.sshd.common.util.buffer.Buffer request, int dstOffset, byte[] dst, AtomicReference<Boolean> eofSignalled) throws IOException - Throws:
IOException
-
checkDataResponse
protected int checkDataResponse(SftpResponse response, int dstoff, byte[] dst, AtomicReference<Boolean> eofSignalled) throws IOException - Throws:
IOException
-
handleUnknownDataPacket
- Throws:
IOException
-
errorData
Description copied from interface:SftpErrorDataHandler
Receive binary data from server error stream- Specified by:
errorData
in interfaceSftpErrorDataHandler
- Parameters:
buf
- The buffer of the incoming datastart
- Offset in buffer to read the datalen
- Available data in buffer- Throws:
IOException
- If failed to receive incoming data
-
write
public void write(SftpClient.Handle handle, long fileOffset, byte[] src, int srcOffset, int len) throws IOException Description copied from interface:SftpClient
Write data to (open) file handle- Specified by:
write
in interfaceSftpClient
- Parameters:
handle
- The fileSftpClient.Handle
fileOffset
- Zero-based offset to write in filesrc
- Data buffersrcOffset
- Offset of valid data in bufferlen
- Number of bytes to write- Throws:
IOException
- If failed to write the data
-
mkdir
Description copied from interface:SftpClient
Create remote directory- Specified by:
mkdir
in interfaceSftpClient
- Parameters:
path
- Remote directory path- Throws:
IOException
- If failed to execute
-
rmdir
Description copied from interface:SftpClient
Remove remote directory- Specified by:
rmdir
in interfaceSftpClient
- Parameters:
path
- Remote directory path- Throws:
IOException
- If failed to execute
-
openDir
Description copied from interface:SftpClient
Obtain a handle for a directory- Specified by:
openDir
in interfaceSftpClient
- Parameters:
path
- Remote directory path- Returns:
- The associated directory
SftpClient.Handle
- Throws:
IOException
- If failed to execute
-
readDir
public List<SftpClient.DirEntry> readDir(SftpClient.Handle handle, AtomicReference<Boolean> eolIndicator) throws IOException - Specified by:
readDir
in interfaceSftpClient
- Parameters:
handle
- DirectorySftpClient.Handle
to read fromeolIndicator
- An indicator that can be used to get information whether end of list has been reached - ignored ifnull
. Upon return, set value indicates whether all entries have been exhausted - anull
value means that this information cannot be provided and another call toreadDir
is necessary in order to verify that no more entries are pending- Returns:
- A
List
of entries -null
to indicate no more entries - Throws:
IOException
- If failed to access the remote site- See Also:
-
checkDirResponse
protected List<SftpClient.DirEntry> checkDirResponse(SftpResponse response, AtomicReference<Boolean> eolIndicator) throws IOException - Throws:
IOException
-
handleUnknownDirListingPacket
protected List<SftpClient.DirEntry> handleUnknownDirListingPacket(SftpResponse response) throws IOException - Throws:
IOException
-
handleUnexpectedPacket
protected IOException handleUnexpectedPacket(int expected, SftpResponse response) throws IOException - Parameters:
expected
- The expected packet typeresponse
- The actualSftpResponse
received- Returns:
- The exception to throw - if
null
then implementor assumed to handle the exception internal. Otherwise, the exception is re-thrown - Throws:
IOException
- If failed to handle the exception internally
-
canonicalPath
Description copied from interface:SftpClient
The effective "normalized" remote path- Specified by:
canonicalPath
in interfaceSftpClient
- Parameters:
path
- The requested path - may be relative, and/or contain dots - e.g., ".", "..", "./foo", "../bar"- Returns:
- The effective "normalized" remote path
- Throws:
IOException
- If failed to execute
-
stat
Description copied from interface:SftpClient
Retrieve remote path meta-data - follow symbolic links if encountered- Specified by:
stat
in interfaceSftpClient
- Parameters:
path
- The remote path- Returns:
- The associated
SftpClient.Attributes
- Throws:
IOException
- If failed to execute
-
lstat
Description copied from interface:SftpClient
Retrieve remote path meta-data - do not follow symbolic links- Specified by:
lstat
in interfaceSftpClient
- Parameters:
path
- The remote path- Returns:
- The associated
SftpClient.Attributes
- Throws:
IOException
- If failed to execute
-
stat
Description copied from interface:SftpClient
Retrieve file/directory handle meta-data- Specified by:
stat
in interfaceSftpClient
- Parameters:
handle
- TheSftpClient.Handle
obtained via one of theopen
calls- Returns:
- The associated
SftpClient.Attributes
- Throws:
IOException
- If failed to execute
-
setStat
Description copied from interface:SftpClient
Update remote node meta-data- Specified by:
setStat
in interfaceSftpClient
- Parameters:
path
- The remote pathattributes
- TheSftpClient.Attributes
to update- Throws:
IOException
- If failed to execute
-
setStat
Description copied from interface:SftpClient
Update remote node meta-data- Specified by:
setStat
in interfaceSftpClient
- Parameters:
handle
- TheSftpClient.Handle
obtained via one of theopen
callsattributes
- TheSftpClient.Attributes
to update- Throws:
IOException
- If failed to execute
-
readLink
Description copied from interface:SftpClient
Retrieve target of a link- Specified by:
readLink
in interfaceSftpClient
- Parameters:
path
- Remote path that represents a link- Returns:
- The link target
- Throws:
IOException
- If failed to execute
-
link
Description copied from interface:SftpClient
Create a link- Specified by:
link
in interfaceSftpClient
- Parameters:
linkPath
- The link locationtargetPath
- The referenced target by the linksymbolic
- Iftrue
then make this a symbolic link, otherwise a hard one- Throws:
IOException
- If failed to execute
-
lock
- Specified by:
lock
in interfaceSftpClient
- Throws:
IOException
-
unlock
- Specified by:
unlock
in interfaceSftpClient
- Throws:
IOException
-
readDir
- Specified by:
readDir
in interfaceSftpClient
- Parameters:
path
- The remote directory path- Returns:
- An
Iterable
that can be used to iterate over all the directory entries (unlikeSftpClient.readDir(Handle)
) - Throws:
IOException
- If failed to access the remote site- See Also:
-
listDir
- Specified by:
listDir
in interfaceSftpClient
- Parameters:
handle
- A directorySftpClient.Handle
- Returns:
- An
Iterable
that can be used to iterate over all the directory entries (likeSftpClient.readDir(String)
). Note: the iterable instance is not re-usable - i.e., files can be iterated only once - Throws:
IOException
- If failed to access the directory
-
openRemoteFileChannel
public FileChannel openRemoteFileChannel(String path, Collection<SftpClient.OpenMode> modes) throws IOException Description copied from interface:SftpClient
Opens anFileChannel
on the specified remote path- Specified by:
openRemoteFileChannel
in interfaceSftpClient
- Parameters:
path
- The remote pathmodes
- The access mode(s) - ifnull
/empty then theSftpClient.DEFAULT_CHANNEL_MODES
are used- Returns:
- The open
FileChannel
- Note: do not close this owner client instance until the channel is no longer needed since it uses the client for providing the channel's functionality. - Throws:
IOException
- If failed to open the channel- See Also:
-
read
public InputStream read(String path, int bufferSize, Collection<SftpClient.OpenMode> mode) throws IOException Description copied from interface:SftpClient
Read a remote file's data via an input stream- Specified by:
read
in interfaceSftpClient
- Parameters:
path
- The remote file pathbufferSize
- The internal read buffer sizemode
- The remote fileSftpClient.OpenMode
s- Returns:
- An
InputStream
for reading the remote file data - Throws:
IOException
- If failed to execute
-
write
public OutputStream write(String path, int bufferSize, Collection<SftpClient.OpenMode> mode) throws IOException Description copied from interface:SftpClient
Write to a remote file via an output stream- Specified by:
write
in interfaceSftpClient
- Parameters:
path
- The remote file pathbufferSize
- The internal write buffer sizemode
- The remote fileSftpClient.OpenMode
s- Returns:
- An
OutputStream
for writing the data - Throws:
IOException
- If failed to execute
-
getReadBufferSize
protected int getReadBufferSize() -
getWriteBufferSize
protected int getWriteBufferSize()
-