Class WebUtils
This class supports parsing and formatting of HTTP cookie headers, case-insensitive HTTP header maps, and discovery
of WebPattern
annotations on handler methods annotated with HandleWeb
.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionasHeaderMap
(Map<String, List<String>> input) Converts the input map into a case-insensitive header map.static String
concatenateUrlParts
(String... parts) Concatenates multiple segments of a URL, ensuring that: Double slashes in schemes likehttps://
are preserved Duplicate slashes between path segments are removed Null or empty segments are skippedReturns a new case-insensitive header map, with keys compared ignoring case.extractPathParameters
(String path) Extracts all named path parameters from the given path.static String
getHandlerPath
(Class<?> targetClass, Object handler, Executable method) Computes the complete@Path
value for the given handler.Retrieves the first header value for the given name from the provided Metadata object.getHeaders
(Metadata metadata) Retrieves a case-insensitive map of headers from the provided Metadata object.static List
<WebPattern> getWebPatterns
(Class<?> targetClass, Object handler, Executable method) Returns allWebPattern
instances declared on the given method in the given target class.static boolean
hasPathParameter
(String path) Checks if the given path contains a named path parameter.static List
<HttpCookie> parseRequestCookieHeader
(String cookieHeader) Parses aCookie
request header string into a list ofHttpCookie
instances.static List
<HttpCookie> parseResponseCookieHeader
(List<String> setCookieHeaders) Parses a list ofSet-Cookie
header values into a list ofHttpCookie
instances.static String
replacePathParameter
(String path, String parameterName, String value) Replaces named path parameter with provided value.static String
toRequestHeaderString
(@NonNull HttpCookie cookie) Returns a formatted string for theCookie
request header containing the given cookie.static String
toResponseHeaderString
(@NonNull HttpCookie cookie) Returns a properly formattedSet-Cookie
header value for the given cookie.
-
Constructor Details
-
WebUtils
public WebUtils()
-
-
Method Details
-
toResponseHeaderString
Returns a properly formattedSet-Cookie
header value for the given cookie.The result includes standard attributes such as
Domain
,Path
,Max-Age
,HttpOnly
, andSecure
if they are set on the cookie.- Parameters:
cookie
- the cookie to format (must not benull
)- Returns:
- a header string suitable for a
Set-Cookie
response header
-
toRequestHeaderString
Returns a formatted string for theCookie
request header containing the given cookie.- Parameters:
cookie
- the cookie to encode (must not benull
)- Returns:
- a header string suitable for inclusion in a
Cookie
request header
-
parseRequestCookieHeader
Parses aCookie
request header string into a list ofHttpCookie
instances.The input is expected to contain one or more name-value pairs separated by semicolons.
- Parameters:
cookieHeader
- the value of theCookie
header, ornull
- Returns:
- a list of parsed
HttpCookie
instances (empty if input isnull
)
-
parseResponseCookieHeader
Parses a list ofSet-Cookie
header values into a list ofHttpCookie
instances.Each value in the input list should be a properly formatted
Set-Cookie
header line.- Parameters:
setCookieHeaders
- the list ofSet-Cookie
header values, ornull
- Returns:
- a list of parsed
HttpCookie
instances (empty if input isnull
)
-
getWebPatterns
public static List<WebPattern> getWebPatterns(Class<?> targetClass, @Nullable Object handler, Executable method) Returns allWebPattern
instances declared on the given method in the given target class.This inspects all
HandleWeb
annotations on the method and resolves any declaredWebParameters
to extract associated patterns.- Parameters:
targetClass
- the target classhandler
- the handler instance (may benull
)method
- the method to inspect- Returns:
- a list of
WebPattern
instances associated with the method
-
getHandlerPath
public static String getHandlerPath(Class<?> targetClass, @Nullable Object handler, @Nullable Executable method) Computes the complete@Path
value for the given handler.targetClass
is required;handler
andmethod
are not. The path value is determined by inspecting the following elements:- sub-packages
- class package
- target class
- handler properties (if the given handler is not null)
- handler method (if the given method is not null)
@Path
values are joined together in the order listed above, with a slash as delimiter. If any of the@Path
values start with a slash, the chain is reset. -
emptyHeaderMap
Returns a new case-insensitive header map, with keys compared ignoring case.- Returns:
- an empty case-insensitive
Map
for headers
-
asHeaderMap
Converts the input map into a case-insensitive header map.Keys in the result will be case-insensitive, with the contents copied from the input map.
- Parameters:
input
- the input map- Returns:
- a case-insensitive map containing the same entries
-
getHeaders
Retrieves a case-insensitive map of headers from the provided Metadata object. -
getHeader
Retrieves the first header value for the given name from the provided Metadata object. -
hasPathParameter
Checks if the given path contains a named path parameter. -
extractPathParameters
Extracts all named path parameters from the given path.Example usage:
E.g. List<String> params = extractPathParameters("/games/{gameId}/refund/{orderId}"); // → ["gameId", "orderId"]
-
replacePathParameter
Replaces named path parameter with provided value.Example usage:
replacePathParameter("/users/{userId}/games/{gameId}", "userId", "123"); // → "/users/123/games/{gameId}"
-
concatenateUrlParts
Concatenates multiple segments of a URL, ensuring that:- Double slashes in schemes like
https://
are preserved - Duplicate slashes between path segments are removed
- Null or empty segments are skipped
- Parameters:
parts
- URL parts, e.g.,"https://", "example.org", "/api/", "/v1/"
- Returns:
- A clean, properly concatenated URL, e.g.
"https://example.org/api/v1/"
- Double slashes in schemes like
-