Package org.apache.juneau.rest.util
Class RestUtils
java.lang.Object
org.apache.juneau.rest.util.RestUtils
Various reusable utility methods.
See Also:
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic String
fixMethodPath
(String path) Normalizes theRestOp.path()
value.static String
getHttpResponseText
(int rc) Returns readable text for an HTTP response code.static String
getPathInfoUndecoded
(jakarta.servlet.http.HttpServletRequest req) Identical toHttpServletRequest.getPathInfo()
but doesn't decode encoded characters.static boolean
isValidContextPath
(String value) Returnstrue if the specified value is a valid context path.static boolean
isValidPathInfo
(String value) Returnstrue if the specified value is a valid path-info path.static boolean
isValidServletPath
(String value) Returnstrue if the specified value is a valid servlet path.static Object
Parses a string that can consist of a simple string or JSON object/array.static String[]
Parses HTTP header.static String[]
Parses key/value pairs separated by either : or =parseQuery
(Object qs) Parses a URL query string or form-data content.parseQuery
(Object qs, Map<String, String[]> map) Same asparseQuery(Object)
but allows you to specify the map to insert values into.static String
Converts the specified path segment to a valid context path.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.static StringBuffer
trimPathInfo
(StringBuffer requestURI, String contextPath, String servletPath) Efficiently trims the path info part from a request URI.static void
validateContextPath
(String value) Throws aRuntimeException
if the methodisValidContextPath(String)
returnsfalse for the specified value.static void
validatePathInfo
(String value) Throws aRuntimeException
if the methodisValidPathInfo(String)
returnsfalse for the specified value.static void
validateServletPath
(String value) Throws aRuntimeException
if the methodisValidServletPath(String)
returnsfalse for the specified value.
-
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.
-
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 byHttpServletRequest.getRequestURL()
contextPath
- The value returned byHttpServletRequest.getContextPath()
servletPath
- The value returned byHttpServletRequest.getServletPath()
- Returns:
- The same StringBuilder with remainder trimmed.
-
parseHeader
Parses HTTP header.- Parameters:
s
- The string to parse.- Returns:
- The parsed string.
-
parseKeyValuePair
Parses key/value pairs separated by either : or =- Parameters:
s
- The string to parse.- Returns:
- The parsed string.
-
parseQuery
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
Same asparseQuery(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
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
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
Normalizes theRestOp.path()
value.- Parameters:
path
- The path to normalize.- Returns:
- The normalized path.
-
isValidContextPath
Returnstrue 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
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
Throws aRuntimeException
if the methodisValidContextPath(String)
returnsfalse for the specified value.- Parameters:
value
- The value to test.
-
isValidServletPath
Returnstrue 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
Throws aRuntimeException
if the methodisValidServletPath(String)
returnsfalse for the specified value.- Parameters:
value
- The value to test.
-
isValidPathInfo
Returnstrue 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
Throws aRuntimeException
if the methodisValidPathInfo(String)
returnsfalse for the specified value.- Parameters:
value
- The value to test.
-