Class UrlPacScriptSource
- java.lang.Object
-
- com.github.markusbernhardt.proxy.selector.pac.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.
-
-
Field Summary
Fields Modifier and Type Field Description private static int
DEFAULT_CONNECT_TIMEOUT
private static int
DEFAULT_READ_TIMEOUT
private long
expireAtMillis
static java.lang.String
OVERRIDE_CONNECT_TIMEOUT
static java.lang.String
OVERRIDE_READ_TIMEOUT
private java.lang.String
scriptContent
private java.lang.String
scriptUrl
-
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()
getScriptContentprotected 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-8private 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()
-
-
-
Field Detail
-
DEFAULT_CONNECT_TIMEOUT
private static final int DEFAULT_CONNECT_TIMEOUT
- See Also:
- Constant Field Values
-
DEFAULT_READ_TIMEOUT
private static final int DEFAULT_READ_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
-
-
Method Detail
-
getScriptContent
public java.lang.String getScriptContent() throws java.io.IOException
getScriptContent- Specified by:
getScriptContent
in interfacePacScriptSource
- Returns:
- a script.
- Throws:
java.io.IOException
- on read error.- See Also:
PacScriptSource.getScriptContent()
-
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 millisecondsdefaultValue
- 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 classjava.lang.Object
- See Also:
Object.toString()
-
isScriptValid
public boolean isScriptValid()
isScriptValid- Specified by:
isScriptValid
in interfacePacScriptSource
- Returns:
- true if everything is fine, else false.
- See Also:
PacScriptSource.isScriptValid()
-
-