Class URLHelper


  • @ThreadSafe
    public final class URLHelper
    extends Object
    URL utilities.
    Author:
    Philip Helger
    • 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
    • Method Detail

      • urlDecodeOrNull

        @Nullable
        public static String urlDecodeOrNull​(@Nonnull
                                             String sValue)
        URL-decode the passed value automatically handling charset issues. The used char set is determined by CHARSET_URL_OBJ.
        Parameters:
        sValue - The value to be decoded. May not be null.
        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 uses URLCodec to do the hard work.
        Parameters:
        sValue - The value to be decoded. May be null.
        aCharset - The charset to use. May not be null.
        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 by CHARSET_URL_OBJ.
        Parameters:
        sValue - The value to be decoded. May not be null.
        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 uses URLCodec to do the hard work.
        Parameters:
        sValue - The value to be decoded. May be null.
        aCharset - The charset to use. May not be null.
        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 by CHARSET_URL_OBJ.
        Parameters:
        sValue - The value to be encoded. May not be null.
        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 be null.
        aCharset - The charset to use. May not be null.
        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 be null.
        Returns:
        The cleaned version or null if the input was null.
      • 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 parsed
        aParameterDecoder - The parameter decoder to use. May be null.
        Returns:
        the corresponding ISimpleURL representation of the passed URL
      • 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 be null.
        sQueryParams - The set of all query parameters already concatenated with the correct characters (& and =). May be null.
        sAnchor - An optional anchor to be added. May be null.
        Returns:
        May be null if path, anchor and parameters are null.
      • 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 be null or empty.
        aQueryParameterEncoder - The encoder to be used to encode parameter names and parameter values. May be null. This may be e.g. a URLParameterEncoder.
        Returns:
        null if no parameter is present.
      • 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 be null.
        aQueryParams - The list of query parameters to be appended. May be null .
        sAnchor - An optional anchor to be added. May be null.
        aQueryParameterEncoder - The parameters encoding to be used. May be null.
        Returns:
        May be null if path, anchor and parameters are null.
      • 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 be null.
        aQueryParams - The list of parameters to be appended. May be null.
        sAnchor - An optional anchor to be added. May be null.
        aParameterCharset - If not null the parameters are encoded using this charset.
        Returns:
        May be null if all parameters are null.
      • 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 URL null is returned.
        Parameters:
        sURL - Source URL. May be null.
        bWhine - true to debug log if conversion failed
        Returns:
        null if 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 URL null is returned.
        Parameters:
        sURL - Source URL. May be null.
        Returns:
        null if 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 URL null is returned.
        Parameters:
        aURI - Source URI. May be null.
        Returns:
        null if 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 URI null is returned.
        Parameters:
        sURI - Source URI. May be null.
        Returns:
        null if 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 URI null is returned.
        Parameters:
        aURL - Source URL. May be null.
        Returns:
        null if 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 be null.
        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:
        null if 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:
        1. Default class loader (usually the context class loader)
        2. The class loader of this class
        3. The system class loader
        Parameters:
        sPath - The path to be resolved. May neither be null nor empty.
        Returns:
        null if the path could not be resolved.
      • 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 be null.
        Returns:
        true if the provided URN is not empty and matches the regular expression REGEX_URN.
        Since:
        10.0.0