Class SpincastUtilsDefault
- All Implemented Interfaces:
SpincastUtils
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionCleans HTML, only allowing basic elements.Cleans HTML, only allowing basic elements.void
clearDirectory
(File dir) Empty a directory if it exists.long[]
convertLongSetToLongPrimitiveArray
(Set<Long> longSet) Convert a string so it can be used in an URL without being escaped: remove accents, spaces, etc.convertToFriendlyToken
(String str, String resultIfEmpty) Convert a string so it can be used in an URL without being escaped: remove accents, spaces, etc.void
copyClasspathDirToFileSystem
(String classpathDirPath, File targetDir) Copy a directory from the classpath (located in a .jar or not) to the specified file system directory.void
copyClasspathFileToFileSystem
(String classpathFilePath, File fileSystemFile) Copy a file from the classpath (located in a .jar or not) to the specified file system file.protected void
copyJarDirToFileSystem
(String jarDirPath, File targetDir) protected void
copyJarFileToFileSystem
(String jarDirPath, File targetFile) Creates a randomly named temp file path in the temporary writable directory.<T extends Enum<?>>
TenumValueOfInsensitive
(Class<T> enumClass, String str) Perform a case insensitivevalueOf(value)
search on an enum.protected String
If the project is running from an executable .jar file, this will return the directory containing this .jar file.If the project is not running from an executable .jar file, this will return the root directory of the project on the file system.The current cache buster to use.getClassLocationDirOrJarFile
(Class<?> clazz) The location of the specified class.getClasspathInputStream
(String classpathPath) Gets theInputStream
from a resource on the classpath.protected String
protected Object
getFileSystemLock
(URI jarDirUri) protected File
Gets the best Locale to use given a "Accept-Language" HTTP header.getMimeTypeFromExtension
(String extension) Gets themime type
from the extension.getMimeTypeFromMultipleSources
(String responseContentTypeHeader, String resourcePath, String requestPath) Gets themime type
using multiple sources of information.getMimeTypeFromPath
(String path) Gets themime type
from a path, using its extension.getParametersFromQuerystring
(String qs, boolean decodeQueryStringFirst) Parses a querystring (starting with a "?" or not) and returns the list of parameters.Return the querystring of an URL, without leading "?".Parses an URL and returns the list of its querystring parameters, if any.<T> T
getRandomElement
(Set<T> set) Returns a random element from the set ornull
if the set is null or empty.protected SpincastConfig
Gets the current Spincast version.inQuotesStringFormat
(String str, boolean singleQuotes) Format a String so it can be outputed in a HTML page, as a one line javascript string or the attribute of an HTML element.protected boolean
boolean
isClassLoadedFromJar
(Class<?> clazz) Was the specified Class loaded from a .jar file?boolean
isClasspathResourceLoadedFromJar
(String resourcePath) Is the classpath resource in a .jar file? Otherwise it is on the file system.boolean
Validate if a String contains characters below 31 or DEL (127).boolean
isContentTypeToSkipGziping
(String contentType) Should the specifiedContent-Type
be gzipped?boolean
isPortOpen
(String host, int port) Validate if a port is open and connectable.boolean
isRequestedResourceNameEndsWithBeforeExtension
(String resourceBaseName, String suffix) Returnstrue
if the name of the requested resource ends with the specified suffix.boolean
isRequestedResourceNameEndsWithBeforeExtension
(URI currentURI, String suffix) Returnstrue
if the name of the requested resource ends with the specified suffix.boolean
Is the current code running from an executable .jar? An executable .jar is a jar withMain-Class
in itsMETA-INF/MANIFEST.MF
.readClasspathFile
(String path) Reads a file on the classpath and returns it as a String.readClasspathFile
(String path, String encoding) Reads a file on the classpath and returns it as a String.removeCacheBusterCodes
(String text) Removes the cache buster code occurences from the given text.<K,
V> LinkedHashMap<K, V> sortMapByValues
(Map<K, V> map) Sort a Map by valuevoid
zipDirectory
(File directoryToZip, File targetZipFile, boolean includeDirItself) Zips a directory.void
zipExtract
(File zipFile, File targetDir) Extracts a .zip file to the specified directory.
-
Field Details
-
logger
protected static final org.slf4j.Logger logger
-
-
Constructor Details
-
SpincastUtilsDefault
-
-
Method Details
-
getSpincastConfig
-
getFileSystemsLocks
-
getFileSystemLock
-
getGeneratedTempFilesDir
-
isContentTypeToSkipGziping
Description copied from interface:SpincastUtils
Should the specifiedContent-Type
be gzipped?- Specified by:
isContentTypeToSkipGziping
in interfaceSpincastUtils
-
getMimeTypeFromMultipleSources
public String getMimeTypeFromMultipleSources(String responseContentTypeHeader, String resourcePath, String requestPath) Description copied from interface:SpincastUtils
Gets themime type
using multiple sources of information.- Specified by:
getMimeTypeFromMultipleSources
in interfaceSpincastUtils
resourcePath
- the path (absolute or relative) to the target resource. Can be null.requestPath
- the path of the current request. Can be null.- Returns:
- the
mime type
ornull
if it can't be decided.
-
getMimeTypeFromPath
Description copied from interface:SpincastUtils
Gets themime type
from a path, using its extension.- Specified by:
getMimeTypeFromPath
in interfaceSpincastUtils
- Returns:
- the
mime type
ornull
if it can't be decided.
-
getMimeTypeFromExtension
Description copied from interface:SpincastUtils
Gets themime type
from the extension.- Specified by:
getMimeTypeFromExtension
in interfaceSpincastUtils
- Returns:
- the
mime type
ornull
if it can't be decided.
-
getLocaleBestMatchFromAcceptLanguageHeader
Description copied from interface:SpincastUtils
Gets the best Locale to use given a "Accept-Language" HTTP header.- Specified by:
getLocaleBestMatchFromAcceptLanguageHeader
in interfaceSpincastUtils
- Returns:
- the best Locale to use or
null
if the given header can't be parsed.
-
isClasspathResourceLoadedFromJar
Description copied from interface:SpincastUtils
Is the classpath resource in a .jar file? Otherwise it is on the file system.- Specified by:
isClasspathResourceLoadedFromJar
in interfaceSpincastUtils
-
getAppJarDirectory
Description copied from interface:SpincastUtils
If the project is running from an executable .jar file, this will return the directory containing this .jar file.- Specified by:
getAppJarDirectory
in interfaceSpincastUtils
- Returns:
- the directory path or
null
if the application is not running from an executable .jar file.
-
isClassLoadedFromJar
Description copied from interface:SpincastUtils
Was the specified Class loaded from a .jar file?- Specified by:
isClassLoadedFromJar
in interfaceSpincastUtils
-
getClassLocationDirOrJarFile
Description copied from interface:SpincastUtils
The location of the specified class.This will be a directory if the class was loaded from the file system as a standalone
.class
file or a .jar file if the class was loaded from a jar.You can use
SpincastUtils.isClassLoadedFromJar(Class)
to know if the class was loaded from a .jar file or not.- Specified by:
getClassLocationDirOrJarFile
in interfaceSpincastUtils
-
isRunningFromExecutableJar
public boolean isRunningFromExecutableJar()Description copied from interface:SpincastUtils
Is the current code running from an executable .jar? An executable .jar is a jar withMain-Class
in itsMETA-INF/MANIFEST.MF
.- Specified by:
isRunningFromExecutableJar
in interfaceSpincastUtils
-
getAppRootDirectoryNoJar
Description copied from interface:SpincastUtils
If the project is not running from an executable .jar file, this will return the root directory of the project on the file system.- Specified by:
getAppRootDirectoryNoJar
in interfaceSpincastUtils
- Returns:
- the directory path or
null
if the application is running from an executable .jar file.
-
getSpincastCurrentVersion
Description copied from interface:SpincastUtils
Gets the current Spincast version.- Specified by:
getSpincastCurrentVersion
in interfaceSpincastUtils
-
getCurrentVersionFromPom
-
zipDirectory
Description copied from interface:SpincastUtils
Zips a directory.- Specified by:
zipDirectory
in interfaceSpincastUtils
targetZipFile
- the target .zip file. If the parent directories don't exist, tries to create them.
-
zipExtract
Description copied from interface:SpincastUtils
Extracts a .zip file to the specified directory.- Specified by:
zipExtract
in interfaceSpincastUtils
targetDir
- The target directory. If it doesn't exist, tried to create it (and its parents, if required).
-
getCacheBusterCode
Description copied from interface:SpincastUtils
The current cache buster to use.This should probably change each time the application is restarted or at least redeployed.
It should also be in such a format that it's possible to remove it from a given text.
This must be kept in sync with
removeCacheBusterCode
!- Specified by:
getCacheBusterCode
in interfaceSpincastUtils
-
generateCacheBusterCode
-
isAlwaysChangeCacheBusterCodeInDevMode
protected boolean isAlwaysChangeCacheBusterCodeInDevMode() -
removeCacheBusterCodes
Description copied from interface:SpincastUtils
Removes the cache buster code occurences from the given text.Note that this won't simply remove the current cache busting code, it will remove any valid cache busting code... This is what we want since we don't want a client sending a request containing an old cache busting code to break!
This must be kept in sync with
getCacheBusterCode
!- Specified by:
removeCacheBusterCodes
in interfaceSpincastUtils
-
readClasspathFile
Description copied from interface:SpincastUtils
Reads a file on the classpath and returns it as a String.Paths are always considered from the root at the classpath. You can start the path with a "/" or not, it makes no difference.
Uses the UTF-8 encoding.
- Specified by:
readClasspathFile
in interfaceSpincastUtils
- Returns:
- the content of the file or
null
if not found.
-
readClasspathFile
Description copied from interface:SpincastUtils
Reads a file on the classpath and returns it as a String.Paths are always considered from the root at the classpath. You can start the path with a "/" or not, it makes no difference.
- Specified by:
readClasspathFile
in interfaceSpincastUtils
- Returns:
- the content of the file or
null
if not found.
-
getClasspathInputStream
Description copied from interface:SpincastUtils
Gets theInputStream
from a resource on the classpath.IMPORTANT : the code is responsible to close the inputstream!
Also note that when an application is running from an executable .jar, you must use this instead of trying to get a
File
from the resource!- Specified by:
getClasspathInputStream
in interfaceSpincastUtils
- Returns:
- the
InputStream
ornull
if the resource is not found.
-
copyClasspathFileToFileSystem
Description copied from interface:SpincastUtils
Copy a file from the classpath (located in a .jar or not) to the specified file system file.If the target file already exists, if will be overwritten.
- Specified by:
copyClasspathFileToFileSystem
in interfaceSpincastUtils
-
copyClasspathDirToFileSystem
Description copied from interface:SpincastUtils
Copy a directory from the classpath (located in a .jar or not) to the specified file system directory.If the target directory already exists, if will be overwritten.
- Specified by:
copyClasspathDirToFileSystem
in interfaceSpincastUtils
-
copyJarDirToFileSystem
-
copyJarFileToFileSystem
-
isContainsSpecialCharacters
Description copied from interface:SpincastUtils
Validate if a String contains characters below 31 or DEL (127).- Specified by:
isContainsSpecialCharacters
in interfaceSpincastUtils
- Returns:
true
if the String contains characters below 31 (or DEL (127). If the String isnull
,false
is returned.
-
inQuotesStringFormat
Description copied from interface:SpincastUtils
Format a String so it can be outputed in a HTML page, as a one line javascript string or the attribute of an HTML element.- Specified by:
inQuotesStringFormat
in interfaceSpincastUtils
singleQuotes
- iffalse
, double quotes are expected as the string delimiter.
-
enumValueOfInsensitive
Description copied from interface:SpincastUtils
Perform a case insensitivevalueOf(value)
search on an enum.- Specified by:
enumValueOfInsensitive
in interfaceSpincastUtils
-
convertLongSetToLongPrimitiveArray
- Specified by:
convertLongSetToLongPrimitiveArray
in interfaceSpincastUtils
-
createTempFilePath
Description copied from interface:SpincastUtils
Creates a randomly named temp file path in the temporary writable directory.- Specified by:
createTempFilePath
in interfaceSpincastUtils
-
basicHtml
Description copied from interface:SpincastUtils
Cleans HTML, only allowing basic elements.Elements from https://jsoup.org/apidocs/org/jsoup/safety/Whitelist.html#basic-- are allowed + "center" tags. Relative paths are allowed in links.
Images tags are not allowed.
- Specified by:
basicHtml
in interfaceSpincastUtils
-
basicHtml
Description copied from interface:SpincastUtils
Cleans HTML, only allowing basic elements.Elements from https://jsoup.org/apidocs/org/jsoup/safety/Whitelist.html#basic-- are allowed + "center" tags. Relative paths are allowed in links.
- Specified by:
basicHtml
in interfaceSpincastUtils
allowImages
- iftrue
image tags will also be allowed.
-
getRandomElement
Description copied from interface:SpincastUtils
Returns a random element from the set ornull
if the set is null or empty.- Specified by:
getRandomElement
in interfaceSpincastUtils
-
getQuerystringFromUrl
Description copied from interface:SpincastUtils
Return the querystring of an URL, without leading "?". Returns an empty string if there is no querystring.The querystring returned is NOT decoded using
URLDecoder.decode
).- Specified by:
getQuerystringFromUrl
in interfaceSpincastUtils
-
getQuerystringParametersFromUrl
Description copied from interface:SpincastUtils
Parses an URL and returns the list of its querystring parameters, if any.The querystring will be decoded using (using
URLDecoder.decode
).- Specified by:
getQuerystringParametersFromUrl
in interfaceSpincastUtils
-
getParametersFromQuerystring
public Map<String,List<String>> getParametersFromQuerystring(String qs, boolean decodeQueryStringFirst) Description copied from interface:SpincastUtils
Parses a querystring (starting with a "?" or not) and returns the list of parameters.- Specified by:
getParametersFromQuerystring
in interfaceSpincastUtils
decodeQueryStringFirst
- iftrue
, the querystring will be decoded (usingURLDecoder.decode
) prior to being parsed.
-
isRequestedResourceNameEndsWithBeforeExtension
Description copied from interface:SpincastUtils
Returnstrue
if the name of the requested resource ends with the specified suffix. The name is the part before the extension, if there is one.Manage the fact that routing may be case sensitive or not.
Note that you should specify a "-" if your suffixe starts with this.
- Specified by:
isRequestedResourceNameEndsWithBeforeExtension
in interfaceSpincastUtils
-
isRequestedResourceNameEndsWithBeforeExtension
public boolean isRequestedResourceNameEndsWithBeforeExtension(String resourceBaseName, String suffix) Description copied from interface:SpincastUtils
Returnstrue
if the name of the requested resource ends with the specified suffix. The name is the part before the extension, if there is one.Manage the fact that routing may be case sensitive or not.
Note that you should specify a "-" if your suffixe starts with this.
- Specified by:
isRequestedResourceNameEndsWithBeforeExtension
in interfaceSpincastUtils
- Parameters:
resourceBaseName
- the base name of the requested resource, as it would be returned byFilenameUtils.getBaseName(String)
, without the potential extension.
-
convertToFriendlyToken
Description copied from interface:SpincastUtils
Convert a string so it can be used in an URL without being escaped: remove accents, spaces, etc.This can be used to create "friendly token" in an SEO optimized URL. It can also be used to create a human friendly file name from a random string.
Be careful if you plan on using the result of this methos as an unique token since many strings may result in the same thing!
- Specified by:
convertToFriendlyToken
in interfaceSpincastUtils
- Returns:
- the string with only A-Z, "-" and "_" characters. Or, if
the resulting string is empty, a random
UUID
will be returned.
-
convertToFriendlyToken
Description copied from interface:SpincastUtils
Convert a string so it can be used in an URL without being escaped: remove accents, spaces, etc.This can be used to create "friendly token" in an SEO optimized URL. It can also be used to create a human friendly file name from a random string.
Be careful if you plan on using the result of this method as an unique token since many strings may result in the same thing!
- Specified by:
convertToFriendlyToken
in interfaceSpincastUtils
- Returns:
- the string with only A-Z, "-" and "_" characters. Or, if
the resulting string is empty, the
resultIfEmpty
param is returned.
-
isPortOpen
Description copied from interface:SpincastUtils
Validate if a port is open and connectable.- Specified by:
isPortOpen
in interfaceSpincastUtils
-
clearDirectory
Description copied from interface:SpincastUtils
Empty a directory if it exists.- Specified by:
clearDirectory
in interfaceSpincastUtils
-
sortMapByValues
Description copied from interface:SpincastUtils
Sort a Map by value- Specified by:
sortMapByValues
in interfaceSpincastUtils
-