Package com.helger.commons.url
Class URLHelper
java.lang.Object
com.helger.commons.url.URLHelper
URL utilities.
- Author:
- Philip Helger
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final charSeparator between params: &static final Stringstatic final CharsetDefault URL charset is UTF-8static final charSeparator between param name and param value: =static final Stringstatic final charSeparator between URL path and anchor name: #static final Stringstatic final StringThe protocol for file resourcesstatic final charSeparator before first param: ?static final Stringstatic final String -
Method Summary
Modifier and TypeMethodDescriptionstatic Filestatic FilegetAsFileOrNull(URL aURL) static URIGet the passed String as an URI.static URIGet the passed URL as an URI.static URLGet the passed String as an URL.static URLGet the passed String as an URL.static URLGet the passed URI as an URL.static ISimpleURLgetAsURLData(String sHref) static ISimpleURLgetAsURLData(String sHref, IDecoder<String, String> aParameterDecoder) Parses the passed URL into a structured formstatic URLgetClassPathURL(String sPath) Get the URL for the specified path using automatic class loader handling.static StringgetCleanURLPartWithoutUmlauts(String sURLPart) Clean an URL part from nasty Umlauts.static InputStreamgetInputStream(URL aURL, int nConnectTimeoutMS, int nReadTimeoutMS, Consumer<? super URLConnection> aConnectionModifier, IMutableWrapper<IOException> aExceptionHolder) Get an input stream from the specified URL.static URLParameterListgetParsedQueryParameters(String sQueryString) static URLParameterListgetParsedQueryParameters(String sQueryString, IDecoder<String, String> aParameterDecoder) static StringgetQueryParametersAsString(List<? extends URLParameter> aQueryParams, IEncoder<String, String> aQueryParameterEncoder) Create a parameter string.static StringgetURLString(ISimpleURL aURL, Charset aParameterCharset) static StringgetURLString(String sPath, String sQueryParams, String sAnchor) Get the final representation of the URL using the specified elements.static StringgetURLString(String sPath, List<? extends URLParameter> aQueryParams, String sAnchor, IEncoder<String, String> aQueryParameterEncoder) Get the final representation of the URL using the specified elements.static StringgetURLString(String sPath, List<? extends URLParameter> aQueryParams, String sAnchor, Charset aParameterCharset) Get the final representation of the URL using the specified elements.static booleanisClassPathURLExisting(String sPath) static booleanisClassPathURLExisting(String sPath, ClassLoader aClassLoader) static booleanisValidURN(String sURN) Check if the provided string is valid according to RFC 2141.static StringURL-decode the passed value automatically handling charset issues.static StringURL-decode the passed value automatically handling charset issues.static StringurlDecodeOrDefault(String sValue, String sDefault) URL-decode the passed value automatically handling charset issues.static StringurlDecodeOrDefault(String sValue, Charset aCharset, String sDefault) URL-decode the passed value automatically handling charset issues.static StringurlDecodeOrNull(String sValue) URL-decode the passed value automatically handling charset issues.static StringurlDecodeOrNull(String sValue, Charset aCharset) URL-decode the passed value automatically handling charset issues.static StringURL-encode the passed value automatically handling charset issues.static StringURL-encode the passed value automatically handling charset issues.
-
Field Details
-
CHARSET_URL_OBJ
Default URL charset is UTF-8 -
QUESTIONMARK
public static final char QUESTIONMARKSeparator before first param: ?- See Also:
-
QUESTIONMARK_STR
-
AMPERSAND
public static final char AMPERSANDSeparator between params: &- See Also:
-
AMPERSAND_STR
-
EQUALS
public static final char EQUALSSeparator between param name and param value: =- See Also:
-
EQUALS_STR
-
HASH
public static final char HASHSeparator between URL path and anchor name: #- See Also:
-
HASH_STR
-
PROTOCOL_FILE
The protocol for file resources- See Also:
-
REGEX_URN
- See Also:
-
-
Method Details
-
urlDecode
URL-decode the passed value automatically handling charset issues. The used char set is determined byCHARSET_URL_OBJ.- Parameters:
sValue- The value to be decoded. May not benull.- Returns:
- The decoded value.
- Throws:
IllegalArgumentException- if something goes wrong- See Also:
-
urlDecode
URL-decode the passed value automatically handling charset issues. The implementation usesURLCodecto do the hard work.- Parameters:
sValue- The value to be decoded. May not benull.aCharset- The charset to use. May not benull.- Returns:
- The decoded value.
- Throws:
IllegalArgumentException- if something goes wrong- See Also:
-
urlDecodeOrNull
URL-decode the passed value automatically handling charset issues. The used char set is determined byCHARSET_URL_OBJ.- Parameters:
sValue- The value to be decoded. May not benull.- Returns:
- The decoded value.
- Since:
- 9.4.1
- See Also:
-
urlDecodeOrNull
URL-decode the passed value automatically handling charset issues. The implementation usesURLCodecto do the hard work.- Parameters:
sValue- The value to be decoded. May benull.aCharset- The charset to use. May not benull.- Returns:
- The decoded value or
null. - Since:
- 9.4.1
- See Also:
-
urlDecodeOrDefault
@Nullable public static String urlDecodeOrDefault(@Nonnull String sValue, @Nullable String sDefault) URL-decode the passed value automatically handling charset issues. The used char set is determined byCHARSET_URL_OBJ.- Parameters:
sValue- The value to be decoded. May not benull.sDefault- The default value to be returned if decoding fails.- Returns:
- The decoded value or the default.
- Since:
- 9.4.1
- See Also:
-
urlDecodeOrDefault
@Nullable public static String urlDecodeOrDefault(@Nullable String sValue, @Nonnull Charset aCharset, @Nullable String sDefault) URL-decode the passed value automatically handling charset issues. The implementation usesURLCodecto do the hard work.- Parameters:
sValue- The value to be decoded. May benull.aCharset- The charset to use. May not benull.sDefault- The default value to be returned if decoding fails.- Returns:
- The decoded value or the default.
- Since:
- 9.4.1
- See Also:
-
urlEncode
URL-encode the passed value automatically handling charset issues. The used char set is determined byCHARSET_URL_OBJ.- Parameters:
sValue- The value to be encoded. May not benull.- Returns:
- The encoded value.
-
urlEncode
URL-encode the passed value automatically handling charset issues. This is a ripped, optimized version of URLEncoder.encode but without the UnsupportedEncodingException.- Parameters:
sValue- The value to be encoded. May not benull.aCharset- The charset to use. May not benull.- Returns:
- The encoded value.
-
getCleanURLPartWithoutUmlauts
Clean an URL part from nasty Umlauts. This mapping needs extension!- Parameters:
sURLPart- The original URL part. May benull.- Returns:
- The cleaned version or
nullif the input wasnull.
-
getAsURLData
-
getAsURLData
@Nonnull public static ISimpleURL getAsURLData(@Nonnull String sHref, @Nullable IDecoder<String, String> aParameterDecoder) Parses the passed URL into a structured form- Parameters:
sHref- The URL to be parsedaParameterDecoder- The parameter decoder to use. May benull.- Returns:
- the corresponding
ISimpleURLrepresentation of the passed URL
-
getParsedQueryParameters
@Nonnull @ReturnsMutableCopy public static URLParameterList getParsedQueryParameters(@Nullable String sQueryString, @Nullable IDecoder<String, String> aParameterDecoder) -
getParsedQueryParameters
@Nonnull @ReturnsMutableCopy public static URLParameterList getParsedQueryParameters(@Nullable String sQueryString) -
getURLString
@Nullable public static String getURLString(@Nullable String sPath, @Nullable String sQueryParams, @Nullable String sAnchor) Get the final representation of the URL using the specified elements.- Parameters:
sPath- The main path. May benull.sQueryParams- The set of all query parameters already concatenated with the correct characters (& and =). May benull.sAnchor- An optional anchor to be added. May benull.- Returns:
- May be
nullif path, anchor and parameters arenull.
-
getQueryParametersAsString
@Nullable public static String getQueryParametersAsString(@Nullable List<? extends URLParameter> aQueryParams, @Nullable IEncoder<String, String> aQueryParameterEncoder) Create a parameter string. This is also suitable for POST body (e.g. for web form submission).- Parameters:
aQueryParams- Parameter map. May benullor empty.aQueryParameterEncoder- The encoder to be used to encode parameter names and parameter values. May benull. This may be e.g. aURLParameterEncoder.- Returns:
nullif no parameter is present.
-
getURLString
-
getURLString
@Nullable public static String getURLString(@Nullable String sPath, @Nullable List<? extends URLParameter> aQueryParams, @Nullable String sAnchor, @Nullable IEncoder<String, String> aQueryParameterEncoder) Get the final representation of the URL using the specified elements.- Parameters:
sPath- The main path. May benull.aQueryParams- The list of query parameters to be appended. May benull.sAnchor- An optional anchor to be added. May benull.aQueryParameterEncoder- The parameters encoding to be used. May benull.- Returns:
- May be
nullif path, anchor and parameters arenull.
-
getURLString
@Nullable public static String getURLString(@Nullable String sPath, @Nullable List<? extends URLParameter> aQueryParams, @Nullable String sAnchor, @Nullable Charset aParameterCharset) Get the final representation of the URL using the specified elements.- Parameters:
sPath- The main path. May benull.aQueryParams- The list of parameters to be appended. May benull.sAnchor- An optional anchor to be added. May benull.aParameterCharset- If notnullthe parameters are encoded using this charset.- Returns:
- May be
nullif all parameters arenull.
-
getAsURL
Get the passed String as an URL. If the string is empty or not an URLnullis returned.- Parameters:
sURL- Source URL. May benull.bWhine-trueto debug log if conversion failed- Returns:
nullif the passed URL is empty or invalid.
-
getAsURL
Get the passed String as an URL. If the string is empty or not an URLnullis returned.- Parameters:
sURL- Source URL. May benull.- Returns:
nullif the passed URL is empty or invalid.
-
getAsURL
Get the passed URI as an URL. If the URI is null or cannot be converted to an URLnullis returned.- Parameters:
aURI- Source URI. May benull.- Returns:
nullif the passed URI is null or cannot be converted to an URL.
-
getAsURI
Get the passed String as an URI. If the string is empty or not an URInullis returned.- Parameters:
sURI- Source URI. May benull.- Returns:
nullif the passed URI is empty or invalid.
-
getAsURI
Get the passed URL as an URI. If the URL is null or not an URInullis returned.- Parameters:
aURL- Source URL. May benull.- Returns:
nullif the passed URL is empty or invalid.
-
getInputStream
@Nullable public static InputStream getInputStream(@Nonnull URL aURL, @CheckForSigned int nConnectTimeoutMS, @CheckForSigned int nReadTimeoutMS, @Nullable Consumer<? super URLConnection> aConnectionModifier, @Nullable IMutableWrapper<IOException> aExceptionHolder) Get an input stream from the specified URL. By default caching is disabled. This method only handles GET requests - POST requests are not possible.- Parameters:
aURL- The URL to use. May not benull.nConnectTimeoutMS- Connect timeout milliseconds. 0 == infinite. < 0: ignored.nReadTimeoutMS- Read timeout milliseconds. 0 == infinite. < 0: ignored.aConnectionModifier- An optional callback object to modify the URLConnection before it is opened.aExceptionHolder- An optional exception holder for further outside investigation.- Returns:
nullif the input stream could not be opened.
-
getAsFile
-
getAsFileOrNull
-
getClassPathURL
Get the URL for the specified path using automatic class loader handling. The class loaders are iterated in the following order:- Default class loader (usually the context class loader)
- The class loader of this class
- The system class loader
- Parameters:
sPath- The path to be resolved. May neither benullnor empty.- Returns:
nullif the path could not be resolved.
-
isClassPathURLExisting
-
isClassPathURLExisting
public static boolean isClassPathURLExisting(@Nonnull @Nonempty String sPath, @Nonnull ClassLoader aClassLoader) -
isValidURN
Check if the provided string is valid according to RFC 2141. Leading and trailing spaces of the value to check will result in a negative result.- Parameters:
sURN- the URN to be validated. May benull.- Returns:
trueif the provided URN is not empty and matches the regular expressionREGEX_URN.- Since:
- 10.0.0
-