Interface SpincastUtils

All Known Implementing Classes:
SpincastUtilsDefault

public interface SpincastUtils
Spincast utilities.
  • Method Details

    • zipDirectory

      void zipDirectory(File directoryToZip, File targetZipFile, boolean includeDirItself)
      Zips a directory.
      Parameters:
      targetZipFile - the target .zip file. If the parent directories don't exist, tries to create them.
      If - true, the directory itself will be included in the zip file, otherwise only its content will be.
    • zipExtract

      void zipExtract(File zipFile, File targetDir)
      Extracts a .zip file to the specified directory.
      Parameters:
      targetDir - The target directory. If it doesn't exist, tried to create it (and its parents, if required).
    • getMimeTypeFromMultipleSources

      String getMimeTypeFromMultipleSources(String responseContentTypeHeader, String resourcePath, String requestPath)
      Gets the mime type using multiple sources of information.
      Parameters:
      contentTypeHeader - an already existing Content-Type header on the response. Can be null.
      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

      String getMimeTypeFromPath(String path)
      Gets the mime type from a path, using its extension.
      Returns:
      the mime type or null if it can't be decided.
    • getMimeTypeFromExtension

      String getMimeTypeFromExtension(String extension)
      Gets the mime type from the extension.
      Returns:
      the mime type or null if it can't be decided.
    • getLocaleBestMatchFromAcceptLanguageHeader

      Locale getLocaleBestMatchFromAcceptLanguageHeader(String acceptLanguageHeader)
      Gets the best Locale to use given a "Accept-Language" HTTP header.
      Returns:
      the best Locale to use or null if the given header can't be parsed.
    • isContentTypeToSkipGziping

      boolean isContentTypeToSkipGziping(String contentType)
      Should the specified Content-Type be gzipped?
    • isClasspathResourceLoadedFromJar

      boolean isClasspathResourceLoadedFromJar(String resourcePath)
      Is the classpath resource in a .jar file? Otherwise it is on the file system.
      Throws:
      an - exception if the resource doesn't exist.
    • isClassLoadedFromJar

      boolean isClassLoadedFromJar(Class<?> clazz)
      Was the specified Class loaded from a .jar file?
    • getClassLocationDirOrJarFile

      File getClassLocationDirOrJarFile(Class<?> clazz)
      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 isClassLoadedFromJar(Class) to know if the class was loaded from a .jar file or not.

    • isRunningFromExecutableJar

      boolean isRunningFromExecutableJar()
      Is the current code running from an executable .jar? An executable .jar is a jar with Main-Class in its META-INF/MANIFEST.MF.
    • getAppJarDirectory

      File getAppJarDirectory()
      If the project is running from an executable .jar file, this will return the directory containing this .jar file.
      Returns:
      the directory path or null if the application is not running from an executable .jar file.
    • getAppRootDirectoryNoJar

      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.
      Returns:
      the directory path or null if the application is running from an executable .jar file.
    • getSpincastCurrentVersion

      String getSpincastCurrentVersion()
      Gets the current Spincast version.
    • getCacheBusterCode

      String getCacheBusterCode()
      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!

    • removeCacheBusterCodes

      String removeCacheBusterCodes(String text)
      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!

    • readClasspathFile

      String readClasspathFile(String path)
      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.

      Returns:
      the content of the file or null if not found.
    • readClasspathFile

      String readClasspathFile(String path, String encoding)
      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.

      Returns:
      the content of the file or null if not found.
    • getClasspathInputStream

      InputStream getClasspathInputStream(String classpathPath)
      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!

      Returns:
      the InputStream or null if the resource is not found.
    • copyClasspathFileToFileSystem

      void copyClasspathFileToFileSystem(String classpathFilePath, File targetFile)
      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.

    • copyClasspathDirToFileSystem

      void copyClasspathDirToFileSystem(String classpathDirPath, File targetDir)
      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.

    • isContainsSpecialCharacters

      boolean isContainsSpecialCharacters(String str)
      Validate if a String contains characters below 31 or DEL (127).
      Returns:
      true if the String contains characters below 31 (or DEL (127). If the String is null, false is returned.
    • inQuotesStringFormat

      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.
      Parameters:
      singleQuotes - if false, double quotes are expected as the string delimiter.
    • enumValueOfInsensitive

      <T extends Enum<?>> T enumValueOfInsensitive(Class<T> enumClass, String str)
      Perform a case insensitive valueOf(value) search on an enum.
    • convertLongSetToLongPrimitiveArray

      long[] convertLongSetToLongPrimitiveArray(Set<Long> longSet)
    • createTempFilePath

      String createTempFilePath()
      Creates a randomly named temp file path in the temporary writable directory.
    • basicHtml

      String basicHtml(boolean newlineToBrFirst, String html)
      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.

    • basicHtml

      String basicHtml(boolean newlineToBrFirst, String html, boolean allowImages)
      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.

      Parameters:
      allowImages - if true image tags will also be allowed.
    • getRandomElement

      <T> T getRandomElement(Set<T> set)
      Returns a random element from the set or null if the set is null or empty.
    • getQuerystringFromUrl

      String getQuerystringFromUrl(String url)
      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).

    • getQuerystringParametersFromUrl

      Map<String,List<String>> getQuerystringParametersFromUrl(String url)
      Parses an URL and returns the list of its querystring parameters, if any.

      The querystring will be decoded using (using URLDecoder.decode).

    • getParametersFromQuerystring

      Map<String,List<String>> getParametersFromQuerystring(String querystring, boolean decodeQueryStringFirst)
      Parses a querystring (starting with a "?" or not) and returns the list of parameters.
      Parameters:
      decodeQueryStringFirst - if true, the querystring will be decoded (using URLDecoder.decode) prior to being parsed.
    • isRequestedResourceNameEndsWithBeforeExtension

      boolean isRequestedResourceNameEndsWithBeforeExtension(URI currentURI, String suffix)
      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.

    • isRequestedResourceNameEndsWithBeforeExtension

      boolean isRequestedResourceNameEndsWithBeforeExtension(String resourceBaseName, String suffix)
      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.

      Parameters:
      resourceBaseName - the base name of the requested resource, as it would be returned by FilenameUtils.getBaseName(String), without the potential extension.
    • convertToFriendlyToken

      String convertToFriendlyToken(String str)
      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!

      Returns:
      the string with only A-Z, "-" and "_" characters. Or, if the resulting string is empty, a random UUID will be returned.
    • convertToFriendlyToken

      String convertToFriendlyToken(String str, String resultIfEmpty)
      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!

      Returns:
      the string with only A-Z, "-" and "_" characters. Or, if the resulting string is empty, the resultIfEmpty param is returned.
    • isPortOpen

      boolean isPortOpen(String host, int port)
      Validate if a port is open and connectable.
    • clearDirectory

      void clearDirectory(File dir)
      Empty a directory if it exists.
    • sortMapByValues

      <K, V> LinkedHashMap<K,V> sortMapByValues(Map<K,V> map)
      Sort a Map by value