Debian Codegraph @

src/main/java/com/github/markusbernhardt/proxy/selector/pac/JavaxPacScriptParser.java

raw symbols SCIP
1 package com.github.markusbernhardt.proxy.selector.pac;
2
3 import java.lang.reflect.Method;
4
5 import javax.script.ScriptEngine;
6 import javax.script.ScriptEngineManager;
7 import javax.script.ScriptException;
8
9 import com.github.markusbernhardt.proxy.util.Logger;
10 import com.github.markusbernhardt.proxy.util.Logger.LogLevel;
11
12 /*****************************************************************************
13 * PAC parser using the Rhino JavaScript engine bundled with Java 1.6<br>
14 *
15 * More information about PAC can be found there:<br>
16 * <a href="http://en.wikipedia.org/wiki/Proxy_auto-config">Proxy_auto-config
17 * </a><br>
18 * <a href=
19 * "http://homepages.tesco.net/~J.deBoynePollard/FGA/web-browser-auto-proxy-configuration.html">
20 * web-browser-auto-proxy-configuration</a>
21 *
22 * @author Markus Bernhardt, Copyright 2016
23 * @author Bernd Rosstauscher, Copyright 2009
24 ****************************************************************************/
25 public class JavaxPacScriptParser implements PacScriptParser {
26 static final String SCRIPT_METHODS_OBJECT = "__pacutil";
27
28 private final PacScriptSource source;
29 private final ScriptEngine engine;
30
31 /*************************************************************************
32 * Constructor
33 *
34 * @param source
35 * the source for the PAC script.
36 * @throws ProxyEvaluationException
37 * on error.
38 ************************************************************************/
39 public JavaxPacScriptParser(PacScriptSource source) throws ProxyEvaluationException {
40 this.source = source;
41 this.engine = setupEngine();
42 }
43
44 /*************************************************************************
45 * Initializes the JavaScript engine and adds aliases for the functions
46 * defined in ScriptMethods.
47 *
48 * @throws ProxyEvaluationException
49 * on error.
50 ************************************************************************/
51 private ScriptEngine setupEngine() throws ProxyEvaluationException {
52 ScriptEngineManager mng = new ScriptEngineManager();
53 ScriptEngine engine = mng.getEngineByMimeType("text/javascript");
54 engine.put(SCRIPT_METHODS_OBJECT, new PacScriptMethods());
55
56 Class<?> scriptMethodsClazz = ScriptMethods.class;
57 Method[] scriptMethods = scriptMethodsClazz.getMethods();
58
59 for (Method method : scriptMethods) {
60 String name = method.getName();
61 int args = method.getParameterTypes().length;
62 StringBuilder toEval = new StringBuilder(name).append(" = function(");
63 for (int i = 0; i < args; i++) {
64 if (i > 0) {
65 toEval.append(",");
66 }
67 toEval.append("arg").append(i);
68 }
69 toEval.append(") {return ");
70
71 String functionCall = buildFunctionCallCode(name, args);
72
73 // If return type is java.lang.String convert it to a JS string
74 if (String.class.isAssignableFrom(method.getReturnType())) {
75 functionCall = "String(" + functionCall + ")";
76 }
77 toEval.append(functionCall).append("; }");
78 try {
79 engine.eval(toEval.toString());
80 } catch (ScriptException e) {
81 Logger.log(getClass(), LogLevel.ERROR, "JS evaluation error when creating alias for " + name + ".", e);
82 throw new ProxyEvaluationException("Error setting up script engine", e);
83 }
84 }
85
86 return engine;
87 }
88
89 /*************************************************************************
90 * Builds a JavaScript code snippet to call a function that we bind.
91 *
92 * @param functionName
93 * of the bound function
94 * @param args
95 * of the bound function
96 * @return the JS code to invoke the method.
97 ************************************************************************/
98
99 private String buildFunctionCallCode(String functionName, int args) {
100 StringBuilder functionCall = new StringBuilder();
101 functionCall.append(SCRIPT_METHODS_OBJECT).append(".").append(functionName).append("(");
102 for (int i = 0; i < args; i++) {
103 if (i > 0) {
104 functionCall.append(",");
105 }
106 functionCall.append("arg").append(i);
107 }
108 functionCall.append(")");
109 return functionCall.toString();
110 }
111
112 /***************************************************************************
113 * Gets the source of the PAC script used by this parser.
114 *
115 * @return a PacScriptSource.
116 **************************************************************************/
117 public PacScriptSource getScriptSource() {
118 return this.source;
119 }
120
121 /*************************************************************************
122 * Evaluates the given URL and host against the PAC script.
123 *
124 * @param url
125 * the URL to evaluate.
126 * @param host
127 * the host name part of the URL.
128 * @return the script result.
129 * @throws ProxyEvaluationException
130 * on execution error.
131 ************************************************************************/
132 public String evaluate(String url, String host) throws ProxyEvaluationException {
133 try {
134 StringBuilder script = new StringBuilder(this.source.getScriptContent());
135 String evalMethod = " ;FindProxyForURL (\"" + url + "\",\"" + host + "\")";
136 script.append(evalMethod);
137 Object result = this.engine.eval(script.toString());
138 return (String) result;
139 } catch (Exception e) {
140 Logger.log(getClass(), LogLevel.ERROR, "JS evaluation error.", e);
141 throw new ProxyEvaluationException("Error while executing PAC script: " + e.getMessage(), e);
142 }
143
144 }
145 }