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 intDEFAULT_CONNECT_TIMEOUTprivate static intDEFAULT_READ_TIMEOUTprivate longexpireAtMillisstatic java.lang.StringOVERRIDE_CONNECT_TIMEOUTstatic java.lang.StringOVERRIDE_READ_TIMEOUTprivate java.lang.StringscriptContentprivate java.lang.StringscriptUrl
-
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.StringdownloadPacContent(java.lang.String url)Downloads the script from a webserver.private java.io.BufferedReadergetReader(java.net.HttpURLConnection con)Build a BufferedReader around the open HTTP connection.java.lang.StringgetScriptContent()getScriptContentprotected intgetTimeOut(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.booleanisScriptValid()isScriptValid(package private) java.lang.StringparseCharsetFromHeader(java.lang.String contentType)Response Content-Type could be something like this: application/x-ns-proxy-autoconfig; charset=UTF-8private java.lang.StringreadAllContent(java.io.BufferedReader r)Reads the whole content available into a String.private java.lang.StringreadPacFileContent(java.lang.String scriptUrl)Reads a PAC script from a local file.private voidsetPacProxySelectorEnabled(boolean enable)Enables/disables the PAC proxy selector while we download to prevent recursion.private java.net.HttpURLConnectionsetupHTTPConnection(java.lang.String url)Configure the connection to download from.java.lang.StringtoString()
-
-
-
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.IOExceptiongetScriptContent- Specified by:
getScriptContentin 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.IOExceptionReads a PAC script from a local file.- Parameters:
scriptUrl-- Returns:
- the content of the script file.
- Throws:
java.io.IOExceptionjava.net.URISyntaxException
-
downloadPacContent
private java.lang.String downloadPacContent(java.lang.String url) throws java.io.IOExceptionDownloads 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.IOExceptionReads 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.IOExceptionBuild a BufferedReader around the open HTTP connection.- Parameters:
con- to read from- Returns:
- the BufferedReader.
- Throws:
java.io.UnsupportedEncodingExceptionjava.io.IOException
-
setupHTTPConnection
private java.net.HttpURLConnection setupHTTPConnection(java.lang.String url) throws java.io.IOException, java.net.MalformedURLExceptionConfigure the connection to download from.- Parameters:
url- to get the pac file content from- Returns:
- a HTTPUrlConnecion to this url.
- Throws:
java.io.IOExceptionjava.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:
toStringin classjava.lang.Object- See Also:
Object.toString()
-
isScriptValid
public boolean isScriptValid()
isScriptValid- Specified by:
isScriptValidin interfacePacScriptSource- Returns:
- true if everything is fine, else false.
- See Also:
PacScriptSource.isScriptValid()
-
-