Class StringUtils

java.lang.Object
com.renomad.minum.utils.StringUtils

public final class StringUtils extends Object
Some helper methods for Strings.
  • Method Details

    • safeHtml

      public static String safeHtml(String input)
      Returns text that has three symbols replaced - the less-than, greater-than, and ampersand. See ...
      
       This will protect against something like <div>$USERNAME</div> allowing
       a username of
            <script>alert(1)</script>
       becoming
            <div><script>alert(1)</script</div>
       and instead becomes
            <div>&lt;script&gt;alert(1)&lt;/script&gt;</div>
       
      If the text is going inside an attribute (e.g. <div class="TEXT_GOES_HERE"> ) Then you need to escape slightly differently. In that case see [safeAttr]
    • safeAttr

      public static String safeAttr(String input)
      Replace dangerous text that would go inside an HTML attribute. See safeHtml(String)

      If we get a null string, just return an empty string

      Note: When cleaning data which will be used in an HTML attribute, ensure that the output of this method will be placed inside single or double quotes.

      Valid use: the key word HERE in the following: <foo class="HERE"> or <foo class='HERE'>

      Invalid use: <foo class=HERE>

      
       example:
         Given
            alert('XSS Attack')
         Get
            alert(&apos;XSS Attack&apos;)
       
    • encode

      public static String encode(String str)
      Encodes UTF-8 text using URL-encoding
    • decode

      public static String decode(String str)
      Decodes URL-encoded UTF-8 text, except that we first check if the string value is the token %NULL%, which is our way to signify null.
    • generateSecureRandomString

      public static String generateSecureRandomString(int length)
      Returns a string of random characters.
      Parameters:
      length - a positive non-zero number less than 1000
    • byteListToString

      public static String byteListToString(List<Byte> byteList)
      Converts a list of bytes to a string. Returns null if the input is null.
    • byteArrayToString

      public static String byteArrayToString(byte[] byteArray)
      Converts an array of bytes to a string. Returns null if the input is null.
    • isPureDigits

      public static boolean isPureDigits(String str)
      A helper method to assess whether the input consists of only digits. This helps us in the case of checking the content-length header, since it must be pure numbers, no "-" or "+" symbols preceding it.
      Returns:
      true if the given string consists exclusively of digits, like "1234", false if anything else, like "+1234" or "12.34" or "" or null.