Class Utils

java.lang.Object
org.dspace.core.Utils

public final class Utils extends Object
Utility functions for DSpace.
Author:
Peter Breton
  • Method Details

    • getMD5

      public static String getMD5(String data)
      Return an MD5 checksum for data in hex format.
      Parameters:
      data - The data to checksum.
      Returns:
      MD5 checksum for the data in hex format.
    • getMD5

      public static String getMD5(byte[] data)
      Return an MD5 checksum for data in hex format.
      Parameters:
      data - The data to checksum.
      Returns:
      MD5 checksum for the data in hex format.
    • getMD5Bytes

      public static byte[] getMD5Bytes(byte[] data)
      Return an MD5 checksum for data as a byte array.
      Parameters:
      data - The data to checksum.
      Returns:
      MD5 checksum for the data as a byte array.
    • toHex

      public static String toHex(byte[] data)
      Return a hex representation of the byte array
      Parameters:
      data - The data to transform.
      Returns:
      A hex representation of the data.
    • generateKey

      public static String generateKey()
      Generate a unique key. The key is a long (length 38 to 40) sequence of digits.
      Returns:
      A unique key as a long sequence of base-10 digits.
    • generateHexKey

      public static String generateHexKey()
      Generate a unique key. The key is a 32-character long sequence of hex digits.
      Returns:
      A unique key as a long sequence of hex digits.
    • generateBytesKey

      public static byte[] generateBytesKey()
      Generate a unique key as a byte array.
      Returns:
      A unique key as a byte array.
    • copy

      public static void copy(InputStream input, OutputStream output) throws IOException
      Copy stream-data from source to destination. This method does not buffer, flush or close the streams, as to do so would require making non-portable assumptions about the streams' origin and further use. If you wish to perform a buffered copy, use bufferedCopy(java.io.InputStream, java.io.OutputStream).
      Parameters:
      input - The InputStream to obtain data from.
      output - The OutputStream to copy data to.
      Throws:
      IOException - if IO error
    • bufferedCopy

      public static void bufferedCopy(InputStream source, OutputStream destination) throws IOException
      Copy stream-data from source to destination, with buffering. This is equivalent to passing copy(java.io.InputStream, java.io.OutputStream)a java.io.BufferedInputStream and java.io.BufferedOutputStream to copy(java.io.InputStream, java.io.OutputStream), and flushing the output stream afterwards. The streams are not closed after the copy.
      Parameters:
      source - The InputStream to obtain data from.
      destination - The OutputStream to copy data to.
      Throws:
      IOException - if IO error
    • addEntities

      public static String addEntities(String value)
      Replace characters that could be interpreted as HTML codes with symbolic references (entities). This function should be called before displaying any metadata fields that could contain the characters "<", ">", "&", "'", and double quotation marks. This will effectively disable HTML links in metadata.
      Parameters:
      value - the metadata value to be scrubbed for display
      Returns:
      the passed-in string, with html special characters replaced with entities.
    • parseDuration

      public static long parseDuration(String duration) throws ParseException
      Utility method to parse durations defined as \d+[smhdwy] (seconds, minutes, hours, days, weeks, years)
      Parameters:
      duration - specified duration
      Returns:
      number of milliseconds equivalent to duration.
      Throws:
      ParseException - if the duration is of incorrect format
    • parseISO8601Date

      public static Instant parseISO8601Date(String s)
      Translates timestamp from an ISO 8601-standard format, which is commonly used in XML and RDF documents.
      Parameters:
      s - the input string
      Returns:
      Instant object, or null if there is a problem translating.
    • formatISO8601Date

      public static String formatISO8601Date(TemporalAccessor date)
      Convert a date to String in the ISO 8601 standard format.
      Parameters:
      date - the input TemporalAccessor (e.g. LocalDate, LocalDateTime, Instant)
      Returns:
      String containing formatted date.
    • emptyIfNull

      public static <E> Collection<E> emptyIfNull(Collection<E> collection)
    • tokenize

      public static String[] tokenize(String metadata)
      Utility method to extract schema, element, qualifier from the metadata field key Keep in mind that this method try to auto discover the common separator used in DSpace ("_" or ".") Return an array of token with size 3 which contains: schema = tokens[0]; element = tokens[1]; qualifier = tokens[2]; //it can be empty string
      Parameters:
      metadata - (the field in the form dc.title or dc_title)
      Returns:
      array of tokens
    • standardize

      public static String standardize(String schema, String element, String qualifier, String separator)
      Make the metadata field key using the separator.
      Parameters:
      schema -
      element -
      qualifier -
      separator - (DSpace common separator are "_" or ".")
      Returns:
      metadata field key
    • getBaseUrl

      public static String getBaseUrl(String urlString)
      Retrieve the baseurl from a given URL string
      Parameters:
      urlString - URL string
      Returns:
      baseurl (without any context path) or null (if URL was invalid)
    • getHostName

      public static String getHostName(String uriString)
      Retrieve the hostname from a given URI string
      Parameters:
      uriString - URI string
      Returns:
      hostname (without any www.) or null (if URI was invalid)
    • getIPAddresses

      public static String[] getIPAddresses(String uriString)
      Retrieve the IP address(es) of a given URI string.

      At this time, DSpace only supports IPv4, so this method will only return IPv4 addresses.

      Parameters:
      uriString - URI string
      Returns:
      IP address(es) in a String array (or null if not found)
    • interpolateConfigsInString

      public static String interpolateConfigsInString(String string)
      Replaces configuration placeholders within a String with the corresponding value from DSpace's Configuration Service.

      For example, given a String like "My DSpace is installed at ${dspace.dir}", this method will replace "${dspace.dir}" with the configured value of that property.

      Parameters:
      string - source string
      Returns:
      string with any placeholders replaced with configured values.
    • getMaxTimestamp

      public static Instant getMaxTimestamp()
      Get the maximum timestamp that can be stored in a PostgreSQL database with hibernate, for our "distant future" access expiry date.
      Returns:
      the maximum timestamp that can be stored with Postgres + Hibernate
    • getMinTimestamp

      public static Instant getMinTimestamp()
      Get the minimum timestamp that can be stored in a PostgreSQL database, for date validation or any other purpose to ensure we don't try to store a date before the epoch.
      Returns:
      the minimum timestamp that can be stored with Postgres + Hibernate