Class URLUtils

java.lang.Object
com.jalios.util.URLUtils

public final class URLUtils extends Object
  • Method Details

    • isAbsoluteUrl

      public static boolean isAbsoluteUrl(String url)
      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

      public static Map<String,String[]> parseUrlQueryString(String url)
      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

      public static String parseUrlQueryStringAsJson(String url)
      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&param2=value21&param2=value22";
       String json = URLUtils.parseUrlQueryStringAsJson(url);
       
       Result: {"param1":["value1"],"param2":["value21","value22"]}
       
      It's possible to use a Function implementation 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&param2=value21&param2=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 of Function to manipulate the Map of parameters extracted from URL
      Returns:
      String containing the parameters (name and values) as JSON format
    • getQueryString

      public static String getQueryString(Map<String,String[]> parametersMap, String... excludedParams)
      Returns a query String from a map of parameters.
      Parameters:
      parametersMap - Map of parameters
      excludedParams - Parameter to exclude in the final query string
      Returns:
      String representing a query string
    • buildUrl

      public static URLUtils.UrlBuilder buildUrl(String url)
      Creates a fluent URL builder using the same parameter merge rules as buildUrl(String, Map).
      Parameters:
      url - the full URL with parameters and hash
      Returns:
      a fluent URL builder
      Since:
      jplatform-11.0.0
    • buildUrl

      public static String buildUrl(String url, Map<String,String[]> parametersMap)

      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&param1=value2&param2=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&param2=value22&param3=value31&param3=value32#hash1
       
      Parameters:
      url - the full URL with parameters and hash
      parametersMap - Map of the params to add/remove
      Returns:
      the new url