From 5f2ecc241796cb69db024f73820cc4c7eb5af8d5 Mon Sep 17 00:00:00 2001
From: Alexander Schlemmer <alexander.schlemmer@ds.mpg.de>
Date: Thu, 2 May 2019 11:16:59 +0200
Subject: [PATCH] DOC: completely documented Utils.java and corrected a typo.

---
 src/main/java/caosdb/server/CaosDBServer.java |  2 +-
 src/main/java/caosdb/server/utils/Utils.java  | 77 +++++++++++++++++--
 2 files changed, 71 insertions(+), 8 deletions(-)

diff --git a/src/main/java/caosdb/server/CaosDBServer.java b/src/main/java/caosdb/server/CaosDBServer.java
index 2e726217..96d7c0d7 100644
--- a/src/main/java/caosdb/server/CaosDBServer.java
+++ b/src/main/java/caosdb/server/CaosDBServer.java
@@ -356,7 +356,7 @@ public class CaosDBServer extends Application {
   }
 
   /**
-   * Start a http server. This is insecure!
+   * Start an http server. This is insecure!
    *
    * @throws Exception
    */
diff --git a/src/main/java/caosdb/server/utils/Utils.java b/src/main/java/caosdb/server/utils/Utils.java
index 10f3de6c..3ce0fc2f 100644
--- a/src/main/java/caosdb/server/utils/Utils.java
+++ b/src/main/java/caosdb/server/utils/Utils.java
@@ -41,14 +41,14 @@ import org.jdom2.output.Format;
 import org.jdom2.output.XMLOutputter;
 
 /**
- * Useful stuff...
- *
- * @author Timm Fitschen, Alexander Schlemmer
+ * Utility functions.
  */
 public class Utils {
 
   /**
-   * @param obj
+   * Check whether obj is non-null and can be parsed to an integer.
+   *
+   * @param obj The object to check.
    * @return true if obj is not null and obj.toString() can be parsed as an integer
    */
   public static boolean isNonNullInteger(final Object obj) {
@@ -63,7 +63,16 @@ public class Utils {
     return true;
   }
 
-  /** Stolen from http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html */
+    /**
+     * Regular expression pattern that checks a mail for RFC822 compliance.
+     *
+     * Taken from: http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html
+     *
+     * This belongs to the perl module "Mail::RFC822::Address".
+     * It is copyright 2001-2002 by Paul Warren.
+     * The license can be found here:
+     * http://www.ex-parrot.com/~pdw/Mail-RFC822-Address/Mail-RFC822-Address.html#copyright%20and%20license
+     */
   public static final Pattern EMAIL_PATTERN =
       Pattern.compile(
           "(?:(?:\\r\\n)?[ \\t])*(?:(?:(?:[^()<>@,;:\\\\\".\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\"()<>@,;:\\\\\".\\[\\]]))|\"(?:[^\\\"\\r\\\\]|\\\\.|(?:(?:\\r\\n)?[ \\t]))*\"(?:(?:\\r\\n)?[ \\t])*)(?:\\.(?:(?:\\r\\n)?[ \\t])*(?:[^()<>@,;:\\\\\".\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\"()<>@,;:\\\\\".\\[\\]]))|\"(?:[^\\\"\\r\\\\]|\\\\.|(?:(?:\\r\\n)?[ \\t]))*\"(?:(?:\\r\\n)?[ \\t])*))*@(?:(?:\\r\\n)?[ \\t])*(?:[^()<>@,;:\\\\\".\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\"()<>@,;:\\\\\".\\[\\]]))|\\[([^\\[\\]\\r\\\\]|\\\\.)*\\](?:(?:\\r\\n)?[ \\t])*)(?:\\.(?:(?:\\r\\n)?[ \\t])*(?:[^()<>@,;:\\\\\".\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\"()<>@,;:\\\\\".\\[\\]]))|\\[([^\\[\\]\\r\\\\]|\\\\.)*\\](?:(?:\\r\\n)?[ \\t])*))*|(?:[^()<>@,;:\\\\\".\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\"()<>@,;:\\\\\".\\[\\]]))|\"(?:[^\\\"\\r\\\\]|\\\\.|(?:(?:\\r\\n)?[ \\t]))*\"(?:(?:\\r\\n)?[ \\t])*)*\\<(?:(?:\\r\\n)?[ \\t])*(?:@(?:[^()<>@,;:\\\\\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\\[([^\\[\\]\\r\\\\]|\\\\.)*\\](?:(?:\\r\\n)?[ \\t])*))*\\>(?:(?:\\r\\n)?[ \\t])*))*)?;\\s*)");
@@ -72,7 +81,7 @@ public class Utils {
    * Check if email is RFC822 compliant.
    *
    * @param email
-   * @return
+   * @return true when the email given by the String email is RFC822 compliant.
    */
   public static boolean isRFC822Compliant(final String email) {
     return EMAIL_PATTERN.matcher(email).matches();
@@ -140,6 +149,17 @@ public class Utils {
     return FileUtils.toHex(result);
   }
 
+    /**
+     * This function returns the SHA512 hash of a given string, salt and number of iterations.
+     * This function terminates the application completely with an error code when this hashing algorithm
+     * is not available.
+     *
+     * @param string The string to be hashed.
+     * @param salt Cryptographic salt.
+     * @param it Number of iterations. Zero and negative integers don't make any sense. Therefore, if
+     *     it<=1 the hashing is done once.
+     * @return Hashed string, or null if string is null
+     */
   public static String sha512(final String string, final String salt, final int it) {
     try {
       return hash(MessageDigest.getInstance("SHA-512"), string, salt, it);
@@ -151,12 +171,18 @@ public class Utils {
     }
   }
 
+    /**
+     * Random number generator initialized with the system time and used for generating UIDs.
+     */
   private static Random rand = new Random(System.currentTimeMillis());
 
   /**
    * Generate a UID (Hexadecimal).
    *
-   * @return
+   * Currently this generates two random long numbers and concatenates their hex representations.
+   * This function is thread-safe.
+   *
+   * @return The UID as a String.
    */
   public static String getUID() {
     synchronized (rand) {
@@ -164,10 +190,20 @@ public class Utils {
     }
   }
 
+    /**
+     * Helper function calling InputStream2String for charset UTF-8.
+     */
   public static String InputStream2String(final InputStream is) {
     return InputStream2String(is, "UTF-8");
   }
 
+    /**
+     * Return a string from the input stream is up to the next LF (line feed, \\A) character.
+     *
+     * @param is The input stream
+     * @return The detected string or an empty string if the stream has become empty.
+     *
+     */
   public static String InputStream2String(final InputStream is, String charset) {
     try (Scanner s = new Scanner(is, charset)) {
       s.useDelimiter("\\A");
@@ -175,6 +211,9 @@ public class Utils {
     }
   }
 
+    /**
+     * Helper function to convert a String into an UTF-8 encoded ByteArrayInputStream.
+     */
   public static InputStream String2InputStream(final String s) throws UnsupportedEncodingException {
     return new ByteArrayInputStream(s.getBytes("UTF-8"));
   }
@@ -217,10 +256,20 @@ public class Utils {
     }
   }
 
+    /**
+     * Intended to compute the maximum exponent for the required conversion.
+     * TODO: Check whether this is correct. Seems to have an additional factor of 2.
+     *
+     */
   public static double log(final int base, final Long x) {
     return Math.log(x) / Math.log10(base);
   }
 
+    /**
+     * Intended to convert sizes to human readably sizes.
+     * TODO: might be broken because of the log function above.
+     *
+     */
   public static String getReadableByteSize(final Long fssize) {
     if (fssize == null) {
       return null;
@@ -243,6 +292,20 @@ public class Utils {
         .toString();
   }
 
+    /**
+     * Determines the strength of a password.
+     * Currently this just throws an error when the String password does not at least contain
+     * - An uppercase char
+     * - A lowercase char
+     * - A number
+     * - A punctuation char
+     * and if the length is not at least 8 characters.
+     *
+     * correcthorsebatterystaple would be rejected.
+     *
+     * @param password The password to be checked.
+     *
+     */
   public static void checkPasswordStrength(final String password) throws Message {
     final boolean length = password.length() >= 8;
     final boolean uppercase = password.matches(".*\\p{Upper}.*");
-- 
GitLab