Class UrlPacScriptSource

  • All Implemented Interfaces:
    PacScriptSource

    public class UrlPacScriptSource
    extends java.lang.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.
    • Constructor Summary

      Constructors 
      Constructor Description
      UrlPacScriptSource​(java.lang.String url)
      Constructor
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private java.lang.String downloadPacContent​(java.lang.String url)
      Downloads the script from a webserver.
      private java.io.BufferedReader getReader​(java.net.HttpURLConnection con)
      Build a BufferedReader around the open HTTP connection.
      java.lang.String getScriptContent()
      getScriptContent
      protected int getTimeOut​(java.lang.String overrideProperty, int defaultValue)
      Gets the timeout value from a property or uses the given default value if the property cannot be parsed.
      boolean isScriptValid()
      isScriptValid
      (package private) java.lang.String parseCharsetFromHeader​(java.lang.String contentType)
      Response Content-Type could be something like this: application/x-ns-proxy-autoconfig; charset=UTF-8
      private java.lang.String readAllContent​(java.io.BufferedReader r)
      Reads the whole content available into a String.
      private java.lang.String readPacFileContent​(java.lang.String scriptUrl)
      Reads a PAC script from a local file.
      private void setPacProxySelectorEnabled​(boolean enable)
      Enables/disables the PAC proxy selector while we download to prevent recursion.
      private java.net.HttpURLConnection setupHTTPConnection​(java.lang.String url)
      Configure the connection to download from.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • DEFAULT_CONNECT_TIMEOUT

        private static final int DEFAULT_CONNECT_TIMEOUT
        See Also:
        Constant Field Values
      • OVERRIDE_CONNECT_TIMEOUT

        public static final java.lang.String OVERRIDE_CONNECT_TIMEOUT
        See Also:
        Constant Field Values
      • OVERRIDE_READ_TIMEOUT

        public static final java.lang.String OVERRIDE_READ_TIMEOUT
        See Also:
        Constant Field Values
      • scriptUrl

        private final java.lang.String scriptUrl
      • scriptContent

        private java.lang.String scriptContent
      • expireAtMillis

        private long expireAtMillis
    • Constructor Detail

      • UrlPacScriptSource

        public UrlPacScriptSource​(java.lang.String url)
        Constructor
        Parameters:
        url - the URL to download the script from.
    • Method Detail

      • readPacFileContent

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

        private java.lang.String downloadPacContent​(java.lang.String url)
                                             throws java.io.IOException
        Downloads the script from a webserver.
        Parameters:
        url - the URL to the script file.
        Returns:
        the script content.
        Throws:
        java.io.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 java.lang.String readAllContent​(java.io.BufferedReader r)
                                         throws java.io.IOException
        Reads the whole content available into a String.
        Parameters:
        r - to read from.
        Returns:
        the complete PAC file content.
        Throws:
        java.io.IOException
      • getReader

        private java.io.BufferedReader getReader​(java.net.HttpURLConnection con)
                                          throws java.io.UnsupportedEncodingException,
                                                 java.io.IOException
        Build a BufferedReader around the open HTTP connection.
        Parameters:
        con - to read from
        Returns:
        the BufferedReader.
        Throws:
        java.io.UnsupportedEncodingException
        java.io.IOException
      • setupHTTPConnection

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

        protected int getTimeOut​(java.lang.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

        java.lang.String parseCharsetFromHeader​(java.lang.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 java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
        See Also:
        Object.toString()