Class URLHelper

java.lang.Object
com.helger.base.url.URLHelper

@ThreadSafe public final class URLHelper extends Object
URL utilities.
Author:
Philip Helger
  • Method Details

    • equalURLs

      public static boolean equalURLs(@Nullable URL aObj1, @Nullable URL aObj2)
      Special equals implementation for URLs because URL.equals performs a host lookup.
      Click here for details
      Parameters:
      aObj1 - first URL
      aObj2 - secondURL
      Returns:
      true if they contain the same string
    • getURLString

      public static @Nullable 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 =). Any percent encoding necessary must also have been done. May be null.
      sAnchor - An optional anchor to be added. Any percent encoding necessary must also have been done. May be null.
      Returns:
      May be null if path, anchor and parameters are null.
    • getAsURL

      public static @Nullable 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

      public static @Nullable 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

      public static @Nullable 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

      public static @Nullable 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

      public static @Nullable 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

      public static @Nullable 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.
    • getAsFile

      public static @NonNull File getAsFile(@NonNull URL aURL)
      Convert a URL to a File. The URL must use the "file" protocol.
      Parameters:
      aURL - The URL to convert. May not be null. Must use the "file" protocol.
      Returns:
      The corresponding File object. The file may be non-existing. Never null.
      Throws:
      IllegalArgumentException - if the URL does not use the "file" protocol.
    • getAsFileOrNull

      public static @Nullable File getAsFileOrNull(@Nullable URL aURL)
      Try to convert a URL to a File. If the URL is null or does not use the "file" protocol, null is returned.
      Parameters:
      aURL - The URL to convert. May be null.
      Returns:
      null if the URL is null or does not use the "file" protocol.
    • getClassPathURL

      public static @Nullable URL getClassPathURL(@Nonempty @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.
    • isClassPathURLExisting

      public static boolean isClassPathURLExisting(@Nonempty @NonNull @Nonempty String sPath)
      Check if a classpath URL exists for the given path.
      Parameters:
      sPath - The path to be resolved. May neither be null nor empty.
      Returns:
      true if the classpath URL exists, false otherwise.
    • isClassPathURLExisting

      public static boolean isClassPathURLExisting(@Nonempty @NonNull @Nonempty String sPath, @NonNull ClassLoader aClassLoader)
      Check if a classpath URL exists for the given path using the specified class loader.
      Parameters:
      sPath - The path to be resolved. May neither be null nor empty.
      aClassLoader - The class loader to use for resolving. May not be null.
      Returns:
      true if the classpath URL exists, false otherwise.