Class UrlPacScriptSource

java.lang.Object
com.github.markusbernhardt.proxy.selector.pac.UrlPacScriptSource
All Implemented Interfaces:
PacScriptSource

public class UrlPacScriptSource extends Object implements PacScriptSource
Script source that will load the content of a PAC file from an webserver. The script content is cached once it was downloaded.
  • Field Details

    • DEFAULT_CONNECT_TIMEOUT

      private static final int DEFAULT_CONNECT_TIMEOUT
      See Also:
    • DEFAULT_READ_TIMEOUT

      private static final int DEFAULT_READ_TIMEOUT
      See Also:
    • OVERRIDE_CONNECT_TIMEOUT

      public static final String OVERRIDE_CONNECT_TIMEOUT
      See Also:
    • OVERRIDE_READ_TIMEOUT

      public static final String OVERRIDE_READ_TIMEOUT
      See Also:
    • scriptUrl

      private final String scriptUrl
    • scriptContent

      private String scriptContent
    • expireAtMillis

      private long expireAtMillis
  • Constructor Details

    • UrlPacScriptSource

      public UrlPacScriptSource(String url)
      Constructor
      Parameters:
      url - the URL to download the script from.
  • Method Details

    • getScriptContent

      public String getScriptContent() throws IOException
      getScriptContent
      Specified by:
      getScriptContent in interface PacScriptSource
      Returns:
      a script.
      Throws:
      IOException - on read error.
      See Also:
    • readPacFileContent

      private String readPacFileContent(String scriptUrl) throws IOException
      Reads a PAC script from a local file.
      Parameters:
      scriptUrl -
      Returns:
      the content of the script file.
      Throws:
      IOException
      URISyntaxException
    • downloadPacContent

      private String downloadPacContent(String url) throws IOException
      Downloads the script from a webserver.
      Parameters:
      url - the URL to the script file.
      Returns:
      the script content.
      Throws:
      IOException - on read error.
    • setPacProxySelectorEnabled

      private void setPacProxySelectorEnabled(boolean enable)
      Enables/disables the PAC proxy selector while we download to prevent recursion. See issue: 26 in the change tracker.
    • readAllContent

      private String readAllContent(BufferedReader r) throws IOException
      Reads the whole content available into a String.
      Parameters:
      r - to read from.
      Returns:
      the complete PAC file content.
      Throws:
      IOException
    • getReader

      Build a BufferedReader around the open HTTP connection.
      Parameters:
      con - to read from
      Returns:
      the BufferedReader.
      Throws:
      UnsupportedEncodingException
      IOException
    • setupHTTPConnection

      private HttpURLConnection setupHTTPConnection(String url) throws IOException, MalformedURLException
      Configure the connection to download from.
      Parameters:
      url - to get the pac file content from
      Returns:
      a HTTPUrlConnecion to this url.
      Throws:
      IOException
      MalformedURLException
    • getTimeOut

      protected int getTimeOut(String overrideProperty, int defaultValue)
      Gets the timeout value from a property or uses the given default value if the property cannot be parsed.
      Parameters:
      overrideProperty - the property to define the timeout value in milliseconds
      defaultValue - the default timeout value in milliseconds.
      Returns:
      the value to use.
    • parseCharsetFromHeader

      String parseCharsetFromHeader(String contentType)
      Response Content-Type could be something like this: application/x-ns-proxy-autoconfig; charset=UTF-8
      Parameters:
      contentType - header field.
      Returns:
      the extracted charset if set or the default charset (UTF-8).
    • toString

      public String toString()
      Overrides:
      toString in class Object
      See Also:
    • isScriptValid

      public boolean isScriptValid()
      isScriptValid
      Specified by:
      isScriptValid in interface PacScriptSource
      Returns:
      true if everything is fine, else false.
      See Also: