Class SFTPEnvironment
- java.lang.Object
-
- com.github.robtimus.filesystems.sftp.SFTPEnvironment
-
public class SFTPEnvironment extends Object implements Map<String,Object>
A utility class to set up environments that can be used in theFileSystemProvider.newFileSystem(URI, Map)andFileSystemProvider.newFileSystem(Path, Map)methods ofSFTPFileSystemProvider.- Author:
- Rob Spoor
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceSFTPEnvironment.QueryParamIndicates which query parameters can be used to define environment values.static interfaceSFTPEnvironment.QueryParamsA container forSFTPEnvironment.QueryParamannotations.
-
Constructor Summary
Constructors Constructor Description SFTPEnvironment()Creates a new SFTP environment.SFTPEnvironment(Map<String,Object> map)Creates a new SFTP environment.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()booleancontainsKey(Object key)booleancontainsValue(Object value)static SFTPEnvironmentcopy(Map<String,?> env)Copies a map to create a newSFTPEnvironmentinstance.Set<Map.Entry<String,Object>>entrySet()booleanequals(Object o)Objectget(Object key)inthashCode()booleanisEmpty()Set<String>keySet()Objectput(String key, Object value)voidputAll(Map<? extends String,? extends Object> m)Objectremove(Object key)static voidsetDefault(SFTPEnvironment defaultEnvironment)Sets the default SFTP environment.intsize()StringtoString()Collection<Object>values()SFTPEnvironmentwithAgentForwarding(boolean agentForwarding)Stores whether or not agent forwarding should be enabled.SFTPEnvironmentwithAppendedConfig(String key, String value)Stores a configuration option to use.SFTPEnvironmentwithAppendedConfig(String key, String value, BinaryOperator<String> appender)Stores a configuration option to use.SFTPEnvironmentwithClientVersion(String version)Stores the client version to use.SFTPEnvironmentwithConfig(String key, String value)Stores a configuration option to use.SFTPEnvironmentwithConfig(Properties config)Stores configuration options to use.SFTPEnvironmentwithConfigRepository(ConfigRepository repository)Stores the config repository to use.SFTPEnvironmentwithConnectTimeout(int timeout)Stores the connection timeout to use.SFTPEnvironmentwithDefaultDirectory(String pathname)Stores the default directory to use.SFTPEnvironmentwithFilenameEncoding(Charset encoding)Stores the filename encoding to use.SFTPEnvironmentwithFileSystemExceptionFactory(FileSystemExceptionFactory factory)Stores the file system exception factory to use.SFTPEnvironmentwithHostKeyAlias(String alias)Stores the host key alias to use.SFTPEnvironmentwithHostKeyRepository(HostKeyRepository repository)Stores the host key repository to use.SFTPEnvironmentwithIdentities(Identity... identities)Stores several identity to use.SFTPEnvironmentwithIdentities(Collection<Identity> identities)Stores several identity to use.SFTPEnvironmentwithIdentity(Identity identity)Stores an identity to use.SFTPEnvironmentwithIdentityRepository(IdentityRepository repository)Stores the identity repository to use.SFTPEnvironmentwithKnownHosts(File knownHosts)Stores the known hosts file to use.SFTPEnvironmentwithPassword(char[] password)Stores the password to use.SFTPEnvironmentwithPoolConfig(SFTPPoolConfig poolConfig)Stores the pool config to use.SFTPEnvironmentwithProxy(Proxy proxy)Stores the proxy to use.SFTPEnvironmentwithServerAliveCountMax(int count)Stores the maximum number of server alive messages that can be sent without any reply before disconnecting.SFTPEnvironmentwithServerAliveInterval(int interval)Stores the server alive interval to use.SFTPEnvironmentwithSocketFactory(SocketFactory factory)Stores the socket factory to use.SFTPEnvironmentwithTimeout(int timeout)Stores the timeout.SFTPEnvironmentwithUserInfo(UserInfo userInfo)Stores the user info to use.SFTPEnvironmentwithUsername(String username)Stores the username to use.-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
-
-
-
-
Method Detail
-
withUsername
public SFTPEnvironment withUsername(String username)
Stores the username to use.- Parameters:
username- The username to use.- Returns:
- This object.
-
withConnectTimeout
@QueryParam("connectTimeout") public SFTPEnvironment withConnectTimeout(int timeout)
Stores the connection timeout to use.- Parameters:
timeout- The connection timeout in milliseconds.- Returns:
- This object.
-
withProxy
public SFTPEnvironment withProxy(Proxy proxy)
Stores the proxy to use.- Parameters:
proxy- The proxy to use.- Returns:
- This object.
-
withUserInfo
public SFTPEnvironment withUserInfo(UserInfo userInfo)
Stores the user info to use.- Parameters:
userInfo- The user info to use.- Returns:
- This object.
-
withPassword
public SFTPEnvironment withPassword(char[] password)
Stores the password to use.- Parameters:
password- The password to use.- Returns:
- This object.
- Since:
- 1.1
-
withConfig
public SFTPEnvironment withConfig(Properties config)
Stores configuration options to use. This method will add not clear any previously set options, but only add new ones.- Parameters:
config- The configuration options to use.- Returns:
- This object.
- Throws:
NullPointerException- if the given properties object isnull.- See Also:
withConfig(String, String)
-
withConfig
@QueryParam("config.<key>") public SFTPEnvironment withConfig(String key, String value)
Stores a configuration option to use. This method will add not clear any previously set options, but only add new ones.- Parameters:
key- The configuration key.value- The configuration value.- Returns:
- This object.
- Throws:
NullPointerException- if the given key or value isnull.- See Also:
withConfig(Properties)
-
withAppendedConfig
@QueryParam("appendedConfig.<key>") public SFTPEnvironment withAppendedConfig(String key, String value)
Stores a configuration option to use. UnlikewithConfig(String, String), configuration options set using this method will be appended to existing configuration options instead of overwriting them. For instance, this method can be used to add support forssh-rsakeys as follows:// JSch way: // session.setConfig("server_host_key", session.getConfig("server_host_key") + ",ssh-rsa"); // session.setConfig("PubkeyAcceptedAlgorithms", session.getConfig("PubkeyAcceptedAlgorithms") + ",ssh-rsa"); // sftp-fs way: SFTPEnvironment env = new SFTPEnvironment() ... .withAppendedConfig("server_host_key", "ssh-rsa") .withAppendedConfig("PubkeyAcceptedAlgorithms", "ssh-rsa");This method will use a comma to append configuration options. If this does not fit your needs, use
withAppendedConfig(String, String, BinaryOperator)with a custom appender.- Parameters:
key- The configuration key.value- The configuration value.- Returns:
- This object.
- Throws:
NullPointerException- If the given key or value isnull.- Since:
- 3.2
-
withAppendedConfig
public SFTPEnvironment withAppendedConfig(String key, String value, BinaryOperator<String> appender)
Stores a configuration option to use. This is a more generalized version ofwithAppendedConfig(String, String)that allows non-default combining of existing and new configuration options.- Parameters:
key- The configuration key.value- The configuration value.appender- A function that takes the previously configured option and the new option and returns a combined configuration option.- Returns:
- This object.
- Throws:
NullPointerException- If the given key, value or appender function isnull.- Since:
- 3.2
-
withSocketFactory
public SFTPEnvironment withSocketFactory(SocketFactory factory)
Stores the socket factory to use.- Parameters:
factory- The socket factory to use.- Returns:
- This object.
-
withTimeout
@QueryParam("timeout") public SFTPEnvironment withTimeout(int timeout)
Stores the timeout.- Parameters:
timeout- The timeout in milliseconds.- Returns:
- This object.
- See Also:
Socket.setSoTimeout(int)
-
withClientVersion
@QueryParam("clientVersion") public SFTPEnvironment withClientVersion(String version)
Stores the client version to use.- Parameters:
version- The client version.- Returns:
- This object.
-
withHostKeyAlias
@QueryParam("hostKeyAlias") public SFTPEnvironment withHostKeyAlias(String alias)
Stores the host key alias to use.- Parameters:
alias- The host key alias.- Returns:
- This object.
-
withServerAliveInterval
@QueryParam("serverAliveInterval") public SFTPEnvironment withServerAliveInterval(int interval)
Stores the server alive interval to use.- Parameters:
interval- The server alive interval in milliseconds.- Returns:
- This object.
-
withServerAliveCountMax
@QueryParam("serverAliveCountMax") public SFTPEnvironment withServerAliveCountMax(int count)
Stores the maximum number of server alive messages that can be sent without any reply before disconnecting.- Parameters:
count- The maximum number of server alive messages.- Returns:
- This object.
-
withIdentityRepository
public SFTPEnvironment withIdentityRepository(IdentityRepository repository)
Stores the identity repository to use.- Parameters:
repository- The identity repository to use.- Returns:
- This object.
-
withIdentity
public SFTPEnvironment withIdentity(Identity identity)
Stores an identity to use. This method will add not clear any previously set identities, but only add new ones.- Parameters:
identity- The identity to use.- Returns:
- This object.
- Throws:
NullPointerException- If the given identity isnull.- Since:
- 1.2
- See Also:
withIdentities(Identity...),withIdentities(Collection)
-
withIdentities
public SFTPEnvironment withIdentities(Identity... identities)
Stores several identity to use. This method will add not clear any previously set identities, but only add new ones.- Parameters:
identities- The identities to use.- Returns:
- This object.
- Throws:
NullPointerException- If any of the given identity isnull.- Since:
- 1.2
- See Also:
withIdentity(Identity),withIdentities(Collection)
-
withIdentities
public SFTPEnvironment withIdentities(Collection<Identity> identities)
Stores several identity to use. This method will add not clear any previously set identities, but only add new ones.- Parameters:
identities- The identities to use.- Returns:
- This object.
- Throws:
NullPointerException- If any of the given identity isnull.- Since:
- 1.2
- See Also:
withIdentity(Identity),withIdentities(Identity...)
-
withHostKeyRepository
public SFTPEnvironment withHostKeyRepository(HostKeyRepository repository)
Stores the host key repository to use.- Parameters:
repository- The host key repository to use.- Returns:
- This object.
-
withKnownHosts
@QueryParam("knownHosts") public SFTPEnvironment withKnownHosts(File knownHosts)
Stores the known hosts file to use. Note that the known hosts file is ignored if aHostKeyRepositoryis set with a non-nullvalue.- Parameters:
knownHosts- The known hosts file to use.- Returns:
- This object.
- Throws:
NullPointerException- If the given file isnull.- Since:
- 1.2
- See Also:
withHostKeyRepository(HostKeyRepository)
-
withConfigRepository
public SFTPEnvironment withConfigRepository(ConfigRepository repository)
Stores the config repository to use.- Parameters:
repository- The config repository to use.- Returns:
- This object.
- Since:
- 3.1
-
withAgentForwarding
@QueryParam("agentForwarding") public SFTPEnvironment withAgentForwarding(boolean agentForwarding)
Stores whether or not agent forwarding should be enabled.- Parameters:
agentForwarding-trueto enable strict agent forwarding, orfalseto disable it.- Returns:
- This object.
-
withFilenameEncoding
@QueryParam("filenameEncoding") public SFTPEnvironment withFilenameEncoding(Charset encoding)
Stores the filename encoding to use.- Parameters:
encoding- The filename encoding to use.- Returns:
- This object.
-
withDefaultDirectory
@QueryParam("defaultDir") public SFTPEnvironment withDefaultDirectory(String pathname)
Stores the default directory to use. If it exists, this will be the directory that relative paths are resolved to.- Parameters:
pathname- The default directory to use.- Returns:
- This object.
-
withPoolConfig
@QueryParam("poolConfig.maxWaitTime") @QueryParam("poolConfig.maxIdleTime") @QueryParam("poolConfig.initialSize") @QueryParam("poolConfig.maxSize") public SFTPEnvironment withPoolConfig(SFTPPoolConfig poolConfig)
Stores the pool config to use.The maximum pool size influences the number of concurrent threads that can access an SFTP file system.
If the maximum wait time is negative, SFTP file systems wait indefinitely until a client connection is available. This is the default setting if no pool config is defined.- Parameters:
poolConfig- The pool config to use.- Returns:
- This object.
- Since:
- 3.0
-
withFileSystemExceptionFactory
public SFTPEnvironment withFileSystemExceptionFactory(FileSystemExceptionFactory factory)
Stores the file system exception factory to use.- Parameters:
factory- The file system exception factory to use.- Returns:
- This object.
-
containsKey
public boolean containsKey(Object key)
- Specified by:
containsKeyin interfaceMap<String,Object>
-
containsValue
public boolean containsValue(Object value)
- Specified by:
containsValuein interfaceMap<String,Object>
-
equals
public boolean equals(Object o)
-
hashCode
public int hashCode()
-
copy
public static SFTPEnvironment copy(Map<String,?> env)
Copies a map to create a newSFTPEnvironmentinstance.- Parameters:
env- The map to copy. It can be anSFTPEnvironmentinstance, but does not have to be.- Returns:
- A new
SFTPEnvironmentinstance that is a copy of the given map. - Since:
- 3.0
-
setDefault
public static void setDefault(SFTPEnvironment defaultEnvironment)
Sets the default SFTP environment. This is used inSFTPFileSystemProvider.getPath(URI)when a file system needs to be created, since no environment can be passed. This way, certain settings likepool configurationcan still be applied.- Parameters:
defaultEnvironment- The default SFTP environment. Usenullto reset it to an empty environment.- Since:
- 3.3
-
-