public class HostConfigEntry extends Object implements MutableUserHolder
Modifier and Type | Field and Description |
---|---|
static String |
ALL_HOSTS_PATTERN |
static boolean |
DEFAULT_EXCLUSIVE_IDENTITIES |
static String |
EXCLUSIVE_IDENTITIES_CONFIG_PROP
Use only the identities specified in the host entry (if any)
|
static Set<String> |
EXPLICIT_PROPERTIES
A case insensitive
Set of the properties that receive special handling |
static char |
HOME_TILDE_CHAR |
static String |
HOST_CONFIG_PROP |
static String |
HOST_NAME_CONFIG_PROP |
static String |
IDENTITY_FILE_CONFIG_PROP |
static char |
LOCAL_HOME_MACRO |
static char |
LOCAL_HOST_MACRO |
static char |
LOCAL_USER_MACRO |
static String |
MULTI_VALUE_SEPARATORS |
static char |
NEGATION_CHAR_PATTERN
Used to negate a host pattern
|
static char |
PATH_MACRO_CHAR |
static String |
PATTERN_CHARS
The available pattern characters
|
static String |
PORT_CONFIG_PROP |
static char |
REMOTE_HOST_MACRO |
static char |
REMOTE_PORT_MACRO |
static char |
REMOTE_USER_MACRO |
static char |
SINGLE_CHAR_PATTERN
Used in a host pattern to denote any one character
|
static String |
STD_CONFIG_FILENAME
Standard OpenSSH config file name
|
static String |
USER_CONFIG_PROP |
static char |
WILDCARD_PATTERN
Used in a host pattern to denote zero or more consecutive characters
|
Constructor and Description |
---|
HostConfigEntry() |
HostConfigEntry(String pattern,
String host,
int port,
String username) |
Modifier and Type | Method and Description |
---|---|
void |
addIdentity(File file) |
void |
addIdentity(Path path) |
void |
addIdentity(String id)
Adds a path to an identity file
|
<A extends Appendable> |
append(A sb) |
static <A extends Appendable> |
appendHostConfigEntries(A sb,
Collection<? extends HostConfigEntry> entries) |
static <A extends Appendable> |
appendNonEmptyPort(A sb,
String name,
int port) |
static <A extends Appendable> |
appendNonEmptyProperties(A sb,
Map<String,?> props)
Appends the extra properties - while skipping the
EXPLICIT_PROPERTIES ones |
static <A extends Appendable> |
appendNonEmptyProperty(A sb,
String name,
Object value) |
static <A extends Appendable> |
appendNonEmptyValues(A sb,
String name,
Collection<?> values) |
static <A extends Appendable> |
appendNonEmptyValues(A sb,
String name,
Object... values) |
String |
appendPropertyValue(String name,
String value)
Appends a value using a comma to an existing one.
|
static StringBuilder |
appendUserHome(StringBuilder sb) |
static StringBuilder |
appendUserHome(StringBuilder sb,
Path userHome) |
static StringBuilder |
appendUserHome(StringBuilder sb,
String userHome) |
static HostConfigEntry |
findBestMatch(Collection<? extends HostConfigEntry> matches)
Finds the best match out of the given ones.
|
static HostConfigEntry |
findBestMatch(Iterable<? extends HostConfigEntry> matches)
Finds the best match out of the given ones.
|
static HostConfigEntry |
findBestMatch(Iterator<? extends HostConfigEntry> matches)
Finds the best match out of the given ones.
|
static List<HostConfigEntry> |
findMatchingEntries(String host,
Collection<? extends HostConfigEntry> entries)
Locates all the matching entries for a give host name / address
|
static List<HostConfigEntry> |
findMatchingEntries(String host,
HostConfigEntry... entries)
Locates all the matching entries for a give host name / address
|
static Path |
getDefaultHostConfigFile() |
String |
getHost() |
String |
getHostName() |
Collection<String> |
getIdentities() |
Collection<Pair<Pattern,Boolean>> |
getPatterns() |
int |
getPort() |
Map<String,String> |
getProperties() |
String |
getProperty(String name) |
String |
getProperty(String name,
String defaultValue) |
String |
getUsername() |
boolean |
isHostMatch(String host)
Checks if a given host name / address matches the entry's host pattern(s)
|
static boolean |
isHostMatch(String host,
Collection<Pair<Pattern,Boolean>> patterns) |
static boolean |
isHostMatch(String host,
Pattern pattern)
Checks if a given host name / address matches a host pattern
|
boolean |
isIdentitiesOnly() |
static boolean |
isSpecificHostPattern(String pattern) |
static boolean |
isValidPatternChar(char ch)
Checks if the given character is valid for a host pattern.
|
static HostConfigEntry |
normalizeEntry(HostConfigEntry entry,
String host,
int port,
String username) |
static List<String> |
parseConfigValue(String value)
Checks if this is a multi-value - allow space and comma
|
static List<Pair<Pattern,Boolean>> |
parsePatterns(Collection<? extends CharSequence> patterns) |
boolean |
processGlobalValues(HostConfigEntry globalEntry)
Updates the values that are not already configured with those
from the global entry
|
void |
processProperty(String name,
Collection<String> valsList,
boolean ignoreAlreadyInitialized) |
static List<HostConfigEntry> |
readHostConfigEntries(BufferedReader rdr)
Reads configuration entries
|
static List<HostConfigEntry> |
readHostConfigEntries(File file) |
static List<HostConfigEntry> |
readHostConfigEntries(InputStream inStream,
boolean okToClose) |
static List<HostConfigEntry> |
readHostConfigEntries(Path path,
OpenOption... options) |
static List<HostConfigEntry> |
readHostConfigEntries(Reader rdr,
boolean okToClose) |
static List<HostConfigEntry> |
readHostConfigEntries(String filePath) |
static List<HostConfigEntry> |
readHostConfigEntries(URL url) |
String |
removeProperty(String name) |
String |
resolveHostName(String originalHost) |
static String |
resolveHostName(String originalName,
String entryName)
Resolves the effective target host
|
static String |
resolveIdentityFilePath(String id,
String host,
int port,
String username) |
int |
resolvePort(int originalPort)
Resolves the effective port to use
|
static int |
resolvePort(int originalPort,
int entryPort)
Resolves the effective port to use
|
String |
resolveUsername(String originalUser)
Resolves the effective username
|
static String |
resolveUsername(String originalUser,
String entryUser)
Resolves the effective username
|
void |
setHost(Collection<String> patterns) |
void |
setHost(String host) |
void |
setHostName(String hostName) |
void |
setIdentities(Collection<String> identities) |
void |
setIdentitiesOnly(boolean identitiesOnly) |
void |
setPort(int port) |
void |
setProperties(Map<String,String> properties) |
String |
setProperty(String name,
String value)
Sets / Replaces the property value
|
void |
setUsername(String username) |
static HostConfigEntryResolver |
toHostConfigEntryResolver(Collection<? extends HostConfigEntry> entries) |
static Pair<Pattern,Boolean> |
toPattern(CharSequence pattern)
Converts a host pattern string to a regular expression matcher.
|
String |
toString() |
static List<HostConfigEntry> |
updateEntriesList(List<HostConfigEntry> entries,
HostConfigEntry curEntry) |
boolean |
updateGlobalHostName(String name) |
boolean |
updateGlobalIdentities(Collection<String> ids) |
boolean |
updateGlobalIdentityOnly(boolean identitiesOnly) |
boolean |
updateGlobalPort(int portValue) |
Map<String,String> |
updateGlobalProperties(Map<String,String> props)
Sets all the properties for which no current value exists in the entry
|
boolean |
updateGlobalUserName(String user) |
static void |
writeHostConfigEntries(File file,
Collection<? extends HostConfigEntry> entries) |
static void |
writeHostConfigEntries(OutputStream outputStream,
boolean okToClose,
Collection<? extends HostConfigEntry> entries) |
static void |
writeHostConfigEntries(Path path,
Collection<? extends HostConfigEntry> entries,
OpenOption... options) |
public static final char WILDCARD_PATTERN
public static final String ALL_HOSTS_PATTERN
public static final String STD_CONFIG_FILENAME
public static final char SINGLE_CHAR_PATTERN
public static final char NEGATION_CHAR_PATTERN
public static final String PATTERN_CHARS
public static final String HOST_CONFIG_PROP
public static final String HOST_NAME_CONFIG_PROP
public static final String PORT_CONFIG_PROP
public static final String USER_CONFIG_PROP
public static final String IDENTITY_FILE_CONFIG_PROP
public static final String EXCLUSIVE_IDENTITIES_CONFIG_PROP
public static final boolean DEFAULT_EXCLUSIVE_IDENTITIES
public static final Set<String> EXPLICIT_PROPERTIES
Set
of the properties that receive special handlingpublic static final String MULTI_VALUE_SEPARATORS
public static final char HOME_TILDE_CHAR
public static final char PATH_MACRO_CHAR
public static final char LOCAL_HOME_MACRO
public static final char LOCAL_USER_MACRO
public static final char LOCAL_HOST_MACRO
public static final char REMOTE_HOST_MACRO
public static final char REMOTE_USER_MACRO
public static final char REMOTE_PORT_MACRO
public String getHost()
public void setHost(String host)
public void setHost(Collection<String> patterns)
public Collection<Pair<Pattern,Boolean>> getPatterns()
public boolean isHostMatch(String host)
host
- The host name / address - ignored if null
/emptytrue
if the name / address matches the pattern(s)isHostMatch(String, Pattern)
public String getHostName()
public void setHostName(String hostName)
public int getPort()
public void setPort(int port)
public int resolvePort(int originalPort)
originalPort
- The original requested portresolvePort(int, int)
public String getUsername()
getUsername
in interface UsernameHolder
null
/emptypublic void setUsername(String username)
setUsername
in interface MutableUserHolder
public String resolveUsername(String originalUser)
originalUser
- The original requested usernamenull
/empty
then it is used, otherwise the original one.resolveUsername(String)
public Collection<String> getIdentities()
null
/emptypublic void addIdentity(File file)
file
- A File
that contains an identity key - never null
public void addIdentity(Path path)
path
- A Path
to a file that contains an identity key
- never null
public void addIdentity(String id)
id
- The identity path to add - never null
public void setIdentities(Collection<String> identities)
public boolean isIdentitiesOnly()
true
if must use only the identities in this entrypublic void setIdentitiesOnly(boolean identitiesOnly)
public Map<String,String> getProperties()
Map
of extra properties that have been read - may be
null
/empty, or even contain some values that have been parsed
and set as members of the entry (e.g., host, port, etc.). Note:
multi-valued keys use a comma-separated list of valuespublic String getProperty(String name)
name
- Property name - never null
/emptynull
if no such propertygetProperty(String, String)
public String getProperty(String name, String defaultValue)
name
- Property name - never null
/emptydefaultValue
- Default value to return if no such propertypublic boolean processGlobalValues(HostConfigEntry globalEntry)
globalEntry
- The global entry - ignored if null
or
same reference as this entrytrue
if anything updatedpublic Map<String,String> updateGlobalProperties(Map<String,String> props)
props
- The global properties - ignored if null
/emptyMap
of the updated propertiespublic boolean updateGlobalIdentities(Collection<String> ids)
ids
- Global identities - ignored if null
/empty or already
have configured identitiestrue
if updated identitiespublic boolean updateGlobalUserName(String user)
user
- The global user name - ignored if null
/empty or
already have a configured usertrue
if updated the usernamepublic boolean updateGlobalHostName(String name)
name
- The global host name - ignored if null
/empty or
already have a configured target hosttrue
if updated the target hostpublic boolean updateGlobalPort(int portValue)
portValue
- The global port value - ignored if not positive
or already have a configured porttrue
if updated the port valuepublic boolean updateGlobalIdentityOnly(boolean identitiesOnly)
identitiesOnly
- Whether to use only the identities in this entry.
Ignored if already settrue
if updated the option valuepublic void processProperty(String name, Collection<String> valsList, boolean ignoreAlreadyInitialized)
name
- Property name - never null
/emptyvalsList
- The available values for the propertyignoreAlreadyInitialized
- If false
and one of the "known"
properties is encountered then throws an exceptionIllegalArgumentException
- If an existing value is overwritten and
ignoreAlreadyInitialized is false
(except for IDENTITY_FILE_CONFIG_PROP
which is cumulativeHOST_NAME_CONFIG_PROP
,
PORT_CONFIG_PROP
,
USER_CONFIG_PROP
,
IDENTITY_FILE_CONFIG_PROP
public String appendPropertyValue(String name, String value)
setProperty(String, String)
.name
- Property name - never null
/emptyvalue
- The value to be appended - ignored if null
/emptynull
if no previous valuepublic String setProperty(String name, String value)
name
- Property name - never null
/emptyvalue
- Property value - if null
/empty then
removeProperty(String)
is callednull
if no such namepublic String removeProperty(String name)
name
- Property name - never null
/emptynull
if no such property namepublic void setProperties(Map<String,String> properties)
properties
- The properties to set - if null
then an empty
map is effectively set. Note: it is highly recommended to use a
case insensitive key mapper.public <A extends Appendable> A append(A sb) throws IOException
IOException
public static <A extends Appendable> A appendNonEmptyPort(A sb, String name, int port) throws IOException
A
- The Appendable
typesb
- The target appendername
- The property name - never null
/emptyport
- The port value - ignored if non-positiveIOException
- If failed to append the requested dataappendNonEmptyProperty(Appendable, String, Object)
public static <A extends Appendable> A appendNonEmptyProperties(A sb, Map<String,?> props) throws IOException
EXPLICIT_PROPERTIES
onesA
- The Appendable
typesb
- The target appenderprops
- The Map
of properties - ignored if null
/emptyIOException
- If failed to append the requested dataappendNonEmptyProperty(Appendable, String, Object)
public static <A extends Appendable> A appendNonEmptyProperty(A sb, String name, Object value) throws IOException
A
- The Appendable
typesb
- The target appendername
- The property name - never null
/emptyvalue
- The property value - ignored if null
. Note:
if the string representation of the value contains any commas, they are
assumed to indicate a multi-valued property which is broken down to
individual lines - one per value.IOException
- If failed to append the requested dataappendNonEmptyValues(Appendable, String, Object...)
public static <A extends Appendable> A appendNonEmptyValues(A sb, String name, Object... values) throws IOException
A
- The Appendable
typesb
- The target appendername
- The property name - never null
/emptyvalues
- The values to be added - one per line - ignored if null
/emptyIOException
- If failed to append the requested dataappendNonEmptyValues(Appendable, String, Collection)
public static <A extends Appendable> A appendNonEmptyValues(A sb, String name, Collection<?> values) throws IOException
A
- The Appendable
typesb
- The target appendername
- The property name - never null
/emptyvalues
- The values to be added - one per line - ignored if null
/emptyIOException
- If failed to append the requested datapublic static HostConfigEntryResolver toHostConfigEntryResolver(Collection<? extends HostConfigEntry> entries)
entries
- The entries - ignored if null
/emptyHostConfigEntryResolver
wrapper using the entriespublic static HostConfigEntry normalizeEntry(HostConfigEntry entry, String host, int port, String username) throws IOException
entry
- The original entry - ignored if null
host
- The original host name / addressport
- The original portusername
- The original user nameIOException
- If failed to normalize the entryresolveHostName(String)
,
resolvePort(int)
,
resolveUsername(String)
,
resolveIdentityFilePath(String, String, int, String)
public static HostConfigEntry findBestMatch(Collection<? extends HostConfigEntry> matches)
matches
- The available matches - ignored if null
/emptynull
if no matches or no best match foundfindBestMatch(Iterator)
public static HostConfigEntry findBestMatch(Iterable<? extends HostConfigEntry> matches)
matches
- The available matches - ignored if null
/emptynull
if no matches or no best match foundfindBestMatch(Iterator)
public static HostConfigEntry findBestMatch(Iterator<? extends HostConfigEntry> matches)
matches
- The available matches - ignored if null
/emptynull
if no matches or no best match foundisSpecificHostPattern(String)
public static boolean isSpecificHostPattern(String pattern)
pattern
- The pattern to check - ignored if null
/emptytrue
if the pattern is not empty and contains no wildcard charactersWILDCARD_PATTERN
,
SINGLE_CHAR_PATTERN
,
SINGLE_CHAR_PATTERN
public static List<HostConfigEntry> findMatchingEntries(String host, HostConfigEntry... entries)
host
- The host name / address - ignored if null
/emptyentries
- The HostConfigEntry
-ies to scan - ignored if null
/emptyList
of all the matching entriesisHostMatch(String)
public static List<HostConfigEntry> findMatchingEntries(String host, Collection<? extends HostConfigEntry> entries)
host
- The host name / address - ignored if null
/emptyentries
- The HostConfigEntry
-ies to scan - ignored if null
/emptyList
of all the matching entriesisHostMatch(String)
public static String resolveHostName(String originalName, String entryName)
originalName
- The original requested hostentryName
- The configured hostnull
/empty
then it is used, otherwise the original one.public static String resolveUsername(String originalUser, String entryUser)
originalUser
- The original requested usernameentryUser
- The configured host entry usernamenull
/empty
then it is used, otherwise the original one.public static int resolvePort(int originalPort, int entryPort)
originalPort
- The original requested portentryPort
- The configured host entry portpublic static boolean isHostMatch(String host, Collection<Pair<Pattern,Boolean>> patterns)
public static boolean isHostMatch(String host, Pattern pattern)
host
- The host name / address - ignored if null
/emptypattern
- The host Pattern
- ignored if null
true
if the name / address matches the patternpublic static List<Pair<Pattern,Boolean>> parsePatterns(Collection<? extends CharSequence> patterns)
public static Pair<Pattern,Boolean> toPattern(CharSequence pattern)
pattern
- The original pattern string - ignored if null
/emptyPattern
and the indication
whether it is a negating pattern or not - null
if no original stringWILDCARD_PATTERN
,
SINGLE_CHAR_PATTERN
,
NEGATION_CHAR_PATTERN
public static boolean isValidPatternChar(char ch)
WILDCARD_PATTERN
SINGLE_CHAR_PATTERN
ch
- The character to validatetrue
if valid pattern characterpublic static List<HostConfigEntry> readHostConfigEntries(File file) throws IOException
IOException
public static List<HostConfigEntry> readHostConfigEntries(Path path, OpenOption... options) throws IOException
IOException
public static List<HostConfigEntry> readHostConfigEntries(URL url) throws IOException
IOException
public static List<HostConfigEntry> readHostConfigEntries(String filePath) throws IOException
IOException
public static List<HostConfigEntry> readHostConfigEntries(InputStream inStream, boolean okToClose) throws IOException
IOException
public static List<HostConfigEntry> readHostConfigEntries(Reader rdr, boolean okToClose) throws IOException
IOException
public static List<HostConfigEntry> readHostConfigEntries(BufferedReader rdr) throws IOException
rdr
- The BufferedReader
to useList
of read HostConfigEntry
-iesIOException
- If failed to parse the read configurationpublic static List<HostConfigEntry> updateEntriesList(List<HostConfigEntry> entries, HostConfigEntry curEntry)
public static void writeHostConfigEntries(File file, Collection<? extends HostConfigEntry> entries) throws IOException
IOException
public static void writeHostConfigEntries(Path path, Collection<? extends HostConfigEntry> entries, OpenOption... options) throws IOException
IOException
public static void writeHostConfigEntries(OutputStream outputStream, boolean okToClose, Collection<? extends HostConfigEntry> entries) throws IOException
IOException
public static <A extends Appendable> A appendHostConfigEntries(A sb, Collection<? extends HostConfigEntry> entries) throws IOException
IOException
public static List<String> parseConfigValue(String value)
value
- The value - ignored if null
/empty (after trimming)List
of the encountered valuespublic static String resolveIdentityFilePath(String id, String host, int port, String username) throws IOException
IOException
public static StringBuilder appendUserHome(StringBuilder sb)
public static StringBuilder appendUserHome(StringBuilder sb, Path userHome)
public static StringBuilder appendUserHome(StringBuilder sb, String userHome)
Copyright © 2008–2016 The Apache Software Foundation. All rights reserved.