Package org.dspace.core
Class Utils
java.lang.Object
org.dspace.core.Utils
Utility functions for DSpace.
- Author:
- Peter Breton
-
Method Summary
Modifier and TypeMethodDescriptionstatic StringaddEntities(String value) Replace characters that could be interpreted as HTML codes with symbolic references (entities).static voidbufferedCopy(InputStream source, OutputStream destination) Copy stream-data from source to destination, with buffering.static voidcopy(InputStream input, OutputStream output) Copy stream-data from source to destination.static <E> Collection<E>emptyIfNull(Collection<E> collection) static StringConvert a date to String in the ISO 8601 standard format.static byte[]Generate a unique key as a byte array.static StringGenerate a unique key.static StringGenerate a unique key.static StringgetBaseUrl(String urlString) Retrieve the baseurl from a given URL stringstatic StringgetHostName(String uriString) Retrieve the hostname from a given URI stringstatic String[]getIPAddresses(String uriString) Retrieve the IP address(es) of a given URI string.static InstantGet the maximum timestamp that can be stored in a PostgreSQL database with hibernate, for our "distant future" access expiry date.static StringgetMD5(byte[] data) Return an MD5 checksum for data in hex format.static StringReturn an MD5 checksum for data in hex format.static byte[]getMD5Bytes(byte[] data) Return an MD5 checksum for data as a byte array.static InstantGet 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.static StringinterpolateConfigsInString(String string) Replaces configuration placeholders within a String with the corresponding value from DSpace's Configuration Service.static longparseDuration(String duration) Utility method to parse durations defined as\d+[smhdwy](seconds, minutes, hours, days, weeks, years)static InstantTranslates timestamp from an ISO 8601-standard format, which is commonly used in XML and RDF documents.static Stringstandardize(String schema, String element, String qualifier, String separator) Make the metadata field key using the separator.static StringtoHex(byte[] data) Return a hex representation of the byte arraystatic String[]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 ".")
-
Method Details
-
getMD5
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
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
Return a hex representation of the byte array- Parameters:
data- The data to transform.- Returns:
- A hex representation of the data.
-
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
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
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, usebufferedCopy(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
Copy stream-data from source to destination, with buffering. This is equivalent to passingcopy(java.io.InputStream, java.io.OutputStream)ajava.io.BufferedInputStreamandjava.io.BufferedOutputStreamtocopy(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
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
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
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
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
-
tokenize
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
Make the metadata field key using the separator.- Parameters:
schema-element-qualifier-separator- (DSpace common separator are "_" or ".")- Returns:
- metadata field key
-
getBaseUrl
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
Retrieve the hostname from a given URI string- Parameters:
uriString- URI string- Returns:
- hostname (without any www.) or null (if URI was invalid)
-
getIPAddresses
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
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
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
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
-