public class SpincastUtilsDefault extends Object implements SpincastUtils
| Modifier and Type | Field and Description |
|---|---|
protected static org.slf4j.Logger |
logger |
| Constructor and Description |
|---|
SpincastUtilsDefault(SpincastConfig spincastConfig) |
| Modifier and Type | Method and Description |
|---|---|
String |
basicHtml(boolean newlineToBrFirst,
String html)
Cleans HTML, only allowing basic elements.
|
String |
basicHtml(boolean newlineToBrFirst,
String html,
boolean allowImages)
Cleans HTML, only allowing basic elements.
|
long[] |
convertLongSetToLongPrimitiveArray(Set<Long> longSet) |
String |
convertToUrlToken(String str)
Convert a string so it can be used in an URL without
being escaped: remove accents, spaces, etc.
|
String |
convertToUrlToken(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) |
String |
createTempFilePath()
Creates a randomly named temp file path targetting the
writable directory.
|
<T extends Enum<?>> |
enumValueOfInsensitive(Class<T> enumClass,
String str)
Perform a case insensitive
valueOf(value) search on an enum. |
protected String |
generateCacheBusterCode() |
File |
getAppJarDirectory()
If the project is running from an executable
.jar file, this will return the directory containing
this .jar file.
|
File |
getAppRootDirectoryNoJar()
If the project is not running from an executable
.jar file, this will return the root directory of the
project on the file system.
|
String |
getCacheBusterCode()
The current cache buster to use.
|
InputStream |
getClasspathInputStream(String classpathPath)
Gets the
InputStream from a resource on the classpath. |
protected String |
getCurrentVersionFromPom() |
protected Object |
getFileSystemLock(URI jarDirUri) |
protected Map<String,Object> |
getFileSystemsLocks() |
protected File |
getGeneratedTempFilesDir() |
Locale |
getLocaleBestMatchFromAcceptLanguageHeader(String header)
Gets the best Locale to use given a "Accept-Language" HTTP header.
|
String |
getMimeTypeFromExtension(String extension)
Gets the
mime type from the extension. |
String |
getMimeTypeFromMultipleSources(String responseContentTypeHeader,
String resourcePath,
String requestPath)
Gets the
mime type using multiple sources of information. |
String |
getMimeTypeFromPath(String path)
Gets the
mime type from a path, using its extension. |
Map<String,List<String>> |
getParametersFromQuerystring(String qs,
boolean decodeQueryStringFirst)
Parses a querystring (starting with a "?" or not) and returns
the list of parameters.
|
String |
getQuerystringFromUrl(String url)
Return the querystring of an URL, without
leading "?".
|
Map<String,List<String>> |
getQuerystringParametersFromUrl(String url)
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 or
null if the set is null or empty. |
protected SpincastConfig |
getSpincastConfig() |
String |
getSpincastCurrentVersion()
Gets the current Spincast version.
|
String |
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 |
isAlwaysChangeCacheBusterCodeInDevMode() |
boolean |
isClasspathResourceInJar(String resourcePath)
Is the classpath resource in a .jar file? Otherwise
it is on the file system.
|
boolean |
isContainsSpecialCharacters(String str)
Valid of a String only contains characters over 31 (or
DEL (127).
|
boolean |
isContentTypeToSkipGziping(String contentType)
Should the specified
Content-Type be gzipped? |
boolean |
isRequestedResourceNameEndsWithBeforeExtension(String resourceBaseName,
String suffix)
Returns
true if the name of the requested
resource ends with the specified suffix. |
boolean |
isRequestedResourceNameEndsWithBeforeExtension(URI currentURI,
String suffix)
Returns
true if the name of the requested
resource ends with the specified suffix. |
boolean |
isRunningFromExecutableJar()
Is the application currently running from
an executable .jar? An executable .jar is a
jar with
Main-Class in its
META-INF/MANIFEST.MF. |
String |
readClasspathFile(String path)
Reads a file on the classpath and returns it as a
String.
|
String |
readClasspathFile(String path,
String encoding)
Reads a file on the classpath and returns it as a
String.
|
String |
removeCacheBusterCodes(String text)
Removes the cache buster code occurences from the
given text.
|
void |
zipDirectory(File directoryToZip,
File targetZipFile,
boolean includeDirItself)
Zips a directory.
|
void |
zipExtract(File zipFile,
File targetDir)
Extracts a .zip file to the specified directory.
|
@Inject public SpincastUtilsDefault(SpincastConfig spincastConfig)
protected SpincastConfig getSpincastConfig()
protected File getGeneratedTempFilesDir()
public boolean isContentTypeToSkipGziping(String contentType)
SpincastUtilsContent-Type be gzipped?isContentTypeToSkipGziping in interface SpincastUtilspublic String getMimeTypeFromMultipleSources(String responseContentTypeHeader, String resourcePath, String requestPath)
SpincastUtilsmime type using multiple sources of information.getMimeTypeFromMultipleSources in interface SpincastUtilsresourcePath - the path (absolute or relative) to the target resource. Can be null.requestPath - the path of the current request. Can be null.mime type or null if it can't be
decided.public String getMimeTypeFromPath(String path)
SpincastUtilsmime type from a path, using its extension.getMimeTypeFromPath in interface SpincastUtilsmime type or null if it can't be
decided.public String getMimeTypeFromExtension(String extension)
SpincastUtilsmime type from the extension.getMimeTypeFromExtension in interface SpincastUtilsmime type or null if it can't be
decided.public Locale getLocaleBestMatchFromAcceptLanguageHeader(String header)
SpincastUtilsgetLocaleBestMatchFromAcceptLanguageHeader in interface SpincastUtilsnull
if the given header can't be parsed.public boolean isClasspathResourceInJar(String resourcePath)
SpincastUtilsisClasspathResourceInJar in interface SpincastUtilspublic File getAppJarDirectory()
SpincastUtilsgetAppJarDirectory in interface SpincastUtilsnull if the
application is not running from an executable .jar
file.public boolean isRunningFromExecutableJar()
SpincastUtilsMain-Class in its
META-INF/MANIFEST.MF.isRunningFromExecutableJar in interface SpincastUtilspublic File getAppRootDirectoryNoJar()
SpincastUtilsgetAppRootDirectoryNoJar in interface SpincastUtilsnull if the
application is running from an executable .jar
file.public String getSpincastCurrentVersion()
SpincastUtilsgetSpincastCurrentVersion in interface SpincastUtilsprotected String getCurrentVersionFromPom()
public void zipDirectory(File directoryToZip, File targetZipFile, boolean includeDirItself)
SpincastUtilszipDirectory in interface SpincastUtilstargetZipFile - the target .zip file. If the parent directories don't
exist, tries to create them.public void zipExtract(File zipFile, File targetDir)
SpincastUtilszipExtract in interface SpincastUtilstargetDir - The target directory. If it doesn't exist, tried to
create it (and its parents, if required).public String getCacheBusterCode()
SpincastUtilsThis 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!
getCacheBusterCode in interface SpincastUtilsprotected String generateCacheBusterCode()
protected boolean isAlwaysChangeCacheBusterCodeInDevMode()
public String removeCacheBusterCodes(String text)
SpincastUtilsNote 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!
removeCacheBusterCodes in interface SpincastUtilspublic String readClasspathFile(String path)
SpincastUtilsPaths 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.
readClasspathFile in interface SpincastUtilsnull
if not found.public String readClasspathFile(String path, String encoding)
SpincastUtilsPaths are always considered from the root at the classpath. You can start the path with a "/" or not, it makes no difference.
readClasspathFile in interface SpincastUtilsnull
if not found.public InputStream getClasspathInputStream(String classpathPath)
SpincastUtilsInputStream 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!
getClasspathInputStream in interface SpincastUtilsInputStream or null if the resource is
not found.public void copyClasspathFileToFileSystem(String classpathFilePath, File fileSystemFile)
SpincastUtilsIf the target file already exists, if will be overwritten.
copyClasspathFileToFileSystem in interface SpincastUtilspublic void copyClasspathDirToFileSystem(String classpathDirPath, File targetDir)
SpincastUtilsIf the target directory already exists, if will be overwritten.
copyClasspathDirToFileSystem in interface SpincastUtilspublic boolean isContainsSpecialCharacters(String str)
SpincastUtilsisContainsSpecialCharacters in interface SpincastUtilstrue if the String only contains
characters over 31 (or DEL (127). If the String is
null, true is returned.public String inQuotesStringFormat(String str, boolean singleQuotes)
SpincastUtilsinQuotesStringFormat in interface SpincastUtilssingleQuotes - if false, double quotes are
expected as the string delimiter.public <T extends Enum<?>> T enumValueOfInsensitive(Class<T> enumClass, String str)
SpincastUtilsvalueOf(value) search on an enum.enumValueOfInsensitive in interface SpincastUtilspublic long[] convertLongSetToLongPrimitiveArray(Set<Long> longSet)
convertLongSetToLongPrimitiveArray in interface SpincastUtilspublic String createTempFilePath()
SpincastUtilscreateTempFilePath in interface SpincastUtilspublic String basicHtml(boolean newlineToBrFirst, String html)
SpincastUtilsElements 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.
basicHtml in interface SpincastUtilspublic String basicHtml(boolean newlineToBrFirst, String html, boolean allowImages)
SpincastUtilsElements from https://jsoup.org/apidocs/org/jsoup/safety/Whitelist.html#basic-- are allowed + "center" tags. Relative paths are allowed in links.
basicHtml in interface SpincastUtilsallowImages - if true image tags will also be allowed.public <T> T getRandomElement(Set<T> set)
SpincastUtilsnull if the set is null or empty.getRandomElement in interface SpincastUtilspublic String getQuerystringFromUrl(String url)
SpincastUtils
The querystring returned is NOT decoded using
URLDecoder.decode).
getQuerystringFromUrl in interface SpincastUtilspublic Map<String,List<String>> getQuerystringParametersFromUrl(String url)
SpincastUtils
The querystring will be decoded using (using URLDecoder.decode).
getQuerystringParametersFromUrl in interface SpincastUtilspublic Map<String,List<String>> getParametersFromQuerystring(String qs, boolean decodeQueryStringFirst)
SpincastUtilsgetParametersFromQuerystring in interface SpincastUtilsdecodeQueryStringFirst - if true, the querystring will be
decoded (using URLDecoder.decode) prior to being parsed.public boolean isRequestedResourceNameEndsWithBeforeExtension(URI currentURI, String suffix)
SpincastUtilstrue 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.
isRequestedResourceNameEndsWithBeforeExtension in interface SpincastUtilspublic boolean isRequestedResourceNameEndsWithBeforeExtension(String resourceBaseName, String suffix)
SpincastUtilstrue 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.
isRequestedResourceNameEndsWithBeforeExtension in interface SpincastUtilsresourceBaseName - the base name of the requested resource, as
it would be returned by FilenameUtils.getBaseName(String),
without the potential extension.public String convertToUrlToken(String str)
SpincastUtilsThis can be used to create "friendly token" in an SEO optimized URL.
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!
convertToUrlToken in interface SpincastUtilsUUID will be returned.public String convertToUrlToken(String str, String resultIfEmpty)
SpincastUtilsThis can be used to create "friendly token" in an SEO optimized URL.
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!
convertToUrlToken in interface SpincastUtilsresultIfEmpty param
is returned.Copyright © 2019. All rights reserved.