Class CoreUtils

java.lang.Object
com.avoka.core.util.CoreUtils
All Implemented Interfaces:
Serializable

public class CoreUtils extends Object implements Serializable
Provides FC core utility methods.
See Also:
  • Constructor Details

    • CoreUtils

      public CoreUtils()
  • Method Details

    • close

      public static void close(Closeable closeable)
      Close the given closeable resource and ignore any exceptions thrown.
      Parameters:
      closeable - the resource to close
      Since:
      4.2.0
    • encodeStringAsBytes

      public static byte[] encodeStringAsBytes(String value)
      Return an encoded version of the String value. The value will be serialized and ZLib compressed.
      Parameters:
      value - the string value to encode
      Returns:
      a serialized and compressed byte array
      Throws:
      IllegalArgumentException - if the value parameter is null
    • decodeBytesAsString

      public static String decodeBytesAsString(byte[] byteData)
      Return an string from the decoded byte array.
      Parameters:
      byteData - the encoded string byte data
      Returns:
      an String value from the encoded byte data
    • parseDocumentFromString

      @Deprecated public Document parseDocumentFromString(String xml, boolean namespaceAware, boolean validating) throws ParserConfigurationException, SAXException
      Deprecated.
      - see XMLUtils instead
      This does the same as DOMUtil.parseDocumentFromString()
      Parameters:
      xml - the XML string content
      namespaceAware - whether to parse the document to aware the namespace
      validating - whether to validate the document
      Returns:
      a new XML Document fromt he given XML string
      Throws:
      ParserConfigurationException - throw a exception if there is a configuration error
      SAXException - throw a exception if there is a parse error
    • toBase64

      public static String toBase64(byte[] byteData)
      Return a Base 64 encoded string from the given byte array.
      Parameters:
      byteData - the byte array data to encode
      Returns:
      a base64 encoded byte array
    • decodeBase64

      public static String decodeBase64(String value)
      Return a Base 64 decoded string from the given encoded value.
      Parameters:
      value - the value to encode
      Returns:
      the decoded string value
    • toMD5Hash

      public static String toMD5Hash(String plaintext)
      Return an 32 char MD5 encoded string from the given plain text. The returned value is MD5 hash compatible with Tomcat catalina Realm.

      Adapted from org.apache.catalina.util.MD5Encoder

      Parameters:
      plaintext - the plain text value to encode
      Returns:
      encoded MD5 string
    • toMD5Hash

      public static String toMD5Hash(byte[] bytes)
      Return an 32 char MD5 encoded string from the given byte array. The returned value is MD5 hash compatible with Tomcat catalina Realm.

      Adapted from org.apache.catalina.util.MD5Encoder

      Parameters:
      bytes - the bytes array to encode
      Returns:
      encoded MD5 string
    • createAltKey

      public static String createAltKey()
      Creates an 32 char alternative key using MD5 encoding with random text
      Returns:
      an 32 char MD5 encoded string
    • limitLength

      public static String limitLength(String value, int maxlength)
      Return the value string limited to maxlength characters. If the string gets curtailed, "..." is appended to it.

      Adapted from Velocity Tools Formatter.

      Parameters:
      value - the string value to limit the length of
      maxlength - the maximum string length
      Returns:
      a length limited string
    • limitLength

      public static String limitLength(String value, int maxlength, String suffix)
      Return the value string limited to maxlength characters. If the string gets curtailed and the suffix parameter is appended to it.

      Adapted from Velocity Tools Formatter.

      Parameters:
      value - the string value to limit the length of
      maxlength - the maximum string length
      suffix - the suffix to append to the length limited string
      Returns:
      a length limited string
    • encrypt

      public static String encrypt(String plaintext)
      Encrypt the given plain text value
      Parameters:
      plaintext - the plain text value to encrypt
      Returns:
      the cypher text for the given plain text value.
    • decrypt

      public static String decrypt(String cyphertext)
      Decrypt the given cyphertext value.
      Parameters:
      cyphertext - the cyper text value to decrypt (required)
      Returns:
      the cypher text for the given plain text value.
    • decryptSafely

      public static String decryptSafely(String cyphertext)
      Decrypt the given cyphertext value without throwing an error if null is passed in
      Parameters:
      cyphertext - the cypher text value to decrypt
      Returns:
      the decrypted plain text value, or an empty string if the input string was empty or null
      Since:
      4.2.0
    • getFileName

      public static String getFileName(String path)
      Extract the file name from a path (e.g. returning "file.txt" for "C:\data\txt\file.txt"). Treats "\" and "/" as separators.
      Parameters:
      path - the file path
      Returns:
      the file name or null if the path was null
    • toSHA256Hash

      public static String toSHA256Hash(byte[] bytes)
      Return a Hex encoded SHA-256 hash of the given form XML data or attachment file data.
      Parameters:
      bytes - the byte array to hash
      Returns:
      SHA-256 hash, encoded as a 64 character Hex string
    • toSHA256Hash

      public static String toSHA256Hash(String plaintext)
      Return a Hex encoded SHA-256 hash of the given string value. Please note the string value will be encode as a byte array using the UTF-8 charset before hashing.
      Parameters:
      plaintext - string value to hash
      Returns:
      SHA-256 hash, encoded as a 64 character Hex string
    • toSHA384Hash

      public static String toSHA384Hash(byte[] bytes, byte[] salt, int numberOfIterations)
      Return a Hex encoded SHA-384 hash of the given form XML data or attachment file data.
      Parameters:
      bytes - the byte array to hash (required)
      salt - the salt
      numberOfIterations - the number of times that the hash will be applied (must be a positive number)
      Returns:
      SHA-384 hash, encoded as a 96 character hex string
      Since:
      4.3.4
    • getMimeType

      public static String getMimeType(String filename)
      Return the mime-type or content-type for the given filename.
      Parameters:
      filename - the filename to obtain the mime-type for
      Returns:
      the mime-type for the given filename, or null if not found
    • stripCDATAWrapper

      public static String stripCDATAWrapper(String value)
      Return a string stripped of any CDATA section wrapper from the given string value.
      Parameters:
      value - the string parameter
      Returns:
      a string stripped of any CDATA section wrapper from the given string value.
    • isBlankList

      public static boolean isBlankList(List<String> valueList)
      Return true if the list is empty or only contains blank values
      Parameters:
      valueList - the value list to test
      Returns:
      true if the list is empty or only contains blank values
      Since:
      3.6.0
    • isFileExtensionMatched

      public static boolean isFileExtensionMatched(String fileName, String fileExtension)
      Return true if the provided file name's extension matches the given file extension
      Parameters:
      fileName - the file name to check
      fileExtension - the file extension to match with
      Returns:
      true if the provided file name's extension matches the given file extension
      Since:
      22.4.1
    • isXssSafe

      public static boolean isXssSafe(String value)
      Checks if is XXS safe. Please see dangerous characters: XSS Filter Evasion Cheat Sheet
      Parameters:
      value - the value to test
      Returns:
      true, if is XSS safe or is null
      Since:
      3.6.0
    • stripControlChars

      public static String stripControlChars(String source)
      Strip ISO control characters, with the exception of '\n', '\r', '\t' form the given string returning the result.
      Parameters:
      source - the source string
      Returns:
      the string value stripped of ISO Control characters.
      Since:
      3.6.0
    • isHealthMonitoringEnabled

      public static boolean isHealthMonitoringEnabled()
      Return true if the system health monitoring is enabled, or false if disabled by a JVM argument com.avoka.disable.health.monitoring.
      Returns:
      true if the system health monitoring is enabled, or false if disabled by a JVM argument
      Since:
      4.0.0
    • isPrimaryKeyValue

      public static boolean isPrimaryKeyValue(Object pk)
      Return true if value can be coerced into a primary key, a positive long value which does not start with 0.
      Parameters:
      pk - the primary key value
      Returns:
      true if value can be coerced into a Long primary key, a positive long value which does not start with 0.
      Since:
      4.0.2
    • isGuidKeyValue

      public static boolean isGuidKeyValue(String value)
      Return true if the specified value could be GUID Key value.
      Parameters:
      value - the value to test
      Returns:
      true if the specified value could be GUID Key value
      Since:
      4.3.0
    • isPositiveInteger

      public static boolean isPositiveInteger(String value)
      Return true if value can be coerced into a positive integer (long) value, which does not start with 0.
      Parameters:
      value - the string value to test
      Returns:
      true true if value can be coerced into a positive integer (long) value, which does not start with 0.
      Since:
      4.1.0
    • isNonNegativeInteger

      public static boolean isNonNegativeInteger(String value)
      Return true if value can be coerced into a positive integer (long) value, which does not start with 0.
      Parameters:
      value - the string value to test
      Returns:
      true true if value can be coerced into a positive integer (long) value, which does not start with 0.
      Since:
      4.3.4
    • escapeJson

      public static String escapeJson(String value)
      Escape the JSON string value.

      http://stackoverflow.com/questions/3020094/how-should-i-escape-strings-in-json

      Parameters:
      value - the string value to escape as value JSON text
      Returns:
      the JSON escaped string value
      Since:
      4.1.0
    • normalizeValue

      public static String normalizeValue(String value)
      Normalize the value for a file system name.
      Parameters:
      value - the value to normalize (required)
      Returns:
      the normalized value for a file system name
      Since:
      17.10.0
    • toLabel

      public static String toLabel(String name)

      Return a field label string from the given field name. For example:

       faxNumber   ->   Fax Number 

      Note toLabel will return an empty String ("") if a null String name is specified.

      Copied from Apache ClickUtils method of the same name.

      Parameters:
      name - the field name (required)
      Returns:
      a field label string from the given field name
      Since:
      17.10.0
    • toName

      public static String toName(String label)

      Return a field name string from the given field label.

      A label of " OK do it!" is returned as "okDoIt". Any &nbsp; characters will also be removed.

      A label of "customerSelect" is returned as "customerSelect".

      Copied from Apache ClickUtils method of the same name.

      Parameters:
      label - the field label or caption (required)
      Returns:
      a field name string from the given field label
      Since:
      17.10.0
    • generateHmacSHA256Hash

      public static String generateHmacSHA256Hash(String convertme, String keyString)
      This method will generate HmacSHA256 hash or the fingerprint
      Parameters:
      convertme - the string to convert to HmacSHA256 hash
      keyString - the key material of the secret key. The contents of the array are copied to protect against subsequent modification.
      Returns:
      HmacSHA256 hash - the fingerprint
      Since:
      19.11.0