public abstract class AbstractSftpClient extends AbstractSubsystemClient implements FullAccessSftpClient, SftpErrorDataHandler
SftpClient.Attribute, SftpClient.Attributes, SftpClient.CloseableHandle, SftpClient.CopyMode, SftpClient.DirEntry, SftpClient.Handle, SftpClient.OpenMode
Modifier and Type | Field and Description |
---|---|
protected SftpErrorDataHandler |
errorDataHandler |
static int |
INIT_COMMAND_SIZE |
log
DEFAULT_CHANNEL_MODES, EMPTY_DIR_ENTRIES, IO_BUFFER_SIZE, MIN_BUFFER_SIZE, MIN_READ_BUFFER_SIZE, MIN_WRITE_BUFFER_SIZE
BY_NAME_COMPARATOR, NAME_EXTRACTOR
EMPTY
Modifier | Constructor and Description |
---|---|
protected |
AbstractSftpClient(SftpErrorDataHandler delegateHandler) |
Modifier and Type | Method and Description |
---|---|
String |
canonicalPath(String path)
The effective "normalized" remote path
|
protected SftpClient.Attributes |
checkAttributes(int cmd,
Buffer request) |
protected SftpClient.Attributes |
checkAttributesResponse(int cmd,
Buffer buffer) |
protected void |
checkCommandStatus(int cmd,
Buffer request)
Sends the specified command, waits for the response and then invokes
checkResponseStatus(int, Buffer) |
protected int |
checkData(int cmd,
Buffer request,
int dstOffset,
byte[] dst,
AtomicReference<Boolean> eofSignalled) |
protected int |
checkDataResponse(int cmd,
Buffer buffer,
int dstoff,
byte[] dst,
AtomicReference<Boolean> eofSignalled) |
protected List<SftpClient.DirEntry> |
checkDirResponse(int cmd,
Buffer buffer,
AtomicReference<Boolean> eolIndicator) |
protected byte[] |
checkHandle(int cmd,
Buffer request) |
protected byte[] |
checkHandleResponse(int cmd,
Buffer buffer) |
protected String |
checkOneName(int cmd,
Buffer request) |
protected String |
checkOneNameResponse(int cmd,
Buffer buffer) |
protected void |
checkResponseStatus(int cmd,
Buffer buffer)
Checks if the incoming response is an
SSH_FXP_STATUS one, and if so whether the substatus is
SSH_FX_OK . |
protected void |
checkResponseStatus(int cmd,
int id,
int substatus,
String msg,
String lang) |
void |
close(SftpClient.Handle handle)
Close the handle obtained from one of the
open methods |
void |
errorData(byte[] buf,
int start,
int len)
Receive binary data from server error stream
|
Channel |
getChannel() |
<E extends SftpClientExtension> |
getExtension(Class<? extends E> extensionType) |
SftpClientExtension |
getExtension(SftpClientExtensionFactory factory) |
protected Map<String,Object> |
getParsedServerExtensions() |
protected Map<String,Object> |
getParsedServerExtensions(Map<String,byte[]> extensions) |
protected int |
getReadBufferSize() |
protected String |
getReferencedName(int cmd,
Buffer buf,
int nameIndex) |
protected int |
getWriteBufferSize() |
protected SftpClient.Attributes |
handleUnexpectedAttributesPacket(int cmd,
int id,
int type,
int length,
Buffer buffer) |
protected byte[] |
handleUnexpectedHandlePacket(int cmd,
int id,
int type,
int length,
Buffer buffer) |
protected IOException |
handleUnexpectedPacket(int cmd,
int expected,
int id,
int type,
int length,
Buffer buffer) |
protected int |
handleUnknownDataPacket(int cmd,
int id,
int type,
int length,
Buffer buffer) |
protected List<SftpClient.DirEntry> |
handleUnknownDirListingPacket(int cmd,
int id,
int type,
int length,
Buffer buffer) |
protected String |
handleUnknownOneNamePacket(int cmd,
int id,
int type,
int length,
Buffer buffer) |
void |
link(String linkPath,
String targetPath,
boolean symbolic)
Create a link
|
Iterable<SftpClient.DirEntry> |
listDir(SftpClient.Handle handle) |
void |
lock(SftpClient.Handle handle,
long offset,
long length,
int mask) |
SftpClient.Attributes |
lstat(String path)
Retrieve remote path meta-data - do not follow symbolic links
|
void |
mkdir(String path)
Create remote directory
|
SftpClient.CloseableHandle |
open(String path,
Collection<SftpClient.OpenMode> options)
Opens a remote file with the specified mode(s)
|
SftpClient.CloseableHandle |
openDir(String path)
Obtain a handle for a directory
|
FileChannel |
openRemoteFileChannel(String path,
Collection<SftpClient.OpenMode> modes)
Opens an
FileChannel on the specified remote path |
protected <B extends Buffer> |
putReferencedName(int cmd,
B buf,
String name,
int nameIndex) |
int |
read(SftpClient.Handle handle,
long fileOffset,
byte[] dst,
int dstOffset,
int len,
AtomicReference<Boolean> eofSignalled)
Reads data from the open (file) handle
|
InputStream |
read(String path,
int bufferSize,
Collection<SftpClient.OpenMode> mode)
Read a remote file's data via an input stream
|
protected SftpClient.Attributes |
readAttributes(int cmd,
Buffer buffer,
AtomicInteger nameIndex) |
List<SftpClient.DirEntry> |
readDir(SftpClient.Handle handle,
AtomicReference<Boolean> eolIndicator) |
Iterable<SftpClient.DirEntry> |
readDir(String path) |
String |
readLink(String path)
Retrieve target of a link
|
void |
remove(String path) |
void |
rename(String oldPath,
String newPath,
Collection<SftpClient.CopyMode> options) |
void |
rmdir(String path)
Remove remote directory
|
void |
setStat(SftpClient.Handle handle,
SftpClient.Attributes attributes)
Update remote node meta-data
|
void |
setStat(String path,
SftpClient.Attributes attributes)
Update remote node meta-data
|
SftpClient.Attributes |
stat(SftpClient.Handle handle)
Retrieve file/directory handle meta-data
|
SftpClient.Attributes |
stat(String path)
Retrieve remote path meta-data - follow symbolic links if encountered
|
protected void |
throwStatusException(int cmd,
int id,
int substatus,
String msg,
String lang) |
void |
unlock(SftpClient.Handle handle,
long offset,
long length) |
protected void |
validateIncomingResponse(int cmd,
int id,
int type,
int length,
Buffer buffer) |
void |
write(SftpClient.Handle handle,
long fileOffset,
byte[] src,
int srcOffset,
int len)
Write data to (open) file handle
|
OutputStream |
write(String path,
int bufferSize,
Collection<SftpClient.OpenMode> mode)
Write to a remote file via an output stream
|
protected <B extends Buffer> |
writeAttributes(int cmd,
B buffer,
SftpClient.Attributes attributes) |
toString
debug, debug, debug, debug, debug, error, error, error, error, error, getSimplifiedLogger, info, info, warn, warn, warn, warn, warn, warn, warn, warn
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
singleSessionInstance
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
getSession
getSessionContext
getClientSession
findByName, findFirstMatchByName, getNameList, getNames, ofName, removeByName, safeCompareByName
getClientChannel
receive, receive, receive, send
public static final int INIT_COMMAND_SIZE
protected final SftpErrorDataHandler errorDataHandler
protected AbstractSftpClient(SftpErrorDataHandler delegateHandler)
public Channel getChannel()
getChannel
in interface ClientChannelHolder
getChannel
in interface ChannelHolder
Channel
instancepublic <E extends SftpClientExtension> E getExtension(Class<? extends E> extensionType)
getExtension
in interface SftpClient
E
- The generic extension typeextensionType
- The extension typeOptionalFeature.isSupported()
- null
if this extension type is not
implemented by the clientSftpClient.getServerExtensions()
public SftpClientExtension getExtension(SftpClientExtensionFactory factory)
getExtension
in interface SftpClient
factory
- The SftpClientExtensionFactory
instance to use - ignored if null
OptionalFeature.isSupported()
- null
if this extension type is not
implemented by the clientprotected Map<String,Object> getParsedServerExtensions(Map<String,byte[]> extensions)
protected String getReferencedName(int cmd, Buffer buf, int nameIndex)
cmd
- The command that was sent whose response contains the name to be decodedbuf
- The Buffer
containing the encoded namenameIndex
- The zero-based order of the requested names for the command - e.g.,
protected <B extends Buffer> B putReferencedName(int cmd, B buf, String name, int nameIndex)
B
- Type of Buffer
being updatedcmd
- 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 linkprotected void checkCommandStatus(int cmd, Buffer request) throws IOException
checkResponseStatus(int, Buffer)
cmd
- The command to sendrequest
- The request Buffer
IOException
- If failed to send, receive or check the returned statusRawSftpClient.send(int, Buffer)
,
RawSftpClient.receive(int)
,
checkResponseStatus(int, Buffer)
protected void checkResponseStatus(int cmd, Buffer buffer) throws IOException
SSH_FXP_STATUS
one, and if so whether the substatus is
SSH_FX_OK
.cmd
- The sent command opcodebuffer
- The received response Buffer
IOException
- If response does not carry a status or carries a bad status codecheckResponseStatus(int, int, int, String, String)
protected void checkResponseStatus(int cmd, int id, int substatus, String msg, String lang) throws IOException
cmd
- The sent command opcodeid
- The request idsubstatus
- The sub-status valuemsg
- The messagelang
- The languageIOException
- if the sub-status is not SSH_FX_OK
throwStatusException(int, int, int, String, String)
protected void throwStatusException(int cmd, int id, int substatus, String msg, String lang) throws IOException
IOException
protected byte[] checkHandle(int cmd, Buffer request) throws IOException
cmd
- Command to be sentrequest
- The Buffer
containing the requestIOException
- If failed to send/receive or process the responseRawSftpClient.send(int, Buffer)
,
RawSftpClient.receive(int)
,
checkHandleResponse(int, Buffer)
protected byte[] checkHandleResponse(int cmd, Buffer buffer) throws IOException
IOException
protected byte[] handleUnexpectedHandlePacket(int cmd, int id, int type, int length, Buffer buffer) throws IOException
IOException
protected SftpClient.Attributes checkAttributes(int cmd, Buffer request) throws IOException
cmd
- Command to be sentrequest
- Request Buffer
Attributes
IOException
- If failed to send/receive or process the responseRawSftpClient.send(int, Buffer)
,
RawSftpClient.receive(int)
,
checkAttributesResponse(int, Buffer)
protected SftpClient.Attributes checkAttributesResponse(int cmd, Buffer buffer) throws IOException
IOException
protected SftpClient.Attributes handleUnexpectedAttributesPacket(int cmd, int id, int type, int length, Buffer buffer) throws IOException
IOException
protected String checkOneName(int cmd, Buffer request) throws IOException
cmd
- Command to be sentrequest
- The request Buffer
IOException
- If failed to send/receive or process the responseRawSftpClient.send(int, Buffer)
,
RawSftpClient.receive(int)
,
checkOneNameResponse(int, Buffer)
protected String checkOneNameResponse(int cmd, Buffer buffer) throws IOException
IOException
protected String handleUnknownOneNamePacket(int cmd, int id, int type, int length, Buffer buffer) throws IOException
IOException
protected SftpClient.Attributes readAttributes(int cmd, Buffer buffer, AtomicInteger nameIndex) throws IOException
IOException
protected <B extends Buffer> B writeAttributes(int cmd, B buffer, SftpClient.Attributes attributes)
public SftpClient.CloseableHandle open(String path, Collection<SftpClient.OpenMode> options) throws IOException
SftpClient
open
in interface SftpClient
path
- The remote pathoptions
- The desired mode - if none specified then SftpClient.OpenMode.Read
is assumedSftpClient.CloseableHandle
IOException
- If failed to open the remote filepublic void close(SftpClient.Handle handle) throws IOException
SftpClient
open
methodsclose
in interface SftpClient
handle
- The Handle
to closeIOException
- If failed to executepublic void remove(String path) throws IOException
remove
in interface SftpClient
path
- The remote path to removeIOException
- If failed to executepublic void rename(String oldPath, String newPath, Collection<SftpClient.CopyMode> options) throws IOException
rename
in interface SftpClient
IOException
public int read(SftpClient.Handle handle, long fileOffset, byte[] dst, int dstOffset, int len, AtomicReference<Boolean> eofSignalled) throws IOException
SftpClient
read
in interface SftpClient
handle
- The file SftpClient.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 not null
then upon return holds a value indicating whether EOF was reached due to
the read. If null
indicator value then this indication is not available-1
if EOF reachedIOException
- If failed to read the dataprotected int checkData(int cmd, Buffer request, int dstOffset, byte[] dst, AtomicReference<Boolean> eofSignalled) throws IOException
IOException
protected int checkDataResponse(int cmd, Buffer buffer, int dstoff, byte[] dst, AtomicReference<Boolean> eofSignalled) throws IOException
IOException
protected int handleUnknownDataPacket(int cmd, int id, int type, int length, Buffer buffer) throws IOException
IOException
public void errorData(byte[] buf, int start, int len) throws IOException
SftpErrorDataHandler
errorData
in interface SftpErrorDataHandler
buf
- The buffer of the incoming datastart
- Offset in buffer to read the datalen
- Available data in bufferIOException
- If failed to receive incoming datapublic void write(SftpClient.Handle handle, long fileOffset, byte[] src, int srcOffset, int len) throws IOException
SftpClient
write
in interface SftpClient
handle
- The file SftpClient.Handle
fileOffset
- Zero-based offset to write in filesrc
- Data buffersrcOffset
- Offset of valid data in bufferlen
- Number of bytes to writeIOException
- If failed to write the datapublic void mkdir(String path) throws IOException
SftpClient
mkdir
in interface SftpClient
path
- Remote directory pathIOException
- If failed to executepublic void rmdir(String path) throws IOException
SftpClient
rmdir
in interface SftpClient
path
- Remote directory pathIOException
- If failed to executepublic SftpClient.CloseableHandle openDir(String path) throws IOException
SftpClient
openDir
in interface SftpClient
path
- Remote directory pathSftpClient.Handle
IOException
- If failed to executepublic List<SftpClient.DirEntry> readDir(SftpClient.Handle handle, AtomicReference<Boolean> eolIndicator) throws IOException
readDir
in interface SftpClient
handle
- Directory SftpClient.Handle
to read fromeolIndicator
- An indicator that can be used to get information whether end of list has been reached -
ignored if null
. Upon return, set value indicates whether all entries have been
exhausted - a null
value means that this information cannot be provided and another
call to readDir
is necessary in order to verify that no more entries are pendingList
of entries - null
to indicate no more entriesIOException
- If failed to access the remote siteprotected List<SftpClient.DirEntry> checkDirResponse(int cmd, Buffer buffer, AtomicReference<Boolean> eolIndicator) throws IOException
IOException
protected void validateIncomingResponse(int cmd, int id, int type, int length, Buffer buffer) throws IOException
IOException
protected List<SftpClient.DirEntry> handleUnknownDirListingPacket(int cmd, int id, int type, int length, Buffer buffer) throws IOException
IOException
protected IOException handleUnexpectedPacket(int cmd, int expected, int id, int type, int length, Buffer buffer) throws IOException
cmd
- The initial command sentexpected
- The expected packet typeid
- The reported identifiertype
- The reported SFTP response typelength
- The packet lengthbuffer
- The Buffer
after reading from it whatever data led to this callnull
then implementor assumed to handle the exception
internal. Otherwise, the exception is re-thrownIOException
- If failed to handle the exception internallypublic String canonicalPath(String path) throws IOException
SftpClient
canonicalPath
in interface SftpClient
path
- The requested path - may be relative, and/or contain dots - e.g., ".",
"..", "./foo", "../bar"IOException
- If failed to executepublic SftpClient.Attributes stat(String path) throws IOException
SftpClient
stat
in interface SftpClient
path
- The remote pathSftpClient.Attributes
IOException
- If failed to executepublic SftpClient.Attributes lstat(String path) throws IOException
SftpClient
lstat
in interface SftpClient
path
- The remote pathSftpClient.Attributes
IOException
- If failed to executepublic SftpClient.Attributes stat(SftpClient.Handle handle) throws IOException
SftpClient
stat
in interface SftpClient
handle
- The SftpClient.Handle
obtained via one of the open
callsSftpClient.Attributes
IOException
- If failed to executepublic void setStat(String path, SftpClient.Attributes attributes) throws IOException
SftpClient
setStat
in interface SftpClient
path
- The remote pathattributes
- The SftpClient.Attributes
to updateIOException
- If failed to executepublic void setStat(SftpClient.Handle handle, SftpClient.Attributes attributes) throws IOException
SftpClient
setStat
in interface SftpClient
handle
- The SftpClient.Handle
obtained via one of the open
callsattributes
- The SftpClient.Attributes
to updateIOException
- If failed to executepublic String readLink(String path) throws IOException
SftpClient
readLink
in interface SftpClient
path
- Remote path that represents a linkIOException
- If failed to executepublic void link(String linkPath, String targetPath, boolean symbolic) throws IOException
SftpClient
link
in interface SftpClient
linkPath
- The link locationtargetPath
- The referenced target by the linksymbolic
- If true
then make this a symbolic link, otherwise a hard oneIOException
- If failed to executepublic void lock(SftpClient.Handle handle, long offset, long length, int mask) throws IOException
lock
in interface SftpClient
IOException
public void unlock(SftpClient.Handle handle, long offset, long length) throws IOException
unlock
in interface SftpClient
IOException
public Iterable<SftpClient.DirEntry> readDir(String path) throws IOException
readDir
in interface SftpClient
path
- The remote directory pathIterable
that can be used to iterate over all the directory entries (unlike
SftpClient.readDir(Handle)
)IOException
- If failed to access the remote siteSftpClient.readDir(Handle)
public Iterable<SftpClient.DirEntry> listDir(SftpClient.Handle handle) throws IOException
listDir
in interface SftpClient
handle
- A directory SftpClient.Handle
Iterable
that can be used to iterate over all the directory entries (like
SftpClient.readDir(String)
). Note: the iterable instance is not re-usable - i.e., files
can be iterated only onceIOException
- If failed to access the directorypublic FileChannel openRemoteFileChannel(String path, Collection<SftpClient.OpenMode> modes) throws IOException
SftpClient
FileChannel
on the specified remote pathopenRemoteFileChannel
in interface SftpClient
path
- The remote pathmodes
- The access mode(s) - if null
/empty then the SftpClient.DEFAULT_CHANNEL_MODES
are usedFileChannel
- 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.IOException
- If failed to open the channelChannels.newInputStream(java.nio.channels.ReadableByteChannel)
,
Channels.newOutputStream(java.nio.channels.WritableByteChannel)
public InputStream read(String path, int bufferSize, Collection<SftpClient.OpenMode> mode) throws IOException
SftpClient
read
in interface SftpClient
path
- The remote file pathbufferSize
- The internal read buffer sizemode
- The remote file SftpClient.OpenMode
sInputStream
for reading the remote file dataIOException
- If failed to executepublic OutputStream write(String path, int bufferSize, Collection<SftpClient.OpenMode> mode) throws IOException
SftpClient
write
in interface SftpClient
path
- The remote file pathbufferSize
- The internal write buffer sizemode
- The remote file SftpClient.OpenMode
sOutputStream
for writing the dataIOException
- If failed to executeprotected int getReadBufferSize()
protected int getWriteBufferSize()
Copyright © 2008–2024 The Apache Software Foundation. All rights reserved.