Package io.toro.martini
Class UrlMethods
- java.lang.Object
-
- io.toro.martini.UrlMethods
-
public final class UrlMethods extends Object
This class provides various methods for manipulating URLs.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static String
asUrlPathSegment(String string)
Escapes the provided string such that they can be safely included in URL path segments.static String
constructUrl(javax.servlet.http.HttpServletRequest request)
Get the HTTP URL to which anHttpServletRequest
was sent without the context path.static String
constructUrl(javax.servlet.http.HttpServletRequest request, boolean includeContextPath)
Get the HTTP URL to which anHttpServletRequest
was sent.static String
extractRemainder(String from, String uri)
Given a certain portion of a URL, extract the remaining portion that immediately follows after the provided portion.static String
filenameFromContentDisposition(String name)
Attempts to extract thefilename
from a provided string.static String
filenameFromUrl(String url)
Attempts to extract a filename based on a provided URL.static String
getBaseUrl(javax.servlet.http.HttpServletRequest req)
Get the base URL to which the request was sent.static String
getBaseUrl(javax.servlet.http.HttpServletRequest req, boolean withProtocol)
Get the base URL to which the request was sent.
-
-
-
Method Detail
-
constructUrl
public static String constructUrl(javax.servlet.http.HttpServletRequest request)
Get the HTTP URL to which anHttpServletRequest
was sent without the context path. The returned HTTP URL will only include the protocol, hostname, and port number (in order).- Returns:
- the URL where the request was sent, without the context path
-
constructUrl
public static String constructUrl(javax.servlet.http.HttpServletRequest request, boolean includeContextPath)
Get the HTTP URL to which anHttpServletRequest
was sent. The returned HTTP URL will only include the protocol, hostname, port number, and optionally the context path (in order). Other parts of the URL, like query parameters, will be excluded.- Parameters:
includeContextPath
- whether or not to include the context path in the returned URL string- Returns:
- the URL where the request was sent
-
getBaseUrl
public static String getBaseUrl(javax.servlet.http.HttpServletRequest req)
Get the base URL to which the request was sent. The base URL will only include the:- protocol;
- host name; and
- port number (if the request was sent to a non-default port)
- Returns:
- the base URL to which the request was sent
-
getBaseUrl
public static String getBaseUrl(javax.servlet.http.HttpServletRequest req, boolean withProtocol)
Get the base URL to which the request was sent. The base URL will only include the:- protocol (if
withProtocol
is true); - host name; and
- port number (if the request was sent to a non-default port)
- Returns:
- the base URL to which the request was sent
- protocol (if
-
extractRemainder
public static String extractRemainder(String from, String uri)
Given a certain portion of a URL, extract the remaining portion that immediately follows after the provided portion.
- Parameters:
from
- indicates where the remaining fragment startsuri
- the URI whose remaining fragment will be extracted- Returns:
- the remaining portion of the URL after
from
-
filenameFromContentDisposition
public static String filenameFromContentDisposition(String name)
Attempts to extract the
filename
from a provided string.Acceptable values are in the form of:
Content-Disposition: inline Content-Disposition: attachment Content-Disposition: attachment; filename="filename.jpg" Content-Disposition: form-data Content-Disposition: form-data; name="fieldName" Content-Disposition: form-data; name="fieldName"; filename="filename.jpg"
The results for the above will then be:filenameFromContentDisposition( "inline" ) => null filenameFromContentDisposition( "attachment" ) => null filenameFromContentDisposition( "attachment; filename=\"filename.jpg\"" ) => "filename.jpg" filenameFromContentDisposition( "form-data" ) => null filenameFromContentDisposition( "form-data; name="fieldName"" ) => null filenameFromContentDisposition( "form-data; name="fieldName"; filename=\"filename.jpg\"" ) => "filename.jpg"
- Parameters:
name
- the value of the Content-Disposition header- Returns:
- the extracted filename based from
name
- See Also:
- Content Disposition on MDN
-
filenameFromUrl
public static String filenameFromUrl(String url)
Attempts to extract a filename based on a provided URL. This method simply takes the last path segment of a URL, stripping query parameters, if any.- Parameters:
url
- the string-based URL from where the filename will be resolved- Returns:
- the extracted filename from
url
, or null
-
asUrlPathSegment
public static String asUrlPathSegment(String string)
Escapes the provided string such that they can be safely included in URL path segments. All non-ASCII and slash / characters in the provided string will be escaped.
When escaping a string, the following rules apply:
- The alphanumeric characters "a" through "z", "A" through "Z", and "0" through "9" remain the same.
- The unreserved characters ".", "-", "~", and "_" remain the same.
- The general delimiters "@" and ":" remain the same.
- The subdelimiters "!", "$", "&", "'", "(", ")", "*", "+", ",", ";", and "=" remain the same.
- The space character " " is converted into %20.
- All other characters are converted into one or more bytes using UTF-8 encoding and each byte is then represented by the 3-character string "%XY", where "XY" is the two-digit, uppercase, hexadecimal representation of the byte value.
-
-