Package jodd.net
Class URLCoder
- java.lang.Object
-
- jodd.net.URLCoder
-
public class URLCoder extends java.lang.ObjectEncodes URLs correctly, significantly faster and more convenient.Here is an example of full URL: https://jodd:ddoj@www.jodd.org:8080/file;p=1?q=2#third. It consist of:
- scheme (https)
- user (jodd)
- password (ddoj)
- host (www.jodd.org)
- port (8080)
- path (file)
- path parameter (p=1)
- query parameter (q=2)
- fragment (third)
builder. It provides fluent interface for defining query parameters.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classURLCoder.Builder(package private) static classURLCoder.URIPartEnumeration to identify the parts of a URI.
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.StringHOST_PATTERNprivate static java.lang.StringHTTP_PATTERNprivate static java.util.regex.PatternHTTP_URL_PATTERNprivate static java.lang.StringLAST_PATTERNprivate static java.lang.StringPATH_PATTERNprivate static java.lang.StringPORT_PATTERNprivate static java.lang.StringQUERY_PATTERNprivate static java.lang.StringSCHEME_PATTERNprivate static java.util.regex.PatternURI_PATTERNprivate static java.lang.StringUSERINFO_PATTERN
-
Constructor Summary
Constructors Constructor Description URLCoder()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static URLCoder.Builderbuild(java.lang.String path)Creates URL builder for user-friendly way of building URLs.static URLCoder.Builderbuild(java.lang.String path, boolean encodePath)Creates URL builder with given path that can be optionally encoded.static java.lang.Stringencode(java.lang.String string)static java.lang.Stringencode(java.lang.String string, java.nio.charset.Charset encoding)Encodes string using default RFCP rules.private static byte[]encodeBytes(byte[] source, URLCoder.URIPart uriPart)Encodes byte array using allowed characters fromURLCoder.URIPart.static java.lang.StringencodeFragment(java.lang.String fragment)static java.lang.StringencodeFragment(java.lang.String fragment, java.nio.charset.Charset encoding)Encodes the given URI fragment with the given encoding.static java.lang.StringencodeHost(java.lang.String host)static java.lang.StringencodeHost(java.lang.String host, java.nio.charset.Charset encoding)Encodes the given URI host with the given encoding.static java.lang.StringencodeHttpUrl(java.lang.String httpUrl)static java.lang.StringencodeHttpUrl(java.lang.String httpUrl, java.nio.charset.Charset encoding)Encodes the given HTTP URI into an encoded String.static java.lang.StringencodePath(java.lang.String path)static java.lang.StringencodePath(java.lang.String path, java.nio.charset.Charset encoding)Encodes the given URI path with the given encoding.static java.lang.StringencodePathSegment(java.lang.String segment)static java.lang.StringencodePathSegment(java.lang.String segment, java.nio.charset.Charset encoding)Encodes the given URI path segment with the given encoding.static java.lang.StringencodePort(java.lang.String port)static java.lang.StringencodePort(java.lang.String port, java.nio.charset.Charset encoding)Encodes the given URI port with the given encoding.static java.lang.StringencodeQuery(java.lang.String query)static java.lang.StringencodeQuery(java.lang.String query, java.nio.charset.Charset encoding)Encodes the given URI query with the given encoding.static java.lang.StringencodeQueryParam(java.lang.String queryParam)static java.lang.StringencodeQueryParam(java.lang.String queryParam, java.nio.charset.Charset encoding)Encodes the given URI query parameter with the given encoding.static java.lang.StringencodeScheme(java.lang.String scheme)static java.lang.StringencodeScheme(java.lang.String scheme, java.nio.charset.Charset encoding)Encodes the given URI scheme with the given encoding.static java.lang.StringencodeUri(java.lang.String uri)static java.lang.StringencodeUri(java.lang.String uri, java.nio.charset.Charset encoding)Encodes the given source URI into an encoded String.private static java.lang.StringencodeUriComponent(java.lang.String source, java.nio.charset.Charset encoding, URLCoder.URIPart uriPart)Encodes single URI component.private static java.lang.StringencodeUriComponents(java.lang.String scheme, java.lang.String authority, java.lang.String userInfo, java.lang.String host, java.lang.String port, java.lang.String path, java.lang.String query, java.lang.String fragment, java.nio.charset.Charset encoding)static java.lang.StringencodeUserInfo(java.lang.String userInfo)static java.lang.StringencodeUserInfo(java.lang.String userInfo, java.nio.charset.Charset encoding)Encodes the given URI user info with the given encoding.
-
-
-
Field Detail
-
SCHEME_PATTERN
private static final java.lang.String SCHEME_PATTERN
- See Also:
- Constant Field Values
-
HTTP_PATTERN
private static final java.lang.String HTTP_PATTERN
- See Also:
- Constant Field Values
-
USERINFO_PATTERN
private static final java.lang.String USERINFO_PATTERN
- See Also:
- Constant Field Values
-
HOST_PATTERN
private static final java.lang.String HOST_PATTERN
- See Also:
- Constant Field Values
-
PORT_PATTERN
private static final java.lang.String PORT_PATTERN
- See Also:
- Constant Field Values
-
PATH_PATTERN
private static final java.lang.String PATH_PATTERN
- See Also:
- Constant Field Values
-
QUERY_PATTERN
private static final java.lang.String QUERY_PATTERN
- See Also:
- Constant Field Values
-
LAST_PATTERN
private static final java.lang.String LAST_PATTERN
- See Also:
- Constant Field Values
-
URI_PATTERN
private static final java.util.regex.Pattern URI_PATTERN
-
HTTP_URL_PATTERN
private static final java.util.regex.Pattern HTTP_URL_PATTERN
-
-
Method Detail
-
encodeUriComponent
private static java.lang.String encodeUriComponent(java.lang.String source, java.nio.charset.Charset encoding, URLCoder.URIPart uriPart)Encodes single URI component.
-
encodeBytes
private static byte[] encodeBytes(byte[] source, URLCoder.URIPart uriPart)Encodes byte array using allowed characters fromURLCoder.URIPart.
-
encode
public static java.lang.String encode(java.lang.String string, java.nio.charset.Charset encoding)Encodes string using default RFCP rules.
-
encode
public static java.lang.String encode(java.lang.String string)
-
encodeScheme
public static java.lang.String encodeScheme(java.lang.String scheme, java.nio.charset.Charset encoding)Encodes the given URI scheme with the given encoding.
-
encodeScheme
public static java.lang.String encodeScheme(java.lang.String scheme)
-
encodeUserInfo
public static java.lang.String encodeUserInfo(java.lang.String userInfo, java.nio.charset.Charset encoding)Encodes the given URI user info with the given encoding.
-
encodeUserInfo
public static java.lang.String encodeUserInfo(java.lang.String userInfo)
-
encodeHost
public static java.lang.String encodeHost(java.lang.String host, java.nio.charset.Charset encoding)Encodes the given URI host with the given encoding.
-
encodeHost
public static java.lang.String encodeHost(java.lang.String host)
-
encodePort
public static java.lang.String encodePort(java.lang.String port, java.nio.charset.Charset encoding)Encodes the given URI port with the given encoding.
-
encodePort
public static java.lang.String encodePort(java.lang.String port)
-
encodePath
public static java.lang.String encodePath(java.lang.String path, java.nio.charset.Charset encoding)Encodes the given URI path with the given encoding.
-
encodePath
public static java.lang.String encodePath(java.lang.String path)
-
encodePathSegment
public static java.lang.String encodePathSegment(java.lang.String segment, java.nio.charset.Charset encoding)Encodes the given URI path segment with the given encoding.
-
encodePathSegment
public static java.lang.String encodePathSegment(java.lang.String segment)
-
encodeQuery
public static java.lang.String encodeQuery(java.lang.String query, java.nio.charset.Charset encoding)Encodes the given URI query with the given encoding.
-
encodeQuery
public static java.lang.String encodeQuery(java.lang.String query)
-
encodeQueryParam
public static java.lang.String encodeQueryParam(java.lang.String queryParam, java.nio.charset.Charset encoding)Encodes the given URI query parameter with the given encoding.
-
encodeQueryParam
public static java.lang.String encodeQueryParam(java.lang.String queryParam)
-
encodeFragment
public static java.lang.String encodeFragment(java.lang.String fragment, java.nio.charset.Charset encoding)Encodes the given URI fragment with the given encoding.
-
encodeFragment
public static java.lang.String encodeFragment(java.lang.String fragment)
-
encodeUri
public static java.lang.String encodeUri(java.lang.String uri)
- See Also:
encodeUri(String, Charset)
-
encodeUri
public static java.lang.String encodeUri(java.lang.String uri, java.nio.charset.Charset encoding)Encodes the given source URI into an encoded String. All various URI components are encoded according to their respective valid character sets.This method does not attempt to encode "=" and "&" characters in query parameter names and query parameter values because they cannot be parsed in a reliable way.
-
encodeHttpUrl
public static java.lang.String encodeHttpUrl(java.lang.String httpUrl)
- See Also:
encodeHttpUrl(String, Charset)
-
encodeHttpUrl
public static java.lang.String encodeHttpUrl(java.lang.String httpUrl, java.nio.charset.Charset encoding)Encodes the given HTTP URI into an encoded String. All various URI components are encoded according to their respective valid character sets.This method does not support fragments (
#), as these are not supposed to be sent to the server, but retained by the client.This method does not attempt to encode "=" and "&" characters in query parameter names and query parameter values because they cannot be parsed in a reliable way.
-
encodeUriComponents
private static java.lang.String encodeUriComponents(java.lang.String scheme, java.lang.String authority, java.lang.String userInfo, java.lang.String host, java.lang.String port, java.lang.String path, java.lang.String query, java.lang.String fragment, java.nio.charset.Charset encoding)
-
build
public static URLCoder.Builder build(java.lang.String path)
Creates URL builder for user-friendly way of building URLs. Provided path is parsed andencoded.- See Also:
build(String, boolean)
-
build
public static URLCoder.Builder build(java.lang.String path, boolean encodePath)
Creates URL builder with given path that can be optionally encoded. Since most of the time path is valid and does not require to be encoded, use this method to gain some performance. When encoding flag is turned off, provided path is used without processing.The purpose of builder is to help with query parameters. All other URI parts should be set previously or after the URL is built.
-
-