Class ConversionUtils
-
- All Implemented Interfaces:
public class ConversionUtils
General purpose conversion utilities related to HTTP/HTML
-
-
Constructor Summary
Constructors Constructor Description ConversionUtils()
-
Method Summary
Modifier and Type Method Description static String
getEncodingFromContentType(String contentType)
Extract the encoding (charset) from the Content-Type, e.g. static String
percentEncode(String value)
Encodes strings for multipart/form-data
names and values.static String
encodeWithEntities(String value, Charset charset)
Encodes non-encodable characters as HTML entities like e.g. static URL
makeRelativeURL(URL baseURL, String location)
Generate an absolute URL from a possibly relative location, allowing for extraneous leading "../" segments. static String
escapeIllegalURLCharacters(String url)
static URI
sanitizeUrl(URL url)
Checks a URL and encodes it if necessary, i.e. static String
removeSlashDotDot(String url)
collapses absolute or relative URLs containing '/.. -
-
Method Detail
-
getEncodingFromContentType
static String getEncodingFromContentType(String contentType)
Extract the encoding (charset) from the Content-Type, e.g. "text/html; charset=utf-8".
- Parameters:
contentType
- string from which the encoding should be extracted
-
percentEncode
@API(status = API.Status.MAINTAINED, since = "5.6") static String percentEncode(String value)
Encodes strings for
multipart/form-data
names and values. The encoding is"
as%22
,CR
as%0D
, andLF
as%0A
. Note:%
is not encoded, so it creates ambiguity which might be resolved in a later specification version.- Parameters:
value
- input value to convert
-
encodeWithEntities
@API(status = API.Status.EXPERIMENTAL, since = "5.6.1") static String encodeWithEntities(String value, Charset charset)
Encodes non-encodable characters as HTML entities like e.g. 😂 for 😂.
- Parameters:
value
- value to encodecharset
- charset that will be used for encoding, defaults to UTF-8 if null
-
makeRelativeURL
static URL makeRelativeURL(URL baseURL, String location)
Generate an absolute URL from a possibly relative location, allowing for extraneous leading "../" segments. The Java URL constructor does not remove these.
- Parameters:
baseURL
- the base URL which is used to resolve missing protocol/host in the locationlocation
- the location, possibly with extraneous leading "..
-
escapeIllegalURLCharacters
static String escapeIllegalURLCharacters(String url)
- Parameters:
url
- String Url to escape
-
sanitizeUrl
static URI sanitizeUrl(URL url)
Checks a URL and encodes it if necessary, i.e. if it is not currently correctly encoded. Warning: it may not work on all unencoded URLs.
- Parameters:
url
- non-encoded URL
-
removeSlashDotDot
static String removeSlashDotDot(String url)
collapses absolute or relative URLs containing '/..' converting
http://host/path1/../path2
tohttp://host/path2
or/one/two/../three
to/one/three
- Parameters:
url
- in which the '/..
-
-
-
-