Package com.helger.commons.url
Class URLHelper
- java.lang.Object
-
- com.helger.commons.url.URLHelper
-
@ThreadSafe public final class URLHelper extends Object
URL utilities.- Author:
- Philip Helger
-
-
Field Summary
Fields Modifier and Type Field Description static charAMPERSANDSeparator between params: &static StringAMPERSAND_STRstatic CharsetCHARSET_URL_OBJDefault URL charset is UTF-8static charEQUALSSeparator between param name and param value: =static StringEQUALS_STRstatic charHASHSeparator between URL path and anchor name: #static StringHASH_STRstatic StringPROTOCOL_FILEThe protocol for file resourcesstatic charQUESTIONMARKSeparator before first param: ?static StringQUESTIONMARK_STRstatic StringREGEX_URN
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static FilegetAsFile(URL aURL)static FilegetAsFileOrNull(URL aURL)static URIgetAsURI(String sURI)Get the passed String as an URI.static URIgetAsURI(URL aURL)Get the passed URL as an URI.static URLgetAsURL(String sURL)Get the passed String as an URL.static URLgetAsURL(String sURL, boolean bWhine)Get the passed String as an URL.static URLgetAsURL(URI aURI)Get 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 StringurlDecode(String sValue)URL-decode the passed value automatically handling charset issues.static StringurlDecode(String sValue, Charset aCharset)URL-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 StringurlEncode(String sValue)URL-encode the passed value automatically handling charset issues.static StringurlEncode(String sValue, Charset aCharset)URL-encode the passed value automatically handling charset issues.
-
-
-
Field Detail
-
CHARSET_URL_OBJ
public static final Charset CHARSET_URL_OBJ
Default URL charset is UTF-8
-
QUESTIONMARK
public static final char QUESTIONMARK
Separator before first param: ?- See Also:
- Constant Field Values
-
QUESTIONMARK_STR
public static final String QUESTIONMARK_STR
-
AMPERSAND
public static final char AMPERSAND
Separator between params: &- See Also:
- Constant Field Values
-
AMPERSAND_STR
public static final String AMPERSAND_STR
-
EQUALS
public static final char EQUALS
Separator between param name and param value: =- See Also:
- Constant Field Values
-
EQUALS_STR
public static final String EQUALS_STR
-
HASH
public static final char HASH
Separator between URL path and anchor name: #- See Also:
- Constant Field Values
-
HASH_STR
public static final String HASH_STR
-
PROTOCOL_FILE
public static final String PROTOCOL_FILE
The protocol for file resources- See Also:
- Constant Field Values
-
REGEX_URN
@RegEx public static final String REGEX_URN
- See Also:
- Constant Field Values
-
-
Method Detail
-
urlDecode
@Nonnull public static String urlDecode(@Nonnull String sValue)
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(String, Charset)
-
urlDecode
@Nonnull public static String urlDecode(@Nonnull String sValue, @Nonnull Charset aCharset)
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:
URLDecoder.decode(String, String)
-
urlDecodeOrNull
@Nullable public static String urlDecodeOrNull(@Nonnull String sValue)
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:
urlDecode(String, Charset)
-
urlDecodeOrNull
@Nullable public static String urlDecodeOrNull(@Nullable String sValue, @Nonnull Charset aCharset)
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:
URLDecoder.decode(String, String)
-
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:
urlDecode(String, Charset)
-
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:
URLDecoder.decode(String, String)
-
urlEncode
@Nonnull public static String urlEncode(@Nonnull String sValue)
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
@Nonnull public static String urlEncode(@Nonnull String sValue, @Nonnull Charset aCharset)
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
@Nullable public static String getCleanURLPartWithoutUmlauts(@Nullable String sURLPart)
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
@Nonnull public static ISimpleURL getAsURLData(@Nonnull String sHref)
-
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
@Nonnull public static String getURLString(@Nonnull ISimpleURL aURL, @Nullable Charset aParameterCharset)
-
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
@Nullable public static URL getAsURL(@Nullable String sURL, boolean bWhine)
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
@Nullable public static URL getAsURL(@Nullable String sURL)
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
@Nullable public static URL getAsURL(@Nullable URI aURI)
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
@Nullable public static URI getAsURI(@Nullable String sURI)
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
@Nullable public static URI getAsURI(@Nullable URL aURL)
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.
-
getClassPathURL
@Nullable public static URL getClassPathURL(@Nonnull @Nonempty String sPath)
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
public static boolean isClassPathURLExisting(@Nonnull @Nonempty String sPath)
-
isClassPathURLExisting
public static boolean isClassPathURLExisting(@Nonnull @Nonempty String sPath, @Nonnull ClassLoader aClassLoader)
-
isValidURN
public static boolean isValidURN(@Nullable String sURN)
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
-
-