Class RequestWithProxyParser

java.lang.Object
one.tranic.t.base.parse.proxy.RequestWithProxyParser

public class RequestWithProxyParser extends Object
A utility class for handling HTTP connections and streams with support for proxy configurations and asynchronous operations.

This class provides methods to open URL connections and input streams synchronously or asynchronously, applying proxy settings retrieved automatically from the environment or system configuration.

  • Constructor Details

    • RequestWithProxyParser

      public RequestWithProxyParser()
  • Method Details

    • setCustomProxy

      public static void setCustomProxy(Proxy proxy)
      Sets a custom proxy for the RequestWithProxyParser class.
      Parameters:
      proxy - the Proxy instance to be set as the custom proxy. This proxy will be used for subsequent network requests within this class.
    • refreshProxy

      public static void refreshProxy()
      Refreshes the proxy configuration used by the application.

      This method retrieves the current proxy settings by invoking the `ProxyConfigReader.getProxy()` method and updates the static `proxy` field with the newly retrieved proxy configuration. It is useful in scenarios where the proxy settings might have changed dynamically and need to be reloaded.

    • openConnectionAsync

      public static Future<HttpURLConnection> openConnectionAsync(URL url) throws IOException
      Opens a connection to the specified URL asynchronously and returns a Future with the result.

      The connection is established using proxy settings if applicable.

      Parameters:
      url - the URL to which the connection should be opened
      Returns:
      a Future containing the established HttpURLConnection, or a RuntimeException if an error occurs
      Throws:
      IOException - if an IO error occurs while attempting to open the connection
    • openConnectionAsync

      public static Future<HttpURLConnection> openConnectionAsync(String url) throws IOException
      Establishes an asynchronous connection to the specified URL.
      Parameters:
      url - the URL as a string to which the connection should be opened.
      Returns:
      a Future representing the pending result of the HttpURLConnection.
      Throws:
      IOException - if an error occurs while forming or opening the connection.
    • openConnection

      public static HttpURLConnection openConnection(String url) throws IOException
      Opens a connection to the specified URL string with proxy settings applied.
      Parameters:
      url - the URL string to which a connection is to be opened
      Returns:
      a HttpURLConnection object pointing to the specified resource
      Throws:
      IOException - if an I/O error occurs while opening the connection
    • openConnection

      public static HttpURLConnection openConnection(URL url) throws IOException
      Opens a connection to the specified URL using the proxy configuration retrieved from the system or environment settings.
      Parameters:
      url - the URL to which the connection needs to be established.
      Returns:
      an instance of HttpURLConnection representing the connection to the specified URL.
      Throws:
      IOException - if an I/O exception occurs while opening the connection.
    • openStream

      public static InputStream openStream(URL url) throws IOException
      Opens a stream for reading from the specified URL, using a proxy configuration retrieved via ProxyConfigReader.getProxy().
      Parameters:
      url - the URL from which the stream should be opened
      Returns:
      an InputStream to read from the given URL
      Throws:
      IOException - if an I/O error occurs while opening the connection or stream
    • openStream

      public static InputStream openStream(String url) throws IOException
      Opens an input stream to the resource referenced by the specified URL string.
      Parameters:
      url - the URL string pointing to the resource to be accessed
      Returns:
      an InputStream to read data from the specified resource
      Throws:
      IOException - if an I/O exception occurs while trying to open the stream
    • openStreamAsync

      public static Future<InputStream> openStreamAsync(URL url) throws IOException
      Opens a stream to a given URL asynchronously using an executor service.

      The returned Future allows retrieving the resulting InputStream when the operation completes. If an I/O error occurs during the stream opening, the exception will be encapsulated in a RuntimeException.

      Parameters:
      url - the URL to open a stream to; must not be null
      Returns:
      a Future representing the pending result of the operation, which will yield an InputStream upon successful completion
      Throws:
      IOException - if an error occurs while initializing the URL stream
    • openStreamAsync

      public static Future<InputStream> openStreamAsync(String url) throws IOException
      Asynchronously opens an input stream to the specified URL string. This method submits a task to an executor for opening the stream and returns a Future representing the pending result.
      Parameters:
      url - the string representation of the URL to open a stream from
      Returns:
      a Future containing the InputStream associated with the specified URL
      Throws:
      IOException - if an I/O error occurs while creating the URL or accessing its stream