Class WebUtilities

  • All Implemented Interfaces:

    
    public final class WebUtilities
    
                        

    Utility-class revolving around interactions with web-resources.

    apiProperties — API configuration of this instance.

    Author:

    Griefed

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Constructor Detail

    • Method Detail

      • openTimedConnection

         final URLConnection openTimedConnection(URL url, Integer readTimeout)

        Opens a connection to url with the configured connect- and read-timeouts applied, defaulting readTimeout to the metadata timeout.

        Every outbound call goes through here. The JDK's default is to wait forever, so a connection opened anywhere else is a hang waiting to happen — the same single-source-of-truth rule this module applies to SupportedModloaders and ModScanner.scannerFor, and for the same reason: the copies drift, and the one without the timeout is the one that strands a user.

        Redirects are deliberately left following (the JDK default): maven.legacyfabric.net answers 302, so a connection which stopped following them would report that host as unreachable.

        Returns URLConnection rather than HttpURLConnection on purpose. The timeout setters live on URLConnection, so narrowing buys nothing — and it costs correctness: downloadFile is published API that accepts any URL, a file: URL yields a FileURLConnection, and casting that throws ClassCastException, which is not an IOException and so escapes every caller's error handling. Callers needing responseCode cast for themselves, exactly as they did before.

        readTimeout — Milliseconds a single read may block. Pass ApiProperties.networkDownloadReadTimeout for file-downloads.

      • openTimedStream

         final InputStream openTimedStream(URL url, Integer readTimeout)

        Opens an input-stream on url with the configured timeouts applied — the timeout-carrying replacement for URL.openStream(), which inherits the JDK's unbounded default.

        url — The URL to read from. readTimeout — Milliseconds a single read may block. Returns the response body's stream. Closing it releases the connection.

      • downloadAndReplaceFile

         final Boolean downloadAndReplaceFile(File fileDestination, URL downloadURL)

        Download the file from the specified URL to the specified destination, replacing the file if it already exists. The destination should end in a valid filename. Any directories up to the specified file will be created.

        Returns boolean. Returns true if the file could be found on the hosts' filesystem.

      • downloadFile

         final Boolean downloadFile(File file, URL downloadURL)

        Download the file from the specified URL to the specified destination. The destination should end in a valid filename. Any directories up to the specified file will be created.

        Returns true if this call wrote the file. A failure answers false whether or not a file is there, because every caller reads a true as "I now have this artifact". A body shorter or longer than the Content-Length the source declared is a failure too, and leaves the existing file untouched.

      • openLinkInBrowser

         final Unit openLinkInBrowser(URL url)

        Open the given url in a browser.

        url — The URI to the website you want to open.

      • openLinkInBrowser

         final Unit openLinkInBrowser(URI uri)

        Open the given uri in a browser.

        uri — The URI to the website you want to open.

      • hasteBinPreChecks

         final Boolean hasteBinPreChecks(File fileToCheck)

        Checks the filesize of the given file whether it is smaller or bigger than 10 MB.

        fileToCheck — The file or directory to check.

      • getResponseAsString

         final String getResponseAsString(URL url)

        Get the response of a call to a URL as a string.

        Two properties an embedder must know, kept because this is published API: line breaks are dropped (the lines are concatenated with no separator, so "a\nb" reads as "ab"), and the body is decoded with the platform default charset, not the one the response declares. Read the stream from openTimedStream instead where either matters.

        url — The URL you want to get the response from.

      • getResponseCode

         final Integer getResponseCode(URL url)

        Get the response-code of a call to a URL as an integer.

        url — The URL you want to get the response from.

      • isReachable

         final Boolean isReachable(URL url)

        Check the availability of the host of the given URL and whether the URL gives a status code of

        • Only when both the host is available and the URL returns a status code of 200 does this method return true.