Class Utils

java.lang.Object
io.jans.casa.misc.Utils

public final class Utils extends Object
Provides miscellaneous utilities.
Author:
jgomer
  • Method Details

    • onWindows

      public static boolean onWindows()
      Whether this call is being performed on Windows operating system or not.
      Returns:
      A boolean value
    • isEmpty

      public static boolean isEmpty(String string)
    • isNotEmpty

      public static boolean isNotEmpty(String string)
    • isEmpty

      public static <T> boolean isEmpty(T[] array)
    • isNotEmpty

      public static <T> boolean isNotEmpty(T[] array)
    • isEmpty

      public static <T> boolean isEmpty(Collection<T> collection)
    • isNotEmpty

      public static <T> boolean isNotEmpty(Collection<T> collection)
    • isEmpty

      public static <K, V> boolean isEmpty(Map<K,V> map)
    • isNotEmpty

      public static <K, V> boolean isNotEmpty(Map<K,V> map)
    • isJarFile

      public static boolean isJarFile(Path path)
      Checks whether the Path instance passed seems to correspond to a jar file.
      Parameters:
      path - Path of file
      Returns:
      A boolean value. A shallow check is performed: if the file exists and has .jar extension
    • isClassFile

      public static boolean isClassFile(Path path)
      Checks whether the Path instance passed seems to correspond to a java class file.
      Parameters:
      path - Path of file
      Returns:
      A boolean value. A shallow check is performed: if the file exists and has .class extension
    • managedBean

      public static <T> T managedBean(Class<T> clazz)
      Given a type obtains a reference to a CDI managed bean living in Gluu Casa webapp. Use interfaces belonging to package io.jans.casa.service when calling this method.
      Type Parameters:
      T - The required type
      Parameters:
      clazz - A Class representing the required type
      Returns:
      An instance object
    • jsonFromObject

      public static String jsonFromObject(Object obj)
      Returns a json representation of an arbitrary object using a default com.fasterxml Jackson mapper object.
      Parameters:
      obj - Object to convert to json representation
      Returns:
      A string with result of the operation. The empty object {} is returned in case of failure when serializing
    • getImageDataUriEncoding

      public static String getImageDataUriEncoding(byte[] bytes, String hintName)
      Computes the data-uri representation of an image whose bytes are given a parameter. This is useful to represent images inlined in CSS.
      Parameters:
      bytes - Array of bytes
      hintName - A file name this image may have if it were physical stored in disk. This is used as a hint for the mime content of the bytes if it cannot be inferred automatically. Pass null if you cannot provide a hint about which could be a suitable file name (note that file extension is the useful portion of this String)
      Returns:
      The data-uri representation (uses Base64 encoding)
    • cloneObject

      public static Object cloneObject(Object obj)
      Returns a clone of the object passsed.
      Parameters:
      obj - Object to be cloned
      Returns:
      A deep clone (using Jackson objectmapper conversion: object -> json string -> object) (null if the cloning process thew an exception)
    • listfromArray

      public static <T> List<T> listfromArray(T[] array)
      Builds a List of elements using the contents of the array supplied.
      Type Parameters:
      T - Type of elements
      Parameters:
      array - Array of elements
      Returns:
      A list (empty if provided array has length zero or is null)
    • arrayFromValue

      public static <T> T[] arrayFromValue(Class<T> cls, T value)
      Returns an array of length zero or one of the specified type
      Type Parameters:
      T - Type of elements
      Parameters:
      cls - Class to which the elements of the array belong
      value - Single value the resulting array will contain. If null, the resulting array will be empty
      Returns:
      An array of T instances
    • nonNullList

      public static <T> List<T> nonNullList(List<T> list)
    • hostAvailabilityCheck

      public static boolean hostAvailabilityCheck(SocketAddress address, int timeout)
      Checks if a socket connection can be established.
      Parameters:
      address - A SocketAddress to establish the connection.
      timeout - A timeout for connection in ms
      Returns:
      Whether the connection was established
    • isValidUrl

      public static boolean isValidUrl(String strUrl)
      Checks if the string passed as parameter resembles a valid URL.
      Parameters:
      strUrl - String to check
      Returns:
      Whether it has the form a valid URL or not
    • firstTrue

      public static <T> int firstTrue(List<T> list, Function<? super T,? extends Boolean> map)
      This method takes a List as input, applies a map turning elements into booleans, and returns the index of first true occurrence.
      Type Parameters:
      T - Type of the list elements
      Parameters:
      list - Input list
      map - mapping function (converts every T into a Boolean)
      Returns:
      Index of the first occurrence of True (-1 if all were False)
    • randomBytes

      public static byte[] randomBytes(int keyLen)
      Generates a sequence of random bytes (using SecureRandom class).
      Parameters:
      keyLen - Number of bytes to return
      Returns:
      Array of random bytes
    • scriptConfigPropertiesAsMap

      public static Map<String,String> scriptConfigPropertiesAsMap(CustomScript script)
      Builds a map of name/value entries based on the configuration properties associated to an object of class CustomScript.
      Parameters:
      script - A CustomScript instance
      Returns:
      A Mapping of property name / property value for the script
    • scriptModulePropertiesAsMap

      public static Map<String,String> scriptModulePropertiesAsMap(CustomScript script)
      Analog method to scriptConfigPropertiesAsMap(CustomScript), this time for obtaining module properties
      Parameters:
      script - A CustomScript instance
      Returns:
      A Mapping of property name / property value for the script
    • stringEncrypter

      public static io.jans.util.security.StringEncrypter stringEncrypter(String saltFile) throws io.jans.util.security.StringEncrypter.EncryptionException
      Throws:
      io.jans.util.security.StringEncrypter.EncryptionException
    • stringEncrypter

      public static io.jans.util.security.StringEncrypter stringEncrypter() throws io.jans.util.security.StringEncrypter.EncryptionException
      Returns a default instance of an encryption/decryption utility object. This is compatible with the EncryptionService accessible in Gluu custom scripts
      Returns:
      Utility object
      Throws:
      io.jans.util.security.StringEncrypter.EncryptionException
    • urlAvailabilityCheck

      public static boolean urlAvailabilityCheck(URL siteURL) throws Exception
      Throws:
      Exception