Class Util
- java.lang.Object
-
- hudson.Util
-
public class Util extends Object
Various utility methods that don't have more proper home.- Author:
- Kohsuke Kawaguchi
-
-
Field Summary
Fields Modifier and Type Field Description static booleanNO_SYMLINKOn Unix environment that cannot run "ln", set this to true.static org.apache.commons.lang.time.FastDateFormatRFC822_DATETIME_FORMATTERDeprecated.static booleanSYMLINK_ESCAPEHATCHstatic org.apache.commons.lang.time.FastDateFormatXS_DATETIME_FORMATTERDeprecated.static DateTimeFormatterXS_DATETIME_FORMATTER2
-
Constructor Summary
Constructors Constructor Description Util()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static FilechangeExtension(File dst, String ext)Returns a file name by changing its extension.static voidcloseAndLogFailures(Closeable toClose, Logger logger, String closeableName, String closeableOwner)Closes the item and logs error to the log in the case of error.static Stringcombine(long n, String suffix)Deprecated.Use individual localization methods instead.static voidcopyFile(File src, File dst)Deprecated.since 2.335; useFiles.copy(Path, Path, CopyOption...)directlystatic voidcopyStream(InputStream in, OutputStream out)Deprecated.UseIOUtils.copy(InputStream, OutputStream)static voidcopyStream(Reader in, Writer out)Deprecated.UseIOUtils.copy(Reader, Writer)static voidcopyStreamAndClose(InputStream in, OutputStream out)Deprecated.UseIOUtils.copy(InputStream, OutputStream)in atry-with-resources blockstatic voidcopyStreamAndClose(Reader in, Writer out)Deprecated.UseIOUtils.copy(Reader, Writer)in atry-with-resources blockstatic PathcreateDirectories(Path dir, FileAttribute<?>... attrs)Create a directory by creating all nonexistent parent directories first.static org.apache.tools.ant.types.FileSetcreateFileSet(File baseDir, String includes)static org.apache.tools.ant.types.FileSetcreateFileSet(File baseDir, String includes, String excludes)Creates AntFileSetwith the base dir and include pattern.static <T> List<T>createSubList(Collection<?> source, Class<T> type)Create a sub-list by only picking up instances of the specified type.static voidcreateSymlink(File baseDir, String targetPath, String symlinkPath, TaskListener listener)Creates a symlink to targetPath at baseDir+symlinkPath.static FilecreateTempDir()Creates a new temporary directory.static longdaysBetween(Date a, Date b)Compute the number of calendar days elapsed since the given date.static longdaysElapsedSince(Date date)static voiddeleteContentsRecursive(File file)Deletes the contents of the given directory (but not the directory itself) recursively.static voiddeleteContentsRecursive(Path path, PathRemover.PathChecker pathChecker)Deletes the given directory contents (but not the directory itself) recursively using a PathChecker.static voiddeleteFile(File f)Deletes this file (and does not take no for an answer).static voiddeleteRecursive(File dir)Deletes the given directory (including its contents) recursively.static voiddeleteRecursive(Path dir, PathRemover.PathChecker pathChecker)Deletes the given directory and contents recursively using a filter.static voiddisplayIOException(IOException e, TaskListener listener)On Windows, error messages for IOException aren't very helpful.static Stringencode(String s)Escapes non-ASCII characters in URL.static StringencodeRFC2396(String url)Deprecated.since 2008-05-13.static StringensureEndsWith(String subject, String suffix)Ensure string ends with suffixstatic Stringescape(String text)Escapes HTML unsafe characters like <, & to the respective character entities.static PathfileToPath(File file)static <T> List<T>filter(Iterable<?> base, Class<T> type)Creates a filtered sublist.static <T> List<T>filter(List<?> base, Class<T> type)Creates a filtered sublist.static StringfixEmpty(String s)Convert empty string to null.static StringfixEmptyAndTrim(String s)Convert empty string to null, and trim whitespace.static <T> Iterable<T>fixNull(Iterable<T> l)static StringfixNull(String s)Convert null to "".static <T> Collection<T>fixNull(Collection<T> l)static <T> List<T>fixNull(List<T> l)static <T> Set<T>fixNull(Set<T> l)static <T> TfixNull(T s, T defaultValue)Convertnullto a default value.static byte[]fromHexString(String data)static StringfullEncode(String s)Encode a single path component for use in an HTTP URL.static StringgetDigestOf(File file)Computes the MD5 digest of a file.static StringgetDigestOf(InputStream source)Computes MD5 digest of the given input stream.static StringgetDigestOf(String text)static StringgetFileName(String filePath)Cuts all the leading path portion and get just the file name.static StringgetHexOfSHA256DigestOf(byte[] input)Returns Hex string of SHA-256 Digest of passed inputstatic StringgetHostName()Guesses the current host name.static <T> TgetNearestAncestorOfTypeOrThrow(org.kohsuke.stapler.StaplerRequest request, Class<T> clazz)Find the specific ancestor, or throw an exception.static StringgetPastTimeString(long duration)Deprecated.Actually identical togetTimeSpanString(long), does not addago.static byte[]getSHA256DigestOf(byte[] input)Returns SHA-256 Digest of input bytesstatic StringgetTimeSpanString(long duration)Returns a human readable text of the time duration, for example "3 minutes 40 seconds".static StringgetWin32ErrorMessage(int n)Gets a human readable message for the given Win32 error code.static StringgetWin32ErrorMessage(IOException e)static StringgetWin32ErrorMessage(Throwable e)Extracts the Win32 error message fromThrowableif possible.static <T> TifOverridden(Supplier<T> supplier, Class<?> base, Class<?> derived, String methodName, Class<?>... types)Calls the given supplier if the method defined on the base type with the given arguments is overridden in the given derived type.static Stringintern(String s)Null-safe String intern method.static booleanisAbsoluteUri(String uri)Deprecated.UseisSafeToRedirectTo(java.lang.String)instead if your goal is to prevent open redirectsstatic booleanisDescendant(File forParent, File potentialChild)A check if a file path is a descendant of a parent pathstatic booleanisOverridden(Class<?> base, Class<?> derived, String methodName, Class<?>... types)Checks whether the method defined on the base type with the given arguments is overridden in the given derived type.static booleanisRelativePath(String path)A mostly accurate check of whether a path is a relative path or not.static booleanisSafeToRedirectTo(String uri)Return true iff the parameter does not denote an absolute URI and not a scheme-relative URI.static booleanisSymlink(File file)Checks if the given file represents a symlink.static booleanisSymlink(Path path)static Stringjoin(Collection<?> strings, String separator)Deprecated.since 2.292; useString.join(CharSequence, Iterable)static <T> List<T>join(Collection<? extends T>... items)Combines all the given collections into a single list.static StringloadFile(File logfile)Deprecated.callloadFile(java.io.File, java.nio.charset.Charset)instead to specify the charset to use for decoding (preferablyStandardCharsets.UTF_8).static StringloadFile(File logfile, Charset charset)Reads the entire contents of the text file atlogfileinto a string usingcharsetfor decoding.static PropertiesloadProperties(String properties)Loads a key/value pair string asPropertiesstatic String[]mapToEnv(Map<String,String> m)Converts the map format of the environment variables to the K=V format in the array.static intmin(int x, int... values)static Set<PosixFilePermission>modeToPermissions(int mode)static Stringnullify(String v)static intpermissionsToMode(Set<PosixFilePermission> permissions)static voidprintRedirect(String contextPath, String redirectUrl, String message, PrintWriter out)static StringrawEncode(String s)Encode a single path component for use in an HTTP URL.static StringremoveTrailingSlash(String s)static StringreplaceMacro(String s, VariableResolver<String> resolver)Replaces the occurrence of '$key' byresolver.get('key').static StringreplaceMacro(String s, Map<String,String> properties)Replaces the occurrence of '$key' byproperties.get('key').static StringresolveSymlink(File link)Resolves symlink, if the given file is a symlink.static StringresolveSymlink(File link, TaskListener listener)Deprecated.as of 1.456 UseresolveSymlink(File)static FileresolveSymlinkToFile(File link)Resolves a symlink to theFilethat points to.static StringsingleQuote(String s)Surrounds by a single-quote.static SecretKeytoAes128Key(String s)Converts a string into 128-bit AES key.static StringtoHexString(byte[] bytes)static StringtoHexString(byte[] data, int start, int len)static String[]tokenize(String s)static String[]tokenize(String s, String delimiter)Tokenizes the text separated by delimiters.static voidtouch(File file)Creates an empty file if nonexistent or truncates the existing file.static NumbertryParseNumber(String numberStr, Number defaultNumber)Returns the parsed string if parsed successful; otherwise returns the default number.static StringwrapToErrorSpan(String s)Wraps with the error icon and the CSS class to render error message.static StringxmlEscape(String text)
-
-
-
Field Detail
-
XS_DATETIME_FORMATTER
@Deprecated public static final org.apache.commons.lang.time.FastDateFormat XS_DATETIME_FORMATTER
Deprecated.
-
XS_DATETIME_FORMATTER2
public static final DateTimeFormatter XS_DATETIME_FORMATTER2
-
RFC822_DATETIME_FORMATTER
@Deprecated public static final org.apache.commons.lang.time.FastDateFormat RFC822_DATETIME_FORMATTER
Deprecated.
-
NO_SYMLINK
public static boolean NO_SYMLINK
On Unix environment that cannot run "ln", set this to true.
-
SYMLINK_ESCAPEHATCH
public static boolean SYMLINK_ESCAPEHATCH
-
-
Method Detail
-
filter
@NonNull public static <T> List<T> filter(@NonNull Iterable<?> base, @NonNull Class<T> type)
Creates a filtered sublist.- Since:
- 1.176
-
filter
@NonNull public static <T> List<T> filter(@NonNull List<?> base, @NonNull Class<T> type)
Creates a filtered sublist.
-
replaceMacro
@Nullable public static String replaceMacro(@CheckForNull String s, @NonNull Map<String,String> properties)
Replaces the occurrence of '$key' byproperties.get('key').Unlike shell, undefined variables are left as-is (this behavior is the same as Ant.)
-
replaceMacro
@Nullable public static String replaceMacro(@CheckForNull String s, @NonNull VariableResolver<String> resolver)
Replaces the occurrence of '$key' byresolver.get('key').Unlike shell, undefined variables are left as-is (this behavior is the same as Ant.)
-
loadFile
@NonNull @Deprecated public static String loadFile(@NonNull File logfile) throws IOException
Deprecated.callloadFile(java.io.File, java.nio.charset.Charset)instead to specify the charset to use for decoding (preferablyStandardCharsets.UTF_8).Reads the entire contents of the text file atlogfileinto a string using thedefault charsetfor decoding. If no such file exists, an empty string is returned.- Parameters:
logfile- The text file to read in its entirety.- Returns:
- The entire text content of
logfile. - Throws:
IOException- If an error occurs while reading the file.
-
loadFile
@NonNull public static String loadFile(@NonNull File logfile, @NonNull Charset charset) throws IOException
Reads the entire contents of the text file atlogfileinto a string usingcharsetfor decoding. If no such file exists, an empty string is returned.- Parameters:
logfile- The text file to read in its entirety.charset- The charset to use for decoding the bytes inlogfile.- Returns:
- The entire text content of
logfile. - Throws:
IOException- If an error occurs while reading the file.
-
deleteContentsRecursive
public static void deleteContentsRecursive(@NonNull File file) throws IOExceptionDeletes the contents of the given directory (but not the directory itself) recursively. It does not take no for an answer - if necessary, it will have multiple attempts at deleting things.- Throws:
IOException- if the operation fails.
-
deleteContentsRecursive
@Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class) public static void deleteContentsRecursive(@NonNull Path path, @NonNull PathRemover.PathChecker pathChecker) throws IOExceptionDeletes the given directory contents (but not the directory itself) recursively using a PathChecker.- Parameters:
path- a directory to deletepathChecker- a security check to validate a path before deleting- Throws:
IOException- if the operation fails
-
deleteFile
public static void deleteFile(@NonNull File f) throws IOExceptionDeletes this file (and does not take no for an answer). If necessary, it will have multiple attempts at deleting things.- Parameters:
f- a file to delete- Throws:
IOException- if it exists but could not be successfully deleted
-
deleteRecursive
public static void deleteRecursive(@NonNull File dir) throws IOExceptionDeletes the given directory (including its contents) recursively. It does not take no for an answer - if necessary, it will have multiple attempts at deleting things.- Throws:
IOException- if the operation fails.
-
deleteRecursive
@Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class) public static void deleteRecursive(@NonNull Path dir, @NonNull PathRemover.PathChecker pathChecker) throws IOExceptionDeletes the given directory and contents recursively using a filter.- Parameters:
dir- a directory to deletepathChecker- a security check to validate a path before deleting- Throws:
IOException- if the operation fails
-
isSymlink
public static boolean isSymlink(@NonNull File file) throws IOExceptionChecks if the given file represents a symlink. UnlikeFiles.isSymbolicLink(Path), this method also considers NTFS junction points as symbolic links.- Throws:
IOException
-
isSymlink
@Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class) public static boolean isSymlink(@NonNull Path path)
-
isRelativePath
public static boolean isRelativePath(String path)
A mostly accurate check of whether a path is a relative path or not. This is designed to take a path against an unknown operating system so may give invalid results.- Parameters:
path- the path.- Returns:
trueif the path looks relative.- Since:
- 1.606
-
isDescendant
public static boolean isDescendant(File forParent, File potentialChild) throws IOException
A check if a file path is a descendant of a parent path- Parameters:
forParent- the parent the child should be a descendant ofpotentialChild- the path to check- Returns:
- true if so
- Throws:
IOException- for invalid paths- Since:
- 2.80
- See Also:
InvalidPathException
-
createTempDir
public static File createTempDir() throws IOException
Creates a new temporary directory.- Throws:
IOException
-
displayIOException
public static void displayIOException(@NonNull IOException e, @NonNull TaskListener listener)On Windows, error messages for IOException aren't very helpful. This method generates additional user-friendly error message to the listener
-
getWin32ErrorMessage
@CheckForNull public static String getWin32ErrorMessage(@NonNull IOException e)
-
getWin32ErrorMessage
@CheckForNull public static String getWin32ErrorMessage(Throwable e)
Extracts the Win32 error message fromThrowableif possible.- Returns:
- null if there seems to be no error code or if the platform is not Win32.
-
getWin32ErrorMessage
@CheckForNull public static String getWin32ErrorMessage(int n)
Gets a human readable message for the given Win32 error code.- Returns:
- null if no such message is available.
-
getHostName
@NonNull public static String getHostName()
Guesses the current host name.
-
copyStream
@Deprecated public static void copyStream(@NonNull InputStream in, @NonNull OutputStream out) throws IOException
Deprecated.UseIOUtils.copy(InputStream, OutputStream)- Throws:
IOException
-
copyStream
@Deprecated public static void copyStream(@NonNull Reader in, @NonNull Writer out) throws IOException
Deprecated.UseIOUtils.copy(Reader, Writer)- Throws:
IOException
-
copyStreamAndClose
@Deprecated public static void copyStreamAndClose(@NonNull InputStream in, @NonNull OutputStream out) throws IOException
Deprecated.UseIOUtils.copy(InputStream, OutputStream)in atry-with-resources block- Throws:
IOException
-
copyStreamAndClose
@Deprecated public static void copyStreamAndClose(@NonNull Reader in, @NonNull Writer out) throws IOException
Deprecated.UseIOUtils.copy(Reader, Writer)in atry-with-resources block- Throws:
IOException
-
tokenize
@NonNull public static String[] tokenize(@NonNull String s, @CheckForNull String delimiter)
Tokenizes the text separated by delimiters.In 1.210, this method was changed to handle quotes like Unix shell does. Before that, this method just used
StringTokenizer.- Since:
- 1.145
- See Also:
QuotedStringTokenizer
-
mapToEnv
@NonNull public static String[] mapToEnv(@NonNull Map<String,String> m)
Converts the map format of the environment variables to the K=V format in the array.
-
min
public static int min(int x, @NonNull int... values)
-
ensureEndsWith
@Nullable public static String ensureEndsWith(@CheckForNull String subject, @CheckForNull String suffix)
Ensure string ends with suffix- Parameters:
subject- Examined stringsuffix- Desired suffix- Returns:
- Original subject in case it already ends with suffix, null in case subject was null and subject + suffix otherwise.
- Since:
- 1.505
-
getDigestOf
@NonNull public static String getDigestOf(@NonNull InputStream source) throws IOException
Computes MD5 digest of the given input stream. This method should only be used for non-security applications where the MD5 weakness is not a problem.- Parameters:
source- The stream will be closed by this method at the end of this method.- Returns:
- 32-char wide string
- Throws:
IOException- See Also:
DigestUtils.md5Hex(InputStream)
-
getDigestOf
@NonNull public static String getDigestOf(@NonNull File file) throws IOException
Computes the MD5 digest of a file.- Parameters:
file- a file- Returns:
- a 32-character string
- Throws:
IOException- in case reading fails- Since:
- 1.525
-
toAes128Key
@NonNull public static SecretKey toAes128Key(@NonNull String s)
Converts a string into 128-bit AES key.- Since:
- 1.308
-
toHexString
@NonNull public static String toHexString(@NonNull byte[] data, int start, int len)
-
toHexString
@NonNull public static String toHexString(@NonNull byte[] bytes)
-
fromHexString
@NonNull public static byte[] fromHexString(@NonNull String data)
-
getTimeSpanString
@NonNull public static String getTimeSpanString(long duration)
Returns a human readable text of the time duration, for example "3 minutes 40 seconds". This version should be used for representing a duration of some activity (like build)- Parameters:
duration- number of milliseconds.
-
getPastTimeString
@Deprecated @NonNull public static String getPastTimeString(long duration)
Deprecated.Actually identical togetTimeSpanString(long), does not addago.Get a human readable string representing strings like "xxx days ago", which should be used to point to the occurrence of an event in the past.
-
combine
@NonNull @Deprecated public static String combine(long n, @NonNull String suffix)
Deprecated.Use individual localization methods instead. SeeMessages.Util_year(Object)for an example. Deprecated since 2009-06-24, remove method after 2009-12-24.Combines number and unit, with a plural suffix if needed.
-
createSubList
@NonNull public static <T> List<T> createSubList(@NonNull Collection<?> source, @NonNull Class<T> type)
Create a sub-list by only picking up instances of the specified type.
-
encode
@NonNull public static String encode(@NonNull String s)
Escapes non-ASCII characters in URL.Note that this methods only escapes non-ASCII but leaves other URL-unsafe characters, such as '#'.
rawEncode(String)should generally be used instead, though be careful to pass only a single path component to that method (it will encode /, but this method does not).
-
rawEncode
@NonNull public static String rawEncode(@NonNull String s)
Encode a single path component for use in an HTTP URL. Escapes all non-ASCII, general unsafe (space and"#%<>[\]^`{|}~) and HTTP special characters (/;:?) as specified in RFC1738. (so alphanumeric and!@$&*()-_=+',.are not encoded) Note that slash (/) is encoded, so the given string should be a single path component used in constructing a URL. Method name inspired by PHP's rawurlencode.
-
fullEncode
@NonNull public static String fullEncode(@NonNull String s)
Encode a single path component for use in an HTTP URL. Escapes all special characters including those outside of the characters specified in RFC1738. All characters outside numbers and letters without diacritic are encoded. Note that slash (/) is encoded, so the given string should be a single path component used in constructing a URL.- Since:
- 2.308
-
escape
@Nullable public static String escape(@CheckForNull String text)
Escapes HTML unsafe characters like <, & to the respective character entities.
-
touch
public static void touch(@NonNull File file) throws IOExceptionCreates an empty file if nonexistent or truncates the existing file. Note: The behavior of this method in the case where the file already exists is unlike the POSIXtouchutility which merely updates the file's access and/or modification time.- Throws:
IOException
-
copyFile
@Deprecated @Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class) @RestrictedSince("2.335") public static void copyFile(@NonNull File src, @NonNull File dst) throws org.apache.tools.ant.BuildException
Deprecated.since 2.335; useFiles.copy(Path, Path, CopyOption...)directlyCopies a single file by using Ant.- Throws:
org.apache.tools.ant.BuildException
-
fixNull
@NonNull public static <T> T fixNull(@CheckForNull T s, @NonNull T defaultValue)Convertnullto a default value.- Parameters:
defaultValue- Default value. It may be immutable or not, depending on the implementation.- Since:
- 2.144
-
fixEmpty
@CheckForNull public static String fixEmpty(@CheckForNull String s)
Convert empty string to null.
-
fixEmptyAndTrim
@CheckForNull public static String fixEmptyAndTrim(@CheckForNull String s)
Convert empty string to null, and trim whitespace.- Since:
- 1.154
-
fixNull
@NonNull public static <T> List<T> fixNull(@CheckForNull List<T> l)
- Type Parameters:
T- Type of the list.- Parameters:
l- list to check.- Returns:
lif l is notnull. An empty immutable list if l isnull.
-
fixNull
@NonNull public static <T> Set<T> fixNull(@CheckForNull Set<T> l)
- Type Parameters:
T- Type of the set.- Parameters:
l- set to check.- Returns:
lif l is notnull. An empty immutable set if l isnull.
-
fixNull
@NonNull public static <T> Collection<T> fixNull(@CheckForNull Collection<T> l)
- Type Parameters:
T- Type of the collection.- Parameters:
l- collection to check.- Returns:
lif l is notnull. An empty immutable set if l isnull.
-
fixNull
@NonNull public static <T> Iterable<T> fixNull(@CheckForNull Iterable<T> l)
- Type Parameters:
T- Type of the iterable.- Parameters:
l- iterable to check.- Returns:
lif l is notnull. An empty immutable set if l isnull.
-
getFileName
@NonNull public static String getFileName(@NonNull String filePath)
Cuts all the leading path portion and get just the file name.
-
join
@Deprecated @NonNull public static String join(@NonNull Collection<?> strings, @NonNull String separator)
Deprecated.since 2.292; useString.join(CharSequence, Iterable)Concatenate multiple strings by inserting a separator.
-
join
@NonNull public static <T> List<T> join(@NonNull Collection<? extends T>... items)
Combines all the given collections into a single list.
-
createFileSet
@NonNull public static org.apache.tools.ant.types.FileSet createFileSet(@NonNull File baseDir, @NonNull String includes, @CheckForNull String excludes)Creates AntFileSetwith the base dir and include pattern.The difference with this and using
AbstractFileSet.setIncludes(String)is that this method doesn't treat whitespace as a pattern separator, which makes it impossible to use space in the file path.- Parameters:
includes- String like "foo/bar/*.xml" Multiple patterns can be separated by ',', and whitespace can surround ',' (so that you can write "abc, def" and "abc,def" to mean the same thing.excludes- Exclusion pattern. Follows the same format as the 'includes' parameter. Can be null.- Since:
- 1.172
-
createFileSet
@NonNull public static org.apache.tools.ant.types.FileSet createFileSet(@NonNull File baseDir, @NonNull String includes)
-
createSymlink
public static void createSymlink(@NonNull File baseDir, @NonNull String targetPath, @NonNull String symlinkPath, @NonNull TaskListener listener) throws InterruptedExceptionCreates a symlink to targetPath at baseDir+symlinkPath.If there's a prior symlink at baseDir+symlinkPath, it will be overwritten.
- Parameters:
baseDir- Base directory to resolve the 'symlinkPath' parameter.targetPath- The file that the symlink should point to. Usually relative to the directory of the symlink but may instead be an absolute path.symlinkPath- Where to create a symlink in (relative tobaseDir)- Throws:
InterruptedException
-
resolveSymlink
@Deprecated public static String resolveSymlink(File link, TaskListener listener) throws InterruptedException, IOException
Deprecated.as of 1.456 UseresolveSymlink(File)- Throws:
InterruptedExceptionIOException
-
resolveSymlinkToFile
@CheckForNull public static File resolveSymlinkToFile(@NonNull File link) throws InterruptedException, IOException
Resolves a symlink to theFilethat points to.- Returns:
- null if the specified file is not a symlink.
- Throws:
InterruptedExceptionIOException
-
resolveSymlink
@CheckForNull public static String resolveSymlink(@NonNull File link) throws IOException
Resolves symlink, if the given file is a symlink. Otherwise return null.If the resolution fails, report an error.
- Returns:
- null if the given file is not a symlink. If the symlink is absolute, the returned string is an absolute path. If the symlink is relative, the returned string is that relative representation. The relative path is meant to be resolved from the location of the symlink.
- Throws:
IOException
-
encodeRFC2396
@Deprecated public static String encodeRFC2396(String url)
Deprecated.since 2008-05-13. This method is broken (see JENKINS-1666). It should probably be removed but I'm not sure if it is considered part of the public API that needs to be maintained for backwards compatibility. Useencode(String)instead.Encodes the URL by RFC 2396. I thought there's another spec that refers to UTF-8 as the encoding, but don't remember it right now.- Since:
- 1.204
-
wrapToErrorSpan
@NonNull public static String wrapToErrorSpan(@NonNull String s)
Wraps with the error icon and the CSS class to render error message.- Since:
- 1.173
-
tryParseNumber
@CheckForNull public static Number tryParseNumber(@CheckForNull String numberStr, @CheckForNull Number defaultNumber)
Returns the parsed string if parsed successful; otherwise returns the default number. If the string is null, empty or a ParseException is thrown then the defaultNumber is returned.- Parameters:
numberStr- string to parsedefaultNumber- number to return if the string can not be parsed- Returns:
- returns the parsed string; otherwise the default number
-
isOverridden
public static boolean isOverridden(@NonNull Class<?> base, @NonNull Class<?> derived, @NonNull String methodName, @NonNull Class<?>... types)Checks whether the method defined on the base type with the given arguments is overridden in the given derived type.- Parameters:
base- The base type.derived- The derived type.methodName- The name of the method.types- The types of the arguments for the method.- Returns:
truewhenderivedprovides the specified method other than as inherited frombase.- Throws:
IllegalArgumentException- Whenderiveddoes not derive frombase, or whenbasedoes not contain the specified method.
-
ifOverridden
public static <T> T ifOverridden(Supplier<T> supplier, @NonNull Class<?> base, @NonNull Class<?> derived, @NonNull String methodName, @NonNull Class<?>... types)
Calls the given supplier if the method defined on the base type with the given arguments is overridden in the given derived type.- Parameters:
supplier- The supplier to call if the method is indeed overridden.base- The base type.derived- The derived type.methodName- The name of the method.types- The types of the arguments for the method.- Returns:
truewhenderivedprovides the specified method other than as inherited frombase.- Throws:
IllegalArgumentException- Whenderiveddoes not derive frombase, or whenbasedoes not contain the specified method.AbstractMethodError- If the derived class doesn't override the given method.- Since:
- 2.259
-
changeExtension
@NonNull public static File changeExtension(@NonNull File dst, @NonNull String ext)
Returns a file name by changing its extension.- Parameters:
ext- For example, ".zip"
-
intern
@Nullable public static String intern(@CheckForNull String s)
Null-safe String intern method.- Returns:
- A canonical representation for the string object. Null for null input strings
-
isAbsoluteUri
@Deprecated @RestrictedSince("1.651.2 / 2.3") @Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class) public static boolean isAbsoluteUri(@NonNull String uri)
Deprecated.UseisSafeToRedirectTo(java.lang.String)instead if your goal is to prevent open redirectsReturn true if the systemId denotes an absolute URI . The same algorithm can be seen inURI, but implementing this by ourselves allow it to be more lenient about escaping of URI.
-
isSafeToRedirectTo
public static boolean isSafeToRedirectTo(@NonNull String uri)Return true iff the parameter does not denote an absolute URI and not a scheme-relative URI.- Since:
- 2.3 / 1.651.2
-
loadProperties
@NonNull public static Properties loadProperties(@NonNull String properties) throws IOException
Loads a key/value pair string asProperties- Throws:
IOException- Since:
- 1.392
-
closeAndLogFailures
@Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class) public static void closeAndLogFailures(@CheckForNull Closeable toClose, @NonNull Logger logger, @NonNull String closeableName, @NonNull String closeableOwner)Closes the item and logs error to the log in the case of error. Logging will be performed on theWARNINGlevel.- Parameters:
toClose- Item to close. Nothing will happen if it isnulllogger- Logger, which receives the errorcloseableName- Name of the closeable itemcloseableOwner- String representation of the closeable holder- Since:
- 2.19, but TODO update once un-restricted
-
permissionsToMode
@Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class) public static int permissionsToMode(Set<PosixFilePermission> permissions)
-
modeToPermissions
@Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class) public static Set<PosixFilePermission> modeToPermissions(int mode) throws IOException
- Throws:
IOException
-
fileToPath
@Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class) @NonNull public static Path fileToPath(@NonNull File file) throws IOException
- Throws:
IOException- iff.toPath()throwsInvalidPathException.
-
createDirectories
@Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class) public static Path createDirectories(@NonNull Path dir, FileAttribute<?>... attrs) throws IOException
Create a directory by creating all nonexistent parent directories first.Unlike
Files.createDirectory(java.nio.file.Path, java.nio.file.attribute.FileAttribute<?>...), an exception is not thrown if the directory could not be created because it already exists. UnlikeFiles.createDirectories(java.nio.file.Path, java.nio.file.attribute.FileAttribute<?>...), an exception is not thrown if the directory (or one of its parents) is a symbolic link.The
attrsparameter contains optionalfile attributesto set atomically when creating the nonexistent directories. Each file attribute is identified by itsFileAttribute.name(). If more than one attribute of the same name is included in the array, then all but the last occurrence is ignored.If this method fails, then it may do so after creating some, but not all, of the parent directories.
- Parameters:
dir- The directory to create.attrs- An optional list of file attributes to set atomically when creating the directory.- Returns:
- The directory.
- Throws:
UnsupportedOperationException- If the array contains an attribute that cannot be set atomically when creating the directory.FileAlreadyExistsException- Ifdirexists but is not a directory.IOException- If an I/O error occurs.- See Also:
Files.createDirectories(Path, FileAttribute[])
-
daysBetween
@Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class) public static long daysBetween(@NonNull Date a, @NonNull Date b)Compute the number of calendar days elapsed since the given date. As it's only the calendar days difference that matter, "11.00pm" to "2.00am the day after" returns 1, even if there are only 3 hours between. As well as "10am" to "2pm" both on the same day, returns 0.
-
daysElapsedSince
@Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class) public static long daysElapsedSince(@NonNull Date date)- Returns:
- positive number of days between the given date and now
- See Also:
daysBetween(Date, Date)
-
getNearestAncestorOfTypeOrThrow
@Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class) @NonNull public static <T> T getNearestAncestorOfTypeOrThrow(@NonNull org.kohsuke.stapler.StaplerRequest request, @NonNull Class<T> clazz)Find the specific ancestor, or throw an exception. Useful for an ancestor we know is inside the URL to ease readability
-
printRedirect
@Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class) public static void printRedirect(String contextPath, String redirectUrl, String message, PrintWriter out)
-
getSHA256DigestOf
@Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class) public static byte[] getSHA256DigestOf(@NonNull byte[] input)Returns SHA-256 Digest of input bytes
-
getHexOfSHA256DigestOf
@Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class) public static String getHexOfSHA256DigestOf(byte[] input) throws IOException
Returns Hex string of SHA-256 Digest of passed input- Throws:
IOException
-
-