public final class UrlMethods extends Object
Modifier and Type | Method and Description |
---|---|
static String |
asUrlPathSegment(String string)
Escapes the provided string such that they can be safely included
in URL path segments.
|
static String |
constructUrl(HttpServletRequest request)
Get the HTTP URL to which an
HttpServletRequest was sent without the context path. |
static String |
constructUrl(HttpServletRequest request,
boolean includeContextPath)
Get the HTTP URL to which an
HttpServletRequest 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 the
filename from a provided string. |
static String |
filenameFromUrl(String url)
Attempts to extract a filename based on a provided URL.
|
static String |
getBaseUrl(HttpServletRequest req)
Get the base URL to which the request was sent.
|
static String |
getBaseUrl(HttpServletRequest req,
boolean withProtocol)
Get the base URL to which the request was sent.
|
public static String constructUrl(HttpServletRequest request)
HttpServletRequest
was sent without the context path.
The returned HTTP URL will only include
the protocol, hostname, and port number (in order).public static String constructUrl(HttpServletRequest request, boolean includeContextPath)
HttpServletRequest
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.includeContextPath
- whether or not to include the context path in the returned URL stringpublic static String getBaseUrl(HttpServletRequest req)
public static String getBaseUrl(HttpServletRequest req, boolean withProtocol)
withProtocol
is true);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.
from
- indicates where the remaining fragment startsuri
- the URI whose remaining fragment will be extractedfrom
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"
name
- the value of the Content-Disposition headername
public static String filenameFromUrl(String url)
url
- the string-based URL from where the filename will be resolvedurl
, or nullpublic 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:
Copyright © 2020. All rights reserved.