Package com.jalios.util
Class URLUtils
java.lang.Object
com.jalios.util.URLUtils
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionstatic URLUtils.UrlBuilderCreates a fluent URL builder using the same parameter merge rules asbuildUrl(String, Map).static StringBuild an URL by adding the given parameters.static StringgetQueryString(Map<String, String[]> parametersMap, String... excludedParams) Returns a query String from a map of parameters.static booleanisAbsoluteUrl(String url) Check if the specified URL is absolute.Parses an URL, extracts the query string and returns a map with the parameter values.static StringParses an URL, extracts the query string and returns the parameters as JSON format.static StringparseUrlQueryStringAsJson(String url, Function<Map<String, String[]>, Map<String, Object>> paramMapUpdator) Parses an URL, extracts the query string and returns the parameters as JSON format.
-
Method Details
-
isAbsoluteUrl
Check if the specified URL is absolute.An URL is considered absolute if it starts with the following element, in that order
- (optionnally) with a protocol name, such as "http:", "https:", "file:"
- a double slash (//)
- anything else
This method does NOT check the URL is valid
- Parameters:
url-- Returns:
- true if URL is absolute, false otherwise
- Since:
- jcms-10.0.5 / JCMS-8281
-
parseUrlQueryString
Parses an URL, extracts the query string and returns a map with the parameter values. The map has the same structure as the one returned by javax.servlet.ServletRequest.getParameterMap(). For each parameter name, the map contains a string array with the parameter values.- Parameters:
url- an URL or an URL query string.- Returns:
- a map containing parameter names as keys and parameter values as map values.
-
parseUrlQueryStringAsJson
Parses an URL, extracts the query string and returns the parameters as JSON format.- Parameters:
url- an URL or an URL query string.- Returns:
- String containing the parameters (name and values) as JSON format
-
parseUrlQueryStringAsJson
public static String parseUrlQueryStringAsJson(String url, Function<Map<String, String[]>, Map<String, Object>> paramMapUpdator) Parses an URL, extracts the query string and returns the parameters as JSON format.Example:
String url = "http://localhost:8080/jcms/jcms/j_6/en/home?param1=value1¶m2=value21¶m2=value22"; String json = URLUtils.parseUrlQueryStringAsJson(url); Result: {"param1":["value1"],"param2":["value21","value22"]}It's possible to use aFunctionimplementation to manipulate the Map of parameters extracted from URL, before to be convert in JSON.Example:
String url = "http://localhost:8080/jcms/jcms/j_6/en/home?param1=value1¶m2=value21¶m2=value22"; json = URLUtils.parseUrlQueryStringAsJson(url, (map) -> Util.getHashMap("params", map)); Result: {"params":{"param1":["value1"],"param2":["value21","value22"]}}- Parameters:
url- an URL or an URL query string.paramMapUpdator- implementation ofFunctionto manipulate the Map of parameters extracted from URL- Returns:
- String containing the parameters (name and values) as JSON format
-
getQueryString
Returns a query String from a map of parameters.- Parameters:
parametersMap-Mapof parametersexcludedParams- Parameter to exclude in the final query string- Returns:
- String representing a query string
-
buildUrl
Creates a fluent URL builder using the same parameter merge rules asbuildUrl(String, Map).- Parameters:
url- the full URL with parameters and hash- Returns:
- a fluent URL builder
- Since:
- jplatform-11.0.0
-
buildUrl
Build an URL by adding the given parameters.
If a parameter to be added is already present in the given URL, the URL's parameter will be replaced by the new parameter.
If a parameter has a null value, this parameter will be removed from the URL.
Example
String url = "http://www.jalios.com/home.jsp?param1=value1¶m1=value2¶m2=value2#hash1"; Map<String, String[]> map = new HashMap<String, String[]>(); map.put("param1", null); map.put("param2", new String[] { "value21", "value22" }); map.put("param3", new String[] { "value31", "value32" }); String url = URLUtils.buildUrl(url, map); The "url" value will be: http://www.jalios.com/home.jsp?param2=value21¶m2=value22¶m3=value31¶m3=value32#hash1- Parameters:
url- the full URL with parameters and hashparametersMap- Map of the params to add/remove- Returns:
- the new url
-