Class RestUtils
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringgetHttpResponseText(int rc) Returns readable text for an HTTP response code.static StringgetPathInfoUndecoded(jakarta.servlet.http.HttpServletRequest req) Identical toHttpServletRequest.getPathInfo()but doesn't decode encoded characters.static ObjectparseIfJson(String value) Parses a string as JSON if it appears to be JSON, otherwise returns the string as-is.parseQuery(Reader qs) Parses a URL query string or form-data content from a reader.parseQuery(String qs) Parses a URL query string or form-data content from a string.static StringConverts the specified path segment to a valid context path.static StringvalidatePathInfo(String value) Validates that the specified value is a valid path-info path and returns it.static StringvalidateServletPath(String value) Validates that the specified value is a valid servlet path and returns it.
-
Constructor Details
-
RestUtils
public RestUtils()
-
-
Method Details
-
getHttpResponseText
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
Identical toHttpServletRequest.getPathInfo()but doesn't decode encoded characters.- Parameters:
req- The HTTP request- Returns:
- The un-decoded path info.
-
parseIfJson
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 Objectresult1 = parseIfJson("{\"name\":\"John\"}" );// Returns a Map with key "name" and value "John" // JSON array is parsed Objectresult2 = parseIfJson("[1,2,3]" );// Returns a List containing [1, 2, 3] // Plain string is returned as-is Objectresult3 = parseIfJson("hello world" );// Returns the string "hello world" - Parameters:
value- The string to parse. Can benull .- Returns:
- The parsed JSON object (if the string was JSON), or the original string (if it was not JSON).
Returns
null if the input isnull .
Return type can be:Map ,List ,String ,Number ,Boolean , ornull . - Throws:
ParseException- If the string appears to be JSON but contains invalid JSON syntax.
-
parseQuery
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 aList .Special cases:
- Empty or
null strings return an empty map - Keys without values (e.g.,
key1&key2 ) are stored withnull 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 benull or empty.- Returns:
- A map of parameter names to lists of values. Returns an empty map if the input is
null or empty.
- Empty or
-
parseQuery
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 aList .Special cases:
null readers return an empty map- Keys without values (e.g.,
key1&key2 ) are stored withnull 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 Readerreader =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 benull .- Returns:
- A map of parameter names to lists of values. Returns an empty map if the input is
null .
-
toValidContextPath
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
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
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 isnull or not a valid servlet path.
- An empty string
-