Class URLUtils


  • public final class URLUtils
    extends java.lang.Object
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String buildUrl​(java.lang.String url, java.util.Map<java.lang.String,​java.lang.String[]> parametersMap)
      Build an URL by adding the given parameters.
      static java.lang.String getQueryString​(java.util.Map<java.lang.String,​java.lang.String[]> parametersMap, java.lang.String... excludedParams)
      Returns a query String from a map of parameters.
      static boolean isAbsoluteUrl​(java.lang.String url)
      Check if the specified URL is absolute.
      static java.util.Map<java.lang.String,​java.lang.String[]> parseUrlQueryString​(java.lang.String url)
      Parses an URL, extracts the query string and returns a map with the parameter values.
      static java.lang.String parseUrlQueryStringAsJson​(java.lang.String url)
      Parses an URL, extracts the query string and returns the parameters as JSON format.
      static java.lang.String parseUrlQueryStringAsJson​(java.lang.String url, java.util.function.Function<java.util.Map<java.lang.String,​java.lang.String[]>,​java.util.Map<java.lang.String,​java.lang.Object>> paramMapUpdator)
      Parses an URL, extracts the query string and returns the parameters as JSON format.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • isAbsoluteUrl

        public static boolean isAbsoluteUrl​(java.lang.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 java.util.Map<java.lang.String,​java.lang.String[]> parseUrlQueryString​(java.lang.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 java.lang.String parseUrlQueryStringAsJson​(java.lang.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 java.lang.String parseUrlQueryStringAsJson​(java.lang.String url,
                                                                 java.util.function.Function<java.util.Map<java.lang.String,​java.lang.String[]>,​java.util.Map<java.lang.String,​java.lang.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 java.lang.String getQueryString​(java.util.Map<java.lang.String,​java.lang.String[]> parametersMap,
                                                      java.lang.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 java.lang.String buildUrl​(java.lang.String url,
                                                java.util.Map<java.lang.String,​java.lang.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