Class ConversionUtils
-
- All Implemented Interfaces:
public class ConversionUtilsGeneral purpose conversion utilities related to HTTP/HTML
-
-
Constructor Summary
Constructors Constructor Description ConversionUtils()
-
Method Summary
Modifier and Type Method Description static StringgetEncodingFromContentType(String contentType)Extract the encoding (charset) from the Content-Type, e.g. static StringpercentEncode(String value)Encodes strings for multipart/form-datanames and values.static StringencodeWithEntities(String value, Charset charset)Encodes non-encodable characters as HTML entities like e.g. static URLmakeRelativeURL(URL baseURL, String location)Generate an absolute URL from a possibly relative location, allowing for extraneous leading "../" segments. static StringescapeIllegalURLCharacters(String url)static URIsanitizeUrl(URL url)Checks a URL and encodes it if necessary, i.e. static StringremoveSlashDotDot(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- Returns:
the charset encoding - or
null, if none was found or the charset is not supported
-
percentEncode
@API(status = API.Status.MAINTAINED, since = "5.6") static String percentEncode(String value)
Encodes strings for
multipart/form-datanames and values. The encoding is"as%22,CRas%0D, andLFas%0A. Note:%is not encoded, so it creates ambiguity which might be resolved in a later specification version.- Parameters:
value- input value to convert- Returns:
converted value
- Since:
5.6
-
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- Returns:
input value with non-encodable characters replaced with HTML entities
-
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 "..- Returns:
URL with extraneous ../ removed
-
escapeIllegalURLCharacters
static String escapeIllegalURLCharacters(String url)
- Parameters:
url- String Url to escape- Returns:
String cleaned up url
-
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- Returns:
URI which has been encoded as necessary
-
removeSlashDotDot
static String removeSlashDotDot(String url)
collapses absolute or relative URLs containing '/..' converting
http://host/path1/../path2tohttp://host/path2or/one/two/../threeto/one/three- Parameters:
url- in which the '/..- Returns:
collapsed URL
-
-
-
-