Class RestUtils

java.lang.Object
org.apache.juneau.rest.util.RestUtils

public class RestUtils extends Object
Various reusable utility methods.
  • Constructor Details

  • Method Details

    • getHttpResponseText

      public static String getHttpResponseText(int rc)
      Returns readable text for an HTTP response code.
      Parameters:
      rc - The HTTP response code.
      Returns:
      Readable text for an HTTP response code, or null if it's an invalid code.
    • getPathInfoUndecoded

      public static String getPathInfoUndecoded(jakarta.servlet.http.HttpServletRequest req)
      Identical to HttpServletRequest.getPathInfo() but doesn't decode encoded characters.
      Parameters:
      req - The HTTP request
      Returns:
      The un-decoded path info.
    • parseIfJson

      public static Object parseIfJson(String value) throws ParseException
      Parses a string as JSON if it appears to be JSON, otherwise returns the string as-is.

      This method attempts to intelligently detect whether the input string is JSON or plain text. If the string appears to be JSON (starts with {, [, or other JSON indicators), it is parsed and returned as a Java object (Map, List, String, Number, etc.). Otherwise, the original string is returned unchanged.

      This is useful when processing input that could be either a JSON value or a plain string, such as configuration values or user input that may or may not be JSON-encoded.

      Example:

      // JSON object is parsed Object result1 = parseIfJson("{\"name\":\"John\"}"); // Returns a Map with key "name" and value "John" // JSON array is parsed Object result2 = parseIfJson("[1,2,3]"); // Returns a List containing [1, 2, 3] // Plain string is returned as-is Object result3 = parseIfJson("hello world"); // Returns the string "hello world"

      Parameters:
      value - The string to parse. Can be null.
      Returns:
      The parsed JSON object (if the string was JSON), or the original string (if it was not JSON). Returns null if the input is null.
      Return type can be: Map, List, String, Number, Boolean, or null.
      Throws:
      ParseException - If the string appears to be JSON but contains invalid JSON syntax.
    • parseQuery

      public static Map<String,List<String>> parseQuery(String qs)
      Parses a URL query string or form-data content from a string.

      Parses key-value pairs from a query string format (e.g., key1=value1&key2=value2). Supports multiple values for the same key, which are collected into a List.

      Special cases:

      • Empty or null strings return an empty map
      • Keys without values (e.g., key1&key2) are stored with null values
      • Keys with empty values (e.g., key=) are stored with empty strings
      • Multiple occurrences of the same key append values to the list
      Example:

      Map<String,List<String>> params = parseQuery("f1=v1&f2=v2&f1=v3"); params.get("f1"); // Returns [v1, v3] params.get("f2"); // Returns [v2]

      Parameters:
      qs - The query string to parse. Can be null or empty.
      Returns:
      A map of parameter names to lists of values. Returns an empty map if the input is null or empty.
    • parseQuery

      public static Map<String,List<String>> parseQuery(Reader qs)
      Parses a URL query string or form-data content from a reader.

      Parses key-value pairs from a query string format (e.g., key1=value1&key2=value2). Supports multiple values for the same key, which are collected into a List.

      Special cases:

      • null readers return an empty map
      • Keys without values (e.g., key1&key2) are stored with null values
      • Keys with empty values (e.g., key=) are stored with empty strings
      • Multiple occurrences of the same key append values to the list
      Example:

      // Parse from a reader Reader reader = new StringReader("f1=v1&f2=v2"); Map<String,List<String>> params = parseQuery(reader); params.get("f1"); // Returns [v1]

      Parameters:
      qs - The reader containing the query string to parse. Can be null.
      Returns:
      A map of parameter names to lists of values. Returns an empty map if the input is null.
    • toValidContextPath

      public static String toValidContextPath(String s)
      Converts the specified path segment to a valid context path.
      • nulls and "/" are converted to empty strings.
      • Trailing slashes are trimmed.
      • Leading slash is added if needed.
      Parameters:
      s - The value to convert.
      Returns:
      The converted path.
    • validatePathInfo

      public static String validatePathInfo(String value)
      Validates that the specified value is a valid path-info path and returns it.

      A valid path-info path must be:

      • null (valid, indicates no extra path information)
      • Non-empty and starting with / (e.g., /users/123)

      The path-info follows the servlet path but precedes the query string.

      Parameters:
      value - The value to validate.
      Returns:
      The validated value (may be null).
      Throws:
      RuntimeException - If the value is not a valid path-info path.
    • validateServletPath

      public static String validateServletPath(String value)
      Validates that the specified value is a valid servlet path and returns it.

      A valid servlet path must be:

      • An empty string "" (valid, indicates servlet matched using /* pattern)
      • Non-empty, starting with /, not ending with /, and not exactly / (e.g., /api, /api/users)

      The servlet path includes either the servlet name or a path to the servlet, but does not include any extra path information or a query string.

      Parameters:
      value - The value to validate.
      Returns:
      The validated value (never null).
      Throws:
      RuntimeException - If the value is null or not a valid servlet path.