Before writing your own implementation it is worth noting that there are existing solutions available, e.g. Proxy Vole.
What is PAC?
Proxy auto-config was defined by Netscape for their Netscape Navigator 2.0 a long time ago (1996). It is a very dynamic method for configuring what proxy to use for accessing a specific URL. It works in the following way:
FindProxyForURL(url, host)to get the proxy configuration to use. The configuration is in the form of a string with ; as separator, for example:
How can the
PACScriptMethods in the example code below which shows how the PAC script can be evaluated:
ScriptEngineManager manager = new ScriptEngineManager();
engine.put("MyJavaPacImpl", new PACScriptMethods());
for(Method method : PACScriptMethods.class.getMethods())
String bridgeFunctionDef = defineBridgeFunction(
// The engine is now ready to be used to evaluate the PAC script
// (passed in as a string)
// Now let's use the FindProxyForURL function to get the proxy
// for the URL we want to access
Invocable invocableEngine = (Invocable) engine;
Object resultObj = invocableEngine.invokeFunction(
String proxyConfig = String.valueOf(resultObj);
The careful reader may have spotted that I left out the
defineBridgeFunction("myFunction", 2) results in:
return MyJavaPacImpl.myFunction(arg0, arg1);
The proxy configuration in
proxyConfig can now easily be parsed (remember “;” as separator) to create a list of standard Java
Proxy objects holding the proxy settings.