Class WebUtilities

  • All Implemented Interfaces:

    
    public final class WebUtilities
    
                        

    Utility-class revolving around interactions with web-resources.

    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

      • WebUtilities

        WebUtilities(ApiProperties apiProperties)
        Parameters:
        apiProperties - API configuration of this instance.
    • 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.

        Parameters:
        url - The URL to connect to.
        readTimeout - Milliseconds a single read may block.
        Returns:

        The opened, timeout-carrying connection.

      • 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.

        Parameters:
        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.

        Parameters:
        fileDestination - The file to store the web-resource in.
        downloadURL - The URL to the file you want to download.
        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.

        Parameters:
        file - The destination where the file should be stored.
        downloadURL - The URL to the file you want to download.
        Returns:

        true if the file was created.

      • openLinkInBrowser

         final Unit openLinkInBrowser(URL url)

        Open the given url in a browser.

        Parameters:
        url - The URI to the website you want to open.
      • openLinkInBrowser

         final Unit openLinkInBrowser(URI uri)

        Open the given uri in a browser.

        Parameters:
        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.

        Parameters:
        fileToCheck - The file or directory to check.
        Returns:

        Boolean. True if the file is smaller, false if the file is bigger than 10 MB.

      • createHasteBinFromString

         final String createHasteBinFromString(String text)

        Create a HasteBin post from a given string. The text provided passed onto Haste zneix which creates a HasteBin post out of the passed String and returns the URL to the newly created post.

        Created with the help of kaimu-ken's hastebin.java (MIT License) and edited to use HasteBin fork zneix/haste-server. My fork of kaimu-kun's hastebin.java is available at Griefed/hastebin.java.

        Parameters:
        text - The file which will be read into a String of which then to create a HasteBin post of.
        Returns:

        String. Returns a String containing the URL to the newly created HasteBin post.

      • createHasteBinFromFile

         final String createHasteBinFromFile(File textFile)

        Create a HasteBin post from a given text file. The text file provided is read into a string and then passed onto Haste zneix which creates a HasteBin post out of the passed String and returns the URL to the newly created post.

        Created with the help of kaimu-ken's hastebin.java (MIT License) and edited to use HasteBin fork zneix/haste-server. My fork of kaimu-kun's hastebin.java is available at Griefed/hastebin.java.

        Parameters:
        textFile - The file which will be read into a String of which then to create a HasteBin post of.
        Returns:

        String. Returns a String containing the URL to the newly created HasteBin post.

      • getResponseAsString

         final String getResponseAsString(URL url)

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

        Parameters:
        url - The URL you want to get the response from
        Returns:

        The response.

      • getResponseCode

         final Integer getResponseCode(URL url)

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

        Parameters:
        url - The URL you want to get the response from
        Returns:

        The response.

      • 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.

        Parameters:
        url - The URL of which to check for host-availability.
        Returns:

        true if, and only if, the host is available and the URL returns the status code 200.