public.javadoc.org.spincast.core.utils.SpincastUtilsDefault.html Maven / Gradle / Ivy
Show all versions of spincast-website Show documentation
SpincastUtilsDefault (org.spincast:spincast-framework 1.0.0 API)
org.spincast.core.utils
Class SpincastUtilsDefault
- java.lang.Object
-
- org.spincast.core.utils.SpincastUtilsDefault
-
- All Implemented Interfaces:
- SpincastUtils
public class SpincastUtilsDefault
extends Object
implements SpincastUtils
Implementations of the Spincast utilities.
-
-
Field Summary
Fields
Modifier and Type
Field and Description
protected org.slf4j.Logger
logger
-
Constructor Summary
Constructors
Constructor and Description
SpincastUtilsDefault(SpincastConfig spincastConfig)
-
Method Summary
All Methods Instance Methods Concrete Methods
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
createTempFilePath()
Creates a randomly named temp file path targetting the
writable directory.
<T extends Enum<?>>
T
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 cache buster to use.
InputStream
getClasspathInputStream(String classpathPath)
Gets the InputStream
from a resource on the classpath.
protected String
getCurrentVersionFromPom()
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
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.
-
-
Constructor Detail
-
SpincastUtilsDefault
@Inject
public SpincastUtilsDefault(SpincastConfig spincastConfig)
-
Method Detail
-
getSpincastConfig
protected SpincastConfig getSpincastConfig()
-
getGeneratedTempFilesDir
protected File getGeneratedTempFilesDir()
-
isContentTypeToSkipGziping
public boolean isContentTypeToSkipGziping(String contentType)
Description copied from interface: SpincastUtils
Should the specified Content-Type
be gzipped?
- Specified by:
isContentTypeToSkipGziping
in interface SpincastUtils
-
getMimeTypeFromMultipleSources
public String getMimeTypeFromMultipleSources(String responseContentTypeHeader,
String resourcePath,
String requestPath)
Description copied from interface: SpincastUtils
Gets the mime type
using multiple sources of information.
- Specified by:
getMimeTypeFromMultipleSources
in interface SpincastUtils
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
or null
if it can't be
decided.
-
getMimeTypeFromPath
public String getMimeTypeFromPath(String path)
Description copied from interface: SpincastUtils
Gets the mime type
from a path, using its extension.
- Specified by:
getMimeTypeFromPath
in interface SpincastUtils
- Returns:
- the
mime type
or null
if it can't be
decided.
-
getMimeTypeFromExtension
public String getMimeTypeFromExtension(String extension)
Description copied from interface: SpincastUtils
Gets the mime type
from the extension.
- Specified by:
getMimeTypeFromExtension
in interface SpincastUtils
- Returns:
- the
mime type
or null
if it can't be
decided.
-
getLocaleBestMatchFromAcceptLanguageHeader
public Locale getLocaleBestMatchFromAcceptLanguageHeader(String header)
Description copied from interface: SpincastUtils
Gets the best Locale to use given a "Accept-Language" HTTP header.
- Specified by:
getLocaleBestMatchFromAcceptLanguageHeader
in interface SpincastUtils
- Returns:
- the best Locale to use or
null
if the given header can't be parsed.
-
getAppJarDirectory
public File 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 interface SpincastUtils
- Returns:
- the directory path or
null
if the
application is not running from an executable .jar
file.
-
isRunningFromExecutableJar
public boolean isRunningFromExecutableJar()
Description copied from interface: SpincastUtils
Is the application currently running from
an executable .jar? An executable .jar is a
jar with Main-Class
in its
META-INF/MANIFEST.MF
.
- Specified by:
isRunningFromExecutableJar
in interface SpincastUtils
-
getAppRootDirectoryNoJar
public File 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 interface SpincastUtils
- Returns:
- the directory path or
null
if the
application is running from an executable .jar
file.
-
getSpincastCurrentVersion
public String getSpincastCurrentVersion()
Description copied from interface: SpincastUtils
Gets the current Spincast version.
- Specified by:
getSpincastCurrentVersion
in interface SpincastUtils
-
getCurrentVersionFromPom
protected String getCurrentVersionFromPom()
-
zipDirectory
public void zipDirectory(File directoryToZip,
File targetZipFile,
boolean includeDirItself)
Description copied from interface: SpincastUtils
Zips a directory.
- Specified by:
zipDirectory
in interface SpincastUtils
targetZipFile
- the target .zip file. If the parent directories don't
exist, tries to create them.
-
zipExtract
public void zipExtract(File zipFile,
File targetDir)
Description copied from interface: SpincastUtils
Extracts a .zip file to the specified directory.
- Specified by:
zipExtract
in interface SpincastUtils
targetDir
- The target directory. If it doesn't exist, tried to
create it (and its parents, if required).
-
getCacheBusterCode
public String getCacheBusterCode()
Description copied from interface: SpincastUtils
The 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 interface SpincastUtils
-
generateCacheBusterCode
protected String generateCacheBusterCode()
-
isAlwaysChangeCacheBusterCodeInDevMode
protected boolean isAlwaysChangeCacheBusterCodeInDevMode()
-
removeCacheBusterCodes
public String removeCacheBusterCodes(String text)
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 interface SpincastUtils
-
readClasspathFile
public String readClasspathFile(String path)
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 interface SpincastUtils
- Returns:
- the content of the file or
null
if not found.
-
readClasspathFile
public String readClasspathFile(String path,
String encoding)
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 interface SpincastUtils
- Returns:
- the content of the file or
null
if not found.
-
getClasspathInputStream
public InputStream getClasspathInputStream(String classpathPath)
Description copied from interface: SpincastUtils
Gets the InputStream
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 interface SpincastUtils
- Returns:
- the
InputStream
or null
if the resource is
not found.
-
isContainsSpecialCharacters
public boolean isContainsSpecialCharacters(String str)
Description copied from interface: SpincastUtils
Valid of a String only contains characters over 31 (or
DEL (127).
- Specified by:
isContainsSpecialCharacters
in interface SpincastUtils
- Returns:
true
if the String only contains
characters over 31 (or DEL (127). If the String is
null
, true
is returned.
-
inQuotesStringFormat
public String inQuotesStringFormat(String str,
boolean singleQuotes)
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 interface SpincastUtils
singleQuotes
- if false
, double quotes are
expected as the string delimiter.
-
enumValueOfInsensitive
public <T extends Enum<?>> T enumValueOfInsensitive(Class<T> enumClass,
String str)
Description copied from interface: SpincastUtils
Perform a case insensitive
valueOf(value)
search on an enum.
- Specified by:
enumValueOfInsensitive
in interface SpincastUtils
-
convertLongSetToLongPrimitiveArray
public long[] convertLongSetToLongPrimitiveArray(Set<Long> longSet)
- Specified by:
convertLongSetToLongPrimitiveArray
in interface SpincastUtils
-
createTempFilePath
public String createTempFilePath()
Description copied from interface: SpincastUtils
Creates a randomly named temp file path targetting the
writable directory.
- Specified by:
createTempFilePath
in interface SpincastUtils
-
basicHtml
public String basicHtml(boolean newlineToBrFirst,
String html)
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 interface SpincastUtils
-
basicHtml
public String basicHtml(boolean newlineToBrFirst,
String html,
boolean allowImages)
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 interface SpincastUtils
allowImages
- if true
image tags will also be allowed.
-
getRandomElement
public <T> T getRandomElement(Set<T> set)
Description copied from interface: SpincastUtils
Returns a random element from the set or
null
if the set is null or empty.
- Specified by:
getRandomElement
in interface SpincastUtils
-
getQuerystringFromUrl
public String getQuerystringFromUrl(String url)
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 interface SpincastUtils
-
getQuerystringParametersFromUrl
public Map<String,List<String>> getQuerystringParametersFromUrl(String url)
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 interface SpincastUtils
-
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 interface SpincastUtils
decodeQueryStringFirst
- if true
, the querystring will be
decoded (using URLDecoder.decode
) prior to being parsed.
-
isRequestedResourceNameEndsWithBeforeExtension
public boolean isRequestedResourceNameEndsWithBeforeExtension(URI currentURI,
String suffix)
Description copied from interface: SpincastUtils
Returns true
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 interface SpincastUtils
-
isRequestedResourceNameEndsWithBeforeExtension
public boolean isRequestedResourceNameEndsWithBeforeExtension(String resourceBaseName,
String suffix)
Description copied from interface: SpincastUtils
Returns true
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 interface SpincastUtils
- Parameters:
resourceBaseName
- the base name of the requested resource, as
it would be returned by FilenameUtils.getBaseName(String)
,
without the potential extension.
Copyright © 2019. All rights reserved.