Class RestUtils

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

public final class RestUtils extends Object
Various reusable utility methods.
See Also:
  • 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.
    • trimPathInfo

      public static StringBuffer trimPathInfo(StringBuffer requestURI, String contextPath, String servletPath)
      Efficiently trims the path info part from a request URI.

      The result is the URI of the servlet itself.

      Parameters:
      requestURI - The value returned by HttpServletRequest.getRequestURL()
      contextPath - The value returned by HttpServletRequest.getContextPath()
      servletPath - The value returned by HttpServletRequest.getServletPath()
      Returns:
      The same StringBuilder with remainder trimmed.
    • parseHeader

      public static String[] parseHeader(String s)
      Parses HTTP header.
      Parameters:
      s - The string to parse.
      Returns:
      The parsed string.
    • parseKeyValuePair

      public static String[] parseKeyValuePair(String s)
      Parses key/value pairs separated by either : or =
      Parameters:
      s - The string to parse.
      Returns:
      The parsed string.
    • parseQuery

      public static Map<String,String[]> parseQuery(Object qs)
      Parses a URL query string or form-data content.
      Parameters:
      qs - A reader or string containing the query string to parse.
      Returns:
      A new map containing the parsed query.
    • parseQuery

      public static Map<String,String[]> parseQuery(Object qs, Map<String,String[]> map)
      Same as parseQuery(Object) but allows you to specify the map to insert values into.
      Parameters:
      qs - A reader containing the query string to parse.
      map - The map to pass the values into.
      Returns:
      The same map passed in, or a new map if it was null.
    • parseAnything

      public static Object parseAnything(String s) throws ParseException
      Parses a string that can consist of a simple string or JSON object/array.
      Parameters:
      s - The string to parse.
      Returns:
      The parsed value, or null if the input is null.
      Throws:
      ParseException - Invalid JSON in string.
    • trimContextPath

      public static String trimContextPath(String contextPath, String path)
      If the specified path-info starts with the specified context path, trims the context path from the path info.
      Parameters:
      contextPath - The context path.
      path - The URL path.
      Returns:
      The path following the context path, or the original path.
    • fixMethodPath

      public static String fixMethodPath(String path)
      Normalizes the RestOp.path() value.
      Parameters:
      path - The path to normalize.
      Returns:
      The normalized path.
    • isValidContextPath

      public static boolean isValidContextPath(String value)
      Returns true if the specified value is a valid context path. The path must start with a "/" character but not end with a "/" character. For servlets in the default (root) context, the value should be "".
      Parameters:
      value - The value to test.
      Returns:
      true if the specified value is a valid context path.
    • 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.
    • validateContextPath

      public static void validateContextPath(String value)
      Throws a RuntimeException if the method isValidContextPath(String) returns false for the specified value.
      Parameters:
      value - The value to test.
    • isValidServletPath

      public static boolean isValidServletPath(String value)
      Returns true if the specified value is a valid servlet path. This path must with a "/" character and includes either the servlet name or a path to the servlet, but does not include any extra path information or a query string. Should be an empty string ("") if the servlet used to process this request was matched using the "/*" pattern.
      Parameters:
      value - The value to test.
      Returns:
      true if the specified value is a valid servlet path.
    • validateServletPath

      public static void validateServletPath(String value)
      Throws a RuntimeException if the method isValidServletPath(String) returns false for the specified value.
      Parameters:
      value - The value to test.
    • isValidPathInfo

      public static boolean isValidPathInfo(String value)
      Returns true if the specified value is a valid path-info path. The extra path information follows the servlet path but precedes the query string and will start with a "/" character. The value should be null if there was no extra path information.
      Parameters:
      value - The value to test.
      Returns:
      true if the specified value is a valid path-info path.
    • validatePathInfo

      public static void validatePathInfo(String value)
      Throws a RuntimeException if the method isValidPathInfo(String) returns false for the specified value.
      Parameters:
      value - The value to test.